org.mule.security.oauth
Class BaseOAuth2Manager<C extends OAuth2Adapter>

java.lang.Object
  extended by org.mule.security.oauth.callback.DefaultHttpCallbackAdapter
      extended by org.mule.security.oauth.BaseOAuth2Manager<C>
All Implemented Interfaces:
MuleContextAware, Capabilities, ProcessAdapter<OAuth2Adapter>, Disposable, Initialisable, Startable, Stoppable, NameableObject, NamedObject, HttpCallbackAdapter, OAuth2Manager<OAuth2Adapter>, OnNoTokenPolicyAware

public abstract class BaseOAuth2Manager<C extends OAuth2Adapter>
extends DefaultHttpCallbackAdapter
implements MuleContextAware, Initialisable, Capabilities, Startable, Stoppable, Disposable, OAuth2Manager<OAuth2Adapter>, NameableObject


Field Summary
protected  FlowConstruct flowConstruct
          Flow Construct
protected  MuleContext muleContext
          muleContext
 
Fields inherited from interface org.mule.api.lifecycle.Initialisable
PHASE_NAME
 
Fields inherited from interface org.mule.api.lifecycle.Startable
PHASE_NAME
 
Fields inherited from interface org.mule.api.lifecycle.Stoppable
PHASE_NAME
 
Fields inherited from interface org.mule.api.lifecycle.Disposable
PHASE_NAME
 
Constructor Summary
BaseOAuth2Manager()
           
 
Method Summary
 OAuth2Adapter acquireAccessToken(String accessTokenId)
          Borrow an access token from the pool
 String buildAuthorizeUrl(Map<String,String> extraParameters, String authorizationUrl, String redirectUri)
          Generates the full URL of an authorization endpoint including query params
 OAuth2Adapter createAdapter(String verifier)
          Create a new adapter using the specified verifier and insert it into the pool.
protected abstract  org.apache.commons.pool.KeyedPoolableObjectFactory<String,OAuth2Adapter> createPoolFactory(OAuth2Manager<OAuth2Adapter> oauthManager, ObjectStore<Serializable> objectStore)
          Returns the concrete instance of KeyedPoolableObjectFactory that's going to be in charge of creating the objects in the pool
 void destroyAccessToken(String userId, OAuth2Adapter connector)
          Destroy an access token
 void dispose()
          if Disposable, then org.mule.security.oauth.BaseOAuth2Manager.defaultUnauthorizedConnector is disposed
 void fetchAccessToken(OAuth2Adapter adapter, String redirectUri)
          Makes an http call to the adapter's accessTokenUrl and extracts the access token, which is then set into the adapter
protected abstract  void fetchCallbackParameters(OAuth2Adapter adapter, String response)
          Extracts any custom parameters from the OAuth response and sets them accordingly on the adapter
 ObjectStore<Serializable> getAccessTokenObjectStore()
          Retrieves accessTokenObjectStore
 org.apache.commons.pool.KeyedPoolableObjectFactory<String,OAuth2Adapter> getAccessTokenPoolFactory()
          Retrieves accessTokenPoolFactory
 String getApplicationName()
          Retrieves applicationName
 String getConsumerKey()
           
 String getConsumerSecret()
           
 String getDefaultAccessTokenId()
           
 OAuth2Adapter getDefaultUnauthorizedConnector()
          Retrieves defaultUnauthorizedConnector
 FlowConstruct getFlowConstruct()
          Retrieves flowConstruct
protected abstract  Logger getLogger()
           
 MuleContext getMuleContext()
          Returns the mule context
 String getName()
          Gets the name of the object
 OnNoTokenPolicy getOnNoTokenPolicy()
           
<T> ProcessTemplate<T,OAuth2Adapter>
getProcessTemplate()
           
 String getScope()
          Retrieves scope
 void hasBeenAuthorized(OAuth2Adapter adapter)
          Validates that there's an access token for the given adapter.
 void initialise()
          Method used to perform any initialisation work.
