Authentication
OAuth authentication is a robust method for securing API requests and managing access tokens. Hantera supports multiple OAuth flows, including the code authorization flow, client credentials, and implicit code grant with PKCE.
Base Path for OAuth Requests
All OAuth-related requests should be directed to the following base path:
OAuth Flows
Code Authorization Flow
The code authorization flow is the most commonly used OAuth flow, suitable for web applications where the client can securely store secrets.
-
Redirect to Authorization Endpoint
Redirect the user to the authorization endpoint to obtain an authorization code:
Parameters:
response_type
: Must be set tocode
client_id
: Your application’s client IDredirect_uri
: The URL to redirect the user after authorizationscope
: The requested permissions (see Scopes)state
: A unique string to maintain state between the request and callback
Example:
-
Handle Redirect
Upon successful authorization, the user will be redirected to your specified
redirect_uri
with an authorization code and the state parameter: -
Exchange Authorization Code for Tokens
Make a POST request to the token endpoint to exchange the authorization code for an access token and refresh token:
Parameters:
grant_type
: Must be set toauthorization_code
code
: The authorization code received in the callbackredirect_uri
: The same redirect URI used in the authorization requestclient_id
: Your application’s client IDclient_secret
: Your application’s client secret
Example:
Client Credentials Flow
The client credentials flow is used for machine-to-machine authentication, where no user interaction is required.
-
Request Tokens
Make a POST request to the token endpoint:
Parameters:
grant_type
: Must be set toclient_credentials
client_id
: Your application’s client IDclient_secret
: Your application’s client secretscope
: The requested permissions (see Scopes)
Example:
Implicit Code Grant with PKCE
The implicit code grant is available, but mandates the use of PKCE (Proof Key for Code Exchange). This option is useful for clients that can not be trusted with a client secret, such as a PWA or phone app.
-
Generate a Code Verifier and Challenge
Generate a secure random string as the code verifier and then create a code challenge using a SHA-256 hash of the code verifier.
-
Redirect to Authorization Endpoint
Redirect the user to the authorization endpoint:
Parameters:
response_type
: Must be set tocode
client_id
: Your application’s client IDredirect_uri
: The URL to redirect the user after authorizationscope
: The requested permissions (see Scopes)state
: A unique string to maintain state between the request and callbackcode_challenge
: The code challenge generated from the code verifiercode_challenge_method
: Must be set toS256
Example:
-
Handle Redirect
Upon successful authorization, the user will be redirected to your specified
redirect_uri
with an authorization code and the state parameter: -
Exchange Authorization Code for Tokens
Make a POST request to the token endpoint to exchange the authorization code for an access token, including the code verifier:
Parameters:
grant_type
: Must be set toauthorization_code
code
: The authorization code received in the callbackredirect_uri
: The same redirect URI used in the authorization requestclient_id
: Your application’s client IDcode_verifier
: The original code verifier used to generate the code challenge
Example:
Token Lifecycle
- Access Token: By default, an access token is valid for 60 minutes.
- Refresh Token: By default, a refresh token is valid for 30 days.
Session Management
A session is created upon a completed authorization and contains both the access and refresh tokens. The first 16 bytes of a token are a UUID that can be used to identify the session.
Revoking Sessions
To revoke a session and disable potentially leaked tokens, use the session revocation endpoint.
Parameters:
token
: The token to be revoked (either access or refresh token)token_type_hint
: A hint about the type of the token being revoked (e.g.,access_token
orrefresh_token
)
Example:
Personal Access Tokens (PAT)
Personal Access Tokens (PAT) provide a way for users to generate long-living access tokens, with a maximum lifespan of one year. These tokens do not include a refresh token but can be renewed through the /me
API.
Creating a Personal Access Token
To create a PAT, use the Portal or the /me
API endpoint:
Request Body:
Response Body:
Revoking a Personal Access Token
To revoke a PAT, use the /me
API endpoint:
Scopes
Scopes in Hantera are made up of Access Control Entries (ACEs). These define the specific permissions granted to the access token. The format for scopes is typically:
For example:
actors/order:*
: Grants all permissions onorder
actors.graph:*
: Grants all permissions on thegraph
.me:*
: Grants all permissions on theme
resource.
Multiple scopes can be requested by separating them with spaces:
Client Identity Management
Client identities are created through the Identity and Access Management (IAM) API. This allows administrators to manage which applications can access the Hantera API and the specific permissions they are granted.
Creating a Client Identity
To create a client identity, use the IAM API to send a POST request with the necessary client details:
Request Body:
Restricting Allowed Redirect URLs
Clients can restrict the allowed redirect URLs to enhance security. This is configured during client identity creation by specifying the redirect_uris
parameter. Only the URLs listed in redirect_uris
will be accepted during the authorization process.
Conclusion
Using OAuth authentication with Hantera ensures secure and controlled access to your resources. By following the steps outlined in this guide, you can implement OAuth flows, manage tokens, handle session revocation, create and manage client identities, and generate Personal Access Tokens effectively. For further details, refer to the Hantera API documentation.