| 1 |
# Introduction |
| 2 |
|
| 3 |
Welcome to the django-allauth API specification. This API is intended to be |
| 4 |
consumed by two different kind of clients: |
| 5 |
|
| 6 |
- Web applications running in a **browser** context. For example, a |
| 7 |
single-page React application, to which the end user can navigate using a web |
| 8 |
browser. |
| 9 |
|
| 10 |
- Applications, **apps** for short, executing in non-browser contexts. For example, |
| 11 |
a mobile Android or iOS application. |
| 12 |
|
| 13 |
The security considerations for these two usage types are different. In a |
| 14 |
browser context, cookies play a role. Without taking special precautions, your |
| 15 |
web application may be vulnerable to Cross-Site Request Forgery attacks. For |
| 16 |
mobile applications, this does not apply. |
| 17 |
|
| 18 |
The API can be used for both use cases. Differences in handling of security is |
| 19 |
automatically adjusted for, based on the request path used to make the API call. |
| 20 |
For example, signing up can either be done using the |
| 21 |
`/_allauth/browser/v1/auth/signup` or the `/_allauth/app/v1/auth/signup` |
| 22 |
endpoint. For the **browser** usage, session cookies and CSRF protection |
| 23 |
applies. For the **app** usage, cookies play no role, instead, a session token |
| 24 |
is used. The paths of all endpoints are documented in the form of |
| 25 |
`/_allauth/{client}/v1/auth/signup`. Depending on the client type (`{client}`), |
| 26 |
there may be slight differences in request/response handling. This is |
| 27 |
documented where applicable. |
| 28 |
|
| 29 |
|
| 30 |
# Scope |
| 31 |
|
| 32 |
The following functionality is all in scope and handled as part of this API: |
| 33 |
|
| 34 |
- Regular accounts: |
| 35 |
- Login |
| 36 |
- Signup |
| 37 |
- Password forgotten |
| 38 |
- Manage email (add, remove, verify, select a different primary) |
| 39 |
- Change password. |
| 40 |
- Verification of email addresses. |
| 41 |
- Two-Factor Authentication: |
| 42 |
- Authentication using an authenticator code |
| 43 |
- (De)activate TOTP |
| 44 |
- (Re)generate recovery codes |
| 45 |
- "Trust this browser" |
| 46 |
- Third-party providers: |
| 47 |
- Authenticate by performing a browser-level redirect (synchronous request). |
| 48 |
- Authenticate by means of a provider token. |
| 49 |
- Connect additional provider accounts. |
| 50 |
- Disconnect existing provider accounts. |
| 51 |
- Setting a password in case no password was set, yet. |
| 52 |
- Querying additional information before signing up. |
| 53 |
- Session management: |
| 54 |
- Listing all sessions for a user. |
| 55 |
- Signing out of any of those sessions. |
| 56 |
|
| 57 |
|
| 58 |
# Browser Usage |
| 59 |
|
| 60 |
For web applications running in a browser, routing needs to be setup correctly |
| 61 |
such that the sessions initiated at the backend are accessible in the frontend. |
| 62 |
|
| 63 |
## Routing |
| 64 |
|
| 65 |
When using the API in a browser context, regular Django sessions are used, along |
| 66 |
with the usual session cookies. There are several options for setting up the |
| 67 |
routing of your application. |
| 68 |
|
| 69 |
|
| 70 |
### Single Domain Routing |
| 71 |
|
| 72 |
With single domain, path-based routing, both your frontend and backend are |
| 73 |
served from the same domain, for example `https://app.org`. You will have to |
| 74 |
make sure that some paths are served by the frontend, and others by the backend. |
| 75 |
|
| 76 |
|
| 77 |
### Sub-domain Routing |
| 78 |
|
| 79 |
With sub-domain based routing, the frontend and backend are served from |
| 80 |
different domains. However, as session cookies are used, these different |
| 81 |
domains must share common main domain. |
| 82 |
|
| 83 |
For example, you may use `app.project.org` for the frontend, which |
| 84 |
interfaces with the backend over at `backend.project.org`. In this |
| 85 |
setup, Django will need to be configured with: |
| 86 |
|
| 87 |
``` |
| 88 |
SESSION_COOKIE_DOMAIN = "project.org" |
| 89 |
CSRF_COOKIE_DOMAIN = "project.org" |
| 90 |
``` |
| 91 |
|
| 92 |
If your organization hosts unrelated applications, for example, a CMS for |
| 93 |
marketing purposes, on the top level domain (`project.org`), it is not advisable |
| 94 |
to set the session cookie domain to `project.org`, as those other applications |
| 95 |
could get access to the session cookie. In that case, it is advised to use |
| 96 |
`backend.app.project.org` for the backend, and set the session cookie domain to |
| 97 |
`app.project.org`. |
| 98 |
|
| 99 |
|
| 100 |
# App Usage |
| 101 |
|
| 102 |
For app based usage, cookies play no role, yet, sessions are still used. When a |
| 103 |
user walks through the authentication flow, a session is created. Having an |
| 104 |
authenticated session is proof that the user is allowed to further interact with |
| 105 |
the backend. Unauthenticated sessions are also needed to remember state while |
| 106 |
the user proceeds to go over the required steps necessary to authenticate. |
| 107 |
|
| 108 |
|
| 109 |
## Session Tokens |
| 110 |
|
| 111 |
Given that there is no cookie to point to the session, the header |
| 112 |
`X-Session-Token` is used instead. The way of working is as follows: |
| 113 |
|
| 114 |
- If you do not have a session token yet, do not send the `X-Session-Token` header. |
| 115 |
|
| 116 |
- When making requests, session tokens can appear in the metadata |
| 117 |
(`meta.session_token`) of authentication related responses. If a session |
| 118 |
token appears, store it (overwriting any previous session token), and ensure |
| 119 |
to add the token to the `X-Session-Token` header of all subsequent requests. |
| 120 |
|
| 121 |
- When receiving an authentication related response with status code 410 |
| 122 |
(`Gone`), that is meant to indicate that the session is no longer valid. |
| 123 |
Remove the session token and start clean. |
| 124 |
|
| 125 |
|
| 126 |
## Access Tokens |
| 127 |
|
| 128 |
While session tokens are required to handle the authentication process, |
| 129 |
depending on your requirements, a different type of token may be needed once |
| 130 |
authenticated. |
| 131 |
|
| 132 |
For example, your app likely needs access to other APIs as well. These APIs may |
| 133 |
even be implemented using different technologies, in which case having a |
| 134 |
stateless token, possibly a JWT encoding the user ID, might be a good fit. |
| 135 |
|
| 136 |
In this API and its implementation no assumptions, and no (limiting) design |
| 137 |
decisions are made in this regard. The token strategy of django-allauth is |
| 138 |
pluggable, such that you can expose your own access token when the user |
| 139 |
authenticates. As for as the API specification is concerned, the access token |
| 140 |
will appear in the response of metadata (`meta.access_token`) of a successful |
| 141 |
authentication request. How you can customize the token strategy can be found |
| 142 |
over at the documentation of the `allauth.headless` Django application. |
| 143 |
|
| 144 |
|
| 145 |
# Responses |
| 146 |
|
| 147 |
Unless documented otherwise, responses are objects with the following |
| 148 |
properties: |
| 149 |
- The `status`, matching the HTTP status code. |
| 150 |
- Data, if any, is returned as part of the `data` key. |
| 151 |
- Metadata, if any, is returned as part of the `meta` key. |
| 152 |
- Errors, if any, are listed in the `errors` key. |
| 153 |
|
| 154 |
|
| 155 |
# Authentication Flows |
| 156 |
|
| 157 |
In order to become authenticated, the user must complete a flow, potentially |
| 158 |
consisting of several steps. For example: |
| 159 |
- A login, after which the user is authenticated. |
| 160 |
- A Login, followed by two-factor authentication, after which the user is |
| 161 |
authenticated. |
| 162 |
- A signup, followed by mandatory email verification, after which the user is |
| 163 |
authenticated. |
| 164 |
|
| 165 |
The API signals to the client that (re)authentication is required by means of a |
| 166 |
`401` or `410` status code: |
| 167 |
- Not authenticated: status `401`. |
| 168 |
- Re-authentication required: status `401`, with `meta.is_authenticated = true`. |
| 169 |
- Invalid session: status `410`. This only occurs for clients of type `app`. |
| 170 |
|
| 171 |
All authentication related responses have status `401` or `410`, and, |
| 172 |
`meta.is_authenticated` indicating whether authentication, or re-authentication |
| 173 |
is required. |
| 174 |
|
| 175 |
The flows the client can perform to initiate or complete the authentication are |
| 176 |
communicates as part of authentication related responses. The authentication can |
| 177 |
be initiated by means of these flows: |
| 178 |
- Login using a local account (`login`). |
| 179 |
- Signup for a local account (`signup`). |
| 180 |
- Login or signup using the third-party provider redirect flow (`provider_redirect`). |
| 181 |
- Login or signup by handing over a third-party provider retrieved elsewhere (`provider_token`). |
| 182 |
- Login using a special code (`login_by_code`). |
| 183 |
- Login using a passkey (`mfa_login_webauthn`). |
| 184 |
- Signup using a passkey (`mfa_signup_webauthn`). |
| 185 |
|
| 186 |
Depending on the state of the account, and the configuration of django-allauth, the flows above |
| 187 |
can either lead to becoming directly authenticated, or, to followup flows: |
| 188 |
- Provider signup (`provider_signup`). |
| 189 |
- Email verification (`verify_email`). |
| 190 |
- Phone verification (`phone_email`). |
| 191 |
- Two-factor authentication required (TOTP, recovery codes, or WebAuthn) (`mfa_authenticate`). |
| 192 |
- Trust this browser (`mfa_trust`). |
| 193 |
|
| 194 |
While authenticated, re-authentication may be required to safeguard the account when sensitive actions |
| 195 |
are performed. The re-authentication flows are the following: |
| 196 |
- Re-authenticate using password (`reauthenticate`). |
| 197 |
- Re-authenticate using a 2FA authenticator (TOTP, recovery codes, or WebAuthn) (`mfa_reauthenticate`). |
| 198 |
|
| 199 |
|
| 200 |
# Security Considerations |
| 201 |
|
| 202 |
## Input Sanitization |
| 203 |
|
| 204 |
The Django framework, by design, does *not* perform input sanitization. For |
| 205 |
example, there is nothing preventing end users from signing up using `<script>` |
| 206 |
or `Robert'); DROP TABLE students` as a first name. Django relies on its |
| 207 |
template language for proper escaping of such values and mitigate any XSS |
| 208 |
attacks. |
| 209 |
|
| 210 |
As a result, any `allauth.headless` client **must** have proper XSS protection |
| 211 |
in place as well. Be prepared that, for example, the WebAuthn endpoints could |
| 212 |
return authenticator names as follows: |
| 213 |
|
| 214 |
{ |
| 215 |
"name": "<script>alert(1)</script>", |
| 216 |
"credential": { |
| 217 |
"type": "public-key", |
| 218 |
... |
| 219 |
} |
| 220 |
} |