protected abstract  OAuth2Adapter instantiateAdapter()
          Creates a concrete instance of the OAuth2Adapter that corresponds with this OAuthManager
 boolean isCapableOf(ModuleCapability capability)
          Returns true if this module implements such capability
 void postAuth(OAuth2Adapter adapter, String accessTokenId)
          Calls the org.mule.security.oauth.OAuth2Connector.postAuth() on the adapter.
 void refreshAccessToken(OAuth2Adapter adapter, String accessTokenId)
          if refresh token is available, then it makes an http call to refresh the access token.
protected  Set<Class<? extends Exception>> refreshAccessTokenOn()
           
 void releaseAccessToken(String userId, OAuth2Adapter connector)
          Return an access token to the pool
 MuleEvent restoreAuthorizationEvent(String eventId)
          Recovers a MuleEvent from the object store.
 void setAccessTokenObjectStore(ObjectStore<Serializable> value)
          Sets accessTokenObjectStore
protected  void setAccessTokenPool(org.apache.commons.pool.impl.GenericKeyedObjectPool<String,OAuth2Adapter> accessTokenPool)
           
 void setAccessTokenUrl(String value)
          Sets accessTokenUrl
 void setApplicationName(String value)
          Sets applicationName
 void setAuthorizationUrl(String value)
          Sets authorizationUrl
 void setConsumerKey(String value)
          Sets consumerKey
 void setConsumerSecret(String value)
          Sets consumerSecret
protected abstract  void setCustomProperties(OAuth2Adapter adapter)
          Populates the adapter with custom properties not accessible from the base interface.
 void setDefaultAccessTokenId(String defaultAccessTokenId)
           
protected  void setDefaultUnauthorizedConnector(OAuth2Adapter defaultUnauthorizedConnector)
           
 void setFlowConstruct(FlowConstruct flowConstruct)
           
 void setHttpUtil(HttpUtil httpUtil)
           
 void setMuleContext(MuleContext muleContext)
           
 void setName(String name)
          Sets the name of the object
 void setOauthResponseParser(OAuthResponseParser oauthResponseParser)
           
 void setOnNoTokenPolicy(OnNoTokenPolicy policy)
           
 void setRefreshTokenManager(RefreshTokenManager refreshTokenManager)
           
 void setScope(String value)
          Sets scope
 void start()
          if Startable, then org.mule.security.oauth.BaseOAuth2Manager.defaultUnauthorizedConnector is started
 void stop()
          if Stoppable, then org.mule.security.oauth.BaseOAuth2Manager.defaultUnauthorizedConnector is stopped
 void storeAuthorizationEvent(MuleEvent event)
          This method is expected to receive the MuleEvent corresponding to the execution of an OAuth2 authorize processor.
 
Methods inherited from class org.mule.security.oauth.callback.DefaultHttpCallbackAdapter
getAsync, getConnector, getDomain, getLocalPort, getPath, getRemotePort, setAsync, setConnector, setDomain, setLocalPort, setPath, setRemotePort
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.mule.security.oauth.callback.HttpCallbackAdapter
getAsync, getConnector, getDomain, getLocalPort, getPath, getRemotePort, setAsync, setConnector, setDomain, setLocalPort, setPath, setRemotePort
 

Field Detail

muleContext

protected MuleContext muleContext
muleContext


flowConstruct

protected FlowConstruct flowConstruct
Flow Construct

Constructor Detail

BaseOAuth2Manager

public BaseOAuth2Manager()
Method Detail

getLogger

protected abstract Logger getLogger()
Returns:
the logger to be used when logging messages.

instantiateAdapter

protected abstract OAuth2Adapter instantiateAdapter()
Creates a concrete instance of the OAuth2Adapter that corresponds with this OAuthManager

Returns:
an instance of OAuth2Adapter

createPoolFactory

protected abstract org.apache.commons.pool.KeyedPoolableObjectFactory<String,OAuth2Adapter> createPoolFactory(OAuth2Manager<OAuth2Adapter> oauthManager,
                                                                                                              ObjectStore<Serializable> objectStore)
Returns the concrete instance of KeyedPoolableObjectFactory that's going to be in charge of creating the objects in the pool

