Skip to main content

Token Fetch

Fetch Result

/**
* Potential status results from an attempt to fetch from the SDK
*/
public enum TokenFetchStatus {
SUCCESS, // fetch was completed successfully
NO_NETWORK, // fetch failed because there is no network connectivity currently
MITM_DETECTED, // fetch failed because there is a Man-In-The-Middle (MITM) to the Approov cloud service
POOR_NETWORK, // fetch failed due to poor network connectivity
NO_APPROOV_SERVICE, // fetch failed, perhaps because Approov services are down
BAD_URL, // provided URL was not https or otherwise in the correct format
UNKNOWN_URL, // provided URL is not one that one configured for Approov
UNPROTECTED_URL, // provided URL does not need an Approov token
NO_NETWORK_PERMISSION, // app does not have ACCESS_NETWORK_STATE or INTERNET permission
MISSING_LIB_DEPENDENCY, // app is missing a needed library dependency
INTERNAL_ERROR, // there has been an internal error in the SDK
REJECTED, // indicates a custom JWT or secure string fetch has been rejected because Approov attestation fails
DISABLED, // indicates that a custom JWT or secure string fetch fails because the feature is not enabled
UNKNOWN_KEY // indicates an attempt to fetch a secure string that has not been defined
}

/**
* Results from a fetch operation, including custom JWT and secure string fetches, as
* well as standard Approov token fetches.
*/
public class TokenFetchResult {
/**
* Gets the status of the last fetch operation.
*
* @return Approov fetch status
*/
public TokenFetchStatus getStatus();

/**
* Gets the token string of the last Approov token fetch. This may be an empty string
* if the fetch did not succeed. This value should not not be cached by the app client
* code.
*
* @return Approov token string
*/
public String getToken();

/**
* Gets an optional base64url encoded traceID for the Approov token fetch which may be used to
* assist with debugging.
*
* @return any traceID associated with the token fetch, or null if not available
*/
public String getTraceID();

/**
* Gets the secure string of the last secure string fetch. This may be null if the
* string is not available. This value should not not be cached by the app client
* code.
*
* @return any secure string or null if not available
*/
public String getSecureString();

/**
* Gets any Attestation Response Code (ARC) associated with the last fetch. This may be an empty string
* if the fetch did not succeed or if ARC is not enabled.
*
* @return ARC string
*/
public String getARC();

/**
* Gets any rejection reasons describing why Approov attestation has failed. This is
* a comma separated list of device properties, or an empty string for a pass or if the
* feature is not enabled.
*
* @return comma separated rejection reasons, or empty string otherwise
*/
public String getRejectionReasons();

/**
* Determines if a new configuration is available from fetchConfig().
*
* @return true if an updated configuration is available
*/
public boolean isConfigChanged();

/**
* Determines if current user APIs must be updated to reflect a new version
* available from getPins() or getPinsJSON(). Calling getPins() or getPinsJSON() will
* clear this flag for the next Approov fetch.
*
* @return true if pins should be updated
*/
public boolean isForceApplyPins();

/**
* Gets a measurement configuration if the last token fetch was to perform an
* integrity measurement and was successful.
*
* @return Approov measurement configuration or null if no measurement made
*/
public byte[] getMeasurementConfig();

/**
* Gets a loggable version of the result Approov token. This provides the decoded JSON payload
* along with the first six characters of the base64 encoded signature as an additional
* "sip" claim. This can be safely logged as it cannot be transformed into a valid token
* since the full signature is not provided, but it can be subsequently checked for validity
* if the shared secret is known, with a very high probability. The loggable token is always
* valid JSON. If there is an error then the type is given with the key "error". Note that
* this is not applicable to JWE tokens.
*
* @return Loggable Approov token string
*/
public String getLoggableToken();
}

Synchronous Token Fetch

/**
* Initiates a synchronous request to obtain an Approov token and other results. If an Approov token fetch
* has been completed previously and the tokens are unexpired then this may return the same one
* without a need to perform a network transaction. Note though that the caller should never cache the
* Approov token as it may become invalidated at any point.
*
* If a new Approov token is required then a more extensive app measurement is performed that involves
* communicating with the Approov cloud service. Thus this method may take up to several seconds to
* return and should not be called from a UI thread. There is also a chance that due to poor network
* connectivity or other factors an Approov token cannot be obtained, and this is reflected in the
* returned status.
*
* All calls must provide a URL which provides the endpoint of the API to which the Approov token is going
* to be sent. Different host domains will have different Approov tokens associated with
* them so it is important that the Approov token is only sent to requests for that domain. If the
* domain has not been configured in the account then an error is obtained. Note that the provided
* URL may be suffixed by "?measurement" to initiate a measurement process for use with integrity and
* device proofs.
*
* @param url provides the URL for which a token is being fetched
* @return results of fetching a token
*/
public static final TokenFetchResult fetchApproovTokenAndWait(String url);

Asynchronous Token Fetch

/**
* Interface that must be implemented to receive token fetch callbacks for the
* fetchApproovToken() method.
*/
public interface TokenFetchCallback {
/**
* Callback function to be implemented in the business logic.
*
* @param result is the TokenFetchResult
*/
void approovCallback(TokenFetchResult result);
}

