com.holonplatform.auth.internal

## Class DefaultAuthContext

- java.lang.Object
- - com.holonplatform.auth.internal.DefaultAuthContext

- All Implemented Interfaces:
    AuthContext, Authentication.AuthenticationNotifier, AuthenticationInspector

      public class DefaultAuthContext
      extends Object
      implements AuthContext

  Default `AuthContext` implementation using a `Realm` to perform authentication and authorization operations.
  Since:
    5.0.0

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from interface com.holonplatform.auth.AuthContext

      `AuthContext.AuthenticationHolder`

  - ### Field Summary

    - ### Fields inherited from interface com.holonplatform.auth.AuthContext

      `CONTEXT_KEY`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DefaultAuthContext(Realm realm)` Constructor using the default `AuthenticationHolder`.
    `DefaultAuthContext(Realm realm, AuthContext.AuthenticationHolder authenticationHolder)` Constructor.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addAuthenticationListener(Authentication.AuthenticationListener authenticationListener)` Register an `Authentication.AuthenticationListener`.
    `Authentication` | `authenticate(AuthenticationToken authenticationToken)` Attempts to authenticate a user using given `AuthenticationToken`.
    `Authentication` | `authenticate(Message<?,?> message, String... schemes)` Attempts to authenticate using given `message` with the same contract as `AuthContext.authenticate(AuthenticationToken)`, using `Realm` registered `AuthenticationToken.AuthenticationTokenResolver`s for given message type, if any.
    `Optional<Authentication>` | `getAuthentication()` Get the current `Authentication`, if available.
    `boolean` | `isPermitted(Collection<? extends Permission> permissions)` Checks if the current `Authentication` has all the specified `Permission`s.
    `boolean` | `isPermitted(Permission... permissions)` Checks if the current `Authentication` has all the specified `Permission`s.
    `boolean` | `isPermitted(String... permissions)` Checks if the current `Authentication` has all the specified permissions, using the `String` permission representation.
    `boolean` | `isPermittedAny(Collection<? extends Permission> permissions)` Checks if the current `Authentication` has any of the specified `Permission`.
    `boolean` | `isPermittedAny(Permission... permissions)` Checks if the current `Authentication` has any of the specified `Permission`.
    `boolean` | `isPermittedAny(String... permissions)` Checks if the current `Authentication` has any of the specified permission, using the `String` permission representation.
    `void` | `removeAuthenticationListener(Authentication.AuthenticationListener authenticationListener)` Removes a registered `Authentication.AuthenticationListener`.
    `Optional<Authentication>` | `unauthenticate()` Unbound current `Authentication` from this AuthContext, if any

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface com.holonplatform.auth.AuthContext

      `create, create, getCurrent, require`

    - ### Methods inherited from interface com.holonplatform.auth.AuthenticationInspector

      `isAuthenticated, requireAuthentication`

