Class DefaultOauthProvider
java.lang.Object
org.ametys.runtime.plugin.component.AbstractLogEnabled
org.ametys.plugins.extrausermgt.oauth.DefaultOauthProvider
- All Implemented Interfaces:
OAuthProvider
,LogEnabled
,Configurable
,Contextualizable
,Serviceable
public class DefaultOauthProvider
extends AbstractLogEnabled
implements OAuthProvider, Configurable, Serviceable, Contextualizable
OAuth provider definition to interact with a Nextcloud server.
This could be a default OAuth provider definition except for the configuration
part and the handling of the user id provided by Nextcloud as a custom parameter
in the token response.
-
Field Summary
Modifier and TypeFieldDescriptionprotected com.nimbusds.oauth2.sdk.auth.ClientAuthentication
the authentication to use when requesting a tokenprotected URI
the authorization endpoint URIprotected com.nimbusds.oauth2.sdk.id.ClientID
the oauth client idthe list of custom parameters returned with the token that must be stored for later useprotected String
The provider idprotected com.nimbusds.oauth2.sdk.Scope
the scope for the tokenprotected URI
the token endpoint URIstatic final String
Oauth access token expiration date session attributestatic final String
Oauth access token session attributestatic final String
Oauth custom parameter session attributestatic final String
OAuth redirect URI to use after a successful token requeststatic final String
Oauth refresh token session attributestatic final String
OAuth state session attribute -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected com.nimbusds.oauth2.sdk.id.State
Generate a state for the given provider.protected com.nimbusds.oauth2.sdk.token.AccessToken
_getAccessTokenFromAuthorizationServerResponse
(com.nimbusds.oauth2.sdk.http.HTTPResponse httpResponse, ZonedDateTime requestDate) Parse the token response to get the token and store it before returning the newly acquired tokenprotected String
Get the value of a configuration element either by retrieving the associated value in the application config or directly the configuration element valueprotected com.nimbusds.oauth2.sdk.AuthorizationRequest
Build an authorization request based on the provide information.void
configure
(Configuration configuration) void
contextualize
(Context context) Optional<com.nimbusds.oauth2.sdk.token.AccessToken>
getAccessToken
(Redirector redirector) Try to get the currently stored access token for the given provider.Get the authorization endpoint for the providercom.nimbusds.oauth2.sdk.auth.ClientAuthentication
Get the client authentication to use for new token requestcom.nimbusds.oauth2.sdk.id.ClientID
Get the client id provided by the authorization server.Get the list of custom parameters returned with the access token.getId()
Get the extension idcom.nimbusds.oauth2.sdk.Scope
getScope()
Get the scope to request to the provider.Optional<com.nimbusds.oauth2.sdk.token.AccessToken>
Get the stored access token if it exist.<T> Optional<T>
getStoredCustomParameter
(String parameter) Retrieve the stored value for a custom parameterGet the token endpoint for the providerboolean
isKnownState
(com.nimbusds.oauth2.sdk.id.State state) Indicate that the provider is expecting an authorization response for the provided state.com.nimbusds.oauth2.sdk.token.AccessToken
requestAccessToken
(com.nimbusds.oauth2.sdk.AuthorizationGrant authorizationGrant) Request a token to an authorization server using an authorization code previously provided by the authorization server.void
service
(ServiceManager manager) protected com.nimbusds.oauth2.sdk.token.AccessToken
storeTokens
(com.nimbusds.oauth2.sdk.AccessTokenResponse response, ZonedDateTime requestDate) Store the tokens information for later uses.Methods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
Field Details
-
OAUTH_STATE_SESSION_ATTRIBUTE
OAuth state session attribute- See Also:
-
OAUTH_REDIRECT_URI_SESSION_ATTRIBUTE
OAuth redirect URI to use after a successful token request- See Also:
-
OAUTH_ACCESS_TOKEN_SESSION_ATTRIBUTE
Oauth access token session attribute- See Also:
-
OAUTH_ACCESS_TOKEN_EXPIRATION_DATE_SESSION_ATTRIBUTE
Oauth access token expiration date session attribute- See Also:
-
OAUTH_REFRESH_TOKEN_SESSION_ATTRIBUTE
Oauth refresh token session attribute- See Also:
-
OAUTH_CUSTOM_PARAMETER
Oauth custom parameter session attribute- See Also:
-
_id
The provider id -
_clientID
the oauth client id -
_auth
the authentication to use when requesting a token -
_authorizationEnpoint
the authorization endpoint URI -
_tokenEndpointURI
the token endpoint URI -
_scope
the scope for the token -
_customParameters
the list of custom parameters returned with the token that must be stored for later use
-
-
Constructor Details
-
DefaultOauthProvider
public DefaultOauthProvider()
-
-
Method Details
-
contextualize
- Specified by:
contextualize
in interfaceContextualizable
- Throws:
ContextException
-
service
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
configure
- Specified by:
configure
in interfaceConfigurable
- Throws:
ConfigurationException
-
_getConfigValue
Get the value of a configuration element either by retrieving the associated value in the application config or directly the configuration element value- Parameters:
cfg
- a configuration element. Can not benull
- Returns:
- the value or null if the value is not present
-
getClientID
Description copied from interface:OAuthProvider
Get the client id provided by the authorization server.- Specified by:
getClientID
in interfaceOAuthProvider
- Returns:
- the client id
-
getAuthorizationEndpointURI
Description copied from interface:OAuthProvider
Get the authorization endpoint for the provider- Specified by:
getAuthorizationEndpointURI
in interfaceOAuthProvider
- Returns:
- the uri
-
getTokenEndpointURI
Description copied from interface:OAuthProvider
Get the token endpoint for the provider- Specified by:
getTokenEndpointURI
in interfaceOAuthProvider
- Returns:
- the uri
-
getId
Description copied from interface:OAuthProvider
Get the extension id- Specified by:
getId
in interfaceOAuthProvider
- Returns:
- the id
-
getClientAuthentication
Description copied from interface:OAuthProvider
Get the client authentication to use for new token request- Specified by:
getClientAuthentication
in interfaceOAuthProvider
- Returns:
- the authentication
-
getScope
Description copied from interface:OAuthProvider
Get the scope to request to the provider.null
if no scope should be use.- Specified by:
getScope
in interfaceOAuthProvider
- Returns:
- the scope or
null
-
getCustomParametersName
Description copied from interface:OAuthProvider
Get the list of custom parameters returned with the access token. The listed parameters are stored with the access token for later use.- Specified by:
getCustomParametersName
in interfaceOAuthProvider
- Returns:
- the list of parameter names
-
isKnownState
Description copied from interface:OAuthProvider
Indicate that the provider is expecting an authorization response for the provided state.- Specified by:
isKnownState
in interfaceOAuthProvider
- Parameters:
state
- the state- Returns:
- true if the provider has initiated a authorization process with this state and the process is not complete yet.
-
getStoredAccessToken
Description copied from interface:OAuthProvider
Get the stored access token if it exist. If a token exists, but is expired, this method will silently try to refresh it.- Specified by:
getStoredAccessToken
in interfaceOAuthProvider
- Returns:
- the access token or empty
-
getStoredCustomParameter
Description copied from interface:OAuthProvider
Retrieve the stored value for a custom parameter- Specified by:
getStoredCustomParameter
in interfaceOAuthProvider
- Type Parameters:
T
- the type of the value to retrieve. This must be a valid JSON type- Parameters:
parameter
- the name of the parameter- Returns:
- the value of the parameter if it exists
-
getAccessToken
public Optional<com.nimbusds.oauth2.sdk.token.AccessToken> getAccessToken(Redirector redirector) throws ProcessingException, IOException Description copied from interface:OAuthProvider
Try to get the currently stored access token for the given provider. If no valid token is available, the method should returnOptional.empty()
and try to redirect to the authorization protocol.- Specified by:
getAccessToken
in interfaceOAuthProvider
- Parameters:
redirector
- the redirector to use if no token are available.- Returns:
- the token or
Optional.empty()
- Throws:
ProcessingException
- if an error occurred while redirectingIOException
- if an error occurred while redirecting- See Also:
-
buildAuthorizationCodeRequest
Build an authorization request based on the provide information. The request will use aResponseType#CODE
for the response type.- Returns:
- an authorization request
-
_generateState
Generate a state for the given provider. The state will be stored in the provider to be able to retrieve the provider responding to a authorize request being processed inOAuthCallbackAction
. We also store the state in session to unsure that the response is linked to the current session.- Returns:
- the newly generated state
-
requestAccessToken
public com.nimbusds.oauth2.sdk.token.AccessToken requestAccessToken(com.nimbusds.oauth2.sdk.AuthorizationGrant authorizationGrant) throws IOException Description copied from interface:OAuthProvider
Request a token to an authorization server using an authorization code previously provided by the authorization server. This method will store the result of the token request in session. SeeOAuthProvider.getStoredAccessToken()
andOAuthProvider.getAccessToken(Redirector)
for how to use the requested token.- Specified by:
requestAccessToken
in interfaceOAuthProvider
- Parameters:
authorizationGrant
- the grant to use to request the token- Returns:
- the new access token
- Throws:
IOException
- if an error occurred while communicating with the token endpoint- See Also:
-
_getAccessTokenFromAuthorizationServerResponse
protected com.nimbusds.oauth2.sdk.token.AccessToken _getAccessTokenFromAuthorizationServerResponse(com.nimbusds.oauth2.sdk.http.HTTPResponse httpResponse, ZonedDateTime requestDate) Parse the token response to get the token and store it before returning the newly acquired token- Parameters:
httpResponse
- the responserequestDate
- the date of the request- Returns:
- the new token
- Throws:
AccessDeniedException
- if the response doesn't indicate success
-
storeTokens
protected com.nimbusds.oauth2.sdk.token.AccessToken storeTokens(com.nimbusds.oauth2.sdk.AccessTokenResponse response, ZonedDateTime requestDate) Store the tokens information for later uses.- Parameters:
response
- the tokens returned by the successful token requestrequestDate
- the request date to compute the expiration date- Returns:
- the access token
-