Package org.gcube.oidc.rest
Class OpenIdConnectRESTHelper
- java.lang.Object
-
- org.gcube.oidc.rest.OpenIdConnectRESTHelper
-
public class OpenIdConnectRESTHelper extends Object
OpenID Connect REST helper.- Author:
- Mauro Mugnaini
-
-
Field Summary
Fields Modifier and Type Field Description protected static org.slf4j.LoggerloggerLogger.
-
Constructor Summary
Constructors Constructor Description OpenIdConnectRESTHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringbuildLoginRequestURL(URL loginURL, String clientId, String state, String redirectURI)Builds the login request URL.protected static StringgetClientIdFromToken(JWTToken token)Returns the client id from the token.static byte[]getUserAvatar(URL avatarURL, String authorization)Returns the user avatar.static byte[]getUserAvatar(URL avatarURL, JWTToken token)Returns the user avatar.static booleanisAccessDeniedNotAuthorizedError(String jsonString)Checks if the error is an access denied not authorized error.static booleanisInvalidBearerTokenError(String jsonString)Checks if the error is an invalid bearer token error.static booleanisTokenNotActiveError(String jsonString)Checks if the error is a token not active error.static booleanlogout(URL logoutUrl, String clientId, JWTToken token)Performs the logout from the session related to the provided client id in the OIDC server.static booleanlogout(URL logoutUrl, JWTToken token)Performs the logout (SSOut) from all the sessions opened in the OIDC server.static StringmapToQueryString(Map<String,List<String>> params)Maps the parameters to a query string.protected static booleanmatchesErrorAndDescription(String jsonString, String expectedError, String exepectedErrorDescription)Checks if the error matches the expected error and description.protected static JWTTokenperformQueryTokenWithPOST(URL tokenURL, String authorization, Map<String,List<String>> params)Performs a query token with POST.protected static JWTTokenperformQueryTokenWithPOST(URL tokenURL, String authorization, Map<String,List<String>> params, Map<String,String> headers)Performs a query token with POST.protected static HttpURLConnectionperformURLEncodedPOSTSendData(URL url, Map<String,List<String>> params, String authorization)Performs a URL encoded POST send data.protected static HttpURLConnectionperformURLEncodedPOSTSendData(URL url, Map<String,List<String>> params, String authorization, Map<String,String> headers)Performs a URL encoded POST send data.static JWTTokenqueryClientToken(String clientId, String clientSecret, URL tokenURL)Queries from the OIDC server an OIDC access token, by using provided clientId and client secret.static JWTTokenqueryClientToken(String clientId, String clientSecret, URL tokenURL, Map<String,String> extraHeaders)Queries from the OIDC server an OIDC access token, by using provided clientId and client secret.static JWTTokenqueryExchangeToken(URL tokenUrl, String authorization, String audience, String clientId, String clientSecret, boolean withRefreshToken, boolean offline, Map<String,String> extraHeaders)Queries from the OIDC server an exchanged token by using provided access token, optionally for the given audience (context) in URLEncoded form or not.static JWTTokenqueryExchangeToken(URL tokenUrl, String authorization, String audience, String clientId, String clientSecret, String requestedTokenType, String scope, Map<String,String> extraHeaders)Queries from the OIDC server an exchanged token by using provided access token, optionally for the given audience (context) in URLEncoded form or not.static JWTTokenqueryExchangeToken(URL tokenUrl, String authorization, String audience, String clientId, String clientSecret, Map<String,String> extraHeaders)Queries from the OIDC server an exchanged token by using provided access token, optionally for the given audience (context) in URLEncoded form or not.static JWTTokenqueryToken(String clientId, URL tokenURL, String code, String scope, String redirectURI)Queries from the OIDC server an OIDC access token, by using provided clientId, code, scope and redirect URI.static JWTTokenqueryToken(String clientId, URL tokenURL, String code, String scope, String redirectURI, Map<String,String> extraHeaders)Queries from the OIDC server an OIDC access token, by using provided clientId, code, scope, redirect URI and extra headers.static JWTTokenqueryUMAToken(URL tokenUrl, String clientId, String clientSecret, String audience, List<String> permissions)Queries from the OIDC server an UMA token, by using provided clientId and client secret for the given audience (context), in URLEncoded form or not, and optionally a list of permissions.static JWTTokenqueryUMAToken(URL tokenUrl, String clientId, String clientSecret, String audience, List<String> permissions, Map<String,String> extraHeaders)Queries from the OIDC server an UMA token, by using provided clientId and client secret for the given audience (context), in URLEncoded form or not, and optionally a list of permissions.static JWTTokenqueryUMAToken(URL tokenUrl, String authorization, String audience, List<String> permissions)Queries from the OIDC server an UMA token, by using provided access token, for the given audience (context), in URLEncoded form or not, and optionally a list of permissions.static JWTTokenqueryUMAToken(URL tokenUrl, String authorization, String audience, List<String> permissions, Map<String,String> extraHeaders)Queries from the OIDC server an UMA token, by using provided access token, for the given audience (context), in URLEncoded form or not, and optionally a list of permissions.static JWTTokenrefreshToken(URL tokenURL, String clientId, String clientSecret, JWTToken token)Refreshes the token from the OIDC server for a specific client represented by the client id.static JWTTokenrefreshToken(URL tokenURL, String clientId, JWTToken token)Refreshes the token from the OIDC server for a specific client represented by the client id.static JWTTokenrefreshToken(URL tokenURL, JWTToken token)Refreshes the token from the OIDC server.
-
-
-
Method Detail
-
buildLoginRequestURL
public static String buildLoginRequestURL(URL loginURL, String clientId, String state, String redirectURI) throws UnsupportedEncodingException
Builds the login request URL.- Parameters:
loginURL- the login URL.clientId- the client id.state- the state.redirectURI- the redirect URI.- Returns:
- the login request URL.
- Throws:
UnsupportedEncodingException- if an error occurs.
-
mapToQueryString
public static String mapToQueryString(Map<String,List<String>> params)
Maps the parameters to a query string.- Parameters:
params- the parameters.- Returns:
- the query string.
-
queryClientToken
public static JWTToken queryClientToken(String clientId, String clientSecret, URL tokenURL) throws OpenIdConnectRESTHelperException
Queries from the OIDC server an OIDC access token, by using provided clientId and client secret.- Parameters:
clientId- the client idclientSecret- the client secrettokenURL- the token endpointURLof the OIDC server- Returns:
- the issued token
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
queryClientToken
public static JWTToken queryClientToken(String clientId, String clientSecret, URL tokenURL, Map<String,String> extraHeaders) throws OpenIdConnectRESTHelperException
Queries from the OIDC server an OIDC access token, by using provided clientId and client secret.- Parameters:
clientId- the client idclientSecret- the client secrettokenURL- the token endpointURLof the OIDC serverextraHeaders- extra HTTP headers to add to the request (e.g.X-D4Science-Contextcustom header), may benull- Returns:
- the issued token
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
queryToken
public static JWTToken queryToken(String clientId, URL tokenURL, String code, String scope, String redirectURI) throws Exception
Queries from the OIDC server an OIDC access token, by using provided clientId, code, scope and redirect URI.- Parameters:
clientId- the client id.tokenURL- the token URL.code- the code.scope- the scope.redirectURI- the redirect URI.- Returns:
- the issued token.
- Throws:
Exception- if an error occurs.
-
queryToken
public static JWTToken queryToken(String clientId, URL tokenURL, String code, String scope, String redirectURI, Map<String,String> extraHeaders) throws Exception
Queries from the OIDC server an OIDC access token, by using provided clientId, code, scope, redirect URI and extra headers.- Parameters:
clientId- the client id.tokenURL- the token URL.code- the code.scope- the scope.redirectURI- the redirect URI.extraHeaders- the extra headers.- Returns:
- the issued token.
- Throws:
Exception- if an error occurs.
-
performQueryTokenWithPOST
protected static JWTToken performQueryTokenWithPOST(URL tokenURL, String authorization, Map<String,List<String>> params) throws OpenIdConnectRESTHelperException
Performs a query token with POST.- Parameters:
tokenURL- the token URL.authorization- the authorization.params- the parameters.- Returns:
- the issued token.
- Throws:
OpenIdConnectRESTHelperException- if an error occurs.
-
performQueryTokenWithPOST
protected static JWTToken performQueryTokenWithPOST(URL tokenURL, String authorization, Map<String,List<String>> params, Map<String,String> headers) throws OpenIdConnectRESTHelperException
Performs a query token with POST.- Parameters:
tokenURL- the token URL.authorization- the authorization.params- the parameters.headers- the headers.- Returns:
- the issued token.
- Throws:
OpenIdConnectRESTHelperException- if an error occurs.
-
performURLEncodedPOSTSendData
protected static HttpURLConnection performURLEncodedPOSTSendData(URL url, Map<String,List<String>> params, String authorization) throws IOException, ProtocolException, UnsupportedEncodingException
Performs a URL encoded POST send data.- Parameters:
url- the URL.params- the parameters.authorization- the authorization.- Returns:
- the connection.
- Throws:
IOException- if an error occurs.ProtocolException- if an error occurs.UnsupportedEncodingException- if an error occurs.
-
performURLEncodedPOSTSendData
protected static HttpURLConnection performURLEncodedPOSTSendData(URL url, Map<String,List<String>> params, String authorization, Map<String,String> headers) throws IOException, ProtocolException, UnsupportedEncodingException
Performs a URL encoded POST send data.- Parameters:
url- the URL.params- the parameters.authorization- the authorization.headers- the headers.- Returns:
- the connection.
- Throws:
IOException- if an error occurs.ProtocolException- if an error occurs.UnsupportedEncodingException- if an error occurs.
-
queryUMAToken
public static JWTToken queryUMAToken(URL tokenUrl, String clientId, String clientSecret, String audience, List<String> permissions) throws OpenIdConnectRESTHelperException
Queries from the OIDC server an UMA token, by using provided clientId and client secret for the given audience (context), in URLEncoded form or not, and optionally a list of permissions.- Parameters:
tokenUrl- the token endpointURLof the OIDC serverclientId- the client idclientSecret- the client secretaudience- the audience (context) where to request the issuing of the token (URLEncoded or not)permissions- a list of permissions, can benull- Returns:
- the issued token
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
queryUMAToken
public static JWTToken queryUMAToken(URL tokenUrl, String clientId, String clientSecret, String audience, List<String> permissions, Map<String,String> extraHeaders) throws OpenIdConnectRESTHelperException
Queries from the OIDC server an UMA token, by using provided clientId and client secret for the given audience (context), in URLEncoded form or not, and optionally a list of permissions.- Parameters:
tokenUrl- the token endpointURLof the OIDC serverclientId- the client idclientSecret- the client secretaudience- the audience (context) where to request the issuing of the token (URLEncoded or not)permissions- a list of permissions, can benullextraHeaders- extra HTTP headers to add to the request (e.g.X-D4Science-Contextcustom header), may benull- Returns:
- the issued token
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
queryUMAToken
public static JWTToken queryUMAToken(URL tokenUrl, String authorization, String audience, List<String> permissions) throws OpenIdConnectRESTHelperException
Queries from the OIDC server an UMA token, by using provided access token, for the given audience (context), in URLEncoded form or not, and optionally a list of permissions.- Parameters:
tokenUrl- the token endpointURLof the OIDC serverauthorization- the auth token (the access token URLEncoded by the "Bearer " string)audience- the audience (context) where to request the issuing of the token (URLEncoded or not)permissions- a list of permissions, can benull- Returns:
- the issued token
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
queryUMAToken
public static JWTToken queryUMAToken(URL tokenUrl, String authorization, String audience, List<String> permissions, Map<String,String> extraHeaders) throws OpenIdConnectRESTHelperException
Queries from the OIDC server an UMA token, by using provided access token, for the given audience (context), in URLEncoded form or not, and optionally a list of permissions.- Parameters:
tokenUrl- the token endpointURLof the OIDC serverauthorization- the auth token (the access token URLEncoded by the "Bearer " string)audience- the audience (context) where to request the issuing of the token (URLEncoded or not)permissions- a list of permissions, can benullextraHeaders- extra HTTP headers to add to the request (e.g.X-D4Science-Contextcustom header), may benull- Returns:
- the issued token
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
queryExchangeToken
public static JWTToken queryExchangeToken(URL tokenUrl, String authorization, String audience, String clientId, String clientSecret, Map<String,String> extraHeaders) throws OpenIdConnectRESTHelperException
Queries from the OIDC server an exchanged token by using provided access token, optionally for the given audience (context) in URLEncoded form or not.- Parameters:
tokenUrl- the token endpointURLof the OIDC serverauthorization- the auth token (the access token URLEncoded by the "Bearer " string)audience- the audience (context) where to request the issuing of the token (URLEncoded or not), may benullclientId- the client idclientSecret- the client secretextraHeaders- extra HTTP headers to add to the request (e.g.X-D4Science-Contextcustom header), may benull- Returns:
- the issued token
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
queryExchangeToken
public static JWTToken queryExchangeToken(URL tokenUrl, String authorization, String audience, String clientId, String clientSecret, boolean withRefreshToken, boolean offline, Map<String,String> extraHeaders) throws OpenIdConnectRESTHelperException
Queries from the OIDC server an exchanged token by using provided access token, optionally for the given audience (context) in URLEncoded form or not.- Parameters:
tokenUrl- the token endpointURLof the OIDC serverauthorization- the auth token (the access token URLEncoded by the "Bearer " string)audience- the audience (context) where to request the issuing of the token (URLEncoded or not), may benullclientId- the client idclientSecret- the client secretwithRefreshToken- request also the refresh token (forced totruefor offline requests)offline- request a refresh token of offline type (TYP claim)extraHeaders- extra HTTP headers to add to the request (e.g.X-D4Science-Contextcustom header), may benull- Returns:
- the issued token
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
queryExchangeToken
public static JWTToken queryExchangeToken(URL tokenUrl, String authorization, String audience, String clientId, String clientSecret, String requestedTokenType, String scope, Map<String,String> extraHeaders) throws OpenIdConnectRESTHelperException
Queries from the OIDC server an exchanged token by using provided access token, optionally for the given audience (context) in URLEncoded form or not.- Parameters:
tokenUrl- the token endpointURLof the OIDC serverauthorization- the auth token (the access token URLEncoded by the "Bearer " string)audience- the audience (context) where to request the issuing of the token (URLEncoded or not), may benullclientId- the client idclientSecret- the client secretrequestedTokenType- the requested token type (e.g.urn:ietf:params:oauth:token-type:refresh_tokenfor refresh token)scope- the optional scope to request (e.g.offline_accessfor an offline token)extraHeaders- extra HTTP headers to add to the request (e.g.X-D4Science-Contextcustom header), may benull- Returns:
- the issued token
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
refreshToken
public static JWTToken refreshToken(URL tokenURL, JWTToken token) throws OpenIdConnectRESTHelperException
Refreshes the token from the OIDC server.- Parameters:
tokenURL- the token endpointURLof the OIDC servertoken- the token to be refreshed- Returns:
- a new token refreshed from the previous one
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
refreshToken
public static JWTToken refreshToken(URL tokenURL, String clientId, JWTToken token) throws OpenIdConnectRESTHelperException
Refreshes the token from the OIDC server for a specific client represented by the client id.- Parameters:
tokenURL- the token endpointURLof the OIDC serverclientId- the client idtoken- the token to be refreshed- Returns:
- a new token refreshed from the previous one
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
refreshToken
public static JWTToken refreshToken(URL tokenURL, String clientId, String clientSecret, JWTToken token) throws OpenIdConnectRESTHelperException
Refreshes the token from the OIDC server for a specific client represented by the client id.- Parameters:
tokenURL- the token endpointURLof the OIDC serverclientId- the client idclientSecret- the client secrettoken- the token to be refreshed- Returns:
- a new token refreshed from the previous one
- Throws:
OpenIdConnectRESTHelperException- if an error occurs (also an unauthorized call), inspect the exception for details
-
getClientIdFromToken
protected static String getClientIdFromToken(JWTToken token)
Returns the client id from the token.- Parameters:
token- the token.- Returns:
- the client id.
-
logout
public static boolean logout(URL logoutUrl, JWTToken token) throws IOException
Performs the logout (SSOut) from all the sessions opened in the OIDC server.- Parameters:
logoutUrl- the logut endpointURLof the OIDC servertoken- the token used to take info from- Returns:
trueif the logout is performed correctly,falseotherwise- Throws:
IOException- if an I/O error occurs during the communication with the server
-
logout
public static boolean logout(URL logoutUrl, String clientId, JWTToken token) throws IOException
Performs the logout from the session related to the provided client id in the OIDC server.- Parameters:
logoutUrl- the logut endpointURLof the OIDC serverclientId- the client idtoken- the token used to take info from- Returns:
trueif the logout is performed correctly,falseotherwise- Throws:
IOException- if an I/O error occurs during the communication with the server
-
getUserAvatar
public static byte[] getUserAvatar(URL avatarURL, JWTToken token)
Returns the user avatar.- Parameters:
avatarURL- the avatar URL.token- the token.- Returns:
- the avatar.
-
getUserAvatar
public static byte[] getUserAvatar(URL avatarURL, String authorization)
Returns the user avatar.- Parameters:
avatarURL- the avatar URL.authorization- the authorization.- Returns:
- the avatar.
-
matchesErrorAndDescription
protected static boolean matchesErrorAndDescription(String jsonString, String expectedError, String exepectedErrorDescription)
Checks if the error matches the expected error and description.- Parameters:
jsonString- the JSON string.expectedError- the expected error.exepectedErrorDescription- the expected error description.- Returns:
- true if matches, false otherwise.
-
isTokenNotActiveError
public static boolean isTokenNotActiveError(String jsonString)
Checks if the error is a token not active error.- Parameters:
jsonString- the JSON string.- Returns:
- true if it is a token not active error, false otherwise.
-
isInvalidBearerTokenError
public static boolean isInvalidBearerTokenError(String jsonString)
Checks if the error is an invalid bearer token error.- Parameters:
jsonString- the JSON string.- Returns:
- true if it is an invalid bearer token error, false otherwise.
-
isAccessDeniedNotAuthorizedError
public static boolean isAccessDeniedNotAuthorizedError(String jsonString)
Checks if the error is an access denied not authorized error.- Parameters:
jsonString- the JSON string.- Returns:
- true if it is an access denied not authorized error, false otherwise.
-
-