Create tree hooks
This section explains how to create a hook used by a node within an authentication tree. These tree hooks can perform custom processing after an authentication tree has successfully completed and a session has been created.
AM includes the following authentication tree hooks:
Tree hook | Used by node | Details |
---|---|---|
|
Creates a JWT with the session, encryption, and node details. The JWT is then used to set a persistent cookie on the response. |
|
|
Adds error details to the response. You can inject a TreeFailureResponse
object into your tree hook that adds error details to the message when the |
|
|
Adds failure details to the response. You can inject a TreeFailureResponse object into your tree hook that adds failure details to the message when the |
|
|
Adds success details to the response. |
|
|
Recreates the specified persistent cookie with new idle time and JWT |
The core class of an authentication tree hook
The following example shows an excerpt from the UpdatePersistentCookieTreehook
class.
The Persistent Cookie Decision node uses this tree hook to recreate the persistent cookie.
/**
* A TreeHook for updating a persistent cookie.
*/
@TreeHook.Metadata(configClass = PersistentCookieDecisionNode.Config.class) (1)
public class UpdatePersistentCookieTreeHook implements TreeHook { (2)
...
@Inject (3)
UpdatePersistentCookieTreeHook(@Assisted Request request,
@Assisted Response response,
@Assisted PersistentCookieDecisionNode.Config config,
@Assisted Realm realm,
PersistentJwtStringSupplier persistentJwtStringSupplier,
PersistentCookieResponseHandler persistentCookieResponseHandler,
SecretReferenceCache secretReferenceCache){
this.request = request;
this.response = response;
this.config = config;
this.persistentJwtStringSupplier = persistentJwtStringSupplier;
this.persistentCookieResponseHandler = persistentCookieResponseHandler;
this.secretCache = secretReferenceCache.realm(realm);
}
@Override
public void accept() throws TreeHookException { (4)
logger.debug("UpdatePersistentCookieTreeHook.accept");
String orgName = PersistentCookieResponseHandler.getOrgName(response);
Cookie originalJwt = getJwtCookie(request, config.persistentCookieName());
if (originalJwt == null) {
return;
}
...
}
...
}
1 | The @TreeHook.Metadata annotation.
Before defining the core class, use a Java @TreeHook.Metadata annotation
to specify the class the tree hook uses for its configuration.
Use the configClass property to specify the configuration class of the node that will be using the tree hook.
|
||
2 | The core class must implement the TreeHook interface.
Learn more in the TreeHook interface in the AM Public API Javadoc. |
||
3 | AM uses Google’s Guice dependency injection framework for authentication nodes and tree hooks.
Use the @Inject annotation to construct a new instance of the tree hook,
specifying the configuration interface set up earlier and any other required parameters.
You can use the
|
||
4 | Implement the public void accept() method to define actions to perform on a successful journey outcome.
Optionally, override the The main logic of a tree hook is handled by these two methods. |