AuthenticationContext Class

  • java.lang.Object
    • com.microsoft.aad.adal4j.AuthenticationContext

public class AuthenticationContext

The main class representing the authority issuing tokens for resources. It provides several ways to request access token, namely via Authorization Code, Confidential Client and Client Certificate.

Constructor Summary

Constructor Description
AuthenticationContext(final String authority, final boolean validateAuthority, final ExecutorService service)

Constructor to create the context with the address of the authority.

Method Summary

Modifier and Type Method and Description
Future<DeviceCode> acquireDeviceCode(final String clientId, final String resource, final AuthenticationCallback<DeviceCode> callback)

Acquires a device code from the authority

Future<AuthenticationResult> acquireToken(final String resource, final AsymmetricKeyCredential credential, final AuthenticationCallback callback)

Acquires security token from the authority.

Future<AuthenticationResult> acquireToken(final String resource, final ClientAssertion clientAssertion, final AuthenticationCallback callback)

Acquires security token from the authority.

Future<AuthenticationResult> acquireToken(final String resource, final ClientCredential credential, final AuthenticationCallback callback)

Acquires security token from the authority.

Future<AuthenticationResult> acquireToken(final String resource, final String clientId, final String username, final String password, final AuthenticationCallback callback)

Acquires a security token from the authority using a username/password flow.

Future<AuthenticationResult> acquireToken(final String resource, final UserAssertion userAssertion, final AsymmetricKeyCredential credential, final AuthenticationCallback callback)

Acquires an access token from the authority on behalf of a user. It requires using a user token previously received. Uses certificate to authenticate client.

Future<AuthenticationResult> acquireToken(final String resource, final UserAssertion userAssertion, final ClientCredential credential, final AuthenticationCallback callback)

Acquires an access token from the authority on behalf of a user. It requires using a user token previously received.

