super(userServiceAdapter, injector);
}
@Override
public Principal getPrincipal(HttpRequest request) {
I18n i18n = injector.getInstance(I18n.class);
try {
String auth = AuthUtil.getHeader(request, "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];
}
log.debug("check for: " + username + " - password of length #" +
(password == null ? 0 : password.length()) + " = <omitted>");
if (userServiceAdapter.validateUser(username, password)) {
Principal principal = createPrincipal(username);
if (log.isDebugEnabled()) {
log.debug("principal created for user '" + username);
}
return principal;
}
else {
throw new UnauthorizedException(i18n.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
.tr("Error contacting user service"));
}
return null;
}