/**
* Initiates an asynchronous request to obtain an Approov token and other results. The call returns
* immediately and when the token is fetched (or if the request times out due to an error) then
* a callback method is called on the supplied interface instance object. This callback is made
* on a different thread. If an Approov token fetch has been completed previously and the tokens
* are unexpired then this callback may be very rapid without a need to perform a network transaction.
* Note though that the caller should never cache the Approov token as it may become invalidated at
* any point.
*
* If a new Approov token is required then a more extensive app measurement is performed that involves
* communicating with the Approov cloud service.There is a chance that due to poor network
* connectivity or other factors an Approov token cannot be obtained, and this is reflected in the
* returned status.
*
* All calls must provide a URL which provides the endpoint of the API to which the Approov
* token is going to be sent. Different host domains will have different Approov tokens associated with
* them so it is important that the Approov token is only sent to requests for that domain. If the
* domain has not been configured in the account then an error is obtained. Note that the provided
* URL may be suffixed by "?measurement" to initiate a measurement process for use with integrity and
* device proofs.
*
* @param callback is an instance that implements TokenFetchCallback whose callback
* @param url provides the URL for which a token is being fetched
*/
public static final void fetchApproovToken(TokenFetchCallback callback, String url);

Synchronous Secure String Fetch

/**
* Initiates a synchronous request to obtain, or update, a string held securely by the SDK. The
* call only returns when the secure string has been obtained (or if the request times out due to
* an error, or if the request is rejected). This provides the secure string value. It is also
* possible to update the secure string value by providing a new definition. In this case the
* new value is returned as the secure string. Use of an empty string for newDef removes the
* string entry. Note that after an initial fetch secure strings will be available for some period
* until they expire, with no latency for a network operation. If the string is not defined then
* the UNKNOWN_KEY error status is returned. If the attestation process fails then the error
* status REJECTED is returned. Finally, if the feature is not enabled then the DISABLED status
* is returned.
*
* @param key is the name of the key (max 64 characters) that the secure string is held against
* @param newDef is any new definition for the secure string, or null otherwise
* @return results of fetching the secure string
* @throws IllegalArgumentException if the key is empty or too long
*/
public static final TokenFetchResult fetchSecureStringAndWait(String key, String newDef);

Asynchronous Secure String Fetch

/**
* Interface that must be implemented to receive token fetch callbacks for the
* fetchSecureString() method.
*/
public interface TokenFetchCallback {
/**
* Callback function to be implemented in the business logic.
*
* @param result is the TokenFetchResult
*/
void approovCallback(TokenFetchResult result);
}

/**
* Initiates an asynchronous request to obtain, or update, a string held securely by the SDK. The call
* returns immediately. After the secure string is fetched (or if the request times out due to an
* error, or if the request is rejected), then a callback method is called on the supplied interface
* instance object. This callback is made on a different thread. This provides the secure string value.
* It is also possible to update the secure string value by providing a new definition. In this case the
* new value is returned as the secure string. Use of an empty string for newDef removes the string entry.
* Note that after an initial fetch secure strings will be available for some period until they expire, with
* no latency for a network operation. If the string is not defined then the UNKNOWN_KEY error status is
* returned. If the attestation process fails then the error status REJECTED is returned. Finally, if the
* feature is not enabled then the DISABLED status is returned.
*
* @param callback is an instance that implements TokenFetchCallback
* @param key is the name of the key (max 64 characters) that the secure string is held against
* @param newDef is any new definition for the secure string, or null otherwise
* @throws IllegalArgumentException if the key is empty or too long
*/
public static final void fetchSecureString(TokenFetchCallback callback, String key, String newDef);

Synchronous Custom JWT Fetch


/**
* Initiates a synchronous request to obtain a custom JWT with the given payload. The call only
* returns when the custom JWT has been obtained (or if the request times out due to an error, or
* if the request is rejected). The payload must be valid JSON. If the attestation process fails
* then the error status REJECTED is returned. Finally, if the feature is not enabled then the
* DISABLED status is returned.
*
* @param payload provide marshaled JSON to be included in the custom JWT to be fetched
* @return results of fetching a token
* @throws IllegalArgumentException if the payload is not valid JSON
*/
public static final TokenFetchResult fetchCustomJWTAndWait(String payload);

Asynchronous Custom JWT Fetch


/**
* Interface that must be implemented to receive token fetch callbacks for the
* fetchCustomJWT() method.
*/
public interface TokenFetchCallback {
/**
* Callback function to be implemented in the business logic.
*
* @param result is the TokenFetchResult
*/
void approovCallback(TokenFetchResult result);
}

/**
* Initiates an asynchronous request to obtain a custom JWT with the given payload. The call returns
* immediately. After the token is fetched (or if the request times out due to an error, or if the
* request is rejected) then a callback method is called on the supplied interface instance object.
* This callback is made on a different thread. The payload must be valid JSON. If the attestation
* process fails then the error status REJECTED is returned. Finally, if the feature is not enabled
* then the DISABLED status is returned.
*
* @param callbackHandler is a function that takes an ApproovTokenFetchResult when the token is obtained
* @param payload provide marshaled JSON to be included in the custom JWT to be fetched
* @throws IllegalArgumentException if the payload is not valid JSON
*/
public static final void fetchCustomJWT(TokenFetchCallback callback, String payload);