Parameters:
oauthManager - the OAuthManager that will manage the created objects
objectStore - an instance of ObjectStore that will be responsible for storing instances of OAuthState
Returns:
an instance of KeyedPoolableObjectFactory

setCustomProperties

protected abstract void setCustomProperties(OAuth2Adapter adapter)
Populates the adapter with custom properties not accessible from the base interface.

Parameters:
adapter - an instance of OAuth2Adapter

fetchCallbackParameters

protected abstract void fetchCallbackParameters(OAuth2Adapter adapter,
                                                String response)
Extracts any custom parameters from the OAuth response and sets them accordingly on the adapter

Parameters:
adapter - the adapter on which the custom parameters will be set on
response - the response obatined from the OAuth provider

initialise

public final void initialise()
                      throws InitialisationException
Description copied from interface: Initialisable
Method used to perform any initialisation work. If a fatal error occurs during initialisation an InitialisationException should be thrown, causing the Mule instance to shutdown. If the error is recoverable, say by retrying to connect, a RecoverableException should be thrown. There is no guarantee that by throwing a Recoverable exception that the Mule instance will not shut down.

Specified by:
initialise in interface Initialisable
Overrides:
initialise in class DefaultHttpCallbackAdapter
Throws:
InitialisationException - if a fatal error occurs causing the Mule instance to shutdown
RecoverableException - if an error occurs that can be recovered from

start

public final void start()
                 throws MuleException
if Startable, then org.mule.security.oauth.BaseOAuth2Manager.defaultUnauthorizedConnector is started

Specified by:
start in interface Startable
Throws:
MuleException

stop

public final void stop()
                throws MuleException
if Stoppable, then org.mule.security.oauth.BaseOAuth2Manager.defaultUnauthorizedConnector is stopped

Specified by:
stop in interface Stoppable
Throws:
MuleException

dispose

public final void dispose()
if Disposable, then org.mule.security.oauth.BaseOAuth2Manager.defaultUnauthorizedConnector is disposed

Specified by:
dispose in interface Disposable

createAdapter

public final OAuth2Adapter createAdapter(String verifier)
                                  throws Exception
Create a new adapter using the specified verifier and insert it into the pool. This adapter will be already initialized and started

Specified by:
createAdapter in interface OAuth2Manager<OAuth2Adapter>
Parameters:
verifier - OAuth verifier
Returns:
A newly created connector
Throws:
Exception - If the access token cannot be retrieved

acquireAccessToken

public final OAuth2Adapter acquireAccessToken(String accessTokenId)
                                       throws Exception
Borrow an access token from the pool

Specified by:
acquireAccessToken in interface OAuth2Manager<OAuth2Adapter>
Parameters:
accessTokenId - User identification used to borrow the access token
Returns:
An existing authorized connector
Throws:
Exception - If the access token cannot be retrieved

releaseAccessToken

public final void releaseAccessToken(String userId,
                                     OAuth2Adapter connector)
                              throws Exception
Return an access token to the pool

Specified by:
releaseAccessToken in interface OAuth2Manager<OAuth2Adapter>
Parameters:
userId - User identification used to borrow the access token
connector - Authorized connector to be returned to the pool
Throws:
Exception - If the access token cannot be returned

destroyAccessToken

public final void destroyAccessToken(String userId,
                                     OAuth2Adapter connector)
                              throws Exception
Destroy an access token

Specified by:
destroyAccessToken in interface OAuth2Manager<OAuth2Adapter>
Parameters:
userId - User identification used to borrow the access token
connector - Authorized connector to the destroyed
Throws:
Exception - If the access token could not be destroyed.

buildAuthorizeUrl

public final String buildAuthorizeUrl(Map<String,String> extraParameters,
                                      String authorizationUrl,
                                      String redirectUri)
Generates the full URL of an authorization endpoint including query params

Specified by:
buildAuthorizeUrl in interface OAuth2Manager<OAuth2Adapter>
Parameters:
extraParameters - a map with non-standard query-param value pairs
authorizationUrl - the url of the authorization endpoint per OAuth specification
redirectUri - the uri of the redirection endpoint
Returns:
the authorization URL as a String

