-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task: [CCLS 2237] Improve accessibility for consuming apps (#5)
* implement auth context holder, use checkstyle * ignore casing in application properties * update readme * update readme
- Loading branch information
Showing
20 changed files
with
946 additions
and
427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
plugins { | ||
id 'net.researchgate.release' | ||
id 'checkstyle' | ||
} | ||
|
||
subprojects { | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
...er-auth/src/main/java/uk/gov/laa/ccms/springboot/auth/ApiAuthenticationContextHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package uk.gov.laa.ccms.springboot.auth; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.annotation.PostConstruct; | ||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.InvalidPropertyException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Holds the authentication context for the API. | ||
*/ | ||
@Slf4j | ||
@Component | ||
public class ApiAuthenticationContextHolder { | ||
|
||
private final AuthenticationProperties authenticationProperties; | ||
|
||
@Getter | ||
private String authenticationHeader; | ||
|
||
@Getter | ||
private String[] unprotectedUris; | ||
|
||
@Getter | ||
private Set<ClientCredential> clientCredentials; | ||
|
||
@Getter | ||
private Set<AuthorizedRole> authorizedRoles; | ||
|
||
@Autowired | ||
public ApiAuthenticationContextHolder(AuthenticationProperties authenticationProperties) { | ||
this.authenticationProperties = authenticationProperties; | ||
} | ||
|
||
/** | ||
* Load authentication context, including authorized clients and roles from JSON. | ||
*/ | ||
@PostConstruct | ||
private void initialize() { | ||
authenticationHeader = authenticationProperties.getAuthenticationHeader(); | ||
unprotectedUris = authenticationProperties.getUnprotectedUris(); | ||
initializeAuthorizedClients(); | ||
initializeAuthorizedRoles(); | ||
} | ||
|
||
/** | ||
* Initialise a set of {@link ClientCredential} from those configured as a JSON string in the | ||
* application properties. | ||
*/ | ||
private void initializeAuthorizedClients() { | ||
try { | ||
clientCredentials = | ||
new ObjectMapper() | ||
.readValue( | ||
authenticationProperties.getAuthorizedClients(), | ||
new TypeReference<Set<ClientCredential>>() {}); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
if (clientCredentials.isEmpty()) { | ||
throw new InvalidPropertyException( | ||
AuthenticationProperties.class, | ||
"authorizedClients", | ||
"At least one authorized client must be provided."); | ||
} | ||
|
||
for (ClientCredential clientCredential : clientCredentials) { | ||
log.info( | ||
"Authorized Client Registered: '{}' Roles: {}", | ||
clientCredential.name(), | ||
clientCredential.roles().toString()); | ||
} | ||
} | ||
|
||
/** | ||
* Initialise a set of {@link AuthorizedRole} from those configured as a JSON string in the | ||
* application properties. | ||
*/ | ||
private void initializeAuthorizedRoles() { | ||
|
||
try { | ||
authorizedRoles = | ||
new ObjectMapper() | ||
.readValue( | ||
authenticationProperties.getAuthorizedRoles(), | ||
new TypeReference<Set<AuthorizedRole>>() {}); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
if (authorizedRoles.isEmpty()) { | ||
throw new InvalidPropertyException( | ||
AuthenticationProperties.class, | ||
"authorizedRoles", | ||
"At least one authorized role must be provided."); | ||
} | ||
|
||
for (AuthorizedRole authorizedRole : authorizedRoles) { | ||
log.info("Authorized Role Registered: '{}'", authorizedRole.name()); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve the client details based on the access token provided. | ||
* | ||
* @param accessToken the client-provided access token | ||
* @return the {@link ClientCredential} associated with the access token | ||
*/ | ||
public Optional<ClientCredential> getMatchingClientCredential(String accessToken) { | ||
return clientCredentials.stream() | ||
.filter(credential -> credential.token().equals(accessToken)) | ||
.findFirst(); | ||
} | ||
|
||
/** | ||
* Retrieve a list of roles associated with the client, based on the access token provided. If the | ||
* client is not in the authorized list, no roles are returned. | ||
* | ||
* @param accessToken the client-provided access token | ||
* @return the set of roles associated with the access token, if authorized | ||
*/ | ||
public Set<String> getClientRoles(String accessToken) { | ||
return getMatchingClientCredential(accessToken) | ||
.map(ClientCredential::roles) | ||
.orElse(Collections.emptySet()); | ||
} | ||
|
||
/** | ||
* Determine whether there is a client associated with the provided token. | ||
* | ||
* @param accessToken the client-provided access token | ||
* @return {@code true} if the access token is authorized and {@code false} otherwise. | ||
*/ | ||
public boolean clientExistsForToken(String accessToken) { | ||
return getMatchingClientCredential(accessToken).isPresent(); | ||
} | ||
|
||
/** | ||
* Retrieve the principal (client name) based on the access token provided. | ||
* | ||
* @param accessToken the client-provided access token | ||
* @return the principal (client name) associated with the access token | ||
*/ | ||
public String getPrincipal(String accessToken) { | ||
return getMatchingClientCredential(accessToken).map(ClientCredential::name).orElse(null); | ||
} | ||
} |
Oops, something went wrong.