Token
1. Requesting tokens with a grant
Clients obtain identity and access tokens from the token endpoint in exchange for an OAuth 2.0 grant. The grant is a recognised credential which lets the client access the requested resource (web API) or user identity.
The token endpoint of the Connect2id server accepts the following grant types:
-
Authorisation code – the code obtained from the authorisation endpoint which the server uses to look up the granted permission or consent.
-
Resource owner password credentials – the client obtains the end-user username and password and passes them to the token endpoint; use of this grant should be limited to highly-trusted clients or devices.
-
Client credentials – the client ID and secret obtained from [client-registration]; this grant is intended for clients acting on their own behalf.
-
Refresh token – a special token which can be used to retrieve a new ID / refresh token.
-
JWT bearer assertion – a signed JSON Web Token (JWT), issued by a third-party token service (STS) or issued by the client itself, to obtain an access and / or ID token.
-
SAML 2.0 bearer assertion – a SAML 2.0 assertion, issued by a third-party token service (STS) or issued by the client itself, to obtain an access and / or ID token.
The implicit grant (or flow) is the only one which doesn’t involve the token endpoint; with it the requested ID / access tokens are returned from the authorisation endpoint.
2. The token endpoint URL
The token endpoint URL can be obtained from the server discovery endpoint:
https://[base-server-url]/token
3. Client authentication
Confidential clients must explicitly authenticate themselves to the Connect2id server with their registered credentials in order to make a token request, unless a self-issued JWT or SAML 2.0 bearer assertion grant is submitted (where the assertion itself serves as implicit client authentication).
Client secret basic is the default authentication method.
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Clients may register an alternative authentication method, such as client secret POST, client secret JWT or private key JWT.
Public clients (that have not been issued a credential) should identify themselves with the client_id parameter.
4. Web API overview
Resources | |
---|---|
Representations | Errors |
4. Resources
4.1 /token
4.1.1 POST
Requests an identity / access / refresh token.
Header parameters:
-
[ Authorization ] Used for HTTP basic authentication of the client.
-
Content-Type Must be set to
application/x-www-form-urlencoded
.
Body:
-
The request parameters for the appropriate OAuth 2.0 grant type:
-
-
grant_type Must be set to
authorization_code
. - code The code received with the authentication response.
-
redirect_uri The value of the
redirect_uri
parameter included in the original authentication request. - [ code_verifier ] The code verifier if Proof Key of Code Exchange (PKCE) is employed (for public OAuth clients).
-
grant_type Must be set to
-
-
grant_type Must be set to
password
. - username The resource owner username.
- password The resource owner password.
- [ scope ] Optional requested scope values for the access token.
-
grant_type Must be set to
-
-
grant_type Must be set to
client_credentials
. - [ scope ] Optional requested scope values for the access token.
-
grant_type Must be set to
-
-
grant_type Must be set to
refresh_token
. - refresh_token The refresh token issued to the client.
- [ scope ] Optional requested scope values for the new access token. Must not include any scope values not originally granted by the resource owner, and if omitted is treated as equal to the originally granted scope.
-
grant_type Must be set to
-
-
grant_type Must be set to
urn:ietf:params:oauth:grant-type:jwt-bearer
. - assertion The JWT assertion.
- [ scope ] Optional requested scope values for the access token.
-
grant_type Must be set to
-
SAML 2.0 bearer assertion grant:
-
grant_type Must be set to
urn:ietf:params:oauth:grant-type:saml2-bearer
. - assertion The SAML 2.0 assertion, base 64 URL encoded.
- [ scope ] Optional requested scope values for the access token.
-
grant_type Must be set to
-
Success:
-
Code:
200
-
Content-Type:
application/json
-
Body: {object} The token response.
Errors:
- 400 Bad Request with a [token error code].
- 401 Unauthorised with a [token error code].
- 500 Internal Server Error
Example token request with a code grant:
POST /token HTTP/1.1
Host: c2id.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
Example token request with a code grant for a public client with PKCE:
POST /token HTTP/1.1
Host: c2id.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=com.example%3A%2F%2F%2Fauth
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
Example token request with a password grant:
POST /token HTTP/1.1
Host: c2id.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=alice&password=secret
Example token request with a client credentials grant:
POST /token HTTP/1.1
Host: c2id.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
Example response with an access and ID token:
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"access_token" : "2YotnFZFEjr1zCsicMWpAA",
"token_type" : "bearer",
"expires_in" : 3600,
"scope" : "openid email profile app:read app:write",
"id_token" : "eyJraWQiOiIxZTlnZGs3IiwiYWxnIjoiUl..."
}
5. Representations
5.1 Token response
JSON object with members (see section 5.1 of the OAuth 2.0 standard for more information):
-
access_token The access token issued by the server.
-
token_type Set to
bearer
. -
expires_in The lifetime of the access token, in seconds.
-
[ id_token ] Optional identity token, issued for the code and password grants. Not provided for client credentials grants.
-
[ refresh_token ] Optional refresh token, which can be used to obtain new access tokens. Issued only for long-lived authorisations that permit it.
-
scope The scope of the access token.
Example token response with an access and ID token:
{
"access_token" : "2YotnFZFEjr1zCsicMWpAA",
"token_type" : "bearer",
"expires_in" : 3600,
"scope" : "openid email profile app:read app:write",
"id_token" : "eyJraWQiOiIxZTlnZGs3IiwiYWxnIjoiUl..."
}
5.2 Token error
JSON object with members:
-
error An error code which can take on of the following values:
- invalid_request The request is missing a required parameter, includes an unsupported parameter value (other than grant type), repeats a parameter, or is otherwise malformed.
- invalid_client Client authentication failed, due to missing or invalid client credentials.
-
invalid_grant The provided OAuth 2.0 grant is invalid, expired or
has been revoked. May also indicate the
redirect_uri
parameter doesn’t match (for a code grant). - unauthorized_client The client is successfully authenticated, but it’s not registered to use the submitted grant type.
- unsupported_grant_type The grant type is not supported by the server.
- invalid_scope The requested scope is invalid, unknown, malformed, or exceeds the scope granted by the resource owner.
-
[ error_description ] Optional text providing additional information about the error that occurred.
-
[ error_uri ] Optional URI for a web page with information about the error that occurred.
Example token error:
{
"error" : "invalid_request"
}
Example token error with additional information:
{
"error" : "invalid_request"
"error_description" : "Missing required grant type parameters"
}
6. Errors
400 Bad Request
Invalid or denied request, see token error codes for more information.
Example:
HTTP/1.1 400 Bad Request
Content-Type: application/json
{
"error" : "invalid_grant",
"error_description" : "Bad request: Invalid or expired authorization code"
}
401 Unauthorized
The request was denied due to an invalid or missing client authentication, see token error codes for more information.
Example:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic
Content-Type: application/json
{
"error" : "invalid_client",
"error_description" : "Missing client authentication"
}
500 Internal Server Error
An internal server error has occurred. Check the Connect2id server logs for details.
Example:
HTTP/1.1 500 Internal Server Error