Search in sources :

Example 1 with TokenValue

use of oidc.model.TokenValue in project OpenConext-oidcng by OpenConext.

the class AuthorizationEndpoint method authorizationEndpointResponse.

private Map<String, Object> authorizationEndpointResponse(User user, OpenIDClient client, AuthorizationRequest authorizationRequest, List<String> scopes, ResponseType responseType, State state) {
    Map<String, Object> result = new LinkedHashMap<>();
    EncryptedTokenValue encryptedAccessToken = tokenGenerator.generateAccessTokenWithEmbeddedUserInfo(user, client, scopes);
    if (responseType.contains(ResponseType.Value.TOKEN.getValue()) || !isOpenIDRequest(authorizationRequest)) {
        String unspecifiedUrnHash = KeyGenerator.oneWayHash(user.getUnspecifiedNameId(), this.salt);
        AccessToken accessToken = new AccessToken(encryptedAccessToken.getJwtId(), user.getSub(), client.getClientId(), scopes, encryptedAccessToken.getKeyId(), accessTokenValidity(client), false, null, unspecifiedUrnHash);
        accessTokenRepository.insert(accessToken);
        result.put("access_token", encryptedAccessToken.getValue());
        result.put("token_type", "Bearer");
    }
    if (responseType.contains(ResponseType.Value.CODE.getValue())) {
        AuthorizationCode authorizationCode = createAndSaveAuthorizationCode(authorizationRequest, client, user);
        result.put("code", authorizationCode.getCode());
    }
    if (responseType.contains(OIDCResponseTypeValue.ID_TOKEN.getValue()) && isOpenIDRequest(scopes) && isOpenIDRequest(authorizationRequest)) {
        AuthenticationRequest authenticationRequest = (AuthenticationRequest) authorizationRequest;
        List<String> claims = getClaims(authorizationRequest);
        TokenValue tokenValue = tokenGenerator.generateIDTokenForAuthorizationEndpoint(user, client, authenticationRequest.getNonce(), responseType, encryptedAccessToken.getValue(), claims, Optional.ofNullable((String) result.get("code")), state);
        result.put("id_token", tokenValue.getValue());
    }
    result.put("expires_in", client.getAccessTokenValidity());
    if (state != null) {
        result.put("state", state.getValue());
    }
    return result;
}
Also used : AuthorizationCode(oidc.model.AuthorizationCode) AccessToken(oidc.model.AccessToken) EncryptedTokenValue(oidc.model.EncryptedTokenValue) AuthenticationRequest(com.nimbusds.openid.connect.sdk.AuthenticationRequest) LinkedHashMap(java.util.LinkedHashMap) TokenValue(oidc.model.TokenValue) EncryptedTokenValue(oidc.model.EncryptedTokenValue)

Example 2 with TokenValue

use of oidc.model.TokenValue in project OpenConext-oidcng by OpenConext.

the class TokenEndpoint method tokenEndpointResponse.

