Using the Android SDK to Consume the Authentication API

Using the Android SDK to Consume the Authentication API

tutorials

What Is the Hypermedia Authentication API?

The Hypermedia Authentication API is a feature of the Curity Identity Server. It enables clients to leverage OAuth and OpenID Connect and Curity’s powerful authentication engine straight from client applications — without the need of a browser. If you want to learn more about the Hypermedia Authentication API, check out the overview here.

It’s All About Security

The browser typically plays a vital role in OAuth and OpenID flows — it makes them more secure. Browser redirects ensure the Authorization Server delivers data to the proper client, and not to an impersonator. Thus, removing the browser from OAuth and OpenID flows may at first seem a bit reckless. As we now use an API to fulfill a vulnerable authentication and authorization flow, we require another method to make sure that the client calling the API is indeed allowed to do so.

We’ve added two security features to the Curity Identity Server to ensure the Authentication API process is just as secure as browser flows. These new security features are:

  • Client attestation.
  • Usage of proof of possession tokens.

Client Attestation

Client attestation is done by verifying the running application’s package ID and the signing credentials. This is processed by the Android Operation System or iOS and bound to a cryptographic key pair. After a positive attestation, the client (the mobile app) is issued a Client Attestation Token (CAT) in the form of a JWT. The concrete implementation of the attestation is out of scope for this article, but what is important to note here is that client attestation asserts a few things about the client that calls the Authentication API:

  1. The app was correctly signed with the credentials configured in the Curity Identity Server for the associated OAuth client. This means the app has not been tampered with. For example, it hasn’t been decompiled, had its code changed, and recompiled into a malicious client. What is essential when using these features is that decompilation cannot provide any information that could help create an impersonated client.
  2. The app runs in a safe environment, that is, on a system that has not been rooted. Otherwise, it wouldn’t be possible to perform the verification described above securely.

Demonstration of Proof-of-Possession

Proof-of-Possession tokens are another way to use access tokens. The most common ones used in APIs are Bearer tokens. Bearer tokens work a bit like cash — any client who posses a Bearer access token can successfully call an API. Proof-of-Possession tokens behave more like credit cards — only legitimate owners of these tokens can use them to access the API. This further increases the security of the communication. Even if an access token is stolen, it can’t be used by another party.

The Hypermedia Authentication API uses a standard for Demonstration of Proof-of-Possession. By adopting this standard, access tokens used to access the Authentication API are bound to a private key that only the legitimate client possesses.

The SDKs for Hypermedia Authentication API

Obtaining CATs and using DPoP to secure requests to the API is a security-sensitive process and quite complicated to implement. The method of obtaining a CAT requires a few different steps, and the DPoP standard requires the client to generate proper DPoP headers for every request. To facilitate the usage of these crucial security components, Curity has released a simple SDK to help you leverage the Hypermedia API.

Prerequisites for using the API

To call the API from an Android app, make sure of the following:

  1. Use at least version 6.2.2. of the Curity Identity Server.
  2. You will require a client with the API capability to use the Android attestation policy. These settings can be set using the admin UI (described below in each tab), but you can also use the following XML file to merge it with your server’s configuration. Make sure to adjust values in the XML below to conform with your environment:

You can use this version of configuration to ease development and allow emulator usage. It has some relaxed requirements for validation. Remember that these options shouldn’t be used in production unless necessary to support special devices.

  • In line 16, change the Token Service’s name, if you don’t use the default.
  • In line 23, set the proper client name.
  • In line 26, set the package name your app uses.
  • In line 27, enter the digest of the credentials used to sign your app. (Read below to learn how to get the digest.)
<config xmlns="http://tail-f.com/ns/config/1.0">
    <facilities xmlns="https://curity.se/ns/conf/base">
        <client-attestation>
            <android-policy xmlns="https://curity.se/ns/conf/client-attestation">
                <id>emulator-policy</id>
                <verify-boot-state>false</verify-boot-state>
                <minimum-security-level>software</minimum-security-level>
                <override-certificate-chain-validation>
                    <do-not-validate-certificate-chain/>
                </override-certificate-chain-validation>
            </android-policy>
        </client-attestation>
    </facilities>
    <profiles xmlns="https://curity.se/ns/conf/base">
        <profile>
            <id>token-service</id>            <type xmlns:as="https://curity.se/ns/conf/profile/oauth">as:oauth-service</type>
            <settings>
                <authorization-server xmlns="https://curity.se/ns/conf/profile/oauth">
                    <client-store>
                        <config-backed>
                            <client>
                                <id>client-name</id>                                <attestation>
                                    <android>
                                        <package-name>com.example.yourpackage</package-name>                                        <signature-digest>A3...4=</signature-digest>                                        <android-policy>emulator-policy</android-policy>
                                    </android>
                                </attestation>
                            </client>
                        </config-backed>
                    </client-store>
                </authorization-server>
            </settings>
        </profile>
    </profiles>
</config>

The configuration above can also be set using the admin UI.

  1. Create an attestation policy under the Facilities menu (scroll all the way to the bottom).
  2. Disable Verify Boot State, set Minimum Security Level to software and disable Override Certificate Chain Validation
    Attestation Policy
  3. In the public client configured to be used with the API, enable the Hypermedia Authentication API capability. Enable HAAPI
  4. Then go to the Client Application Settings section and expand Advanced. Enable Attestation and configure according to the screenshot below. Here you can point to the attestation policy that was just created. Make sure to set the Package Name and Signature Digest accordingly. (Read below to learn how to get the digest.) Development Client Attestation

