“The OAuth 2.0 authorization framework enables a third-party application to obtain limited access to an HTTP service, either on behalf of a resource owner by orchestrating an approval interaction between the resource owner and the HTTP service, or by allowing the third-party application to obtain access on its own behalf.” — RFC 6749OAuth 2.0 provides a mechanism for client applications to request limited access to resources hosted by a resource server, on behalf of a resource owner (typically a user), without exposing the resource owner’s credentials. This allows secure sharing of data between applications—for example, allowing a calendar app to access a user’s contacts to automatically find available time slots for meetings. OAuth 2.0 has many variations and flows suited for different use cases, this section will provide an overview of the core principles, terminology, and key concepts, specifically focusing on how you can implement OAuth 2.0 with Tyk.
Action | Endpoint | Reference |
---|---|---|
Register a new client app | POST /api/apis/oauth/{{api-id}} | link |
Get a list of registered client apps | GET /api/apis/oauth/{{api-id}} | link |
Get the details of a client app | GET /api/apis/oauth/{{api-id}}/{{client_id}} | link |
Delete a client app | DELETE /api/apis/oauth/{{api-id}}/{{client_id}} | link |
HTTP 307 Temporary Redirect
, redirecting the user to an Identity Server
(5) the user must log in on the Identity Server and authorize the client
(6) when the user successfully authenticates and authorizes the request, the Identity Server must request an Authorization Code from the Auth Server
(8) The Identity Server provides the Authorization Code to the client
(9) The client exchanges the Authorization Code for an Access Token from the Auth Server
(10) The client uses the Access Token to authenticate with the protected API using the Auth Token method
POST /<listen-path>/oauth/authorize
The following parameters are required in a request to this endpoint:
Parameter | Value |
---|---|
response_type | code |
client_id | client Id |
redirect_uri | Redirect URI (URL encoded) |
/my-api
proxy deployed on a Tyk Gateway at https://tyk.cloud.tyk.io
.
If the client Id (my-client-id
) is valid, the response will be HTTP 307 Temporary Redirect
with the redirect URI (http://identityserver.com/client-redirect-uri
) in the location
header.
POST /api/apis/{api_id}/authorize-client
. The same redirect_uri
as provided in the original request must be provided alongside the client_id
as a security feature to verify the client identity.
This endpoint is protected using the Dashboard API secret assigned to the Identity Server, which must be provided in the Authorization
header.
The following parameters are required in a POST
request to this endpoint:
Parameter | Value |
---|---|
response_type | code |
client_id | client Id |
redirect_uri | Redirect URI (URL encoded) |
https://admin.cloud.tyk.io
to access the proxy with API Id my-api-id
.
If the client Id (my-client-id
) is valid and redirect_uri
matches the one provided in the initial request, an authorization code will be provided in the response payload, for example:
POST /<listen-path>/oauth/token
.
This endpoint is protected using Basic Authentication where the username is the client Id and the password is the client secret.
The following parameters are required in the request:
Parameter | Value |
---|---|
grant_type | authorization_code |
client_id | client Id |
code | Authorization Code |
redirect_uri | Redirect URI (URL encoded) |
/my-api
proxy deployed on a Tyk Gateway at https://tyk.cloud.tyk.io
. The basic auth key is the base64 encoded representation of my-client-id:my-client-secret
The client_id
and redirect_uri
match those provided in the initial authorization request. The code
is the authorization code provided to the identity server in the authorization code request.
The response payload contains:
access_token
: the token which can be used by the client to access the protected APIexpires_in
: the expiration date/time of the access tokentoken_type
: set to bearer
indicating that the access token should be provided in an Auth Token request to the protected APIrefresh_token
: [optional] a special token that can be used in the Refresh Token flowPOST /<listen-path>/oauth/token
.
This endpoint is protected using Basic Authentication where the username is the client Id and the password is the client secret.
The following parameters are required in the request:
Parameter | Value |
---|---|
grant_type | client_credentials |
client_id | client Id |
secret | client secret |
/my-api
proxy deployed on a Tyk Gateway at https://tyk.cloud.tyk.io
. The basic auth key is the base64 encoded representation of my-client-id:my-client-secret
The client_id
and client_secret
match those allocated by Tyk (the auth server) for the client app.
The response payload contains:
access_token
: the token which can be used by the client to access the protected APIexpires_in
: the expiration date/time of the access tokentoken_type
: set to bearer
indicating that the access token should be provided in an Auth Token request to the protected APIPOST /<listen-path>/oauth/token
.
This endpoint is protected using Basic Authentication where the username is the client Id and the password is the client secret.
The following parameters are required in the request:
Parameter | Value |
---|---|
grant_type | password |
client_id | client Id |
username | resource owner’s username (resource-owner-username ) |
password | resource owner’s password (resource-owner-password ) |
/my-api
proxy deployed on a Tyk Gateway at https://tyk.cloud.tyk.io
. The basic auth key is the base64 encoded representation of my-client-id:my-client-secret
The client_id
and client_secret
match those allocated by Tyk (the auth server) for the client app.
The response payload contains:
access_token
: the token which can be used by the client to access the protected APIexpires_in
: the expiration date/time of the access tokentoken_type
: set to bearer
indicating that the access token should be provided in an Auth Token request to the protected APIrefresh_token
: [optional] a special token that can be used in the Refresh Token flowredirect_uri
provided in the client app (and in the client’s authentication request). The Notifications configuration can be provided for Authorization Code and Password grants.
components.securitySchemes
object using the type: oauth2
. Tyk supports the authorizationCode, clientCredentials and password flows and implements Relative Endpoint URLs for the authorizationUrl
, tokenUrl
and refreshUrl
.
Authorization
header, but Tyk is configurable, via the Tyk Vendor Extension, to support custom header keys and credential passing via query parameter or cooke.
authentication.stripAuthorizationData
field (Tyk Classic: strip_auth_data
).
With the OAuth method selected, you’ll need to configure Tyk to handle the specific configuration of OAuth grants that you will support. All of the OAuth specific configuration is performed within the authentication.securitySchemes.oauth object in the Tyk Vendor Extension.
For example:
oauth
security scheme that expects Authorization Code flow. Note that the authorization URL
and token URL
are declared relative to the API proxy listen pathPOST /my-oauth-api/oauth/authorize
) will be redirected to http://<identity-server>/client-redirect-uri
where the Resource Owner should be prompted to authorize the requesthttp://notifyme.com
with the X-Tyk-Shared-Secret
header set to oauth-shared-secret
Authorization
header of the request.
The configuration above is a complete and valid Tyk OAS API Definition that you can import into Tyk and, with correctly configured and integrated identity server can be used to try out OAuth Client Authentication using Tyk as the Authorization Server.
use_oauth2
option.
POST /<listen-path>/oauth/token
endpoint as follows:
Parameter | Value |
---|---|
grant_type | refresh_token |
client_id | client Id |
client_secret | client secret |
refresh_token | The refresh token provided with the original access token |
/my-api
proxy deployed on a Tyk Gateway at https://tyk.cloud.tyk.io
. The basic auth key is the base64 encoded representation of my-client-id:my-client-secret
The client_id
and client_secret
match those allocated by Tyk (the auth server) for the client app. The refresh_token
is a valid refresh token previously issued to the client.
The response payload contains:
access_token
: a new access token which can be used by the client to access the protected APIexpires_in
: the expiration date/time of the access tokentoken_type
: set to bearer
indicating that the access token should be provided in an Auth Token request to the protected APIrefresh_token
: a new refresh token that can be used later to refresh the new access tokenAuthorization
header.
In this example, we issue a request to the /revoke
endpoint of the auth server via the Tyk Dashboard API to invalidate a specific access token:
token_type_hint
must be set to access_token
or refresh_token
to match the type of token
to be revoked.
POST
request and a “shared secret” value that will be provided in a header (X-Tyk-Shared-Secret
) used to secure the communication to the target application. The OAuth token notification webhook does not support any other authentication method.
The body of the webhook request will have this content:
auth_code
is the Authorization Code that has been issuednew_oauth_token
is the Access Token that has been issuedrefresh_token
is the Refresh Token that has been issuedold_refresh_token
is the Refresh Token that has been consumed when refreshing an access tokennotification_type
will indicate the cause of the event:
new
: a new access token has been issuedrefresh
: a token has been refreshed and a new refresh token has been issued