-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from xenit-eu/ACC-1450-delegated-authmanager
ACC-1459: authentication for delegated access tokens
- Loading branch information
Showing
14 changed files
with
378 additions
and
35 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...grid/gateway/runtime/security/authority/ExtensionDelegationGrantedAuthorityConverter.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,45 @@ | ||
package com.contentgrid.gateway.runtime.security.authority; | ||
|
||
import com.contentgrid.gateway.runtime.security.jwt.ContentGridClaimNames; | ||
import com.contentgrid.gateway.security.authority.Actor; | ||
import com.contentgrid.gateway.security.authority.DelegatedAuthenticationDetailsGrantedAuthority; | ||
import com.contentgrid.gateway.security.jwt.issuer.encrypt.TextEncryptorFactory; | ||
import com.nimbusds.jwt.JWTClaimsSet; | ||
import java.text.ParseException; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.core.ClaimAccessor; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
|
||
@RequiredArgsConstructor | ||
public class ExtensionDelegationGrantedAuthorityConverter implements | ||
Converter<Jwt, Collection<GrantedAuthority>> { | ||
|
||
private final TextEncryptorFactory encryptorFactory; | ||
private final Converter<ClaimAccessor, Actor> actorConverter; | ||
|
||
@Override | ||
public Collection<GrantedAuthority> convert(Jwt source) { | ||
var principal = actorConverter.convert(decryptClaims(source.getClaimAsString(ContentGridClaimNames.RESTRICT_PRINCIPAL_CLAIMS))); | ||
if (principal == null) { | ||
return null; | ||
} | ||
var actor = actorConverter.convert(() -> source.getClaimAsMap(ContentGridClaimNames.ACT)); | ||
if (actor == null) { | ||
return null; | ||
} | ||
return List.of(new DelegatedAuthenticationDetailsGrantedAuthority(principal, actor)); | ||
} | ||
|
||
@SneakyThrows(ParseException.class) | ||
private ClaimAccessor decryptClaims(String encryptedClaims) { | ||
var decryptedClaimsString = encryptorFactory.newEncryptor().decrypt(encryptedClaims); | ||
var claimSet = JWTClaimsSet.parse(decryptedClaimsString); | ||
return claimSet::toJSONObject; | ||
} | ||
|
||
} |
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
28 changes: 28 additions & 0 deletions
28
src/main/java/com/contentgrid/gateway/runtime/security/jwt/ContentGridAudiences.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,28 @@ | ||
package com.contentgrid.gateway.runtime.security.jwt; | ||
|
||
import com.contentgrid.gateway.runtime.application.ApplicationId; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class ContentGridAudiences { | ||
|
||
/** | ||
* Audience for the 'authentication' endpoint | ||
* @see <a href="https://github.com/xenit-eu/contentgrid-system-design/blob/main/specs/automation-extension-authentication.md#client-facing-token-exchange">Automation extension authentication spec</a> | ||
*/ | ||
public static final String SYSTEM_ENDPOINT_AUTHENTICATION = systemEndpoint("authentication"); | ||
|
||
public static String systemEndpoint(String endpointId) { | ||
return "contentgrid:system:endpoints:"+endpointId; | ||
} | ||
|
||
/** | ||
* Audience for an application | ||
* | ||
* @see <a href="https://github.com/xenit-eu/contentgrid-system-design/blob/main/specs/automation-extension-authentication.md#gateway-extension">Automation extension authentication spec</a> | ||
*/ | ||
public static String application(ApplicationId applicationId) { | ||
return "contentgrid:application:"+applicationId.getValue(); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/contentgrid/gateway/runtime/security/jwt/ContentGridClaimNames.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,31 @@ | ||
package com.contentgrid.gateway.runtime.security.jwt; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class ContentGridClaimNames { | ||
|
||
/** | ||
* Contains encrypted claims of the principal in a delegated authentication token | ||
* @see <a href="https://github.com/xenit-eu/contentgrid-system-design/blob/main/specs/automation-extension-authentication.md#additional-jwt-claims">Automation extension authentication spec</a> | ||
*/ | ||
public static final String RESTRICT_PRINCIPAL_CLAIMS = "restrict:principal_claims"; | ||
|
||
/** | ||
* The application ID ({@link com.contentgrid.gateway.runtime.application.ApplicationId}) for which the token is valid | ||
* @see <a href="https://github.com/xenit-eu/contentgrid-system-design/blob/main/specs/automation-extension-authentication.md#additional-jwt-claims">Automation extension authentication spec</a> | ||
*/ | ||
public static final String CONTEXT_APPLICATION_ID = "context:application:id"; | ||
|
||
/** | ||
* All domain names belonging to the application for which the token is valid | ||
* @see <a href="https://github.com/xenit-eu/contentgrid-system-design/blob/main/specs/automation-extension-authentication.md#additional-jwt-claims">Automation extension authentication spec</a> | ||
*/ | ||
public static final String CONTEXT_APPLICATION_DOMAINS = "context:application:domains"; | ||
|
||
/** | ||
* Contains the claims of the actor in a delegated authentication token | ||
* @see <a href="https://www.rfc-editor.org/rfc/rfc8693.html#name-act-actor-claim">RFC8693</a> | ||
*/ | ||
public static final String ACT = "act"; | ||
} |
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
Oops, something went wrong.