use of org.candlepin.common.exceptions.ServiceUnavailableException in project candlepin by candlepin.
the class BasicAuth method getPrincipal.
@Override
public Principal getPrincipal(HttpRequest httpRequest) {
try {
String auth = AuthUtil.getHeader(httpRequest, "Authorization");
if (auth != null && auth.toUpperCase().startsWith("BASIC ")) {
String userpassEncoded = auth.substring(6);
String[] userpass = new String(Base64.decodeBase64(userpassEncoded)).split(":", 2);
String username = userpass[0];
String password = null;
if (userpass.length > 1) {
password = userpass[1];
}
if (log.isDebugEnabled()) {
Integer length = (password == null) ? 0 : password.length();
log.debug("check for: {} - password of length {}", username, length);
}
if (userServiceAdapter.validateUser(username, password)) {
Principal principal = createPrincipal(username);
log.debug("principal created for user '{}'", username);
return principal;
} else {
throw new NotAuthorizedException(i18n.get().tr("Invalid Credentials"));
}
}
} catch (CandlepinException e) {
if (log.isDebugEnabled()) {
log.debug("Error getting principal " + e);
}
throw e;
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Error getting principal " + e);
}
throw new ServiceUnavailableException(i18n.get().tr("Error contacting user service"));
}
return null;
}
Aggregations