Rename role -> permissionRole

This commit is contained in:
René Pfeuffer
2020-10-01 10:18:48 +02:00
parent 4ec75781b7
commit bd247a4332
8 changed files with 30 additions and 20 deletions

View File

@@ -30,12 +30,16 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.validation.constraints.NotEmpty;
@Getter
@Setter
@NoArgsConstructor
public class ApiKeyDto extends HalRepresentation {
@NotEmpty
private String displayName;
private String role;
@NotEmpty
private String permissionRole;
public ApiKeyDto(Links links) {
super(links);

View File

@@ -154,7 +154,7 @@ public class ApiKeyResource {
schema = @Schema(implementation = ErrorDto.class)
))
public Response create(@Valid ApiKeyDto apiKey) {
final ApiKeyService.CreationResult newKey = apiKeyService.createNewKey(apiKey.getDisplayName(), apiKey.getRole());
final ApiKeyService.CreationResult newKey = apiKeyService.createNewKey(apiKey.getDisplayName(), apiKey.getPermissionRole());
return Response.status(CREATED)
.entity(newKey.getToken())
.location(URI.create(resourceLinks.apiKey().self(newKey.getId())))

View File

@@ -32,9 +32,9 @@ import lombok.Getter;
public class ApiKey {
private final String id;
private final String displayName;
private final String role;
private final String permissionRole;
ApiKey(ApiKeyWithPassphrase apiKeyWithPassphrase) {
this(apiKeyWithPassphrase.getId(), apiKeyWithPassphrase.getDisplayName(), apiKeyWithPassphrase.getRole());
this(apiKeyWithPassphrase.getId(), apiKeyWithPassphrase.getDisplayName(), apiKeyWithPassphrase.getPermissionRole());
}
}

View File

@@ -31,6 +31,7 @@ import lombok.NoArgsConstructor;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.Collection;
@@ -42,8 +43,9 @@ import static java.util.stream.Collectors.toList;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@XmlRootElement(name = "keys")
class ApiKeyCollection {
@XmlElement(name = "key")
private Collection<ApiKeyWithPassphrase> keys;
public ApiKeyCollection add(ApiKeyWithPassphrase key) {

View File

@@ -66,9 +66,9 @@ public class ApiKeyRealm extends AuthenticatingRealm {
checkArgument(token instanceof BearerToken, "%s is required", BearerToken.class);
BearerToken bt = (BearerToken) token;
ApiKeyService.CheckResult check = apiKeyService.check(bt.getCredentials());
RepositoryRole repositoryRole = repositoryRoleManager.get(check.getRole());
RepositoryRole repositoryRole = repositoryRoleManager.get(check.getPermissionRole());
if (repositoryRole == null) {
throw new AuthorizationException("api key has unknown role: " + check.getRole());
throw new AuthorizationException("api key has unknown role: " + check.getPermissionRole());
}
String scope = "repository:" + String.join(",", repositoryRole.getVerbs()) + ":*";
return helper

View File

@@ -72,12 +72,12 @@ public class ApiKeyService {
this.passphraseGenerator = passphraseGenerator;
}
public CreationResult createNewKey(String name, String role) {
public CreationResult createNewKey(String name, String permissionRole) {
String user = currentUser();
String passphrase = passphraseGenerator.get();
String hashedPassphrase = passwordService.encryptPassword(passphrase);
final String id = keyGenerator.createKey();
ApiKeyWithPassphrase key = new ApiKeyWithPassphrase(id, name, role, hashedPassphrase);
ApiKeyWithPassphrase key = new ApiKeyWithPassphrase(id, name, permissionRole, hashedPassphrase);
Lock lock = locks.get(user).writeLock();
lock.lock();
try {
@@ -132,7 +132,7 @@ public class ApiKeyService {
.stream()
.filter(key -> key.getId().equals(id))
.filter(key -> passwordService.passwordsMatch(passphrase, key.getPassphrase()))
.map(ApiKeyWithPassphrase::getRole)
.map(ApiKeyWithPassphrase::getPermissionRole)
.map(role -> new CheckResult(user, role))
.findAny()
.orElseThrow(AuthorizationException::new);
@@ -183,6 +183,6 @@ public class ApiKeyService {
@AllArgsConstructor
public static class CheckResult {
private final String user;
private final String role;
private final String permissionRole;
}
}

View File

@@ -31,6 +31,7 @@ import lombok.NoArgsConstructor;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@@ -38,7 +39,9 @@ import javax.xml.bind.annotation.XmlAccessorType;
@XmlAccessorType(XmlAccessType.FIELD)
class ApiKeyWithPassphrase {
private String id;
@XmlElement(name = "display-name")
private String displayName;
private String role;
@XmlElement(name = "permission-role")
private String permissionRole;
private String passphrase;
}