|
|
@@ -3,19 +3,24 @@
|
|
|
*/
|
|
|
package cz.hsrs.rest.provider;
|
|
|
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.security.KeyStore;
|
|
|
+import java.security.KeyStoreException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.PublicKey;
|
|
|
+import java.security.cert.Certificate;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
import javax.naming.AuthenticationException;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.ws.rs.Consumes;
|
|
|
-import javax.ws.rs.GET;
|
|
|
-import javax.ws.rs.POST;
|
|
|
-import javax.ws.rs.Path;
|
|
|
-import javax.ws.rs.Produces;
|
|
|
+import javax.ws.rs.*;
|
|
|
import javax.ws.rs.core.Context;
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
|
|
+import io.jsonwebtoken.*;
|
|
|
import org.mortbay.jetty.HttpStatus;
|
|
|
|
|
|
import cz.hsrs.db.model.NoItemFoundException;
|
|
|
@@ -27,50 +32,118 @@ import cz.hsrs.rest.util.UserRestUtil;
|
|
|
import cz.hsrs.servlet.security.LoginUser;
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
|
|
+import static cz.hsrs.core.ApplicationInfo.globalProperties;
|
|
|
+
|
|
|
/**
|
|
|
* @author mkepka
|
|
|
*
|
|
|
*/
|
|
|
-@Path("/user")
|
|
|
+@Path("/manage/user")
|
|
|
public class UserRest {
|
|
|
-
|
|
|
- /**
|
|
|
- * Empty constructor
|
|
|
- */
|
|
|
- public UserRest() {
|
|
|
+
|
|
|
+ private static final String KEYSTORE_PATH = globalProperties().keyStonePath();
|
|
|
+ private static final String KEYSTORE_PASS = globalProperties().keyStonePass();
|
|
|
+ private static final String CERT_ALIAS = globalProperties().authCertAlias();
|
|
|
+
|
|
|
+ private static JwtParser jwtParser;
|
|
|
+
|
|
|
+ static {
|
|
|
+ try {
|
|
|
+ FileInputStream is = new FileInputStream(KEYSTORE_PATH);
|
|
|
+ KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
|
+ keystore.load(is, KEYSTORE_PASS.toCharArray());
|
|
|
+
|
|
|
+ Certificate cer = keystore.getCertificate(CERT_ALIAS);
|
|
|
+
|
|
|
+ PublicKey publicKey = cer.getPublicKey();
|
|
|
+ jwtParser = Jwts.parser().verifyWith(publicKey).build();
|
|
|
+ } catch (IOException | KeyStoreException | NoSuchAlgorithmException | CertificateException e) {
|
|
|
+ System.err.println(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @POST @Path("/new")
|
|
|
+ @Produces(MediaType.APPLICATION_JSON)
|
|
|
+ @Consumes(MediaType.APPLICATION_JSON)
|
|
|
+ public Response insertNewUser(JSONObject jsonBody, @Context HttpServletRequest req) {
|
|
|
+ UserBean user;
|
|
|
+ try {
|
|
|
+ String userId = req.getAttribute("userId").toString();
|
|
|
+ user = UserRestUtil.getUser(userId);
|
|
|
+ } catch (SQLException e) {
|
|
|
+ int code = HttpStatus.ORDINAL_500_Internal_Server_Error;
|
|
|
+ return Response.status(code)
|
|
|
+ .entity(new JSONObject()
|
|
|
+ .element("code", code)
|
|
|
+ .element("message", e.getMessage())
|
|
|
+ ).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (user.rightsId != 0) {
|
|
|
+ int code = HttpStatus.ORDINAL_403_Forbidden;
|
|
|
+ return Response.status(code)
|
|
|
+ .entity(new JSONObject()
|
|
|
+ .element("code", code)
|
|
|
+ .element("message", "Not enough rights!")
|
|
|
+ ).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ Claims claims;
|
|
|
+ try {
|
|
|
+ String profileRaw = jsonBody.getString("profile");
|
|
|
+ Jwt<JwsHeader, Claims> jwt = jwtParser.parseSignedClaims(profileRaw);
|
|
|
+ claims = jwt.getPayload();
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ int code = HttpStatus.ORDINAL_500_Internal_Server_Error;
|
|
|
+ return Response.status(code)
|
|
|
+ .entity(new JSONObject()
|
|
|
+ .element("code", code)
|
|
|
+ .element("message", e.getMessage())
|
|
|
+ ).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ String username = claims.get("nickname", String.class);
|
|
|
+ String fullName = claims.get("name", String.class);
|
|
|
+
|
|
|
+ int groupId = jsonBody.getInt("groupId");
|
|
|
+
|
|
|
+ try {
|
|
|
+ UserRestUtil.saveUser(new UserBean(-1, username, fullName, groupId, 1));
|
|
|
+ return Response.ok().build();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ int code = HttpStatus.ORDINAL_500_Internal_Server_Error;
|
|
|
+ return Response.status(code)
|
|
|
+ .entity(new JSONObject()
|
|
|
+ .element("code", code)
|
|
|
+ .element("message", e.getMessage())
|
|
|
+ ).build();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Method for getting user details
|
|
|
- * URL: /rest/user
|
|
|
- * @param req
|
|
|
- * @return
|
|
|
+ * URL: /manage/user
|
|
|
*/
|
|
|
@GET
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
public Response getUser(@Context HttpServletRequest req) {
|
|
|
try {
|
|
|
- LoginUser loggedUser = AuthUtil.getAuthenticatedLoginUser(req);
|
|
|
- UserBean userDetails = UserRestUtil.getUser(loggedUser.getUserName());
|
|
|
- return Response.ok().entity(userDetails)
|
|
|
- .build();
|
|
|
- } catch (AuthenticationException e1) {
|
|
|
- return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
|
|
|
- .entity(new ExceptionBean(e1.getClass().getName(), "Authentication failure for request!"))
|
|
|
- .build();
|
|
|
+ String userId = req.getAttribute("userId").toString();
|
|
|
+ UserBean user = UserRestUtil.getUser(userId);
|
|
|
+ return Response.ok().entity(user).build();
|
|
|
} catch (SQLException e) {
|
|
|
- return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
|
|
|
- .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
|
|
|
- .build();
|
|
|
+ int code = HttpStatus.ORDINAL_500_Internal_Server_Error;
|
|
|
+ return Response.status(code)
|
|
|
+ .entity(new JSONObject()
|
|
|
+ .element("code", code)
|
|
|
+ .element("message", e.getMessage())
|
|
|
+ ).build();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Method for inserting user
|
|
|
- * URL: /rest/user
|
|
|
- * @param userJSON
|
|
|
- * @param req
|
|
|
- * @return
|
|
|
+ * URL: /api/manage/user
|
|
|
*/
|
|
|
@POST
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
@@ -85,28 +158,22 @@ public class UserRest {
|
|
|
userJSON.getString("userRealName"),
|
|
|
userJSON.getInt("groupId"),
|
|
|
userJSON.getInt("rightsId"));
|
|
|
- return Response.ok()
|
|
|
- .build();
|
|
|
+ return Response.ok().build();
|
|
|
} else {
|
|
|
return Response.status(HttpStatus.ORDINAL_409_Conflict)
|
|
|
- .entity(new ExceptionBean("Exception", "User with given name cannot be created!"))
|
|
|
- .build();
|
|
|
+ .entity(new ExceptionBean("Exception", "User with given name cannot be created!")).build();
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
else {
|
|
|
return Response.status(HttpStatus.ORDINAL_403_Forbidden)
|
|
|
- .entity(new ExceptionBean("AuthenticationException", "Not enough rights!"))
|
|
|
- .build();
|
|
|
+ .entity(new ExceptionBean("AuthenticationException", "Not enough rights!")).build();
|
|
|
}
|
|
|
} catch (AuthenticationException e1) {
|
|
|
return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
|
|
|
- .entity(new ExceptionBean(e1.getClass().getName(), "Authentication failure for request!"))
|
|
|
- .build();
|
|
|
+ .entity(new ExceptionBean(e1.getClass().getName(), "Authentication failure for request!")).build();
|
|
|
} catch (SQLException e) {
|
|
|
return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
|
|
|
- .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
|
|
|
- .build();
|
|
|
+ .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage())).build();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -115,7 +182,6 @@ public class UserRest {
|
|
|
* @param userName - name of user
|
|
|
* @return true if userName is already used
|
|
|
* false if userName is not used
|
|
|
- * @throws SQLException
|
|
|
*/
|
|
|
private boolean checkDuplicity(String userName) throws SQLException {
|
|
|
UserUtil uUtil = new UserUtil();
|
|
|
@@ -131,28 +197,20 @@ public class UserRest {
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
- * URL: /rest/user/rights
|
|
|
- * @param req
|
|
|
- * @return
|
|
|
+ * URL: /api/manage/user/rights
|
|
|
*/
|
|
|
- @Path("/rights")
|
|
|
- @GET
|
|
|
+ @GET @Path("/rights")
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
public Response getRights(@Context HttpServletRequest req) {
|
|
|
try {
|
|
|
-
|
|
|
AuthUtil.getAuthenticatedLoginUser(req);
|
|
|
-
|
|
|
- return Response.ok(UserRestUtil.getAllRights())
|
|
|
- .build();
|
|
|
+ return Response.ok(UserRestUtil.getAllRights()).build();
|
|
|
} catch (AuthenticationException e) {
|
|
|
return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
|
|
|
- .entity(new ExceptionBean(e.getClass().getName(), "Authentication failure for request!"))
|
|
|
- .build();
|
|
|
+ .entity(new ExceptionBean(e.getClass().getName(), "Authentication failure for request!")).build();
|
|
|
} catch (SQLException e) {
|
|
|
return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
|
|
|
- .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
|
|
|
- .build();
|
|
|
+ .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage())).build();
|
|
|
}
|
|
|
}
|
|
|
}
|