use of org.apache.commons.codec.Decoder in project archiva by apache.
the class RssFeedServlet method isAllowed.
/**
* Basic authentication.
*
* @param req
* @param repositoryId TODO
* @param groupId TODO
* @param artifactId TODO
* @return
*/
private boolean isAllowed(HttpServletRequest req, String repositoryId, String groupId, String artifactId) throws UserNotFoundException, AccountLockedException, AuthenticationException, MustChangePasswordException, UnauthorizedException {
String auth = req.getHeader("Authorization");
List<String> repoIds = new ArrayList<>();
if (repositoryId != null) {
repoIds.add(repositoryId);
} else if (artifactId != null && groupId != null) {
if (auth != null) {
if (!auth.toUpperCase().startsWith("BASIC ")) {
return false;
}
Decoder dec = new Base64();
String usernamePassword = "";
try {
usernamePassword = new String((byte[]) dec.decode(auth.substring(6).getBytes()));
} catch (DecoderException ie) {
log.warn("Error decoding username and password: {}", ie.getMessage());
}
if (usernamePassword == null || usernamePassword.trim().equals("")) {
repoIds = getObservableRepos(UserManager.GUEST_USERNAME);
} else {
String[] userCredentials = usernamePassword.split(":");
repoIds = getObservableRepos(userCredentials[0]);
}
} else {
repoIds = getObservableRepos(UserManager.GUEST_USERNAME);
}
} else {
return false;
}
for (String repoId : repoIds) {
try {
AuthenticationResult result = httpAuth.getAuthenticationResult(req, null);
SecuritySession securitySession = httpAuth.getSecuritySession(req.getSession(true));
if (//
servletAuth.isAuthenticated(req, result) && //
servletAuth.isAuthorized(//
req, //
securitySession, //
repoId, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS)) {
return true;
}
} catch (AuthorizationException e) {
log.debug("AuthorizationException for repoId: {}", repoId);
} catch (UnauthorizedException e) {
log.debug("UnauthorizedException for repoId: {}", repoId);
}
}
throw new UnauthorizedException("Access denied.");
}
Aggregations