Skip to content

Commit

Permalink
Clean up class
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeuffer committed Oct 5, 2020
1 parent b3b887d commit 2e11491
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions scm-webapp/src/main/java/sonia/scm/security/ApiKeyRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
"%s is required", BearerToken.class);
String password = getPassword(token);
ApiKeyService.CheckResult check = apiKeyService.check(password);
RepositoryRole repositoryRole = repositoryRoleManager.get(check.getPermissionRole());
if (repositoryRole == null) {
throw new AuthorizationException("api key has unknown role: " + check.getPermissionRole());
}
String scope = "repository:" + String.join(",", repositoryRole.getVerbs()) + ":*";
return buildAuthenticationInfo(token, check);
}

private AuthenticationInfo buildAuthenticationInfo(AuthenticationToken token, ApiKeyService.CheckResult check) {
RepositoryRole repositoryRole = determineRole(check);
Scope scope = createScope(repositoryRole);
return helper
.authenticationInfoBuilder(check.getUser())
.withSessionId(getPrincipal(token))
.withScope(Scope.valueOf(scope))
.withScope(scope)
.build();
}

Expand All @@ -88,11 +89,23 @@ private String getPassword(AuthenticationToken token) {
}
}

private RepositoryRole determineRole(ApiKeyService.CheckResult check) {
RepositoryRole repositoryRole = repositoryRoleManager.get(check.getPermissionRole());
if (repositoryRole == null) {
throw new AuthorizationException("api key has unknown role: " + check.getPermissionRole());
}
return repositoryRole;
}

private Scope createScope(RepositoryRole repositoryRole) {
return Scope.valueOf("repository:" + String.join(",", repositoryRole.getVerbs()) + ":*");
}

private SessionId getPrincipal(AuthenticationToken token) {
if (token instanceof BearerToken) {
return ((BearerToken) token).getPrincipal();
} else {
return SessionId.valueOf((((UsernamePasswordToken) token).getPrincipal()).toString());
return SessionId.valueOf((token.getPrincipal()).toString());
}
}
}

0 comments on commit 2e11491

Please sign in to comment.