2017-11-03 00:18:27 +03:00
## Introduction
Starting from Tower 3.3, OAuth 2 will be used as the new means of token-based authentication. Users
2018-01-18 18:57:54 +03:00
will be able to manage OAuth 2 tokens as well as applications, a server-side representation of API
clients used to generate tokens. With OAuth 2, a user can authenticate by passing a token as part of
the HTTP authentication header. The token can be scoped to have more restrictive permissions on top of
the base RBAC permissions of the user. Refer to [RFC 6749 ](https://tools.ietf.org/html/rfc6749 ) for
more details of OAuth 2 specification.
2017-11-03 00:18:27 +03:00
2018-06-01 23:17:15 +03:00
## Basic Usage
To get started using OAuth 2 tokens for accessing the browsable API using OAuth 2, we will walkthrough acquiring a token, and using it.
1. Make an application with authorization_grant_type set to 'password'. HTTP POST the following to the `/api/v2/applications/` endpoint (supplying your own organization-id):
```
{
"name": "Admin Internal Application",
"description": "For use by secure services & clients. ",
"client_type": "confidential",
"redirect_uris": "",
"authorization_grant_type": "password",
"skip_authorization": false,
"organization": < organization-id >
}
```
2. Make a token with a POST to the `/api/v2/tokens/` endpoint:
```
{
"description": "My Access Token",
"application": < application-id > ,
"scope": "write"
}
```
This will return a `<token-value>` that you can use to authenticate with for future requests (this will not be shown again)
3. Use token to access a resource. We will use curl to demonstrate this:
```
curl -H "Authorization: Bearer < token-value > " -X GET https://< awx > /api/v2/users/
```
> The `-k` flag may be needed if you have not set up a CA yet and are using SSL.
This token can be revoked by making a DELETE on the detail page for that token. All you need is that token's id. For example:
```
curl -ku < user > :< password > -X DELETE https://< awx > /api/v2/tokens/< pk > /
```
Similarly, using a token:
```
curl -H "Authorization: Bearer < token-value > " -X DELETE https://< awx > /api/v2/tokens/< pk > / -k
```
## More Information
2017-11-03 00:18:27 +03:00
2018-01-18 18:57:54 +03:00
#### Managing OAuth 2 applications and tokens
Applications and tokens can be managed as a top-level resource at `/api/<version>/applications` and
`/api/<version>/tokens` . These resources can also be accessed respective to the user at
`/api/<version>/users/N/<resource>` . Applications can be created by making a POST to either `api/<version>/applications`
or `/api/<version>/users/N/applications` .
2017-11-03 00:18:27 +03:00
2018-06-01 23:17:15 +03:00
Each OAuth 2 application represents a specific API client on the server side. For an API client to use the API via an application token,
it must first have an application and issue an access token.
2017-11-03 00:18:27 +03:00
Individual applications will be accessible via their primary keys:
2018-06-01 23:17:15 +03:00
`/api/<version>/applications/<pk>/` . Here is a typical application:
2017-11-03 00:18:27 +03:00
```
{
"id": 1,
2018-01-18 18:57:54 +03:00
"type": "o_auth2_application",
"url": "/api/v2/applications/1/",
2017-11-03 00:18:27 +03:00
"related": {
"user": "/api/v2/users/1/",
2018-01-18 18:57:54 +03:00
"tokens": "/api/v2/applications/1/tokens/",
"activity_stream": "/api/v2/applications/1/activity_stream/"
2017-11-03 00:18:27 +03:00
},
"summary_fields": {
"user": {
"id": 1,
2018-01-18 18:57:54 +03:00
"username": "root",
2017-11-03 00:18:27 +03:00
"first_name": "",
"last_name": ""
},
"tokens": {
2018-01-18 18:57:54 +03:00
"count": 1,
2017-11-03 00:18:27 +03:00
"results": [
{
2018-01-18 18:57:54 +03:00
"scope": "read",
2018-04-10 17:52:59 +03:00
"token": "*************",
2018-01-18 18:57:54 +03:00
"id": 2
}
2017-11-03 00:18:27 +03:00
]
}
},
2018-01-18 18:57:54 +03:00
"created": "2018-02-20T23:06:43.215315Z",
"modified": "2018-02-20T23:06:43.215375Z",
"name": "Default application for root",
2017-11-03 00:18:27 +03:00
"user": 1,
2018-01-18 18:57:54 +03:00
"client_id": "BIyE720WAjr14nNxGXrBbsRsG0FkjgeL8cxNmIWP",
"client_secret": "OdO6TMNAYxUVv4HLitLOnRdAvtClEV8l99zlb8EJEZjlzVNaVVlWiKXicznLDeANwu5qRgeQRvD3AnuisQGCPXXRCx79W1ARQ5cSmc9mrU1JbqW7nX3IZYhLIFgsDH8u",
2017-11-03 00:18:27 +03:00
"client_type": "confidential",
"redirect_uris": "",
"authorization_grant_type": "password",
"skip_authorization": false
2018-01-18 18:57:54 +03:00
},
2017-11-03 00:18:27 +03:00
```
2018-06-01 23:17:15 +03:00
In the above example, `user` is the primary key of the user associated to this application and `name` is
2018-01-18 18:57:54 +03:00
a human-readable identifier for the application. The other fields, like `client_id` and
`redirect_uris` , are mainly used for OAuth 2 authorization, which will be covered later in the 'Using
2018-06-01 23:17:15 +03:00
OAuth 2 Token System' section.
2017-11-03 00:18:27 +03:00
Fields `client_id` and `client_secret` are immutable identifiers of applications, and will be
generated during creation; Fields `user` and `authorization_grant_type` , on the other hand, are
*immutable on update*, meaning they are required fields on creation, but will become read-only after
that.
On RBAC side:
- system admins will be able to see and manipulate all applications in the system;
- Organization admins will be able to see and manipulate all applications belonging to Organization
members;
- Other normal users will only be able to see, update and delete their own applications, but
2018-01-18 18:57:54 +03:00
cannot create any new applications.
2017-11-03 00:18:27 +03:00
2018-02-23 20:42:44 +03:00
Tokens, on the other hand, are resources used to actually authenticate incoming requests and mask the
2018-02-27 18:50:48 +03:00
permissions of the underlying user. Tokens can be created by POSTing to `/api/v2/tokens/`
2018-02-23 20:42:44 +03:00
endpoint by providing `application` and `scope` fields to point to related application and specify
2018-03-08 20:31:40 +03:00
token scope; or POSTing to `/api/v2/applications/<pk>/tokens/` by providing only `scope` , while
2018-02-23 20:42:44 +03:00
the parent application will be automatically linked.
Individual tokens will be accessible via their primary keys:
2018-06-01 23:17:15 +03:00
`/api/<version>/tokens/<pk>/` . Here is a typical token:
2018-02-23 20:42:44 +03:00
```
{
2018-02-27 18:50:48 +03:00
"id": 4,
"type": "o_auth2_access_token",
"url": "/api/v2/tokens/4/",
2018-02-23 20:42:44 +03:00
"related": {
"user": "/api/v2/users/1/",
2018-02-27 18:50:48 +03:00
"application": "/api/v2/applications/1/",
"activity_stream": "/api/v2/tokens/4/activity_stream/"
2018-02-23 20:42:44 +03:00
},
"summary_fields": {
"application": {
2018-02-27 18:50:48 +03:00
"id": 1,
"name": "Default application for root",
"client_id": "mcU5J5uGQcEQMgAZyr5JUnM3BqBJpgbgL9fLOVch"
2018-02-23 20:42:44 +03:00
},
"user": {
"id": 1,
2018-02-27 18:50:48 +03:00
"username": "root",
2018-02-23 20:42:44 +03:00
"first_name": "",
"last_name": ""
}
},
2018-02-27 18:50:48 +03:00
"created": "2018-02-23T14:39:32.618932Z",
"modified": "2018-02-23T14:39:32.643626Z",
"description": "App Token Test",
2018-02-23 20:42:44 +03:00
"user": 1,
2018-02-27 18:50:48 +03:00
"token": "*************",
2018-04-10 17:52:59 +03:00
"refresh_token": "*************",
2018-02-27 18:50:48 +03:00
"application": 1,
"expires": "2018-02-24T00:39:32.618279Z",
2018-02-23 20:42:44 +03:00
"scope": "read"
2018-02-27 18:50:48 +03:00
},
2018-02-23 20:42:44 +03:00
```
2018-03-08 20:31:40 +03:00
For an OAuth 2 token, the only fully mutable fields are `scope` and `description` . The `application`
field is *immutable on update* , and all other fields are totally immutable, and will be auto-populated
during creation
2018-06-01 23:17:15 +03:00
* `user` field corresponds to the user the token is created for
2018-03-08 20:31:40 +03:00
* `expires` will be generated according to Tower configuration setting `OAUTH2_PROVIDER`
* `token` and `refresh_token` will be auto-generated to be non-clashing random strings.
Both application tokens and personal access tokens will be shown at the `/api/v2/tokens/`
endpoint. Personal access tokens can be identified by the `application` field being `null` .
2018-02-23 20:42:44 +03:00
On RBAC side:
- A user will be able to create a token if they are able to see the related application;
- System admin is able to see and manipulate every token in the system;
- Organization admins will be able to see and manipulate all tokens belonging to Organization
members;
2018-06-01 23:17:15 +03:00
System Auditors can see all tokens and applications
2018-02-23 20:42:44 +03:00
- Other normal users will only be able to see and manipulate their own tokens.
> Note: Users can only see the token or refresh-token _value_ at the time of creation ONLY.
2018-06-01 23:17:15 +03:00
#### Using OAuth 2 Token System for Personal Access Tokens (PAT)
2018-02-23 20:42:44 +03:00
The most common usage of OAuth 2 is authenticating users. The `token` field of a token is used
as part of the HTTP authentication header, in the format `Authorization: Bearer <token field value>` . This _Bearer_
2018-03-08 20:31:40 +03:00
token can be obtained by doing a curl to the `/api/o/token/` endpoint. For example:
```
2018-04-20 21:56:27 +03:00
curl -ku < user > :< password > -H "Content-Type: application/json" -X POST \
-d '{"description":"Tower CLI", "application":null, "scope":"write"}' \
2018-06-01 23:17:15 +03:00
https://< awx > /api/v2/users/1/personal_tokens/ | python -m json.tool
2018-03-08 20:31:40 +03:00
```
2018-02-23 20:42:44 +03:00
Here is an example of using that PAT to access an API endpoint using `curl` :
```
2018-06-01 23:17:15 +03:00
curl -H "Authorization: Bearer kqHqxfpHGRRBXLNCOXxT5Zt3tpJogn" http://< awx > /api/v2/credentials/
2018-02-23 20:42:44 +03:00
```
According to OAuth 2 specification, users should be able to acquire, revoke and refresh an access
2018-06-01 23:17:15 +03:00
token. In AWX the equivalent, and easiest, way of doing that is creating a token, deleting
a token, and deleting a token quickly followed by creating a new one.
2018-02-23 20:42:44 +03:00
2018-03-08 20:31:40 +03:00
The specification also provides standard ways of doing this. RFC 6749 elaborates
2018-02-27 18:50:48 +03:00
on those topics, but in summary, an OAuth 2 token is officially acquired via authorization using
2018-02-23 20:42:44 +03:00
authorization information provided by applications (special application fields mentioned above).
2018-02-27 18:50:48 +03:00
There are dedicated endpoints for authorization and acquiring tokens. The `token` endpoint
is also responsible for token refresh, and token revoke can be done by the dedicated token revoke endpoint.
2018-02-23 20:42:44 +03:00
2018-02-27 18:50:48 +03:00
In AWX, our OAuth 2 system is built on top of
2018-02-23 20:42:44 +03:00
[Django Oauth Toolkit ](https://django-oauth-toolkit.readthedocs.io/en/latest/ ), which provides full
support on standard authorization, token revoke and refresh. AWX implements them and puts related
endpoints under `/api/o/` endpoint. Detailed examples on the most typical usage of those endpoints
2018-03-15 17:24:52 +03:00
are available as description text of `/api/o/` . See below for information on Application Access Token usage.
2018-06-01 23:17:15 +03:00
> Note: The `/api/o/` endpoints can only be used for application tokens, and are not valid for personal access tokens.
2018-02-23 20:42:44 +03:00
#### Token scope mask over RBAC system
2018-02-27 18:50:48 +03:00
The scope of an OAuth 2 token is a space-separated string composed of keywords like 'read' and 'write'.
2018-02-23 20:42:44 +03:00
These keywords are configurable and used to specify permission level of the authenticated API client.
2018-02-27 18:50:48 +03:00
For the initial OAuth 2 implementation, we use the most simple scope configuration, where the only
2018-02-23 20:42:44 +03:00
valid scope keywords are 'read' and 'write'.
Read and write scopes provide a mask layer over the RBAC permission system of AWX. In specific, a
2018-02-27 18:50:48 +03:00
'write' scope gives the authenticated user the full permissions the RBAC system provides, while 'read'
2018-02-23 20:42:44 +03:00
scope gives the authenticated user only read permissions the RBAC system provides.
2018-02-27 18:50:48 +03:00
For example, if a user has admin permission to a job template, he/she can both see and modify, launch
and delete the job template if authenticated via session or basic auth. On the other hand, if the user
is authenticated using OAuth 2 token, and the related token scope is 'read', the user can only see but
not manipulate or launch the job template, despite being an admin. If the token scope is
'write' or 'read write', she can take full advantage of the job template as its admin. Note, that 'write'
implies 'read' as well.
2018-02-23 20:42:44 +03:00
2018-03-15 17:24:52 +03:00
## Application Functions
2018-07-30 17:23:27 +03:00
This page lists OAuth 2 utility endpoints used for authorization, token refresh and revoke.
2018-03-15 17:24:52 +03:00
Note endpoints other than `/api/o/authorize/` are not meant to be used in browsers and do not
support HTTP GET. The endpoints here strictly follow
2018-07-30 17:23:27 +03:00
[RFC specs for OAuth 2 ](https://tools.ietf.org/html/rfc6749 ), so please use that for detailed
2018-03-15 17:24:52 +03:00
reference. The `implicit` grant type can only be used to acquire a access token if the user is already logged in via session authentication, as that confirms that the user is authorized to create an access token. Here we give some examples to demonstrate the typical usage of these endpoints in
AWX context (Note AWX net location default to `http://localhost:8013` in examples):
#### Application using `authorization code` grant type
This application grant type is intended to be used when the application is executing on the server. To create
an application named `AuthCodeApp` with the `authorization-code` grant type,
Make a POST to the `/api/v2/applications/` endpoint.
```text
{
"name": "AuthCodeApp",
"user": 1,
"client_type": "confidential",
2018-06-01 23:17:15 +03:00
"redirect_uris": "http://< awx > /api/v2",
2018-03-15 17:24:52 +03:00
"authorization_grant_type": "authorization-code",
"skip_authorization": false
}
```
You can test the authorization flow out with this new application by copying the client_id and URI link into the
homepage [here ](http://django-oauth-toolkit.herokuapp.com/consumer/ ) and click submit. This is just a simple test
application Django-oauth-toolkit provides.
From the client app, the user makes a GET to the Authorize endpoint with the `response_type` ,
`client_id` , `redirect_uris` , and `scope` . AWX will respond with the authorization `code` and `state`
to the redirect_uri specified in the application. The client application will then make a POST to the
`api/o/token/` endpoint on AWX with the `code` , `client_id` , `client_secret` , `grant_type` , and `redirect_uri` .
AWX will respond with the `access_token` , `token_type` , `refresh_token` , and `expires_in` . For more
information on testing this flow, refer to [django-oauth-toolkit ](http://django-oauth-toolkit.readthedocs.io/en/latest/tutorial/tutorial_01.html#test-your-authorization-server ).
#### Application using `implicit` grant type
The use case: single page web apps that can't keep a client_secret as secure. This method with skips the
authorization code part of the flow and just returns an access token.
Suppose we have an application `admin's app` of grant type `implicit` :
```text
{
"id": 1,
"type": "application",
"related": {
...
"name": "admin's app",
"user": 1,
"client_id": "L0uQQWW8pKX51hoqIRQGsuqmIdPi2AcXZ9EJRGmj",
"client_secret": "9Wp4dUrUsigI8J15fQYJ3jn0MJHLkAjyw7ikBsABeWTNJbZwy7eB2Xro9ykYuuygerTPQ2gIF2DCTtN3kurkt0Me3AhanEw6peRNvNLs1NNfI4f53mhX8zo5JQX0BKy5",
"client_type": "confidential",
2018-06-01 23:17:15 +03:00
"redirect_uris": "http://< awx > /api/",
2018-03-15 17:24:52 +03:00
"authorization_grant_type": "implicit",
"skip_authorization": false
}
```
In API browser, first make sure the user is logged in via session auth, then visit authorization
endpoint with given parameters:
```text
http://localhost:8013/api/o/authorize/?response_type=token& client_id=L0uQQWW8pKX51hoqIRQGsuqmIdPi2AcXZ9EJRGmj& scope=read
```
Here the value of `client_id` should be the same as that of `client_id` field of underlying application.
On success, an authorization page should be displayed asking the logged in user to grant/deny the access token.
Once the user clicks on 'grant', the API browser will try POSTing to the same endpoint with the same parameters in POST body, on success a 302 redirect will be returned:
```text
HTTP/1.1 302 Found
Connection:keep-alive
Content-Language:en
Content-Length:0
Content-Type:text/html; charset=utf-8
Date:Tue, 05 Dec 2017 20:36:19 GMT
Location:http://localhost:8013/api/#access_token=0lVJJkolFTwYawHyGkk7NTmSKdzBen& token_type=Bearer& state=& expires_in=315360000000& scope=read
Server:nginx/1.12.2
Strict-Transport-Security:max-age=15768000
Vary:Accept-Language, Cookie
```
#### Application using `password` grant type
This is also called the `resource owner credentials grant` . This is for use by users who have
native access to the web app. This should be used when the client is the Resource owner. Suppose
we have an application `Default Application` with grant type `password` :
```text
{
"id": 6,
"type": "application",
...
"name": "Default Application",
"user": 1,
"client_id": "gwSPoasWSdNkMDtBN3Hu2WYQpPWCO9SwUEsKK22l",
"client_secret": "fI6ZpfocHYBGfm1tP92r0yIgCyfRdDQt0Tos9L8a4fNsJjQQMwp9569eIaUBsaVDgt2eiwOGe0bg5m5vCSstClZmtdy359RVx2rQK5YlIWyPlrolpt2LEpVeKXWaiybo",
"client_type": "confidential",
"redirect_uris": "",
"authorization_grant_type": "password",
"skip_authorization": false
}
```
Log in is not required for `password` grant type, so we can simply use `curl` to acquire a personal access token
via `/api/o/token/` :
```bash
curl -X POST \
-d "grant_type=password& username=< username > & password=< password > & scope=read" \
-u "gwSPoasWSdNkMDtBN3Hu2WYQpPWCO9SwUEsKK22l:fI6ZpfocHYBGfm1tP92r0yIgCyfRdDQt0Tos9L8a4fNsJjQQMwp9569e
IaUBsaVDgt2eiwOGe0bg5m5vCSstClZmtdy359RVx2rQK5YlIWyPlrolpt2LEpVeKXWaiybo" \
2018-06-01 23:17:15 +03:00
http://< awx > /api/o/token/ -i
2018-03-15 17:24:52 +03:00
```
In the above post request, parameters `username` and `password` are username and password of the related
AWX user of the underlying application, and the authentication information is of format
`<client_id>:<client_secret>` , where `client_id` and `client_secret` are the corresponding fields of
underlying application.
Upon success, access token, refresh token and other information are given in the response body in JSON
format:
```text
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 05 Dec 2017 16:48:09 GMT
Content-Type: application/json
Content-Length: 163
Connection: keep-alive
Content-Language: en
Vary: Accept-Language, Cookie
Pragma: no-cache
Cache-Control: no-store
Strict-Transport-Security: max-age=15768000
{"access_token": "9epHOqHhnXUcgYK8QanOmUQPSgX92g", "token_type": "Bearer", "expires_in": 315360000000, "refresh_token": "jMRX6QvzOTf046KHee3TU5mT3nyXsz", "scope": "read"}
```
## Token Functions
#### Refresh an existing access token
Suppose we have an existing access token with refresh token provided:
```text
{
"id": 35,
"type": "access_token",
...
"user": 1,
"token": "omMFLk7UKpB36WN2Qma9H3gbwEBSOc",
"refresh_token": "AL0NK9TTpv0qp54dGbC4VUZtsZ9r8z",
"application": 6,
"expires": "2017-12-06T03:46:17.087022Z",
"scope": "read write"
}
```
The `/api/o/token/` endpoint is used for refreshing access token:
```bash
curl -X POST \
-d "grant_type=refresh_token& refresh_token=AL0NK9TTpv0qp54dGbC4VUZtsZ9r8z" \
-u "gwSPoasWSdNkMDtBN3Hu2WYQpPWCO9SwUEsKK22l:fI6ZpfocHYBGfm1tP92r0yIgCyfRdDQt0Tos9L8a4fNsJjQQMwp9569eIaUBsaVDgt2eiwOGe0bg5m5vCSstClZmtdy359RVx2rQK5YlIWyPlrolpt2LEpVeKXWaiybo" \
2018-06-01 23:17:15 +03:00
http://< awx > /api/o/token/ -i
2018-03-15 17:24:52 +03:00
```
In the above post request, `refresh_token` is provided by `refresh_token` field of the access token
above. The authentication information is of format `<client_id>:<client_secret>` , where `client_id`
and `client_secret` are the corresponding fields of underlying related application of the access token.
Upon success, the new (refreshed) access token with the same scope information as the previous one is
given in the response body in JSON format:
```text
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 05 Dec 2017 17:54:06 GMT
Content-Type: application/json
Content-Length: 169
Connection: keep-alive
Content-Language: en
Vary: Accept-Language, Cookie
Pragma: no-cache
Cache-Control: no-store
Strict-Transport-Security: max-age=15768000
{"access_token": "NDInWxGJI4iZgqpsreujjbvzCfJqgR", "token_type": "Bearer", "expires_in": 315360000000, "refresh_token": "DqOrmz8bx3srlHkZNKmDpqA86bnQkT", "scope": "read write"}
```
Internally, the refresh operation deletes the existing token and a new token is created immediately
after, with information like scope and related application identical to the original one. We can
verify by checking the new token is present and the old token is deleted at the /api/v2/tokens/ endpoint.
#### Revoke an access token
2018-06-01 23:17:15 +03:00
##### Alternatively Revoke using the /api/o/revoke-token/ endpoint
Revoking an access token by this method is the same as deleting the token resource object, but it allows you to delete a token by providing its token value, and the associated `client_id` (and `client_secret` if the application is `confidential` ). For example:
2018-03-15 17:24:52 +03:00
```bash
curl -X POST -d "token=rQONsve372fQwuc2pn76k3IHDCYpi7" \
-u "gwSPoasWSdNkMDtBN3Hu2WYQpPWCO9SwUEsKK22l:fI6ZpfocHYBGfm1tP92r0yIgCyfRdDQt0Tos9L8a4fNsJjQQMwp9569eIaUBsaVDgt2eiwOGe0bg5m5vCSstClZmtdy359RVx2rQK5YlIWyPlrolpt2LEpVeKXWaiybo" \
2018-06-01 23:17:15 +03:00
http://< awx > /api/o/revoke_token/ -i
2018-03-15 17:24:52 +03:00
```
`200 OK` means a successful delete.
We can verify the effect by checking if the token is no longer present
at /api/v2/tokens/.
2018-02-23 20:42:44 +03:00
## Acceptance Criteria
2018-02-27 18:50:48 +03:00
* All CRUD operations for OAuth 2 applications and tokens should function as described.
2018-07-30 17:23:27 +03:00
* RBAC rules applied to OAuth 2 applications and tokens should behave as described.
2018-02-23 20:42:44 +03:00
* A default application should be auto-created for each new user.
2018-02-27 18:50:48 +03:00
* Incoming requests using unexpired OAuth 2 token correctly in authentication header should be able
2018-02-23 20:42:44 +03:00
to successfully authenticate themselves.
* Token scope mask over RBAC should work as described.
* Tower configuration setting `OAUTH2_PROVIDER` should be configurable and function as described.
* `/api/o/` endpoint should work as expected. In specific, all examples given in the description
help text should be working (user following the steps should get expected result).