1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-27 17:55:10 +03:00
awx/docs/auth/session.md

86 lines
5.0 KiB
Markdown
Raw Normal View History

## Introduction
2018-01-18 18:57:54 +03:00
2021-04-30 21:14:38 +03:00
Session-based authentication is the main authentication method, and auth tokens have been replaced by OAuth 2 tokens.
2019-09-17 22:49:01 +03:00
Session authentication is a safer way of utilizing HTTP(S) cookies. Theoretically, the user can provide authentication information, like username and password, as part of the
`Cookie` header, but this method is vulnerable to cookie hijacks, where crackers can see and steal user
2019-09-17 22:49:01 +03:00
information from the cookie payload.
2019-09-17 22:49:01 +03:00
Session authentication, on the other hand, sets a single `session_id` cookie. The `session_id`
is *a random string which will be mapped to user authentication informations by server*. Crackers who
2019-09-17 22:49:01 +03:00
hijack cookies will only get the `session_id` itself, which does not imply any critical user info, is valid only for
a limited time, and can be revoked at any time.
2019-09-17 22:49:01 +03:00
> Note: The CSRF token will by default allow HTTP. To increase security, the `CSRF_COOKIE_SECURE` setting should
be set to True.
2018-02-27 18:50:48 +03:00
2019-09-17 22:49:01 +03:00
## Usage
2018-02-23 20:42:44 +03:00
In session authentication, users log in using the `/api/login/` endpoint. A GET to `/api/login/` displays the
2019-09-17 22:49:01 +03:00
login page of API browser:
![Example session log in page](../img/auth_session_1.png?raw=true)
2019-09-17 22:49:01 +03:00
Users should enter correct username and password before clicking on the 'LOG IN' button, which fires a POST
to `/api/login/` to actually log the user in. The return code of a successful login is 302, meaning upon
2019-09-17 22:49:01 +03:00
successful login, the browser will be redirected; the redirected destination is determined by the `next` form
item described below.
2019-09-17 22:49:01 +03:00
It should be noted that the POST body of `/api/login/` is *not* in JSON, but in HTTP form format. Four items should
be provided in the form:
* `username`: The username of the user trying to log in.
* `password`: The password of the user trying to log in.
* `next`: The path of the redirect destination, in API browser `"/api/"` is used.
* `csrfmiddlewaretoken`: The CSRF token, usually populated by using Django template `{% csrf_token %}`.
2019-09-17 22:49:01 +03:00
The `session_id` is provided as a return `Set-Cookie` header. Here is a typical one:
```
Set-Cookie: sessionid=lwan8l5ynhrqvps280rg5upp7n3yp6ds; expires=Tue, 21-Nov-2017 16:33:13 GMT; httponly; Max-Age=1209600; Path=/
```
Any client should follow the standard rules of [cookie protocol](https://tools.ietf.org/html/rfc6265) to
2018-02-27 18:50:48 +03:00
parse that header to obtain information about the session, such as session cookie name (`session_id`),
session cookie value, expiration date, duration, etc.
The duration of the cookie is configurable by Tower Configuration setting `SESSION_COOKIE_AGE` under
category `authentication`. It is an integer denoting the number of seconds the session cookie should
2019-09-17 22:49:01 +03:00
live. The default session cookie age is two weeks.
2019-09-17 22:49:01 +03:00
After a valid session is acquired, a client should provide the `session_id` as a cookie for subsequent requests
2018-02-23 20:42:44 +03:00
in order to be authenticated. For example:
```
Cookie: sessionid=lwan8l5ynhrqvps280rg5upp7n3yp6ds; ...
```
2019-09-17 22:49:01 +03:00
User should use the `/api/logout/` endpoint to log out. In the API browser, a logged-in user can do that by
simply clicking logout button on the nav bar. Under the hood, the click issues a GET to `/api/logout/`.
Upon success, the server will invalidate the current session and the response header will indicate for the client
to delete the session cookie. The user should no longer try using this invalid session.
2019-09-17 22:49:01 +03:00
The duration of a session is constant. However, a user can extend the expiration date of a valid session
by performing session acquire with the session provided.
A Tower configuration setting, `SESSIONS_PER_USER` under category `authentication`, is used to set the
maximum number of valid sessions a user can have at the same time. For example, if `SESSIONS_PER_USER`
2019-09-17 22:49:01 +03:00
is set to three and the same user is logged in from five different places, the earliest two sessions created will be invalidated. Tower will try
broadcasting, via websocket, to all available clients. The websocket message body will contain a list of
invalidated sessions. If a client finds its session in that list, it should try logging out.
2019-09-17 22:49:01 +03:00
Unlike tokens, sessions are meant to be short-lived and UI-only; therefore, whenever a user's password
is updated, all sessions she owned will be invalidated and deleted.
2019-09-17 22:49:01 +03:00
## Acceptance Criteria
2019-09-17 22:49:01 +03:00
* Users should be able to log in via the `/api/login/` endpoint by correctly providing all necessary fields.
* Logged-in users should be able to authenticate themselves by providing correct session auth info.
* Logged-in users should be able to log out via `/api/logout/`.
* The duration of a session cookie should be configurable by `SESSION_COOKIE_AGE`.
* The maximum number of concurrent login for one user should be configurable by `SESSIONS_PER_USER`,
and over-limit user sessions should be warned by websocket.
* When a user's password is changed, all her sessions should be invalidated and deleted.
2019-09-17 22:49:01 +03:00
* User should not be able to authenticate by HTTPS(S) request nor websocket connection using invalid
sessions.
2019-09-17 22:49:01 +03:00
* No existing behavior, like job runs, inventory updates or callback receiver, should be affected
by session auth.