use of io.divide.server.dao.ServerCredentials in project divide by HiddenStage.
the class SecurityFilter method authenticate.
private UserContext authenticate(ContainerRequestContext request) {
// Extract authentication credentials
String authentication = request.getHeaderString(ContainerRequest.AUTHORIZATION);
System.out.println("HeaderCount: " + request.getHeaders().keySet().size());
System.out.println(request.getHeaders().keySet());
System.out.println(request.getPropertyNames());
System.out.println(request.getCookies().keySet());
if (authentication == null) {
return abort(request, "Authentication credentials are required");
}
if (!authentication.startsWith("CUSTOM ")) {
return abort(request, "Only CUSTOM authentication is supported: " + authentication);
}
authentication = authentication.substring("CUSTOM ".length());
String token = authentication;
if (token == null) {
return abort(request, "Missing token");
}
// TODO verify
try {
AuthTokenUtils.AuthToken authToken = new AuthTokenUtils.AuthToken(securityManager.getSymmetricKey(), token);
if (authToken.isExpired()) {
return abort(request, "Auth Token Expired: " + System.currentTimeMillis() + " : " + authToken.expirationDate);
}
} catch (AuthTokenUtils.AuthenticationException e) {
return abort(request, "Auth Token Expired: " + e.getMessage());
}
// Validate the extracted credentials
synchronized (dao) {
Query q = new QueryBuilder().select().from(Credentials.class).where(Credentials.AUTH_TOKEN_KEY, OPERAND.EQ, token).build();
try {
TransientObject temp = ObjectUtils.get1stOrNull(dao.query(q));
if (temp != null) {
ServerCredentials creds = new ServerCredentials(temp);
// creds.setAuthToken(AuthTokenUtils.getNewToken(securityManager.getSymmetricKey(),creds)); // assign new token
return new UserContext(request.getUriInfo(), new ServerCredentials(creds));
} else {
// System.err.println("IN DB: " + dao.query(new QueryBuilder().select().from(Credentials.class).build()));
request.abortWith(notAuthReponse("Invalid authentication token"));
return abort(request, "Invalid authentication token");
}
} catch (ServerDAO.DAOException e) {
log.severe("Authentication Failed(" + e.getStatusCode() + ") " + e.getMessage());
e.printStackTrace();
return abort(request, "Invalid authentication token");
}
}
}
use of io.divide.server.dao.ServerCredentials in project divide by HiddenStage.
the class TestEndpoint method setup.
@Produces(MediaType.APPLICATION_JSON)
@GET
@Path("/setup")
public Response setup() throws Exception {
// logger.info("setup");
// Credentials user = TestUtils.getTestUser();
// user = new ServerCredentials(user);
// user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt(10)));
ServerCredentials toSave = new ServerCredentials(TestUtils.getTestUser());
// String en = toSave.getPassword();
// toSave.decryptPassword(keyManager.getPrivateKey()); //decrypt the password
// String de = toSave.getPassword();
String ha = BCrypt.hashpw(toSave.getPassword(), BCrypt.gensalt(10));
// hash the password for storage
toSave.setPassword(ha);
toSave.setAuthToken(AuthTokenUtils.getNewToken(securityManager.getSymmetricKey(), toSave));
toSave.setRecoveryToken(AuthTokenUtils.getNewToken(securityManager.getSymmetricKey(), toSave));
toSave.setOwnerId(dao.count(Credentials.class.getName()) + 1);
dao.save(toSave);
return Response.ok().entity(toSave).build();
}
use of io.divide.server.dao.ServerCredentials in project divide by HiddenStage.
the class TestUtils method getTestUser.
public static Credentials getTestUser() {
Credentials c = new Credentials("someUsername", "someEmail", "somePassword");
ServerCredentials sc = new ServerCredentials(c);
sc.setOwnerId(1);
FilePermissions fp = sc.getFilePermissions();
fp.setReadable(true, FilePermissions.Level.WORLD);
fp.setWritable(true, FilePermissions.Level.WORLD);
sc.setFilePermissions(fp);
sc.setAuthToken(AuthTokenUtils.getNewToken(KEY, sc));
sc.setRecoveryToken(AuthTokenUtils.getNewToken(KEY, sc));
return sc;
}
Aggregations