Search in sources :

Example 1 with ClientAuthenticationMethod

use of com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod in project di-authentication-api by alphagov.

the class TokenService method generateClientCredentialsSelector.

private ClientCredentialsSelector<?> generateClientCredentialsSelector(String publicKey) {
    return new ClientCredentialsSelector<>() {

        @Override
        public List<Secret> selectClientSecrets(ClientID claimedClientID, ClientAuthenticationMethod authMethod, com.nimbusds.oauth2.sdk.auth.verifier.Context context) {
            return null;
        }

        @Override
        public List<PublicKey> selectPublicKeys(ClientID claimedClientID, ClientAuthenticationMethod authMethod, JWSHeader jwsHeader, boolean forceRefresh, com.nimbusds.oauth2.sdk.auth.verifier.Context context) {
            byte[] decodedKey = Base64.getMimeDecoder().decode(publicKey);
            try {
                X509EncodedKeySpec x509publicKey = new X509EncodedKeySpec(decodedKey);
                KeyFactory kf = KeyFactory.getInstance("RSA");
                return Collections.singletonList(kf.generatePublic(x509publicKey));
            } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
        }
    };
}
Also used : PublicKey(java.security.PublicKey) ClientAuthenticationMethod(com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientCredentialsSelector(com.nimbusds.oauth2.sdk.auth.verifier.ClientCredentialsSelector) Secret(com.nimbusds.oauth2.sdk.auth.Secret) ClientID(com.nimbusds.oauth2.sdk.id.ClientID) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) JWSHeader(com.nimbusds.jose.JWSHeader) KeyFactory(java.security.KeyFactory)

Example 2 with ClientAuthenticationMethod

use of com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod in project Kustvakt by KorAP.

the class OpenIdTokenService method extractClientCredentials.

private String[] extractClientCredentials(ClientAuthentication clientAuthentication) throws KustvaktException {
    ClientAuthenticationMethod method = clientAuthentication.getMethod();
    String clientSecret;
    String clientId;
    if (method.equals(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)) {
        ClientSecretBasic basic = (ClientSecretBasic) clientAuthentication;
        clientSecret = basic.getClientSecret().getValue();
        clientId = basic.getClientID().getValue();
    } else if (method.equals(ClientAuthenticationMethod.CLIENT_SECRET_POST)) {
        ClientSecretPost post = (ClientSecretPost) clientAuthentication;
        clientSecret = post.getClientSecret().getValue();
        clientId = post.getClientID().getValue();
    } else {
        // client authentication method is not supported
        throw new KustvaktException(StatusCodes.UNSUPPORTED_AUTHENTICATION_METHOD, method.getValue() + " is not supported.", OAuth2Error.INVALID_CLIENT);
    }
    return new String[] { clientId, clientSecret };
}
Also used : ClientSecretPost(com.nimbusds.oauth2.sdk.auth.ClientSecretPost) KustvaktException(de.ids_mannheim.korap.exceptions.KustvaktException) ClientAuthenticationMethod(com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod) ClientSecretBasic(com.nimbusds.oauth2.sdk.auth.ClientSecretBasic)

Example 3 with ClientAuthenticationMethod

use of com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod in project di-ipv-cri-uk-passport-back by alphagov.

the class TokenRequestValidator method authenticateClientWithJwt.

private void authenticateClientWithJwt(String requestBody) throws ClientAuthenticationException {
    PrivateKeyJWT clientJwt;
    try {
        clientJwt = PrivateKeyJWT.parse(requestBody);
        String clientId = clientJwt.getClientID().getValue();
        String clientAuthenticationMethod = configurationService.getClientAuthenticationMethod(clientId);
        if (clientAuthenticationMethod.equals(NONE)) {
            return;
        }
        verifier.verify(clientJwtWithConcatSignature(clientJwt, requestBody), null, null);
        validateMaxAllowedAuthClientTtl(clientJwt.getJWTAuthenticationClaimsSet());
    } catch (ParseException | InvalidClientException | JOSEException | java.text.ParseException e) {
        LOGGER.error("Validation of client_assertion jwt failed");
        throw new ClientAuthenticationException(e);
    }
}
Also used : ClientAuthenticationException(uk.gov.di.ipv.cri.passport.accesstoken.exceptions.ClientAuthenticationException) PrivateKeyJWT(com.nimbusds.oauth2.sdk.auth.PrivateKeyJWT) InvalidClientException(com.nimbusds.oauth2.sdk.auth.verifier.InvalidClientException) ParseException(com.nimbusds.oauth2.sdk.ParseException) JOSEException(com.nimbusds.jose.JOSEException)

Aggregations

ClientAuthenticationMethod (com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod)2 JOSEException (com.nimbusds.jose.JOSEException)1 JWSHeader (com.nimbusds.jose.JWSHeader)1 ParseException (com.nimbusds.oauth2.sdk.ParseException)1 ClientSecretBasic (com.nimbusds.oauth2.sdk.auth.ClientSecretBasic)1 ClientSecretPost (com.nimbusds.oauth2.sdk.auth.ClientSecretPost)1 PrivateKeyJWT (com.nimbusds.oauth2.sdk.auth.PrivateKeyJWT)1 Secret (com.nimbusds.oauth2.sdk.auth.Secret)1 ClientCredentialsSelector (com.nimbusds.oauth2.sdk.auth.verifier.ClientCredentialsSelector)1 InvalidClientException (com.nimbusds.oauth2.sdk.auth.verifier.InvalidClientException)1 ClientID (com.nimbusds.oauth2.sdk.id.ClientID)1 KustvaktException (de.ids_mannheim.korap.exceptions.KustvaktException)1 KeyFactory (java.security.KeyFactory)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PublicKey (java.security.PublicKey)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 ClientAuthenticationException (uk.gov.di.ipv.cri.passport.accesstoken.exceptions.ClientAuthenticationException)1