use of org.platformlayer.auth.AuthenticatorException in project platformlayer by platformlayer.
the class LoginService method authenticate.
public AuthenticateResponse authenticate(HttpServletRequest httpRequest, AuthenticateRequest request) {
AuthenticateResponse response = new AuthenticateResponse();
String username = null;
UserEntity user = null;
if (request.auth.passwordCredentials != null) {
username = request.auth.passwordCredentials.username;
String password = request.auth.passwordCredentials.password;
try {
user = userAuthenticator.authenticate(username, password);
} catch (AuthenticatorException e) {
// An exception indicates something went wrong (i.e. not just
// bad credentials)
log.warn("Error while getting user info", e);
throw new IllegalStateException("Error while getting user info", e);
}
} else if (request.auth.certificateCredentials != null) {
username = request.auth.certificateCredentials.username;
X509Certificate[] certificateChain = HttpUtils.getCertificateChain(httpRequest);
if (certificateChain == null) {
return null;
}
byte[] challengeResponse = request.auth.certificateCredentials.challengeResponse;
CertificateAuthenticationRequest details = new CertificateAuthenticationRequest();
details.certificateChain = certificateChain;
details.username = username;
// details.projectKey = projectKey;
details.challengeResponse = challengeResponse;
CertificateAuthenticationResponse result = null;
try {
result = userAuthenticator.authenticate(details);
} catch (AuthenticatorException e) {
log.warn("Error while authenticating by certificate", e);
throw new IllegalStateException("Error while authenticating by certificate", e);
}
if (result == null) {
return null;
}
if (challengeResponse != null) {
if (result.user == null) {
return null;
}
user = (UserEntity) result.user;
} else {
log.debug("Returning authentication challenge for user: " + username);
response.challenge = result.challenge;
return response;
}
} else {
return null;
}
if (user == null) {
log.debug("Authentication request failed. Username=" + username);
return null;
}
log.debug("Successful authentication for user: " + user.key);
response.access = tokenHelpers.buildAccess(user);
return response;
}
use of org.platformlayer.auth.AuthenticatorException in project platformlayer by platformlayer.
the class KeychainResource method authorizeCertificateChain.
@POST
public ValidateTokenResponse authorizeCertificateChain(@QueryParam("project") String project, CertificateChainInfo chain) {
try {
requireSystemAccess();
} catch (AuthenticatorException e) {
log.warn("Error while checking system token", e);
throwInternalError();
}
UserEntity userEntity = null;
try {
boolean unlock = false;
userEntity = userAuthenticator.findUserFromKeychain(chain, unlock);
} catch (AuthenticatorException e) {
log.warn("Error while fetching user", e);
throwInternalError();
}
if (userEntity == null) {
throw404NotFound();
}
ValidateTokenResponse response = new ValidateTokenResponse();
response.access = new ValidateAccess();
response.access.user = Mapping.mapToUserValidation(userEntity);
// response.access.token = new Token();
// response.access.token.expires = checkTokenInfo.expiration;
// response.access.token.id = checkToken;
String checkProject = project;
if (checkProject != null) {
ProjectEntity projectEntity = null;
try {
projectEntity = userAuthenticator.findProject(checkProject);
} catch (AuthenticatorException e) {
log.warn("Error while fetching project", e);
throwInternalError();
}
if (projectEntity == null) {
throw404NotFound();
}
// Note that we do not unlock the user / project; we don't have any secret material
// TODO: We could return stuff encrypted with the user's public key
// projectEntity.unlockWithUser(userEntity);
//
// if (!projectEntity.isSecretValid()) {
// throw404NotFound();
// }
UserProjectEntity userProject = null;
try {
userProject = userAuthenticator.findUserProject(userEntity, projectEntity);
} catch (AuthenticatorException e) {
log.warn("Error while fetching project", e);
throwInternalError();
}
if (userProject == null) {
// Not a member of project
throw404NotFound();
}
response.access.project = Mapping.mapToProject(projectEntity);
response.access.project.roles = Mapping.mapToRoles(userProject.getRoles());
}
return response;
}
use of org.platformlayer.auth.AuthenticatorException in project platformlayer by platformlayer.
the class ServicesResource method checkServiceAccess.
@POST
@Path("check")
public CheckServiceAccessResponse checkServiceAccess(CheckServiceAccessRequest request) {
try {
requireSystemAccess();
} catch (AuthenticatorException e) {
log.warn("Error while checking system token", e);
throwInternalError();
}
ServiceAccountEntity serviceAccount = null;
try {
serviceAccount = systemAuthenticator.authenticate(request.chain);
} catch (AuthenticatorException e) {
log.warn("Error while authenticating chain", e);
throwInternalError();
}
CheckServiceAccessResponse response = new CheckServiceAccessResponse();
if (serviceAccount != null) {
response.serviceAccount = serviceAccount.subject;
}
return response;
}
use of org.platformlayer.auth.AuthenticatorException in project platformlayer by platformlayer.
the class ClientCertificateSystemAuthenticator method authenticate.
@Override
public ServiceAccountEntity authenticate(CertificateChainInfo certChainInfo) throws AuthenticatorException {
if (certChainInfo.certificates.size() == 0) {
log.debug("Chain empty; can't authenticate");
return null;
}
// If it's a single cert; we check the cert.
// Otherwise, we assume a CA signed the tail cert, so we check the penultimate cert
CertificateInfo inspect;
if (certChainInfo.certificates.size() == 1) {
inspect = certChainInfo.certificates.get(0);
} else {
inspect = certChainInfo.certificates.get(1);
}
String subject = inspect.subjectDN;
if (Strings.isNullOrEmpty(inspect.publicKey)) {
throw new IllegalArgumentException();
}
byte[] publicKey = Hex.fromHex(inspect.publicKey);
ServiceAccountEntity auth;
try {
auth = repository.findServiceAccount(subject, publicKey);
} catch (RepositoryException e) {
throw new AuthenticatorException("Error while authenticating user", e);
}
if (auth == null) {
log.debug("Certificate validation failed (though the caller was authenticated)");
log.debug("Certificate validation failed - public key not recognized: " + Hex.toHex(publicKey));
log.debug("Certificate validation failed - chain: " + certChainInfo);
}
return auth;
}
use of org.platformlayer.auth.AuthenticatorException in project platformlayer by platformlayer.
the class KeystoneRepositoryAuthenticator method findUserFromKeychain.
@Override
public UserEntity findUserFromKeychain(CertificateChainInfo chain, boolean unlock) throws AuthenticatorException {
if (chain.certificates == null || chain.certificates.isEmpty()) {
return null;
}
for (int i = 0; i < chain.certificates.size(); i++) {
String publicKeyHash = chain.certificates.get(i).publicKeyHash;
if (Strings.isNullOrEmpty(publicKeyHash)) {
continue;
}
log.debug("Checking publicKeyHash: " + publicKeyHash);
byte[] hash = Hex.fromHex(publicKeyHash);
UserEntity user;
try {
user = repository.findUserByPublicKey(hash);
} catch (RepositoryException e) {
throw new AuthenticatorException("Error while authenticating user", e);
}
if (user != null) {
return user;
}
}
return null;
}
Aggregations