Calling APIs from Mobile Apps
In order to access an API from a mobile app, you need to implement the Authorization Code using Proof Key for Code Exchange (PKCE) OAuth 2.0 grant. In this document we will see how this flow works.
Overview of the flow
The Authorization Code Grant has some security issues, when implemented on native applications. For instance, a malicious attacker can intercept the
authorization_code returned by Auth0 and exchange it for an Access Token (and possibly a Refresh Token).
The Proof Key for Code Exchange (PKCE) (defined in RFC 7636) is a technique used to mitigate this authorization code interception attack.
With PKCE, the application creates, for every authorization request, a cryptographically random key called
code_verifier and its transformed value called
code_challenge, which is sent to Auth0 to get the
authorization_code. When the application receives the
authorization_code, it will send the code and the
code_verifier to Auth0's token endpoint to exchange them for the requested tokens.
The native application initiates the flow and redirects the user to Auth0 (specifically to the /authorize endpoint), sending the
Auth0 redirects the user to the native application with an
authorization_codein the querystring.
The native application sends the
code_verifiertogether with the
client_idto Auth0. This is done using the /oauth/token endpoint.
Auth0 validates this information and returns an Access Token (and optionally a Refresh Token).
The native application can use the Access Token to call the API on behalf of the user.
How to implement the flow
For details on how to implement this using Auth0, refer to Execute an Authorization Code Grant Flow with PKCE.
Rules will run for the Authorization Code (PKCE) grant. If you wish to execute special logic unique to the Authorization Code grant, you can look at the
context.protocol property in your rule. If the value is
oidc-basic-profile, then the rule is running during the Authorization Code grant.
For details on how to implement this, refer to Execute an Authorization Code Grant Flow with PKCE: Customize the Tokens.