private Map<String, Object> tokenEndpointResponse(Optional<User> user, OpenIDClient client, List<String> scopes, List<String> idTokenClaims, boolean clientCredentials, String nonce, Optional<Long> authorizationTime, Optional<String> authorizationCodeId) {
    Map<String, Object> map = new LinkedHashMap<>();
    EncryptedTokenValue encryptedAccessToken = user.map(u -> tokenGenerator.generateAccessTokenWithEmbeddedUserInfo(u, client, scopes)).orElse(tokenGenerator.generateAccessToken(client, scopes));
    String sub = user.map(User::getSub).orElse(client.getClientId());
    String unspecifiedUrnHash = user.map(u -> KeyGenerator.oneWayHash(u.getUnspecifiedNameId(), this.salt)).orElse(null);
    AccessToken accessToken = new AccessToken(encryptedAccessToken.getJwtId(), sub, client.getClientId(), scopes, encryptedAccessToken.getKeyId(), accessTokenValidity(client), !user.isPresent(), authorizationCodeId.orElse(null), unspecifiedUrnHash);
    accessToken = accessTokenRepository.insert(accessToken);
    map.put("access_token", encryptedAccessToken.getValue());
    map.put("token_type", "Bearer");
    if (client.getGrants().contains(GrantType.REFRESH_TOKEN.getValue())) {
        EncryptedTokenValue encryptedRefreshToken = user.map(u -> tokenGenerator.generateRefreshTokenWithEmbeddedUserInfo(u, client)).orElse(tokenGenerator.generateRefreshToken(client));
        String refreshTokenValue = encryptedRefreshToken.getValue();
        refreshTokenRepository.insert(new RefreshToken(encryptedRefreshToken.getJwtId(), accessToken, refreshTokenValidity(client)));
        map.put("refresh_token", refreshTokenValue);
    }
    map.put("expires_in", client.getAccessTokenValidity());
    if (isOpenIDRequest(scopes) && !clientCredentials) {
        TokenValue tokenValue = tokenGenerator.generateIDTokenForTokenEndpoint(user, client, nonce, idTokenClaims, scopes, authorizationTime);
        map.put("id_token", tokenValue.getValue());
    }
    return map;
}
Also used : AuthorizationCodeRepository(oidc.repository.AuthorizationCodeRepository) CodeVerifierMissingException(oidc.exceptions.CodeVerifierMissingException) Date(java.util.Date) JOSEException(com.nimbusds.jose.JOSEException) ClientAuthentication(com.nimbusds.oauth2.sdk.auth.ClientAuthentication) User(oidc.model.User) MACVerifier(com.nimbusds.jose.crypto.MACVerifier) RefreshTokenGrant(com.nimbusds.oauth2.sdk.RefreshTokenGrant) Map(java.util.Map) ClientSecretJWT(com.nimbusds.oauth2.sdk.auth.ClientSecretJWT) CodeVerifier(com.nimbusds.oauth2.sdk.pkce.CodeVerifier) PostMapping(org.springframework.web.bind.annotation.PostMapping) APPLICATION_JSON(org.apache.http.entity.ContentType.APPLICATION_JSON) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) SignedJWT(com.nimbusds.jwt.SignedJWT) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) TokenRequest(com.nimbusds.oauth2.sdk.TokenRequest) List(java.util.List) OpenIDClientRepository(oidc.repository.OpenIDClientRepository) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Scope(oidc.model.Scope) Optional(java.util.Optional) TokenGenerator(oidc.secure.TokenGenerator) LogFactory(org.apache.commons.logging.LogFactory) Pattern(java.util.regex.Pattern) JWTAuthentication(com.nimbusds.oauth2.sdk.auth.JWTAuthentication) CodeChallengeMethod(com.nimbusds.oauth2.sdk.pkce.CodeChallengeMethod) AuthorizationCode(oidc.model.AuthorizationCode) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) GrantType(com.nimbusds.oauth2.sdk.GrantType) MessageDigest(java.security.MessageDigest) ServletUtils(com.nimbusds.oauth2.sdk.http.ServletUtils) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) RedirectMismatchException(oidc.exceptions.RedirectMismatchException) JWTAuthorizationGrantsException(oidc.exceptions.JWTAuthorizationGrantsException) UnknownClientException(oidc.exceptions.UnknownClientException) RefreshTokenRepository(oidc.repository.RefreshTokenRepository) Value(org.springframework.beans.factory.annotation.Value) LinkedHashMap(java.util.LinkedHashMap) CodeChallenge(com.nimbusds.oauth2.sdk.pkce.CodeChallenge) KeyGenerator(oidc.crypto.KeyGenerator) HttpServletRequest(javax.servlet.http.HttpServletRequest) TokenValue(oidc.model.TokenValue) InvalidClientException(oidc.exceptions.InvalidClientException) ParseException(com.nimbusds.oauth2.sdk.ParseException) PlainClientSecret(com.nimbusds.oauth2.sdk.auth.PlainClientSecret) OpenIDClient(oidc.model.OpenIDClient) MDCContext(oidc.log.MDCContext) JWTRequest(oidc.secure.JWTRequest) ClientCredentialsGrant(com.nimbusds.oauth2.sdk.ClientCredentialsGrant) HTTPRequest(com.nimbusds.oauth2.sdk.http.HTTPRequest) AuthorizationGrant(com.nimbusds.oauth2.sdk.AuthorizationGrant) AuthorizationCodeGrant(com.nimbusds.oauth2.sdk.AuthorizationCodeGrant) TokenAlreadyUsedException(oidc.exceptions.TokenAlreadyUsedException) InvalidGrantException(oidc.exceptions.InvalidGrantException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) AccessTokenRepository(oidc.repository.AccessTokenRepository) EncryptedTokenValue(oidc.model.EncryptedTokenValue) UserRepository(oidc.repository.UserRepository) HttpStatus(org.springframework.http.HttpStatus) AccessToken(oidc.model.AccessToken) BadJOSEException(com.nimbusds.jose.proc.BadJOSEException) UnauthorizedException(oidc.exceptions.UnauthorizedException) Clock(java.time.Clock) Log(org.apache.commons.logging.Log) ResponseEntity(org.springframework.http.ResponseEntity) NoSuchProviderException(java.security.NoSuchProviderException) Collections(java.util.Collections) AuthorizationEndpoint.validateScopes(oidc.endpoints.AuthorizationEndpoint.validateScopes) RefreshToken(oidc.model.RefreshToken) RefreshToken(oidc.model.RefreshToken) AccessToken(oidc.model.AccessToken) EncryptedTokenValue(oidc.model.EncryptedTokenValue) LinkedHashMap(java.util.LinkedHashMap) TokenValue(oidc.model.TokenValue) EncryptedTokenValue(oidc.model.EncryptedTokenValue)

