Skip to main content

Context manager

The Context Manager simplifies the usage of complex SDK functions by reducing the number of parameters required when setting the operation context, authentication tokens, and other session data.

info

All the values that are provided to the context manager are automatically stored in secure storage.

Set API environment​

Changes the environment the SDK operates against. It can be changed at any time, although it is recommended to set it up through the SdkConfig builder instead.

sdk.contextManager().setApiEnvironment(ApiEnvironment.PROD)

Get API environment​

Returns the environment the SDK is currently operating against. Defaults to PROD when it has never been set.

// Returns an ApiEnvironment
val apiEnvironment = sdk.contextManager().getApiEnvironment()

Set cloud auth token​

If the SDK was initialized without an authentication token, you can provide or update the token using this function, also it is set automatically by login, register, refresh-token and the automatic refresh. Passing null removes the stored token.

sdk.contextManager().setCloudAuthToken("AUTH_TOKEN")

Get cloud auth token​

val token = sdk.contextManager().getCloudAuthToken()

Is cloud auth token invalid or expired​

Checks whether the cloud auth token from the context can still be used:

  • A token that is missing, invalid or expires within the next 24 hours is first refreshed with the stored refresh token. This is a network request, performed regardless of checkServerInvalidation.
  • It returns true only when the token could not be refreshed and is missing, invalid or past its expiration. A token that could not be refreshed but has not expired yet is still valid, since the backend accepts it until then.
  • When checkServerInvalidation is true, a token that passes the checks above is also verified with the backend (an additional network request). When false, the token might be accepted even if it has been invalidated on the server.
// Returns a Boolean
val result = sdk.contextManager().isCloudAuthTokenInvalidOrExpired(true)

Set cloud refresh token​

It is set automatically by login, register, and refresh-tokens. Passing null removes the stored token.

info

While a refresh token is stored, the SDK refreshes the cloud auth token automatically: before a request, when the auth token is missing, invalid or expires within the next 24 hours, and once more after an unauthorized response. A refresh token rejected by the backend is removed from the context.

sdk.contextManager().setCloudRefreshToken("REFRESH_TOKEN")

Get cloud refresh token​

val token = sdk.contextManager().getCloudRefreshToken()

Set fusion host​

Sets the host of the on-premise Fusion server used by the fusion resource. It can be changed at any time, although it is recommended to set it up through the SdkConfig builder instead.

sdk.contextManager().setFusionHost(URI("http://localhost:27700"))

Get fusion host​

Returns the currently configured Fusion host. Defaults to http://localhost:27700 when it has never been set.

// Returns a URI
val fusionHost = sdk.contextManager().getFusionHost()

Set fusion auth token​

It is set automatically by fusion login.

sdk.contextManager().setFusionAuthToken("FUSION_AUTH_TOKEN")

Get fusion auth token​

val token = sdk.contextManager().getFusionAuthToken()

Set user ID​

Sets the user identifier used by the secure lock operations. It is usually set for you by set operation context, register ephemeral key and verify ephemeral key.

sdk.contextManager().setUserId(USER_ID)

Get user ID​

// Returns a UUID
val userId = sdk.contextManager().getUserId()

Set user email​

Sets the email address associated with the context. It is set automatically by login and registration.

sdk.contextManager().setUserEmail("EMAIL")

Get user email​

val email = sdk.contextManager().getUserEmail()

Set certificate chain​

Sets the certificate chain used to sign the secure lock operations. It is usually set for you by set operation context, register ephemeral key and verify ephemeral key.

info

Only the first certificate of the chain is inspected by is certificate chain invalid or expired.

sdk.contextManager().setCertificateChain(USER_CERTIFICATE_CHAIN_LIST)

Get certificate chain​

Returns the stored certificate chain, or null when none has been set.

// Returns a List<X509Certificate>
val certificateChain = sdk.contextManager().getCertificateChain()

Is certificate chain invalid or expired​

Checks if the current certificate chain from the context is invalid or expired (we consider it expired if it will expire within the next 7 days).

val result = sdk.contextManager().isCertificateChainInvalidOrExpired()

Set key pair​

Sets the key pair used to sign the secure lock operations. It is usually set for you by set operation context, register ephemeral key and verify ephemeral key.

info

Setting a new key pair does not mark it as verified. Use set key pair verified, or register it through register ephemeral key plus and verify ephemeral key.

sdk.contextManager().setKeyPair(KEY_PAIR)

Get key pair​

Returns the stored key pair, or null when either the public or the private key is missing.

// Returns a KeyPair
val keyPair = sdk.contextManager().getKeyPair()

Set key pair verified​

Marks a public key as verified, meaning it has successfully completed two-factor verification. Pass null to clear the verification.

info

Is key pair verified compares the value stored here against the current public key, so setting a key pair that does not match clears the verified state in practice.

sdk.contextManager().setKeyPairVerified(PUBLIC_KEY)

Is key pair verified​

Checks if the current key pair from the context has been verified.

val result = sdk.contextManager().isKeyPairVerified()

Is key pair valid​

Checks if the current key pair from the context is valid.

val result = sdk.contextManager().isKeyPairValid()

Set operation context​

sdk.contextManager().setOperationContext(
userId = USER_ID,
certificateChain = USER_CERTIFICATE_CHAIN_LIST,
keyPair = KEY_PAIR,
isKeyPairVerified = IS_KEY_PAIR_VERIFIED
)

Get Context State​

Checks the state of the context by verifying that the auth token is valid, the key pair is valid and verified, and the certificate chain is valid. The first check to fail determines the returned state. checkServerInvalidation behaves as described in is cloud auth token invalid or expired.

// Returns a ContextState
val result = sdk.contextManager().getContextState(true)

Clear context​

This function removes all stored context fields from the secure storage.

sdk.contextManager().clearContext()