- - ### Constructor Detail

    - #### DefaultAuthContext

          public DefaultAuthContext(Realm realm)

      Constructor using the default `AuthenticationHolder`.
      Parameters:
        `realm` - the Realm which will be used as `Authenticator` and `Authorizer` (not null)

    - #### DefaultAuthContext

          public DefaultAuthContext(Realm realm,
                                    AuthContext.AuthenticationHolder authenticationHolder)

      Constructor.
      Parameters:
        `realm` - the Realm which will be used as `Authenticator` and `Authorizer` (not null)
        `authenticationHolder` - `Authentication` holder (not null)

  - ### Method Detail

    - #### getAuthentication

          public Optional<Authentication> getAuthentication()

      Description copied from interface: `AuthenticationInspector`
      Get the current `Authentication`, if available.
      Specified by:
        `getAuthentication` in interface `AuthenticationInspector`
      Returns:
        The current `Authentication`, or an empty Optional if not available.

    - #### authenticate

          public Authentication authenticate(AuthenticationToken authenticationToken)
                                      throws AuthenticationException

      Description copied from interface: `AuthContext`
      Attempts to authenticate a user using given `AuthenticationToken`.

      If the authentication is successful, an `Authentication` instance is stored in this AuthContext and can be retrieved using `AuthenticationInspector.getAuthentication()`. That Authentication instance is also returned by this method.

      If authentication is not successful, a suitable exception is thrown.

      Specified by:
        `authenticate` in interface `AuthContext`
      Parameters:
        `authenticationToken` - the authentication request token
      Returns:
        The `Authentication` instance result of authentication process
      Throws:
        `AuthenticationException` - Authentication failed

    - #### authenticate

          public Authentication authenticate(Message<?,?> message,
                                             String... schemes)
                                      throws AuthenticationException

      Description copied from interface: `AuthContext`
      Attempts to authenticate using given `message` with the same contract as `AuthContext.authenticate(AuthenticationToken)`, using `Realm` registered `AuthenticationToken.AuthenticationTokenResolver`s for given message type, if any.

      If the authentication is successful, an `Authentication` instance is stored in this AuthContext and can be retrieved using `AuthenticationInspector.getAuthentication()`. That Authentication instance is also returned by this method.

      If authentication is not successful, a suitable exception is thrown.

      Specified by:
        `authenticate` in interface `AuthContext`
      Parameters:
        `message` - Authentication request message
        `schemes` - Optional authentication schemes to use. If not null or empty, only `AuthenticationToken.AuthenticationTokenResolver`s bound to given scheme names will be used
      Returns:
        he `Authentication` instance result of authentication process
      Throws:
        `AuthenticationException` - Authentication failed

    - #### unauthenticate

          public Optional<Authentication> unauthenticate()

      Description copied from interface: `AuthContext`
      Unbound current `Authentication` from this AuthContext, if any
      Specified by:
        `unauthenticate` in interface `AuthContext`
      Returns:
        Previous Authentication bound to this context, if any. An empty Optional otherwise.

    - #### isPermitted

          public boolean isPermitted(Permission... permissions)

      Description copied from interface: `AuthenticationInspector`
      Checks if the current `Authentication` has all the specified `Permission`s.

      The permission control strategy depends on the concrete authorization strategy of the current authentication context.

      Specified by:
        `isPermitted` in interface `AuthenticationInspector`
      Parameters:
        `permissions` - The permissions to be checked
      Returns:
        `true` if the current `Authentication` has all the specified permission, `false` otherwise

    - #### isPermitted

          public boolean isPermitted(String... permissions)

      Description copied from interface: `AuthenticationInspector`
      Checks if the current `Authentication` has all the specified permissions, using the `String` permission representation.

      The permission control strategy depends on the concrete authorization strategy of the current authentication context.

      Specified by:
        `isPermitted` in interface `AuthenticationInspector`
      Parameters:
        `permissions` - The permissions to be checked
      Returns:
        `true` if the current `Authentication` has all the specified permission, `false` otherwise

    - #### isPermittedAny

          public boolean isPermittedAny(Permission... permissions)

      Description copied from interface: `AuthenticationInspector`
      Checks if the current `Authentication` has any of the specified `Permission`.

      The permission control strategy depends on the concrete authorization strategy of the current authentication context.

      Specified by:
        `isPermittedAny` in interface `AuthenticationInspector`
      Parameters:
        `permissions` - The permissions to be checked
      Returns:
        `true` if the current `Authentication` has any of the specified permission, `false` otherwise

    - #### isPermittedAny

          public boolean isPermittedAny(String... permissions)

      Description copied from interface: `AuthenticationInspector`
      Checks if the current `Authentication` has any of the specified permission, using the `String` permission representation.

      The permission control strategy depends on the concrete authorization strategy of the current authentication context.

      Specified by:
        `isPermittedAny` in interface `AuthenticationInspector`
      Parameters:
        `permissions` - The permissions to be checked
      Returns:
        `true` if the current `Authentication` has any of the specified permission, `false` otherwise

    - #### isPermitted

          public boolean isPermitted(Collection<? extends Permission> permissions)

      Description copied from interface: `AuthenticationInspector`
      Checks if the current `Authentication` has all the specified `Permission`s.

      The permission control strategy depends on the concrete authorization strategy of the current authentication context.

      Specified by:
        `isPermitted` in interface `AuthenticationInspector`
      Parameters:
        `permissions` - The permissions to be checked
      Returns:
        `true` if the current `Authentication` has all the specified permission, `false` otherwise

    - #### isPermittedAny

          public boolean isPermittedAny(Collection<? extends Permission> permissions)

      Description copied from interface: `AuthenticationInspector`
      Checks if the current `Authentication` has any of the specified `Permission`.

      The permission control strategy depends on the concrete authorization strategy of the current authentication context.

      Specified by:
        `isPermittedAny` in interface `AuthenticationInspector`
      Parameters:
        `permissions` - The permissions to be checked
      Returns:
        `true` if the current `Authentication` has any of the specified permission, `false` otherwise

    - #### addAuthenticationListener

          public void addAuthenticationListener(Authentication.AuthenticationListener authenticationListener)

      Description copied from interface: `Authentication.AuthenticationNotifier`
      Register an `Authentication.AuthenticationListener`.
      Specified by:
        `addAuthenticationListener` in interface `Authentication.AuthenticationNotifier`
      Parameters:
        `authenticationListener` - The listener to add (not null)

    - #### removeAuthenticationListener

          public void removeAuthenticationListener(Authentication.AuthenticationListener authenticationListener)

      Description copied from interface: `Authentication.AuthenticationNotifier`
      Removes a registered `Authentication.AuthenticationListener`.
      Specified by:
        `removeAuthenticationListener` in interface `Authentication.AuthenticationNotifier`
      Parameters:
        `authenticationListener` - Listener to remove

Copyright © 2019 The Holon Platform. All rights reserved.