fetchAccessToken

public final void fetchAccessToken(OAuth2Adapter adapter,
                                   String redirectUri)
                            throws org.mule.common.security.oauth.exception.UnableToAcquireAccessTokenException
Makes an http call to the adapter's accessTokenUrl and extracts the access token, which is then set into the adapter

Specified by:
fetchAccessToken in interface OAuth2Manager<OAuth2Adapter>
Parameters:
adapter - the connector's adapter
redirectUri - the redirection URI
Throws:
org.mule.common.security.oauth.exception.UnableToAcquireAccessTokenException

hasBeenAuthorized

public final void hasBeenAuthorized(OAuth2Adapter adapter)
                             throws org.mule.common.security.oauth.exception.NotAuthorizedException
Validates that there's an access token for the given adapter.

Specified by:
hasBeenAuthorized in interface OAuth2Manager<OAuth2Adapter>
Parameters:
adapter - the adapter which authorization you want to test
Throws:
org.mule.common.security.oauth.exception.NotAuthorizedException - if no access token available for this adapter

refreshAccessToken

public final void refreshAccessToken(OAuth2Adapter adapter,
                                     String accessTokenId)
                              throws org.mule.common.security.oauth.exception.UnableToAcquireAccessTokenException
if refresh token is available, then it makes an http call to refresh the access token. All newly obtained tokens are set into the adapter. After the token has been refreshed, org.mule.security.oauth.OAuth2Manager.postAuth(OAuth2Adapter, String) is invoked

Specified by:
refreshAccessToken in interface OAuth2Manager<OAuth2Adapter>
Parameters:
adapter - the connector's adapter
accessTokenId - the id of the token you're trying to refresh
Throws:
org.mule.common.security.oauth.exception.UnableToAcquireAccessTokenException

storeAuthorizationEvent

public void storeAuthorizationEvent(MuleEvent event)
                             throws Exception
This method is expected to receive the MuleEvent corresponding to the execution of an OAuth2 authorize processor. The event will be persisted in this manager's object store following these rules:

Specified by:
storeAuthorizationEvent in interface OAuth2Manager<OAuth2Adapter>
Parameters:
event - a mule event
Throws:
Exception

restoreAuthorizationEvent

public MuleEvent restoreAuthorizationEvent(String eventId)
                                    throws ObjectStoreException,
                                           ObjectDoesNotExistException
Recovers a MuleEvent from the object store. The key that is fetched comes from replacing the given eventId into the template on org.mule.security.oauth.OAuthProperties.AUTHORIZATION_EVENT_KEY_TEMPLATE

Specified by:
restoreAuthorizationEvent in interface OAuth2Manager<OAuth2Adapter>
Parameters:
eventId - the id of the event to be restored
Returns:
a MuleEvent
Throws:
ObjectStoreException - if there was an error accessing the object store
ObjectDoesNotExistException - if there's no entry for the event id

postAuth

public void postAuth(OAuth2Adapter adapter,
                     String accessTokenId)
              throws Exception
Calls the org.mule.security.oauth.OAuth2Connector.postAuth() on the adapter. If it fails due to access token expiration and accessTokenId is not null, then the token is refresh and the operation is re-attempted. If token refreshment fails or if accessTokenId is null, then the original exception is thrown

Specified by:
postAuth in interface OAuth2Manager<OAuth2Adapter>
Parameters:
adapter - the connector adapter
accessTokenId - the id of the accessToken to be used
Throws:
Exception

refreshAccessTokenOn

protected Set<Class<? extends Exception>> refreshAccessTokenOn()

isCapableOf

public final boolean isCapableOf(ModuleCapability capability)
Returns true if this module implements such capability

Specified by:
isCapableOf in interface Capabilities
Parameters:
capability - The capability to test for
Returns:
True if it does, false otherwise

getProcessTemplate

public <T> ProcessTemplate<T,OAuth2Adapter> getProcessTemplate()
Specified by:
getProcessTemplate in interface ProcessAdapter<OAuth2Adapter>
Specified by:
getProcessTemplate in interface OAuth2Manager<OAuth2Adapter>

