Authentication¶
Token Authentication¶
When using the REST API, the standard basic and session authentication methods are available in most of the endpoints. But our platform’s API has also an extra method, the Token authentication.
Tokens can be managed by users on their profile page, or by administrators under each user management page:
The token key is only visible at the creation time and cannot be read back again. In case a token is forgotten or lost, it should be removed and a new one must be created. When creating a token is also possible to assign and expiration date in case is needed for a particular use case.
Tokens can also be created or managed via the REST API, more information about how this works is available at the API Reference, accessible on the “Help” section of Cantemo’s main menu bar.
Once a token is created, it can be used to authenticate towards most of the REST API endpoints. The way to use tokens is by adding a HTTP header with name “AUTH-TOKEN” and the token key as the value. For example, a CURL request to Cantemo with token authentication, looks like:
curl -X GET http://exampledomain.com/API/v2/healthcheck/simple -H 'AUTH-TOKEN: 5778161da0fd75b498c66647d9846f08eead727249508’
Session and Token Credentials¶
When a request supplies both a session cookie and an AUTH-TOKEN header, Cantemo checks for conflicting authenticated identities before either credential can replace the session or Run As is applied.
Credentials |
Behavior |
|---|---|
Valid session and API token for the same active user |
The endpoint applies its configured authentication methods and permissions. Endpoints supporting session authentication require CSRF protection for unsafe requests, including when token authentication is configured first. Token-only endpoints retain their token authentication policy. |
Valid session and API token for different active users |
The request is rejected with HTTP 403 and the message |
Invalid or expired session cookie and a valid API token |
The token can authenticate the request if the endpoint accepts API tokens. The stale cookie does not trigger session CSRF checks. |
A single credential type |
The endpoint uses its configured authentication methods. |
An invalid, expired or inactive-user token does not supply a conflicting authenticated identity. The endpoint’s configured authenticators still determine whether the request can authenticate; an unusable extra token does not automatically invalidate a valid session on an endpoint that selects session authentication.
Integrations should send credentials for one user and use RUNAS for explicit administrator impersonation. When both credentials are supplied with RUNAS, they must identify the same authenticated administrator; the Run As target is checked separately.
If Basic credentials are also supplied alongside a matching session and API token, they must identify the same user. A different Basic identity returns HTTP 403 with the message Conflicting session, API token and Basic credentials.. Matching Basic credentials preserve the existing session and its CSRF protection on endpoints supporting session authentication.
The default middleware configuration includes portal.utils.middleware_authentication.SessionTokenAuth before BasicAuth and APITokenAuth. Custom middleware configurations must preserve this ordering, after SessionMiddleware, so conflicts are checked before authentication changes the session. The conflict check does not enable authentication methods that an endpoint excludes.
Custom DRF views should inherit from portal.generic.baseviews.CView or place portal.utils.runas.RunAsMixin before their DRF base class to enforce matching-credential CSRF protection before authentication selects a method. This is required for custom views that try Basic authentication before session authentication.
Run As¶
The “run as” feature will allow administrator users (users with portal_administrator role) to execute requests as if the request has been made by another specified user. This way, the request will not behave as if the request was made by the administrator user, but as the specified user.
The way to use this feature is by adding a HTTP header with name “RUNAS” and the target username as the value. The target user must exist and be active. For example, a CURL request to Cantemo with the “run as” feature, looks like:
curl -X GET https://portal.example.com/API/v2/whoami/ -H 'AUTH-TOKEN: <admin-token>' -H 'RUNAS: someuser'
Run As works with the authentication methods accepted by each endpoint, including Basic, session and API token authentication. It does not enable additional authentication methods on an endpoint. Permissions are checked for the target user, and impersonation lasts only for the current request. Existing sessions and explicitly requested temporary sessions retain the authenticated administrator’s identity.
For custom Django REST Framework views, inherit from portal.generic.baseviews.CView or place portal.utils.runas.RunAsMixin before the DRF base class. The mixin handles Run As after authentication and before permission checks. RunAsMiddleware must remain enabled; it handles legacy views and defers supported DRF views to the mixin.
Custom DRF views without this hook reject requests containing Run As with HTTP 403, to prevent subsequent authentication from silently replacing the target user with the administrator.