Future<AuthenticationResult> acquireTokenByAuthorizationCode(final String authorizationCode, final String resource, final String clientId, final URI redirectUri, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Future<AuthenticationResult> acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final AsymmetricKeyCredential credential, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Future<AuthenticationResult> acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final AsymmetricKeyCredential credential, final String resource, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Future<AuthenticationResult> acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final ClientAssertion clientAssertion, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Future<AuthenticationResult> acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final ClientAssertion clientAssertion, final String resource, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Future<AuthenticationResult> acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final ClientCredential credential, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Future<AuthenticationResult> acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final ClientCredential credential, final String resource, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Future<AuthenticationResult> acquireTokenByDeviceCode(final DeviceCode deviceCode, final AuthenticationCallback callback)

Acquires security token from the authority using an device code previously received.

Future<AuthenticationResult> acquireTokenByRefreshToken(final String refreshToken, final AsymmetricKeyCredential credential, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Future<AuthenticationResult> acquireTokenByRefreshToken(final String refreshToken, final AsymmetricKeyCredential credential, final String resource, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Future<AuthenticationResult> acquireTokenByRefreshToken(final String refreshToken, final ClientCredential credential, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Future<AuthenticationResult> acquireTokenByRefreshToken(final String refreshToken, final ClientCredential credential, final String resource, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Future<AuthenticationResult> acquireTokenByRefreshToken(final String refreshToken, final String clientId, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received. This method is suitable for the daemon OAuth2 flow when a client secret is not possible.

Future<AuthenticationResult> acquireTokenByRefreshToken(final String refreshToken, final String clientId, final ClientAssertion clientAssertion, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Future<AuthenticationResult> acquireTokenByRefreshToken(final String refreshToken, final String clientId, final ClientAssertion clientAssertion, final String resource, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Future<AuthenticationResult> acquireTokenByRefreshToken(final String refreshToken, final String clientId, final String resource, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received. This method is suitable for the daemon OAuth2 flow when a client secret is not possible.

String getAuthority()

Authority associated with the context instance

String getCorrelationId()

Returns the correlation id configured by the user. It does not return the id automatically generated by the API in case the user does not provide one.

Proxy getProxy()

Returns Proxy configuration

SSLSocketFactory getSslSocketFactory()

Returns SSLSocketFactory configuration object.

boolean isLogPii()

Returns logPii - boolean value, which determines whether Pii (personally identifiable information) will be logged in

void setCorrelationId(final String correlationId)

Set optional correlation id to be used by the API. If not provided, the API generates a random id.

void setLogPii(boolean logPii)

Set logPii - boolean value, which determines whether Pii (personally identifiable information) will be logged in

void setProxy(Proxy proxy)

Sets Proxy configuration to be used by the context for all network communication. Default is null and system defined properties if any, would be used.

void setSslSocketFactory(SSLSocketFactory sslSocketFactory)

Sets SSLSocketFactory object to be used by the context.

boolean shouldValidateAuthority()

Returns validateAuthority boolean value passed as a constructor parameter.

Constructor Details

AuthenticationContext

public AuthenticationContext(final String authority, final boolean validateAuthority, final ExecutorService service)

Constructor to create the context with the address of the authority.

Parameters:

authority - URL of the authenticating authority
validateAuthority - flag to enable/disable authority validation.
service - ExecutorService to be used to execute the requests. Developer is responsible for maintaining the lifetime of the ExecutorService.

Throws:

MalformedURLException - thrown if URL is invalid

Method Details

acquireDeviceCode

public Future acquireDeviceCode(final String clientId, final String resource, final AuthenticationCallback callback)

Acquires a device code from the authority

Parameters:

clientId - Identifier of the client requesting the token
resource - Identifier of the target resource that is the recipient of the requested token.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the DeviceCode of the call. It contains device code, user code, its expiration date, message which should be displayed to the user.

Throws:

AuthenticationException - thrown if the device code is not acquired successfully

acquireToken

public Future acquireToken(final String resource, final AsymmetricKeyCredential credential, final AuthenticationCallback callback)

Acquires security token from the authority.

Parameters:

resource - Identifier of the target resource that is the recipient of the requested token.
credential - object representing Private Key to use for token acquisition.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token and the Access Token's expiration time. Refresh Token property will be null for this overload.

Throws:

AuthenticationException -

AuthenticationException

acquireToken

public Future acquireToken(final String resource, final ClientAssertion clientAssertion, final AuthenticationCallback callback)

Acquires security token from the authority.

Parameters:

resource - Identifier of the target resource that is the recipient of the requested token.
clientAssertion - The client assertion to use for client authentication.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token and the Access Token's expiration time. Refresh Token property will be null for this overload.

acquireToken

public Future acquireToken(final String resource, final ClientCredential credential, final AuthenticationCallback callback)

Acquires security token from the authority.

Parameters:

resource - Identifier of the target resource that is the recipient of the requested token.
credential - The client credential to use for token acquisition.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token and the Access Token's expiration time. Refresh Token property will be null for this overload.

acquireToken

public Future acquireToken(final String resource, final String clientId, final String username, final String password, final AuthenticationCallback callback)

Acquires a security token from the authority using a username/password flow.

Parameters:

resource - Identifier of the target resource that is the recipient of the requested token.
clientId - Name or ID of the client requesting the token.
username - Username of the managed or federated user.
password - Password of the managed or federated user. If null, integrated authentication will be used.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

acquireToken

public Future acquireToken(final String resource, final UserAssertion userAssertion, final AsymmetricKeyCredential credential, final AuthenticationCallback callback)

Acquires an access token from the authority on behalf of a user. It requires using a user token previously received. Uses certificate to authenticate client.

Parameters:

resource - Identifier of the target resource that is the recipient of the requested token.
userAssertion - userAssertion to use as Authorization grant
credential - The certificate based client credential to use for token acquisition.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token and the Access Token's expiration time. Refresh Token property will be null for this overload.

Throws:

AuthenticationException -

AuthenticationException

acquireToken

public Future acquireToken(final String resource, final UserAssertion userAssertion, final ClientCredential credential, final AuthenticationCallback callback)

Acquires an access token from the authority on behalf of a user. It requires using a user token previously received.

Parameters:

resource - Identifier of the target resource that is the recipient of the requested token.
userAssertion - userAssertion to use as Authorization grant
credential - The client credential to use for token acquisition.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token and the Access Token's expiration time. Refresh Token property will be null for this overload.

Throws:

AuthenticationException -

AuthenticationException

acquireTokenByAuthorizationCode

public Future acquireTokenByAuthorizationCode(final String authorizationCode, final String resource, final String clientId, final URI redirectUri, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Parameters:

authorizationCode - The authorization code received from service authorization
resource - Identifier of the target resource that is the recipient of the requested token.
clientId - The client assertion to use for token acquisition endpoint.
redirectUri - The redirect address used for obtaining authorization code.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

acquireTokenByAuthorizationCode

public Future acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final AsymmetricKeyCredential credential, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Parameters:

authorizationCode - The authorization code received from service authorization endpoint.
redirectUri - The redirect address used for obtaining authorization code.
credential - object representing Private Key to use for token acquisition.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

Throws:

AuthenticationException - thrown if AsymmetricKeyCredential fails to sign the JWT token.

acquireTokenByAuthorizationCode

public Future acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final AsymmetricKeyCredential credential, final String resource, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Parameters:

authorizationCode - The authorization code received from service authorization endpoint.
redirectUri - The redirect address used for obtaining authorization code.
credential - object representing Private Key to use for token acquisition.
resource - Identifier of the target resource that is the recipient of the requested token. It can be null if provided earlier to acquire authorizationCode.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

Throws:

AuthenticationException - thrown if AsymmetricKeyCredential fails to sign the JWT token.

acquireTokenByAuthorizationCode

public Future acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final ClientAssertion clientAssertion, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Parameters:

authorizationCode - The authorization code received from service authorization endpoint.
redirectUri - The redirect address used for obtaining authorization code.
clientAssertion - The client assertion to use for client authentication.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

acquireTokenByAuthorizationCode

public Future acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final ClientAssertion clientAssertion, final String resource, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Parameters:

authorizationCode - The authorization code received from service authorization endpoint.
redirectUri - The redirect address used for obtaining authorization code.
clientAssertion - The client assertion to use for client authentication.
resource - Identifier of the target resource that is the recipient of the requested token. It can be null if provided earlier to acquire authorizationCode.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

acquireTokenByAuthorizationCode

public Future acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final ClientCredential credential, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Parameters:

authorizationCode - The authorization code received from service authorization endpoint.
redirectUri - The redirect address used for obtaining authorization code.
credential - The client credential to use for token acquisition.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

acquireTokenByAuthorizationCode

public Future acquireTokenByAuthorizationCode(final String authorizationCode, final URI redirectUri, final ClientCredential credential, final String resource, final AuthenticationCallback callback)

Acquires security token from the authority using an authorization code previously received.

Parameters:

authorizationCode - The authorization code received from service authorization endpoint.
redirectUri - The redirect address used for obtaining authorization code.
credential - The client credential to use for token acquisition.
resource - Identifier of the target resource that is the recipient of the requested token. It can be null if provided earlier to acquire authorizationCode.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

acquireTokenByDeviceCode

public Future acquireTokenByDeviceCode(final DeviceCode deviceCode, final AuthenticationCallback callback)

Acquires security token from the authority using an device code previously received.

Parameters:

deviceCode - The device code result received from calling acquireDeviceCode.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains AccessToken, Refresh Token and the Access Token's expiration time.

Throws:

AuthenticationException - thrown if authorization is pending or another error occurred. If the errorCode of the exception is AdalErrorCode.AUTHORIZATION_PENDING, the call needs to be retried until the AccessToken is returned. DeviceCode.interval - The minimum amount of time in seconds that the client SHOULD wait between polling requests to the token endpoin

acquireTokenByRefreshToken

public Future acquireTokenByRefreshToken(final String refreshToken, final AsymmetricKeyCredential credential, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Parameters:

refreshToken - Refresh Token to use in the refresh flow.
credential - object representing Private Key to use for token acquisition.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

Throws:

AuthenticationException - thrown if AsymmetricKeyCredential fails to sign the JWT token.

acquireTokenByRefreshToken

public Future acquireTokenByRefreshToken(final String refreshToken, final AsymmetricKeyCredential credential, final String resource, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Parameters:

refreshToken - Refresh Token to use in the refresh flow.
credential - object representing Private Key to use for token acquisition.
resource - Identifier of the target resource that is the recipient of the requested token. If null, token is requested for the same resource refresh token was originally issued for. If passed, resource should match the original resource used to acquire refresh token unless token service supports refresh token for multiple resources.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

Throws:

AuthenticationException - thrown if AsymmetricKeyCredential fails to sign the JWT token.

acquireTokenByRefreshToken

public Future acquireTokenByRefreshToken(final String refreshToken, final ClientCredential credential, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Parameters:

refreshToken - Refresh Token to use in the refresh flow.
credential - The client credential used for token acquisition.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

acquireTokenByRefreshToken

public Future acquireTokenByRefreshToken(final String refreshToken, final ClientCredential credential, final String resource, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Parameters:

refreshToken - Refresh Token to use in the refresh flow.
credential - The client credential used for token acquisition.
resource - Identifier of the target resource that is the recipient of the requested token. If null, token is requested for the same resource refresh token was originally issued for. If passed, resource should match the original resource used to acquire refresh token unless token service supports refresh token for multiple resources.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

acquireTokenByRefreshToken

public Future acquireTokenByRefreshToken(final String refreshToken, final String clientId, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received. This method is suitable for the daemon OAuth2 flow when a client secret is not possible.

Parameters:

refreshToken - Refresh Token to use in the refresh flow.
clientId - Name or ID of the client requesting the token.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

Throws:

AuthenticationException - thrown if the access token is not refreshed successfully

acquireTokenByRefreshToken

public Future acquireTokenByRefreshToken(final String refreshToken, final String clientId, final ClientAssertion clientAssertion, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Parameters:

refreshToken - Refresh Token to use in the refresh flow.
clientId - Name or ID of the client requesting the token.
clientAssertion - The client assertion to use for client authentication.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

acquireTokenByRefreshToken

public Future acquireTokenByRefreshToken(final String refreshToken, final String clientId, final ClientAssertion clientAssertion, final String resource, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received.

Parameters:

refreshToken - Refresh Token to use in the refresh flow.
clientId - Name or ID of the client requesting the token.
clientAssertion - The client assertion to use for client authentication.
resource - Identifier of the target resource that is the recipient of the requested token. If null, token is requested for the same resource refresh token was originally issued for. If passed, resource should match the original resource used to acquire refresh token unless token service supports refresh token for multiple resources.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

acquireTokenByRefreshToken

public Future acquireTokenByRefreshToken(final String refreshToken, final String clientId, final String resource, final AuthenticationCallback callback)

Acquires a security token from the authority using a Refresh Token previously received. This method is suitable for the daemon OAuth2 flow when a client secret is not possible.

Parameters:

refreshToken - Refresh Token to use in the refresh flow.
clientId - Name or ID of the client requesting the token.
resource - Identifier of the target resource that is the recipient of the requested token. If null, token is requested for the same resource refresh token was originally issued for. If passed, resource should match the original resource used to acquire refresh token unless token service supports refresh token for multiple resources.
callback - optional callback object for non-blocking execution.

Returns:

A Future object representing the AuthenticationResult of the call. It contains Access Token, Refresh Token and the Access Token's expiration time.

Throws:

AuthenticationException - thrown if the access token is not refreshed successfully

getAuthority

public String getAuthority()

Authority associated with the context instance

Returns:

String value

getCorrelationId

public String getCorrelationId()

Returns the correlation id configured by the user. It does not return the id automatically generated by the API in case the user does not provide one.

Returns:

String value of the correlation id

getProxy

public Proxy getProxy()

Returns Proxy configuration

Returns:

Proxy Object

getSslSocketFactory

public SSLSocketFactory getSslSocketFactory()

Returns SSLSocketFactory configuration object.

Returns:

SSLSocketFactory object

isLogPii

public boolean isLogPii()

Returns logPii - boolean value, which determines whether Pii (personally identifiable information) will be logged in

Returns:

boolean value of logPii

setCorrelationId

public void setCorrelationId(final String correlationId)

Set optional correlation id to be used by the API. If not provided, the API generates a random id.

Parameters:

correlationId - String value

setLogPii

public void setLogPii(boolean logPii)

Set logPii - boolean value, which determines whether Pii (personally identifiable information) will be logged in

Parameters:

logPii - boolean value

setProxy

public void setProxy(Proxy proxy)

Sets Proxy configuration to be used by the context for all network communication. Default is null and system defined properties if any, would be used.

Parameters:

proxy - Proxy configuration object

setSslSocketFactory

public void setSslSocketFactory(SSLSocketFactory sslSocketFactory)

Sets SSLSocketFactory object to be used by the context.

Parameters:

sslSocketFactory - The SSL factory object to set

shouldValidateAuthority

public boolean shouldValidateAuthority()

Returns validateAuthority boolean value passed as a constructor parameter.

Returns:

boolean value

Applies to