getDefaultUnauthorizedConnector

public OAuth2Adapter getDefaultUnauthorizedConnector()
Retrieves defaultUnauthorizedConnector

Specified by:
getDefaultUnauthorizedConnector in interface OAuth2Manager<OAuth2Adapter>

setApplicationName

public void setApplicationName(String value)
Sets applicationName

Parameters:
value - Value to set

getApplicationName

public String getApplicationName()
Retrieves applicationName


setScope

public void setScope(String value)
Sets scope

Parameters:
value - Value to set

getScope

public String getScope()
Retrieves scope


getMuleContext

public final MuleContext getMuleContext()
Returns the mule context

Specified by:
getMuleContext in interface OAuth2Manager<OAuth2Adapter>

setMuleContext

public final void setMuleContext(MuleContext muleContext)
Specified by:
setMuleContext in interface MuleContextAware

getFlowConstruct

public FlowConstruct getFlowConstruct()
Retrieves flowConstruct


setFlowConstruct

public void setFlowConstruct(FlowConstruct flowConstruct)

getAccessTokenObjectStore

public ObjectStore<Serializable> getAccessTokenObjectStore()
Retrieves accessTokenObjectStore


setAccessTokenObjectStore

public void setAccessTokenObjectStore(ObjectStore<Serializable> value)
Sets accessTokenObjectStore

Parameters:
value - Value to set

getAccessTokenPoolFactory

public org.apache.commons.pool.KeyedPoolableObjectFactory<String,OAuth2Adapter> getAccessTokenPoolFactory()
Retrieves accessTokenPoolFactory

Specified by:
getAccessTokenPoolFactory in interface OAuth2Manager<OAuth2Adapter>

setHttpUtil

public void setHttpUtil(HttpUtil httpUtil)

setOauthResponseParser

public void setOauthResponseParser(OAuthResponseParser oauthResponseParser)

setAuthorizationUrl

public void setAuthorizationUrl(String value)
Sets authorizationUrl

Parameters:
value - Value to set

setAccessTokenUrl

public void setAccessTokenUrl(String value)
Sets accessTokenUrl

Parameters:
value - Value to set

setConsumerKey

public void setConsumerKey(String value)
Sets consumerKey

Parameters:
value - Value to set

setConsumerSecret

public void setConsumerSecret(String value)
Sets consumerSecret

Parameters:
value - Value to set

setDefaultUnauthorizedConnector

protected void setDefaultUnauthorizedConnector(OAuth2Adapter defaultUnauthorizedConnector)

getConsumerKey

public String getConsumerKey()

getConsumerSecret

public String getConsumerSecret()

getName

public String getName()
Description copied from interface: NamedObject
Gets the name of the object

Specified by:
getName in interface NamedObject
Returns:
the name of the object

setName

public void setName(String name)
Description copied from interface: NameableObject
Sets the name of the object

Specified by:
setName in interface NameableObject
Parameters:
name - the name of the object

getOnNoTokenPolicy

public OnNoTokenPolicy getOnNoTokenPolicy()
Specified by:
getOnNoTokenPolicy in interface OnNoTokenPolicyAware
Returns:
a non-null instance of OnNoTokenPolicy that specifies the behavior to take when token is not set

setOnNoTokenPolicy

public void setOnNoTokenPolicy(OnNoTokenPolicy policy)
Specified by:
setOnNoTokenPolicy in interface OnNoTokenPolicyAware

getDefaultAccessTokenId

public String getDefaultAccessTokenId()
Specified by:
getDefaultAccessTokenId in interface OAuth2Manager<OAuth2Adapter>

setDefaultAccessTokenId

public void setDefaultAccessTokenId(String defaultAccessTokenId)

setRefreshTokenManager

public void setRefreshTokenManager(RefreshTokenManager refreshTokenManager)

setAccessTokenPool

protected void setAccessTokenPool(org.apache.commons.pool.impl.GenericKeyedObjectPool<String,OAuth2Adapter> accessTokenPool)


Copyright © 2003-2014 MuleSoft, Inc.. All Rights Reserved.