Class OAuthIdentity

java.lang.Object
com.sodius.oslc.server.oauth.OAuthIdentity
All Implemented Interfaces:
Serializable

public class OAuthIdentity extends Object implements Serializable
Represents the identity of a user that successfully authenticated to the application. Identity and user authentication is managed by the OAuthFilter class.

This class is concrete and can be instantiated as is since 1.13.0.

This class MUST no to be subclassed and will be marked final in a future release.

Since:
1.3.0
See Also:
  • Constructor Details

    • OAuthIdentity

      public OAuthIdentity(Person user)
      Creates a new identity instance for the specified user.
      Parameters:
      user - the user who has authenticated.

      Cluster-compatible applications must NOT use a subclass of Person.

      Throws:
      NullPointerException - if user is null.
  • Method Details

    • getThreadInstance

      public static Optional<OAuthIdentity> getThreadInstance()
      Returns the OAuth identity associated to the current thread, if any.

      If a servlet request was associated to the current thread at the time the identity was registered, both objects are considered linked and sharing the same validity period. When the servlet request is no longer valid, i.e. when the servlet execution completed, the corresponding identity is also considered invalid and this method returns an empty optional.

      Caller may rather use requireThreadInstance() if an identity is required to be present.

      Returns:
      an identity if user is authenticated, an empty optional otherwise.
      Since:
      4.0.0
      See Also:
    • requireThreadInstance

      public static OAuthIdentity requireThreadInstance()
      Returns the OAuth identity associated to the current thread. Throws an exception if called from a thread with no associated identity, e.g. when user has not yet authenticated in the current servlet request.

      Caller may use getThreadInstance() if it uncertain an OAuth identity is present.

      Returns:
      the identity of the authenticated user
      Throws:
      IllegalStateException - if no OAuth identity is set for current thread
      Since:
      4.0.0
      See Also:
    • setThreadInstance

      public static void setThreadInstance(OAuthIdentity identity)
      Sets an OAuth identity to associate to the current thread.

      If a servlet request is associated to the current thread at the time the method is called, both objects are considered linked and sharing the same validity period. When the servlet request is no longer valid, i.e. when the servlet execution completed, the corresponding identity is also considered invalid and getThreadInstance() returns an empty optional.

      Parameters:
      identity - an OAuth identity, null if there's no longer an identity for this thread
    • executeImpersonated

      public static final <V, X extends Exception> V executeImpersonated(Person functionalUser, OAuthIdentity.ImpersonationCallable<V,X> callable) throws X
      Executes a callable on behalf of the given functional user. Impersonation makes the functional user to become the OAuth identity of current thread for the lifetime of this method call. The servlet request possibly associated to the current thread has no impact on the behavior of this method.
      Type Parameters:
      V - the result type of method call
      X - the type of exception potentially raised by the callable execution
      Parameters:
      functionalUser - the functional user
      callable - the code to execute on behalf of the functional user
      Returns:
      the result of OAuthIdentity.ImpersonationCallable.call()
      Throws:
      X - if the callable throws such exception
      Since:
      4.2.0
    • getUser

      public Person getUser()
      Returns the authenticated user.
      Returns:
      the authenticated user.