Using the iOS SDK to Consume the Authentication API

Using the iOS SDK to Consume the Authentication API

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 the Curity Identity Server'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 article.

It's All About Security

The browser plays a vital role in OAuth and OIDC flows — it makes them more secure. Browser redirects ensure that the Authorization Server delivers data to the proper client, and not to an impersonator. Thus, removing the browser from OAuth and OIDC 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.

Two security features of the Curity Identity Server ensure that the Authentication API process is just as secure as browser flows:

  • client attestation
  • 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 the scope of this article, but what is important to note, is that client attestation asserts a few things about the calling client application:

  1. The app was correctly signed with the credentials configured in the Curity Identity Server for the associated OAuth client. This means that the app has not been tampered with. For example, it hasn't been decompiled, had its code changed, and recompiled into a malicious client. When using these features, 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 securely perform the verification described above.

Demonstration of Proof-of-Possession

Proof-of-Possession tokens are another way to use access tokens. The most common way used in APIs is using Bearer tokens. Bearer tokens work a bit like cash — any client who possesses 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 the access token is stolen, another party can't use it.

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 SDKs to help you leverage the Hypermedia API: the driver SDK and model SDK. The driver SDK is responsible for handling the low-level security features of the API. The model SDK introduces classes representing possible responses from the authentication API. Normally you will work with the model SDK, which internally uses the driver SDK to handle communication.

Prerequisites for using the API

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

  1. Use at least version 6.2.2. of the Curity Identity Server.

  2. You will require an iOS attestation policy. To add one, follow the instructions below:

    • Open the Facilities menu in the top right corner of the admin UI and scroll down to Client Attestations.
    • Click on the New button next to iOS policies.

new attestation

  • Enter a suitable name for the policy and click Create.

new attestation name

  • Choose the appropriate mode. production mode tells the Curity Identity Server to validate the app against its release signing settings. Choosing non-production will validate the app against the debug signing settings. In some rare cases, it might also be necessary to overwrite Apple's Certificate Chain, but normally you shouldn't need to use this setting. Close the dialog once you choose all the settings.

ios attestation configuration

  1. You will also need a client with the API capability which uses the iOS attestation policy.

    • Go to Profiles -> Token Service -> Clients, and edit the client of your choice.
    • In the Capabilities section, make sure your client has at least these two enabled: Hypermedia Authentication API and Code Flow. Use any URL for the redirect URL (e.g. https://localhost) and choose no-authentication as the Client authentication method.
    • Scroll down to the Client Application Settings tab and open the Advanced section. Enable the Attestation, and select ios as attestation type. Enter your App ID following the instructions available next to the input field and choose the Attestation Policy that you've previously created.
    • If you want to test or develop the app using a simulated device turn on the Disable Attestation Validation option. When this is on, the client and server will still perform the Attestation, but the server will not validate the attestation. Attestation functionality does not work on simulated iOS devices, so you have to enable this option when using a simulator. Remember not to use this setting for production clients!

client attestation settings

Remember to commit all the changes made to the configuration.

Note that you can also use the configuration CLI or RESTCONF API to set these settings.

Using the model SDK in an iOS app

You can obtain the SDK from the Curity Github. You can add it to your project either by using the Swift Package Manager or through CocoaPods.

You can add the following to your Package.swift file:

    .package(url: "https://github.com/curityio/ios-idsvr-haapi-sdk-dist")

If you're not using the Package.swift file, and you prefer to add the dependencies directly in Xcode, follow these steps:

  • In Xcode choose File -> Add Packages... from the menu.
  • Enter https://github.com/curityio/ios-idsvr-haapi-sdk-dist to the search field.
  • Choose the desired version policy (you can leave the default settings).
  • Click Add Package to add the dependency.

Add the following code to your Podfile. Remember to change the <Your Target Name> to the name of the target used in your project.

platform :ios, '14.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'IdsvrHaapiSdk'
end

Install the dependency by running pod install.

The SDK exposes two classes: HaapiTokenManager and OAuthTokenManager. The managers accept a configuration object and are responsible for communication with the Curity Identity Server. The HaapiManager exposes the following methods:

  • start
  • followLink
  • submitForm
  • close

The first three are used to call subsequent steps of the authentication flow. Each method returns a HaapiResult, through a completion handler. The HaapiResult encloses a HaapiRepresentation, a ProblemRepresentation or an Error. HaapiRepresentation is a sealed interface, implementations of which represent concrete response objects from the authentication API.

The app should call the API until it eventually gets an OAuthAuthorizationResponseStep, which signals that the authentication has finished, and the app can now obtain access, refresh and ID tokens from the Curity Identity Server. This can be easily done using the OAuthTokenManager object's fetchAccessToken method.

The OAuthTokenManager also exposes a refreshAccessToken method for refreshing access tokens.

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


import IdsvrHaapiSdk
...

let haapiConfiguration = HaapiConfiguration(name: name,
    clientId: "ios-client",
    baseURL: URL(string: "https://idsvr.example.com")!,
    tokenEndpointURL: URL(string: "https://idsvr.example.com/oauth/v2/oauth-token")!,
    authorizationEndpointURL: URL(string: "https://idsvr.example.com/oauth/v2/oauth-authorize")!,
    appRedirect: "app://redirect",
    isAutoRedirect: true,
    urlSession: URLSessionConfiguration.default)
    }

let haapiManager = HaapiManager(haapiConfiguration: haapiConfiguration)
let tokenManager = OAuthTokenManager(oauthTokenConfiguration: haapiConfiguration)

haapiManager.start(OAuthAuthorizationParameters(scopes: ["openid", "profile"]),
                            completionHandler:
{ haapiResult in
    if (haapiResult is OAuthAuthorizationResponseStep) {
        tokenManager.fetchAccessToken(
            with: haapiResult.oauthAuthorizationResponseProperties.code,
            completionHandler: { oauthResponse in
                if (oauthResponse is SuccessfulTokenResponse) {
                    print(oauthResponse.accessToken)
                }
            }
    } else {
        print(haapiResult.type)
    }
})
 

Conclusion

With the SDKs, you can start using the Authentication API in minutes without delving into the specific details of the API's security enhancements. Thanks to the model 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.