Package org.gcube.oidc.rest
Class OpenIdConnectRESTHelper
- java.lang.Object
-
- org.gcube.oidc.rest.OpenIdConnectRESTHelper
-
public class OpenIdConnectRESTHelper extends Object
-
-
Field Summary
Fields Modifier and Type Field Description protected static org.slf4j.Loggerlogger
-
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)protected static StringgetClientIdFromToken(JWTToken token)static byte[]getUserAvatar(URL avatarURL, String authorization)static byte[]getUserAvatar(URL avatarURL, JWTToken token)static booleanisAccessDeniedNotAuthorizedError(String jsonString)static booleanisInvalidBearerTokenError(String jsonString)static booleanisTokenNotActiveError(String jsonString)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)protected static booleanmatchesErrorAndDescription(String jsonString, String expectedError, String exepectedErrorDescription)protected static JWTTokenperformQueryTokenWithPOST(URL tokenURL, String authorization, Map<String,List<String>> params)protected static JWTTokenperformQueryTokenWithPOST(URL tokenURL, String authorization, Map<String,List<String>> params, Map<String,String> headers)protected static HttpURLConnectionperformURLEncodedPOSTSendData(URL url, Map<String,List<String>> params, String authorization)protected static HttpURLConnectionperformURLEncodedPOSTSendData(URL url, Map<String,List<String>> params, String authorization, Map<String,String> headers)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 client_id, String client_secret, 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)static JWTTokenqueryToken(String clientId, URL tokenURL, String code, String scope, String redirectURI, Map<String,String> extraHeaders)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
- Throws:
UnsupportedEncodingException
-
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
- Throws:
Exception
-
queryToken
public static JWTToken queryToken(String clientId, URL tokenURL, String code, String scope, String redirectURI, Map<String,String> extraHeaders) throws Exception
- Throws:
Exception
-
performQueryTokenWithPOST
protected static JWTToken performQueryTokenWithPOST(URL tokenURL, String authorization, Map<String,List<String>> params) throws OpenIdConnectRESTHelperException
- Throws:
OpenIdConnectRESTHelperException
-
performQueryTokenWithPOST
protected static JWTToken performQueryTokenWithPOST(URL tokenURL, String authorization, Map<String,List<String>> params, Map<String,String> headers) throws OpenIdConnectRESTHelperException
- Throws:
OpenIdConnectRESTHelperException
-
performURLEncodedPOSTSendData
protected static HttpURLConnection performURLEncodedPOSTSendData(URL url, Map<String,List<String>> params, String authorization) throws IOException, ProtocolException, UnsupportedEncodingException
-
performURLEncodedPOSTSendData
protected static HttpURLConnection performURLEncodedPOSTSendData(URL url, Map<String,List<String>> params, String authorization, Map<String,String> headers) throws IOException, ProtocolException, UnsupportedEncodingException
-
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 client_id, String client_secret, 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
-
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
-
matchesErrorAndDescription
protected static boolean matchesErrorAndDescription(String jsonString, String expectedError, String exepectedErrorDescription)
-
isTokenNotActiveError
public static boolean isTokenNotActiveError(String jsonString)
-
isInvalidBearerTokenError
public static boolean isInvalidBearerTokenError(String jsonString)
-
isAccessDeniedNotAuthorizedError
public static boolean isAccessDeniedNotAuthorizedError(String jsonString)
-
-