Is the API-key enough? Common API security issues - and how to fix them.
Tags: api, security, authentication, oauth
Published: 2020-02-25 05:52:34


It can be seen over and over again: API-keys are directly embedded in the source code of an app. The most [recent incident](https://www.vice.com/en_us/article/3a8ajj/an-off-the-shelf-skeleton-project-experts-analyze-the-app-that-broke-iowa) as of February 2020 is the [Iowa caucus app](https://www.vice.com/en_us/article/3a8ajj/an-off-the-shelf-skeleton-project-experts-analyze-the-app-that-broke-iowa), that contained API-keys right inside the source code.

![API keys](https://api-university.com/wp-content/uploads/2020/02/api-key.png)

But it should be clear, that source code or the corresponding assembly/binary/bytecode is not secure storage for credentials, especially when rolled out to the end-users' device! Hardcoding API credentials into the source code is a no-go. There are at least two reasons:
 * Source code may be uploaded to a public repository (e.g. GitHub). By the time it is uploaded, the uploader may not be aware of the credentials being included in the sources.
 * Assembly/binary/bytecode can be easily disassembled/decoded and any API-keys can be easily extracted.

If source code and the resulting assembly/binary/bytecode are not a secure place for API credentials, how should API credentials be persisted instead? 
There are several aspects to the answer:
 * Static credentials such as API-keys should not be used in the first place. Instead, dynamic credentials should be used. This needs to be fixed by the API provider. See section "Static vs Dynamic Credentials".
 * Where credentials are stored. Not in assembly/binary/bytecode, but in secure storage. This needs to be properly managed by the API consumer. See the section "Credential Storage".

# Static vs Dynamic Credentials
API-keys are static credentials and thus can be easily transferred and used in unintended scenarios. They do not even adequately identify the app. And under no circumstances should access to user-data be made, based on an API-key.

This is a problem, that first of all needs to be addressed by the API provider. The API provider needs to offer APIs protected by dynamic credentials. Dynamic credentials, such as [OAuth](https://api-university.com/books/oauth-2-0-book/), require to include the end-user in the loop, who has to authenticate. And the end-user should be able to check the trustworthiness/authenticity of the app.

Dynamic credentials typically have several components, (1) identification of the app with a static credential, called clientID and clientSecret in OAuth, and (2) the end-user identification. The static credentials of the app should be stored in secure credential storage, offered by the operating system (see next section). If the end-user also decides to store end-user credentials, they should also be stored in secure credential storage.

If you want to learn more about OAuth, read up in the [OAuth Book](https://api-university.com/books/oauth-2-0-book/) or in the [OAuth Course](https://api-university.com/courses/oauth-2-0-course/).

# Credential Storage
If the API provider did their homework and protected the APIs with OAuth, it is now the turn of the API consumer to store the necessary credentials appropriately. 

Apps should use the secure storage offered by the operating system to store app identifiers/credentials and user identifiers/credentials. 
Both iOS and Android have appropriate libraries:
 * The [Keychain service](https://developer.apple.com/documentation/security/keychain_services) in iOS
 * The [Keystore system](https://developer.android.com/training/articles/keystore) on Android, and the [Account Manager](https://developer.android.com/reference/android/accounts/AccountManager.html) on Android

I hope to have shown you, more secure alternatives to protect your APIs and how to protect API credentials in your apps. The centerpiece? Use OAuth. Correctly.