public class OidcClientHelper
extends java.lang.Object
This API has utility methods for interacting with the OpenID Connect (OIDC) Relying Party (RP) TAI.
This API contains two sets of methods; one set that operates on the runAs subject and another that operates on a Subject that is passed into the method. If you are running the methods from a business application, the standard versions that use the runAs subject will suffice. However, if you are running the methods from a JAAS login module, there are conditions where the runAs subject may not be updated with the OIDC data, but the JAAS Subject has. In this case, you would use the set of methods where you pass the Subject into the method.
The OidcClientHelper class can be found in the following jars:
Sample code to get the JWT claims from an ID token on the runAs Subject:
String idTokenJwt = OidcClientHelper.getIdTokenFromSubject(); //get the claims string String idTokenClaims = OidcClientHelper.getJwtClaimsAsString(idtokenJwt); //-or- get the claims map Map<String,Object> claimsMap = OidcClientHelper.getJwtClaimsAsMap(idtokenJwt);
Sample code to get the Introspection response from the runAs subject as a Map:
String response = OidcClientHelper.getIntrospectionResponseFromSubject(); Map<String,Object> map = OidcClientHelper.json2map(response);
Constructor and Description |
---|
OidcClientHelper() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
getAccessTokenFromSubject()
Retrieve the access token from the current runAs Subject
In the OpenID Connect scenario, this is the access_token
that is returned from the provider.
|
static java.lang.String |
getAccessTokenFromSubject(javax.security.auth.Subject subj)
Retrieve the access token from the input Subject
In the OpenID Connect scenario, this is the access_token that is returned from the provider.
|
static java.lang.String |
getClientCredentialsGrantAccessToken()
Deprecated.
|
static java.lang.String |
getEndSessionEndpoint()
Retrieve the end session endpoint associated with the access
token on the current runAs Subject.
|
static java.lang.String |
getEndSessionEndpoint(javax.security.auth.Subject subj)
Retrieve the end session endpoint associated with the access
token on the from the input Subject
A null value will be returned in the following instances:
Administrative security is not enabled.
|
static java.lang.String |
getIdTokenFromSubject()
Retrieve the ID token from the current runAs Subject
This String is the id_token JSON Web Token (JWT) that is
returned from the OpenID provider.
|
static java.lang.String |
getIdTokenFromSubject(javax.security.auth.Subject subj)
Retrieve the ID token from the input Subject
This String is the id_token JWT that is returned from the
OpenID provider.
|
static java.lang.String |
getIntrospectionResponseFromSubject()
Retrieve the introspection response String from the current runAs Subject.
|
static java.lang.String |
getIntrospectionResponseFromSubject(javax.security.auth.Subject subj)
Retrieve the introspection response String from the input Subject.
|
static java.util.Map<java.lang.String,java.lang.Object> |
getJwtClaimsAsMap(java.lang.String jwtString)
Get the JWT claims from a JWT as a Map.
|
static java.lang.String |
getJwtClaimsAsString(java.lang.String jwtString)
Get the JWT claims from a JWT as a JSON String.
|
static java.lang.String |
getJwtFromSubject()
Retrieve the JWT Authentication token from the current runAs Subject.
|
static java.lang.String |
getJwtFromSubject(javax.security.auth.Subject subj)
Retrieve the JWT Authentication token from the input Subject.
|
static java.lang.String |
getRefreshTokenFromSubject()
Retrieve the refresh token from the current runAs Subject
|
static java.lang.String |
getRefreshTokenFromSubject(javax.security.auth.Subject subj)
Retrieve the refresh token from the input Subject
|
static javax.security.auth.Subject |
getRunAsSubject()
getRunAsSubject returns a JAAS subject that represents the RunAs identity. |
static java.lang.String |
getScopeFromSubject()
Retrieve the scope of the access token from the current runAs Subject
|
static java.lang.String |
getScopeFromSubject(javax.security.auth.Subject subj)
Retrieve the scope of the access token from the input Subject
|
static java.lang.String |
getTokenTypeFromSubject()
Retrieve the token type from the current runAs Subject
|
static java.lang.String |
getTokenTypeFromSubject(javax.security.auth.Subject subj)
Retrieve the token type from the input Subject
|
static java.lang.String |
getUserInfoFromServer()
Retrieve the userInfo for the associated with the access token from the configured userinfoEndpointUrl.
|
static java.lang.String |
getUserInfoFromServer(javax.security.auth.Subject subj)
Retrieve the userInfo for the associated with the access token from the configured userinfoEndpointUrl.
|
static java.lang.String |
getUserInfoFromSubject()
Retrieve the userInfo associated with the access token from the current runAs Subject.
|
static java.lang.String |
getUserInfoFromSubject(javax.security.auth.Subject subj)
Retrieve the userInfo associated with the access token from the input Subject.
|
static java.lang.String |
getValidAccessToken()
Retrieve a valid access token based on the access token in the current runAs Subject.
|
static java.lang.String |
getValidAccessToken(javax.security.auth.Subject subj)
Retrieve a valid access token based on the access token in the input Subject.
|
static java.util.Map<java.lang.String,java.lang.Object> |
json2map(java.lang.String jsonString)
Convert a JSON String to a Map.
|
static boolean |
verifyJwt(java.lang.String jwtString)
Verify a JWT
To find the information required to verify the JWT, this
method will first attempt to find an OIDC TAI config entry
that matches the iss claim in the JWT.
|
static boolean |
verifyJwt(java.lang.String jwtString,
java.lang.String defaultIssuer)
Verify a JWT
To find the information required to verify the JWT, this
method will first attempt to find an OIDC TAI config entry
that matches the issuer.
|
static boolean |
verifyJwtUsingDiscovery(java.lang.String jwtString)
Verify a JWT using discovery
This method will obtain the information to verify the JWT
using discovery.
|
static boolean |
verifyJwtUsingDiscovery(java.lang.String jwtString,
java.lang.String discoveryUrl)
Verify a JWT using discovery
This method will obtain the information to verify the JWT
using the specified discovery endpoint.
|
static boolean |
verifyJwtUsingTAIConfig(java.lang.String jwtString)
Verify a JWT using the OpenID Connect (OIDC) TAI
configuration
To find the information required to verify the JWT, this
method will attempt to find an OIDC TAI config entry that
matches the iss claim in the JWT.
|
static boolean |
verifyJwtUsingTAIConfig(java.lang.String jwtString,
java.lang.String defaultIssuer)
Verify a JWT using the OpenID Connect (OIDC) TAI
configuration
To find the information required to verify the JWT,
this method will attempt to find an OIDC TAI config entry
that matches the issuer.
|
public static java.lang.String getIdTokenFromSubject() throws java.lang.Exception
This String is the id_token JSON Web Token (JWT) that is returned from the OpenID provider.
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getIdTokenFromSubject(javax.security.auth.Subject subj) throws java.lang.Exception
This String is the id_token JWT that is returned from the OpenID provider.
java.lang.Exception
- if an error occurs when accessing the private credentials in the Subject.public static java.lang.String getAccessTokenFromSubject() throws java.lang.Exception
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getAccessTokenFromSubject(javax.security.auth.Subject subj) throws java.lang.Exception
java.lang.Exception
- if an error occurs when accessing the private credentials in the Subject.public static java.lang.String getRefreshTokenFromSubject() throws java.lang.Exception
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getRefreshTokenFromSubject(javax.security.auth.Subject subj) throws java.lang.Exception
java.lang.Exception
- if an error occurs when accessing the private credentials in the Subject.public static java.lang.String getTokenTypeFromSubject() throws java.lang.Exception
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getTokenTypeFromSubject(javax.security.auth.Subject subj) throws java.lang.Exception
java.lang.Exception
- if an error occurs when accessing the private credentials in the Subject.public static java.lang.String getScopeFromSubject() throws java.lang.Exception
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getScopeFromSubject(javax.security.auth.Subject subj) throws java.lang.Exception
java.lang.Exception
- if an error occurs when accessing the private credentials in the Subject.public static java.lang.String getUserInfoFromSubject() throws java.lang.Exception
If there is a userinfoEndpointUrl configured for the access token on the Subject, this method will return the userInfo associated with the access token.
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getUserInfoFromSubject(javax.security.auth.Subject subj) throws java.lang.Exception
If there is a userinfoEndpointUrl configured for the access token on the Subject, this method will return the userInfo associated with the access token.
java.lang.Exception
- if an error occurs when accessing the private credentials in the Subject.public static java.lang.String getValidAccessToken() throws java.lang.Exception
Retrieve a valid access token based on the access token in the current runAs Subject.
A null value will be returned in the following instances:
If the access token retrieved from the runAs subject has expired and is successfully refreshed, the following will be updated with the new access token:
This method will only throw an exception if errors occur related to the initial processing of the runAs Subject. If an error occurs after information has been retrieved from the runAs Subject, an FFDC entry will be created, but the exception will not be emitted by this method and the method will return null in most cases. If the access token has been refreshed, but the runAs Subject cannot be updated, an FFDC will be emitted and the method will return the new access token.
If there is a userinfoEndpointUrl configured for the access token, the user info will also be refreshed.
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getValidAccessToken(javax.security.auth.Subject subj) throws java.lang.Exception
Retrieve a valid access token based on the access token in the input Subject.
A null value will be returned in the following instances:
If the access token retrieved from the input subject has expired and is successfully refreshed, the following will be updated with the new access token:
This method will only throw an exception if errors occur related to the initial processing of the input Subject. If an error occurs after information has been retrieved from the input Subject, an FFDC entry will be created, but the exception will not be emitted by this method and the method will return null in most cases. If the access token has been refreshed, but the input Subject cannot be updated, an FFDC will be emitted and the method will return the new access token.
If there is a userinfoEndpointUrl configured for the access token, the user info will also be refreshed.
java.lang.Exception
- if an error occurs when accessing the private credentials in the Subject.public static java.lang.String getUserInfoFromServer() throws java.lang.Exception
Retrieve the userInfo for the associated with the access token from the configured userinfoEndpointUrl.
A null value will be returned in the following instances:
If the user info is successfully retrieved server, the following will be updated with the new user info:
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getUserInfoFromServer(javax.security.auth.Subject subj) throws java.lang.Exception
Retrieve the userInfo for the associated with the access token from the configured userinfoEndpointUrl.
A null value will be returned in the following instances:
If the user info is successfully retrieved server, the following will be updated with the new user info:
java.lang.Exception
- if an error occurs when accessing the private credentials in the Subject.public static javax.security.auth.Subject getRunAsSubject() throws java.lang.Exception
getRunAsSubject
returns a JAAS subject that represents the RunAs identity.java.lang.Exception
- if an error occurs while obtaining the runAs Subject@Deprecated public static java.lang.String getClientCredentialsGrantAccessToken() throws java.lang.Exception
java.lang.Exception
public static java.lang.String getJwtClaimsAsString(java.lang.String jwtString) throws java.lang.Exception
For example:
{"sub":"1234567890","name":"John Doe", "admin": true, "exp":1588806453}
java.lang.Exception
- if an error occurs decoding the JWTpublic static java.util.Map<java.lang.String,java.lang.Object> getJwtClaimsAsMap(java.lang.String jwtString) throws java.lang.Exception
The Map will have value types that correspond to the values in the claims string. For instance,
the following claims string:
{"sub":"1234567890","name":"John Doe", "admin": true, "exp":1588806453}
will produce the map entries with the value types: String, String, Boolean, Long
java.lang.Exception
- if an error occurs decoding the JWTpublic static java.util.Map<java.lang.String,java.lang.Object> json2map(java.lang.String jsonString) throws java.lang.Exception
The Map will have value types that correspond to the values in the JSON string. For instance,
the following JSON string:
{"sub":"1234567890","name":"John Doe", "admin": true, "exp":1588806453}
will produce the map entries with the value types: String, String, Boolean, Long
java.lang.Exception
- if an error occurs creating the Mappublic static java.lang.String getJwtFromSubject() throws java.lang.Exception
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getJwtFromSubject(javax.security.auth.Subject subj) throws java.lang.Exception
java.lang.Exception
- if an error occurs when accessing the private credentials in the Subject.public static java.lang.String getIntrospectionResponseFromSubject() throws java.lang.Exception
This method will return null if the OIDC session data associated with this subject was not created via introspection.
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getIntrospectionResponseFromSubject(javax.security.auth.Subject subj) throws java.lang.Exception
This method will return null if the OIDC session data associated with this subject was not created via introspection.
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getEndSessionEndpoint() throws java.lang.Exception
A null value will be returned in the following instances:
java.lang.Exception
- if an error occurs either while obtaining the runAs Subject or accessing the private credentials.public static java.lang.String getEndSessionEndpoint(javax.security.auth.Subject subj) throws java.lang.Exception
A null value will be returned in the following instances:
java.lang.Exception
- if an error occurs either while obtaining
the accessing the private credentials from
the input Subject.public static boolean verifyJwt(java.lang.String jwtString) throws java.lang.Exception
(issuer)/.well-known/openid-configuration
jwtString
- JWT string to verifytrue
if the JWT is validjava.lang.Exception
- if the JWT is not valid or an error occurs
while accessing the discovery endpoint on
the OP.public static boolean verifyJwt(java.lang.String jwtString, java.lang.String defaultIssuer) throws java.lang.Exception
(issuer)/.well-known/openid-configuration
jwtString
- JWT string to verifydefaultIssuer
- Default issuer to use if there is no iss
claim in the JWTtrue
if the JWT is validjava.lang.Exception
- if the JWT is not valid or an error occurs
while accessing the discovery endpoint on
the OP.public static boolean verifyJwtUsingDiscovery(java.lang.String jwtString) throws java.lang.Exception
(issuer)/.well-known/openid-configuration
jwtString
- JWT string to verifytrue
if the JWT is validjava.lang.Exception
- if the JWT is not valid or an error occurs
while accessing the discovery endpoint on
the OP.public static boolean verifyJwtUsingDiscovery(java.lang.String jwtString, java.lang.String discoveryUrl) throws java.lang.Exception
jwtString
- JWT string to verifydiscoveryUrl
- Discovery endpoint URLtrue
if the JWT is validjava.lang.Exception
- if the JWT is not valid or an error occurs
while accessing the discovery endpoint on
the OP.public static boolean verifyJwtUsingTAIConfig(java.lang.String jwtString) throws java.lang.Exception
jwtString
- JWT string to verifytrue
if the JWT is validjava.lang.Exception
- if the JWT is not valid or an error occurs
while attempting to find an OIDC TAI config
entry that matches the issuer.public static boolean verifyJwtUsingTAIConfig(java.lang.String jwtString, java.lang.String defaultIssuer) throws java.lang.Exception
To find the information required to verify the JWT, this method will attempt to find an OIDC TAI config entry that matches the issuer.
jwtString
- JWT string to verifydefaultIssuer
- Default issuer to use if there is no iss
claim in the JWTtrue
if the JWT is validjava.lang.Exception
- if the JWT is not valid or an error occurs
while attempting to find an OIDC TAI config
entry that matches the issuer.