Skip to content

Latest commit

 

History

History
99 lines (69 loc) · 6.63 KB

API-PROTECTION.md

File metadata and controls

99 lines (69 loc) · 6.63 KB

API Protection

You should use this option if you wish to protect access to your APIs using an Approov token. We recommend this approach where it is possible to modify the backend API implementation to perform the token verification. Various Backend API Quickstarts are available to suit your particular situation depending on the backend technology used. You will need to implement this in addition to the steps in this frontend guide.

These steps require access to the Approov CLI, please follow the Installation instructions.

ADDING API DOMAINS

In order for Approov tokens to be added by the interceptor for particular API domains it is necessary to inform Approov about them. Execute the following command:

approov api -add your.domain

Approov tokens will then be added automatically to any requests to that domain (using the Approov-Token header, by default).

Note that this will use Managed Trust Roots to ensure that no Man-in-the-Middle attacks on your app's communication are possible.

NOTE: By default a symmetric account key is used to sign the Approov token (HS256 algorithm), so that all API domains will share the same signing secret. Alternatively, it is possible to use a keyset key which may differ for each API domain and for which a wide range of different signing algorithms and key types are available. This requires you to first add a new key, and then specify it when adding each API domain. Note that this will impact how you verify the token on your API backend.

ADD YOUR SIGNING CERTIFICATE TO APPROOV

In order for Approov to recognize the app as being valid, the local certificate used to sign the app needs to be added to Approov. The following assumes it is in PKCS12 format:

approov appsigncert -add ~/.android/debug.keystore -storePassword android -autoReg

This ensures that any app signed with the certificate used on your development machine will be recognized by Approov.

See Android App Signing Certificates if your keystore format is not recognized or if you have any issues adding the certificate. This also provides information about adding certificates for when releasing to the Play Store. Note also that you need to apply specific Android Obfuscation rules when creating an app release.

FURTHER OPTIONS

See Exploring Other Approov Features for information about additional Approov features you may wish to try.

Development Key

You may wish to set a development key in order to force an app to be passed, if it may be resigned by a different app signing certificate to which you don't have access. Perform the call:

ApproovService.setDevKey("uDW9FuLVpL1_4zo1");

See using a development key to understand how to obtain the development key which is the parameter to the call.

Changing Approov Token Header Name

The default header name of Approov-Token can be changed as follows:

ApproovService.SetTokenHeaderAndPrefix("Authorization", "Bearer ");

The first parameter is the new header name and the second a prefix to be added to the Approov token. This is primarily for integrations where the Approov Token JWT might need to be prefixed with Bearer and passed in the Authorization header.

Token Binding

If want to use Token Binding then set the header holding the value to be used for binding as follows:

ApproovService.SetBindingHeader("Authorization");

In this case it means that the value of Authorization holds the token value to be bound. This only needs to be called once. On subsequent requests the value of the specified header is read and its value set as the token binding value. Note that you should select a header whose value does not typically change from request to request, as each change requires a new Approov token to be fetched.

Prefetching

If you wish to reduce the latency associated with fetching the first Approov token, then make this call immediately after initializing ApproovService:

ApproovService.Prefetch();

This initiates the process of fetching an Approov token as a background task, so that a cached token is available immediately when subsequently needed, or at least the fetch time is reduced. Note that there is no point in performing a prefetch if you are using token binding.

Prechecking

You may wish to do an early check in your app to present a warning to the user if it is not going to be able to obtain valid Approov tokens because it fails the attestation process. Here is an example of calling the appropriate method in ApproovService:

using Approov;

...

try {
    ApproovService.Precheck();
}
catch(RejectionException e) {
    // failure due to the attestation being rejected
}
catch(NetworkingErrorException e) {
    // failure due to a potentially temporary networking issue, allow for a user initiated retry
}
catch(PermanentException e) {
   // a more permanent error, see e.getMessage()
}
// app has passed the precheck

Note you should NEVER use this as the only form of protection in your app, this is simply to provide an early indication of failure to your users as a convenience. You must always also have APIs protected with Approov tokens that are essential to the operation of your app. This is because, although the Approov attestation itself is heavily secured, it may be possible for an attacker to bypass its result or prevent it being called at all.

If you wish to provide more direct feedback with the Rejection Reasons feature use:

approov policy -setRejectionReasons on

Note that this command requires an admin role.