Class AuthenticationContext

java.lang.Object
org.eclipse.aether.repository.AuthenticationContext
All Implemented Interfaces:
Closeable, AutoCloseable

public final class AuthenticationContext extends Object implements Closeable
A glorified map of key value pairs holding (cleartext) authentication data. Authentication contexts are used internally when network operations need to access secured repositories or proxies. Each authentication context manages the credentials required to access a single host. Unlike Authentication callbacks which exist for a potentially long time like the duration of a repository system session, an authentication context has a supposedly short lifetime and should be closed as soon as the corresponding network operation has finished:
 AuthenticationContext context = AuthenticationContext.forRepository( session, repository );
 try {
     // get credentials
     char[] password = context.get( AuthenticationContext.PASSWORD, char[].class );
     // perform network operation using retrieved credentials
     ...
 } finally {
     // erase confidential authentication data from heap memory
     AuthenticationContext.close( context );
 }
 
The same authentication data can often be presented using different data types, e.g. a password can be presented using a character array or (less securely) using a string. For ease of use, an authentication context treats the following groups of data types as equivalent and converts values automatically during retrieval:
  • String, char[]
  • String, File
An authentication context is thread-safe.
  • Field Details

  • Method Details

    • forRepository

      Gets an authentication context for the specified repository.
      Parameters:
      session - The repository system session during which the repository is accessed, may be null.
      repository - The repository for which to create an authentication context, must not be null.
      Returns:
      An authentication context for the repository or null if no authentication is configured for it.
    • forProxy

      Gets an authentication context for the proxy of the specified repository.
      Parameters:
      session - The repository system session during which the repository is accessed, may be null.
      repository - The repository for whose proxy to create an authentication context, must not be null.
      Returns:
      An authentication context for the proxy or null if no proxy is set or no authentication is configured for it.
    • getSession

      Gets the repository system session during which the authentication happens (if within session).
      Returns:
      The repository system session, may be null if context is created outside of session.
    • getRepository

      Gets the repository requiring authentication. If getProxy() is not null, the data gathered by this authentication context does not apply to the repository's host but rather the proxy.
      Returns:
      The repository to be contacted, never null.
    • getProxy

      public Proxy getProxy()
      Gets the proxy (if any) to be authenticated with.
      Returns:
      The proxy or null if authenticating directly with the repository's host.
    • get

      public String get(String key)
      Gets the authentication data for the specified key.
      Parameters:
      key - The key whose authentication data should be retrieved, must not be null.
      Returns:
      The requested authentication data or null if none.
    • get

      public <T> T get(String key, Class<T> type)
      Gets the authentication data for the specified key.
      Type Parameters:
      T - The data type of the authentication data.
      Parameters:
      key - The key whose authentication data should be retrieved, must not be null.
      type - The expected type of the authentication data, must not be null.
      Returns:
      The requested authentication data or null if none or if the data doesn't match the expected type.
    • get

      public <T> T get(String key, Map<String,String> data, Class<T> type)
      Gets the authentication data for the specified key.
      Type Parameters:
      T - The data type of the authentication data.
      Parameters:
      key - The key whose authentication data should be retrieved, must not be null.
      data - Any (read-only) extra data in form of key value pairs that might be useful when getting the authentication data, may be null.
      type - The expected type of the authentication data, must not be null.
      Returns:
      The requested authentication data or null if none or if the data doesn't match the expected type.
    • put

      public void put(String key, Object value)
      Puts the specified authentication data into this context. This method should only be called from implementors of Authentication.fill(AuthenticationContext, String, Map). Passed in character arrays are not cloned and become owned by this context, i.e. get erased when the context gets closed.
      Parameters:
      key - The key to associate the authentication data with, must not be null.
      value - The (cleartext) authentication data to store, may be null.
    • close

      public void close()
      Closes this authentication context and erases sensitive authentication data from heap memory. Closing an already closed context has no effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • close

      public static void close(AuthenticationContext context)
      Closes the specified authentication context. This is a convenience method doing a null check before calling close() on the given context.
      Parameters:
      context - The authentication context to close, may be null.