Use the following configuration for production environments. It uses default settings for the attestation policy.

  • In line 4, change the Token Service’s name, if you don’t use the default.
  • In line 11, set the proper client name.
  • In line 14, set the package name your app uses.
  • In line 15, enter the digest of the credentials used to sign your app. (Read below to learn how to get the digest.)
<config xmlns="http://tail-f.com/ns/config/1.0">
    <profiles xmlns="https://curity.se/ns/conf/base">
        <profile>
            <id>token-service</id>            <type xmlns:as="https://curity.se/ns/conf/profile/oauth">as:oauth-service</type>
            <settings>
                <authorization-server xmlns="https://curity.se/ns/conf/profile/oauth">
                    <client-store>
                        <config-backed>
                            <client>
                                <id>client-name</id>                                <attestation>
                                    <android>
                                        <package-name>com.example.yourpackage</package-name>                                        <signature-digest>A3...4=</signature-digest>                                    </android>
                                </attestation>
                            </client>
                        </config-backed>
                    </client-store>
                </authorization-server>
            </settings>
        </profile>
    </profiles>
</config>

The configuration above can also be set using the admin UI. This follows the same approach as described in the Development tab. Instead of making changes to the attestation policy leave it at default. Attestation Policy

The attestation configuration on the client looks the same as for Development but with one very important difference. Make sure that Disable Attestation Validation is not enabled.

Production Client Attestation

Remember, you can also use the configuration CLI or RESTCONF API to set these settings.

Obtaining a Signing Key Digest

You can use the gradle task signingReport to obtain hashes of your signing keys. If you use the admin UI, you can paste the generated SHA-256 there. If you to import configuration from XML or use CLI or RESTCONF API, you must first base64-encode the SHA-256 hash. Here’s how to do it from the terminal:

$ ./gradlew signingReport
...
SHA-256: 67:60:CA:11:93:...:4A:7E:A0:A4
...
$ echo "67:60:CA:11:93:...:4A:7E:A0:A4" | xxd -r -p | base64
Z2DKE...9oKQ=

You now have the Server and a client configured to use the authentication API from an Android app.

Using the SDK in an Android app

Add the SDK dependency to your project. The SDK is available on Maven Central.

dependencies {
    ...
    implementation 'se.curity.identityserver:identityserver.haapi.android.sdk:1.0.1'
}
<dependency>
  <groupId>se.curity.identityserver</groupId>
  <artifactId>identityserver.haapi.android.sdk</artifactId>
  <version>1.0.1</version>
  <scope>provided</scope>
</dependency>

The SDK is available for Kotlin and Java, depending on the chosen language of your project.

The SDK exposes an object HaapiTokenManager and a utility function for OkHttp HTTP client, which registers an interceptor. The token manager accepts some configuration options and is responsible for obtaining a Client Attestation token and creating proper DPoP headers. The interceptor is used to enrich requests with the necessary tokens and headers.

Below is an example of calling the API with the SDK. Note that no additional workaround Client Attestation or DPoP is needed. Thanks to the SDK, these are still used in the communication with the API.

val haapiTokenManager = HaapiTokenManager(
    URI("https://curity-instance.example.com/oauth/v2/oauth-token"),
    "client-name"
)

val httpClient = OkHttpClient.Builder()
    .addHaapiInterceptor(haapiTokenManager)
    .build()

val authorizeUrl = Uri.Builder()
        .scheme("https")
        .authority("curity-instance.example.com")
        .appendPath("oauth")
        .appendPath("v2")
        .appendPath("oauth-authorize")
        .appendQueryParameter("client_id", "client-name")
        .appendQueryParameter("scope", "openid")
        .appendQueryParameter("response_type", "code")
        .build()
        .toString()

val request = Request.Builder()
    .get()
    .url(authorizeUrl)
    .build()

val response = httpClient.newCall(request).execute()
val responseBody = haapiResponse.body?.string() ?: "{}"

val responseObject = JSONObject(responseBody)

println("Type of the response from API: ${responseObject["type"]}")
HaapiTokenManager haapiTokenManager = new HaapiTokenManager.Builder(new URI("https://curity-instance.example.com/oauth/v2/oauth-token"), "client-name").build();

OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();

OkHttpUtils.addHaapiInterceptor(httpClientBuilder, haapiTokenManager);
OkHttpClient httpClient = httpClientBuilder.build();

String authorizeUrl = new Uri.Builder()
    .scheme("https")
    .authority("curity-instance.example.com")
    .appendPath("oauth")
    .appendPath("v2")
    .appendPath("oauth-authorize")
    .appendQueryParameter("client_id", "client-name")
    .appendQueryParameter("scope", "openid")
    .appendQueryParameter("response_type", "code")
    .build()
    .toString();

Request request = new Request.Builder()
    .get()
    .url(authorizeUrl)
    .build();

Response response = httpClient.newCall(request).execute();

String responseBodyString;

try {
    ResponseBody responseBody = response.body();
    if (responseBody != null) {
        responseBodyString = responseBody.string();
    } else {
        responseBodyString = "{}";
    }
} catch (IOException e) {
    responseBodyString = "{}";
}

JSONObject responseObject = new JSONObject(responseBodyString);

System.out.println("Type of the response from API: " + responseObject.getString("type"));

Conclusion

With the SDK, you can start using the Authentication API in minutes without delving into the specific details of the API’s security enhancements. Thanks to this SDK, you can quickly focus on creating user interfaces.

To learn more about implementing clients that consume this API, have a look at the Authentication API documentation.

If you need help setting up the API or SDK, or have any other questions or comments, please do not hesitate to contact us.

Let’s Stay in Touch!

Get the latest on identity management, API Security and authentication straight to your inbox.

Keep up with our latest articles and how-tos using RSS feeds