Class TestTokenHelper


  • public final class TestTokenHelper
    extends Object
    A helper class for generating tokens for testing.
    • Constructor Detail

      • TestTokenHelper

        private TestTokenHelper()
        Private constructor.
    • Method Detail

      • createBasicClaims

        public static com.nimbusds.jwt.JWTClaimsSet createBasicClaims​(Map<String,​Object> overrideClaims)
        Build a basic JWTClaimsSet from the supplied parameters.
        Parameters:
        overrideClaims - claims to override
        Returns:
        the JWT claims set.
      • createBasicUserInfoClaims

        public static com.nimbusds.jwt.JWTClaimsSet createBasicUserInfoClaims​(Map<String,​Object> overrideClaims)
        Build a basic JWTClaimsSet from the supplied parameters to mock a UserInfo response.
        Parameters:
        overrideClaims - claims to override
        Returns:
        the JWT claims set.
      • getClaimValue

        private static <T> T getClaimValue​(Map<String,​Object> overrideClaims,
                                           String claimName,
                                           Object defaultValue,
                                           Class<T> type)
        Extract any override from the overrides map, or return the default value if there is none.
        Type Parameters:
        T - the type to look for and return
        Parameters:
        overrideClaims - the overrides map
        claimName - the claim name
        defaultValue - the default value if no override is found
        type - the type to return
        Returns:
        the override or the default value.
      • createJWT

        public static com.nimbusds.jwt.JWT createJWT​(@Nonnull
                                                     com.nimbusds.jwt.JWTClaimsSet payload,
                                                     @Nullable
                                                     com.nimbusds.jose.JWSAlgorithm sigAlg,
                                                     @Nullable
                                                     com.nimbusds.jose.JWEAlgorithm jweAlg,
                                                     @Nullable
                                                     com.nimbusds.jose.EncryptionMethod enc,
                                                     @Nullable
                                                     Credential sigKey,
                                                     @Nullable
                                                     Credential encKey)
                                              throws com.nimbusds.jose.JOSEException,
                                                     ParseException
        Create a JWT from the given payload. The JWT can either be plain, or signed and encrypted. If encrypted, it must be signed.
        Parameters:
        payload - the payload to create the JWT from
        sigAlg - the signature alg to use. Null if the JWT is not going to be signed and/or encrypted.
        jweAlg - the encryption alg to use. Null if the JWT is not going to be encrypted. Note, if encrypted the signature algorithm must also be supplied, otherwise a plain JWT will be returned.
        enc - the content encryption algorithm to use.
        sigKey - the key to use for signing
        encKey - the key to use for encryption
        Returns:
        and plain JWT, JWS, or JWE.
        Throws:
        com.nimbusds.jose.JOSEException - on error.
        ParseException - on error.
      • createJWTUserInfoResponse

        public static com.nimbusds.jwt.JWT createJWTUserInfoResponse​(Map<String,​Object> overrideClaims,
                                                                     @Nullable
                                                                     com.nimbusds.jose.JWSAlgorithm sigAlg,
                                                                     @Nullable
                                                                     com.nimbusds.jose.JWEAlgorithm jweAlg,
                                                                     @Nullable
                                                                     com.nimbusds.jose.EncryptionMethod enc,
                                                                     @Nullable
                                                                     Credential sigKey,
                                                                     @Nullable
                                                                     Credential encKey)
                                                              throws com.nimbusds.jose.JOSEException,
                                                                     ParseException
        Create a JWT UserInfo response. Can be a JWS or JWE depending on the input values.
        Parameters:
        overrideClaims - claims to override
        sigAlg - the signature algorithm
        jweAlg - the encryption algorithm
        enc - the content encryption algorithm
        sigKey - the signing key
        encKey - the encryption key
        Returns:
        the created JWT
        Throws:
        com.nimbusds.jose.JOSEException - on error
        ParseException - on error
      • createAccessTokenResponseJSON

        public static String createAccessTokenResponseJSON​(Map<String,​Object> overrideClaims,
                                                           @Nullable
                                                           com.nimbusds.jose.JWSAlgorithm sigAlg,
                                                           @Nullable
                                                           com.nimbusds.jose.JWEAlgorithm jweAlg,
                                                           @Nullable
                                                           com.nimbusds.jose.EncryptionMethod enc,
                                                           @Nullable
                                                           Credential sigKey,
                                                           @Nullable
                                                           Credential encKey)
                                                    throws Exception
        Create an access token response JSON object and build a suitable id_token to include in the response. Any claim contained in the overrides list overrides the default value built by the basic claims method.
        Parameters:
        overrideClaims - claims to override in the id_token
        sigAlg - the signature algorithm
        jweAlg - the encryption algorithm
        enc - the content encryption algorithm
        sigKey - the signing key
        encKey - the encryption key
        Returns:
        an access token response JSON object
        Throws:
        Exception - on error.
      • createPlainUserInfoResponseString

        public static String createPlainUserInfoResponseString​(Map<String,​Object> overrideClaims)
                                                        throws com.fasterxml.jackson.core.JsonProcessingException
        Create a plain JSON object based UserInfo response.
        Parameters:
        overrideClaims - claims to override
        Returns:
        a plain JSON object representing the UserInfo response
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - on error
      • createPlainJWTUserInfoResponseJSON

        public static com.nimbusds.jwt.PlainJWT createPlainJWTUserInfoResponseJSON​(Map<String,​Object> overrideClaims)
                                                                            throws com.nimbusds.jose.JOSEException
        Create a Plain UserInfo response JWT.
        Parameters:
        overrideClaims - claims to override
        Returns:
        the signed JWT
        Throws:
        com.nimbusds.jose.JOSEException - on error
      • buildTemplateAccessTokenJSONResponse

        private static String buildTemplateAccessTokenJSONResponse​(String serializedJWT)
        Build a simple OAuth2.0/OIDC Access Token JSON response using the serialized JWT.
        Parameters:
        serializedJWT - the id_token serialized
        Returns:
        An access token response