Example 3 with TokenValue

use of oidc.model.TokenValue in project OpenConext-oidcng by OpenConext.

the class TokenGeneratorTest method defaultAcrValue.

@Test
public void defaultAcrValue() throws IOException, JOSEException, NoSuchAlgorithmException, NoSuchProviderException, ParseException {
    User user = new User("sub", "unspecifiedNameId", "http://mockidp", "clientId", getUserInfo(), emptyList());
    OpenIDClient client = openIDClient("mock-sp");
    TokenValue tokenValue = tokenGenerator.generateIDTokenForTokenEndpoint(Optional.of(user), client, "nonce", emptyList(), emptyList(), Optional.empty());
    SignedJWT jwt = SignedJWT.parse(tokenValue.getValue());
    Object acr = jwt.getJWTClaimsSet().getClaim("acr");
    assertEquals("http://test.surfconext.nl/assurance/loa1", acr);
}
Also used : User(oidc.model.User) OpenIDClient(oidc.model.OpenIDClient) SignedJWT(com.nimbusds.jwt.SignedJWT) TokenValue(oidc.model.TokenValue) EncryptedTokenValue(oidc.model.EncryptedTokenValue) AbstractIntegrationTest(oidc.AbstractIntegrationTest) Test(org.junit.Test)

Example 4 with TokenValue

use of oidc.model.TokenValue in project OpenConext-oidcng by OpenConext.

the class TokenGeneratorTest method invalidAcrValueIsAllowed.

@Test
public void invalidAcrValueIsAllowed() throws IOException, ParseException {
    User user = new User("sub", "unspecifiedNameId", "http://mockidp", "clientId", getUserInfo(), Arrays.asList("http://test.surfconext.nl/assurance/loa3", "invalid_acr"));
    OpenIDClient client = openIDClient("mock-sp");
    TokenValue tokenValue = tokenGenerator.generateIDTokenForTokenEndpoint(Optional.of(user), client, "nonce", emptyList(), emptyList(), Optional.empty());
    SignedJWT jwt = SignedJWT.parse(tokenValue.getValue());
    Object acr = jwt.getJWTClaimsSet().getClaim("acr");
    assertEquals("http://test.surfconext.nl/assurance/loa3 invalid_acr", acr);
}
Also used : User(oidc.model.User) OpenIDClient(oidc.model.OpenIDClient) SignedJWT(com.nimbusds.jwt.SignedJWT) TokenValue(oidc.model.TokenValue) EncryptedTokenValue(oidc.model.EncryptedTokenValue) AbstractIntegrationTest(oidc.AbstractIntegrationTest) Test(org.junit.Test)

Aggregations

EncryptedTokenValue (oidc.model.EncryptedTokenValue)4 TokenValue (oidc.model.TokenValue)4 SignedJWT (com.nimbusds.jwt.SignedJWT)3 OpenIDClient (oidc.model.OpenIDClient)3 User (oidc.model.User)3 LinkedHashMap (java.util.LinkedHashMap)2 AbstractIntegrationTest (oidc.AbstractIntegrationTest)2 Test (org.junit.Test)2 JOSEException (com.nimbusds.jose.JOSEException)1 MACVerifier (com.nimbusds.jose.crypto.MACVerifier)1 BadJOSEException (com.nimbusds.jose.proc.BadJOSEException)1 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 AuthorizationCodeGrant (com.nimbusds.oauth2.sdk.AuthorizationCodeGrant)1 AuthorizationGrant (com.nimbusds.oauth2.sdk.AuthorizationGrant)1 ClientCredentialsGrant (com.nimbusds.oauth2.sdk.ClientCredentialsGrant)1 GrantType (com.nimbusds.oauth2.sdk.GrantType)1 ParseException (com.nimbusds.oauth2.sdk.ParseException)1 RefreshTokenGrant (com.nimbusds.oauth2.sdk.RefreshTokenGrant)1 TokenRequest (com.nimbusds.oauth2.sdk.TokenRequest)1 ClientAuthentication (com.nimbusds.oauth2.sdk.auth.ClientAuthentication)1