| 1 |
import { getCSRFToken } from './django' |
| 2 |
|
| 3 |
export const Client = Object.freeze({ |
| 4 |
APP: 'app', |
| 5 |
BROWSER: 'browser' |
| 6 |
}) |
| 7 |
|
| 8 |
export const settings = { |
| 9 |
client: Client.BROWSER, |
| 10 |
baseUrl: `/_allauth/${Client.BROWSER}/v1`, |
| 11 |
withCredentials: false |
| 12 |
} |
| 13 |
|
| 14 |
const ACCEPT_JSON = { |
| 15 |
accept: 'application/json' |
| 16 |
} |
| 17 |
|
| 18 |
export const AuthProcess = Object.freeze({ |
| 19 |
LOGIN: 'login', |
| 20 |
CONNECT: 'connect' |
| 21 |
}) |
| 22 |
|
| 23 |
export const Flows = Object.freeze({ |
| 24 |
LOGIN: 'login', |
| 25 |
LOGIN_BY_CODE: 'login_by_code', |
| 26 |
MFA_AUTHENTICATE: 'mfa_authenticate', |
| 27 |
MFA_REAUTHENTICATE: 'mfa_reauthenticate', |
| 28 |
MFA_TRUST: 'mfa_trust', |
| 29 |
MFA_WEBAUTHN_SIGNUP: 'mfa_signup_webauthn', |
| 30 |
PASSWORD_RESET_BY_CODE: 'password_reset_by_code', |
| 31 |
PROVIDER_REDIRECT: 'provider_redirect', |
| 32 |
PROVIDER_SIGNUP: 'provider_signup', |
| 33 |
REAUTHENTICATE: 'reauthenticate', |
| 34 |
SIGNUP: 'signup', |
| 35 |
VERIFY_EMAIL: 'verify_email', |
| 36 |
}) |
| 37 |
|
| 38 |
export const URLs = Object.freeze({ |
| 39 |
// Meta |
| 40 |
CONFIG: '/config', |
| 41 |
|
| 42 |
// Account management |
| 43 |
CHANGE_PASSWORD: '/account/password/change', |
| 44 |
EMAIL: '/account/email', |
| 45 |
PROVIDERS: '/account/providers', |
| 46 |
|
| 47 |
// Account management: 2FA |
| 48 |
AUTHENTICATORS: '/account/authenticators', |
| 49 |
RECOVERY_CODES: '/account/authenticators/recovery-codes', |
| 50 |
TOTP_AUTHENTICATOR: '/account/authenticators/totp', |
| 51 |
|
| 52 |
// Auth: Basics |
| 53 |
LOGIN: '/auth/login', |
| 54 |
REQUEST_LOGIN_CODE: '/auth/code/request', |
| 55 |
CONFIRM_LOGIN_CODE: '/auth/code/confirm', |
| 56 |
SESSION: '/auth/session', |
| 57 |
REAUTHENTICATE: '/auth/reauthenticate', |
| 58 |
REQUEST_PASSWORD_RESET: '/auth/password/request', |
| 59 |
RESET_PASSWORD: '/auth/password/reset', |
| 60 |
SIGNUP: '/auth/signup', |
| 61 |
VERIFY_EMAIL: '/auth/email/verify', |
| 62 |
|
| 63 |
// Auth: 2FA |
| 64 |
MFA_AUTHENTICATE: '/auth/2fa/authenticate', |
| 65 |
MFA_REAUTHENTICATE: '/auth/2fa/reauthenticate', |
| 66 |
MFA_TRUST: '/auth/2fa/trust', |
| 67 |
|
| 68 |
// Auth: Social |
| 69 |
PROVIDER_SIGNUP: '/auth/provider/signup', |
| 70 |
REDIRECT_TO_PROVIDER: '/auth/provider/redirect', |
| 71 |
PROVIDER_TOKEN: '/auth/provider/token', |
| 72 |
|
| 73 |
// Auth: Sessions |
| 74 |
SESSIONS: '/auth/sessions', |
| 75 |
|
| 76 |
// Auth: WebAuthn |
| 77 |
REAUTHENTICATE_WEBAUTHN: '/auth/webauthn/reauthenticate', |
| 78 |
AUTHENTICATE_WEBAUTHN: '/auth/webauthn/authenticate', |
| 79 |
LOGIN_WEBAUTHN: '/auth/webauthn/login', |
| 80 |
SIGNUP_WEBAUTHN: '/auth/webauthn/signup', |
| 81 |
WEBAUTHN_AUTHENTICATOR: '/account/authenticators/webauthn' |
| 82 |
}) |
| 83 |
|
| 84 |
export const AuthenticatorType = Object.freeze({ |
| 85 |
TOTP: 'totp', |
| 86 |
RECOVERY_CODES: 'recovery_codes', |
| 87 |
WEBAUTHN: 'webauthn' |
| 88 |
}) |
| 89 |
|
| 90 |
function postForm (action, data) { |
| 91 |
const f = document.createElement('form') |
| 92 |
f.method = 'POST' |
| 93 |
f.action = settings.baseUrl + action |
| 94 |
|
| 95 |
for (const key in data) { |
| 96 |
const d = document.createElement('input') |
| 97 |
d.type = 'hidden' |
| 98 |
d.name = key |
| 99 |
d.value = data[key] |
| 100 |
f.appendChild(d) |
| 101 |
} |
| 102 |
document.body.appendChild(f) |
| 103 |
f.submit() |
| 104 |
} |
| 105 |
|
| 106 |
const tokenStorage = window.sessionStorage |
| 107 |
|
| 108 |
export function getSessionToken () { |
| 109 |
return tokenStorage.getItem('sessionToken') |
| 110 |
} |
| 111 |
|
| 112 |
async function request (method, path, data, headers) { |
| 113 |
const options = { |
| 114 |
method, |
| 115 |
headers: { |
| 116 |
...ACCEPT_JSON, |
| 117 |
...headers |
| 118 |
} |
| 119 |
} |
| 120 |
if (settings.withCredentials) { |
| 121 |
options.credentials = 'include' |
| 122 |
} |
| 123 |
// Don't pass along authentication related headers to the config endpoint. |
| 124 |
if (path !== URLs.CONFIG) { |
| 125 |
if (settings.client === Client.BROWSER) { |
| 126 |
options.headers['X-CSRFToken'] = getCSRFToken() |
| 127 |
} else if (settings.client === Client.APP) { |
| 128 |
// IMPORTANT!: Do NOT use `Client.APP` in a browser context, as you will |
| 129 |
// be vulnerable to CSRF attacks. This logic is only here for |
| 130 |
// development/demonstration/testing purposes... |
| 131 |
options.headers['User-Agent'] = 'django-allauth example app' |
| 132 |
const sessionToken = getSessionToken() |
| 133 |
if (sessionToken) { |
| 134 |
options.headers['X-Session-Token'] = sessionToken |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if (typeof data !== 'undefined') { |
| 140 |
options.body = JSON.stringify(data) |
| 141 |
options.headers['Content-Type'] = 'application/json' |
| 142 |
} |
| 143 |
const resp = await fetch(settings.baseUrl + path, options) |
| 144 |
const msg = await resp.json() |
| 145 |
if (msg.status === 410) { |
| 146 |
tokenStorage.removeItem('sessionToken') |
| 147 |
} |
| 148 |
if (msg.meta?.session_token) { |
| 149 |
tokenStorage.setItem('sessionToken', msg.meta.session_token) |
| 150 |
} |
| 151 |
if ([401, 410].includes(msg.status) || (msg.status === 200 && msg.meta?.is_authenticated)) { |
| 152 |
const event = new CustomEvent('allauth.auth.change', { detail: msg }) |
| 153 |
document.dispatchEvent(event) |
| 154 |
} |
| 155 |
return msg |
| 156 |
} |
| 157 |
|
| 158 |
export async function login (data) { |
| 159 |
return await request('POST', URLs.LOGIN, data) |
| 160 |
} |
| 161 |
|
| 162 |
export async function reauthenticate (data) { |
| 163 |
return await request('POST', URLs.REAUTHENTICATE, data) |
| 164 |
} |
| 165 |
|
| 166 |
export async function logout () { |
| 167 |
return await request('DELETE', URLs.SESSION) |
| 168 |
} |
| 169 |
|
| 170 |
export async function signUp (data) { |
| 171 |
return await request('POST', URLs.SIGNUP, data) |
| 172 |
} |
| 173 |
|
| 174 |
export async function signUpByPasskey (data) { |
| 175 |
return await request('POST', URLs.SIGNUP_WEBAUTHN, data) |
| 176 |
} |
| 177 |
|
| 178 |
export async function providerSignup (data) { |
| 179 |
return await request('POST', URLs.PROVIDER_SIGNUP, data) |
| 180 |
} |
| 181 |
|
| 182 |
export async function getProviderAccounts () { |
| 183 |
return await request('GET', URLs.PROVIDERS) |
| 184 |
} |
| 185 |
|
| 186 |
export async function disconnectProviderAccount (providerId, accountUid) { |
| 187 |
return await request('DELETE', URLs.PROVIDERS, { provider: providerId, account: accountUid }) |
| 188 |
} |
| 189 |
|
| 190 |
export async function requestPasswordReset (email) { |
| 191 |
return await request('POST', URLs.REQUEST_PASSWORD_RESET, { email }) |
| 192 |
} |
| 193 |
|
| 194 |
export async function requestLoginCode (email) { |
| 195 |
return await request('POST', URLs.REQUEST_LOGIN_CODE, { email }) |
| 196 |
} |
| 197 |
|
| 198 |
export async function confirmLoginCode (code) { |
| 199 |
return await request('POST', URLs.CONFIRM_LOGIN_CODE, { code }) |
| 200 |
} |
| 201 |
|
| 202 |
export async function getEmailVerification (key) { |
| 203 |
return await request('GET', URLs.VERIFY_EMAIL, undefined, { 'X-Email-Verification-Key': key }) |
| 204 |
} |
| 205 |
|
| 206 |
export async function getEmailAddresses () { |
| 207 |
return await request('GET', URLs.EMAIL) |
| 208 |
} |
| 209 |
export async function getSessions () { |
| 210 |
return await request('GET', URLs.SESSIONS) |
| 211 |
} |
| 212 |
|
| 213 |
export async function endSessions (ids) { |
| 214 |
return await request('DELETE', URLs.SESSIONS, { sessions: ids }) |
| 215 |
} |
| 216 |
|
| 217 |
export async function getAuthenticators () { |
| 218 |
return await request('GET', URLs.AUTHENTICATORS) |
| 219 |
} |
| 220 |
|
| 221 |
export async function getTOTPAuthenticator () { |
| 222 |
return await request('GET', URLs.TOTP_AUTHENTICATOR) |
| 223 |
} |
| 224 |
|
| 225 |
export async function mfaAuthenticate (code) { |
| 226 |
return await request('POST', URLs.MFA_AUTHENTICATE, { code }) |
| 227 |
} |
| 228 |
|
| 229 |
export async function mfaReauthenticate (code) { |
| 230 |
return await request('POST', URLs.MFA_REAUTHENTICATE, { code }) |
| 231 |
} |
| 232 |
|
| 233 |
export async function mfaTrust (trust) { |
| 234 |
return await request('POST', URLs.MFA_TRUST, { trust }) |
| 235 |
} |
| 236 |
|
| 237 |
export async function activateTOTPAuthenticator (code) { |
| 238 |
return await request('POST', URLs.TOTP_AUTHENTICATOR, { code }) |
| 239 |
} |
| 240 |
|
| 241 |
export async function deactivateTOTPAuthenticator () { |
| 242 |
return await request('DELETE', URLs.TOTP_AUTHENTICATOR) |
| 243 |
} |
| 244 |
|
| 245 |
export async function getRecoveryCodes () { |
| 246 |
return await request('GET', URLs.RECOVERY_CODES) |
| 247 |
} |
| 248 |
|
| 249 |
export async function generateRecoveryCodes () { |
| 250 |
return await request('POST', URLs.RECOVERY_CODES) |
| 251 |
} |
| 252 |
|
| 253 |
export async function getConfig () { |
| 254 |
return await request('GET', URLs.CONFIG) |
| 255 |
} |
| 256 |
|
| 257 |
export async function addEmail (email) { |
| 258 |
return await request('POST', URLs.EMAIL, { email }) |
| 259 |
} |
| 260 |
|
| 261 |
export async function deleteEmail (email) { |
| 262 |
return await request('DELETE', URLs.EMAIL, { email }) |
| 263 |
} |
| 264 |
|
| 265 |
export async function markEmailAsPrimary (email) { |
| 266 |
return await request('PATCH', URLs.EMAIL, { email, primary: true }) |
| 267 |
} |
| 268 |
|
| 269 |
export async function requestEmailVerification (email) { |
| 270 |
return await request('PUT', URLs.EMAIL, { email }) |
| 271 |
} |
| 272 |
|
| 273 |
export async function verifyEmail (key) { |
| 274 |
return await request('POST', URLs.VERIFY_EMAIL, { key }) |
| 275 |
} |
| 276 |
|
| 277 |
export async function getPasswordReset (key) { |
| 278 |
return await request('GET', URLs.RESET_PASSWORD, undefined, { 'X-Password-Reset-Key': key }) |
| 279 |
} |
| 280 |
|
| 281 |
export async function resetPassword (data) { |
| 282 |
return await request('POST', URLs.RESET_PASSWORD, data) |
| 283 |
} |
| 284 |
|
| 285 |
export async function changePassword (data) { |
| 286 |
return await request('POST', URLs.CHANGE_PASSWORD, data) |
| 287 |
} |
| 288 |
|
| 289 |
export async function getAuth () { |
| 290 |
return await request('GET', URLs.SESSION) |
| 291 |
} |
| 292 |
|
| 293 |
export async function authenticateByToken (providerId, token, process = AuthProcess.LOGIN) { |
| 294 |
return await request('POST', URLs.PROVIDER_TOKEN, { |
| 295 |
provider: providerId, |
| 296 |
token, |
| 297 |
process |
| 298 |
} |
| 299 |
) |
| 300 |
} |
| 301 |
|
| 302 |
export function redirectToProvider (providerId, callbackURL, process = AuthProcess.LOGIN) { |
| 303 |
postForm(URLs.REDIRECT_TO_PROVIDER, { |
| 304 |
provider: providerId, |
| 305 |
process, |
| 306 |
callback_url: window.location.protocol + '//' + window.location.host + callbackURL, |
| 307 |
csrfmiddlewaretoken: getCSRFToken() |
| 308 |
}) |
| 309 |
} |
| 310 |
|
| 311 |
export async function getWebAuthnCreateOptions (passwordless) { |
| 312 |
let url = URLs.WEBAUTHN_AUTHENTICATOR |
| 313 |
if (passwordless) { |
| 314 |
url += '?passwordless' |
| 315 |
} |
| 316 |
return await request('GET', url) |
| 317 |
} |
| 318 |
|
| 319 |
export async function getWebAuthnCreateOptionsAtSignup () { |
| 320 |
return await request('GET', URLs.SIGNUP_WEBAUTHN) |
| 321 |
} |
| 322 |
|
| 323 |
export async function addWebAuthnCredential (name, credential) { |
| 324 |
return await request('POST', URLs.WEBAUTHN_AUTHENTICATOR, { |
| 325 |
name, |
| 326 |
credential |
| 327 |
}) |
| 328 |
} |
| 329 |
|
| 330 |
export async function signupWebAuthnCredential (name, credential) { |
| 331 |
return await request('PUT', URLs.SIGNUP_WEBAUTHN, { |
| 332 |
name, |
| 333 |
credential |
| 334 |
}) |
| 335 |
} |
| 336 |
|
| 337 |
export async function deleteWebAuthnCredential (ids) { |
| 338 |
return await request('DELETE', URLs.WEBAUTHN_AUTHENTICATOR, { authenticators: ids }) |
| 339 |
} |
| 340 |
|
| 341 |
export async function updateWebAuthnCredential (id, data) { |
| 342 |
return await request('PUT', URLs.WEBAUTHN_AUTHENTICATOR, { id, ...data }) |
| 343 |
} |
| 344 |
|
| 345 |
export async function getWebAuthnRequestOptionsForReauthentication () { |
| 346 |
return await request('GET', URLs.REAUTHENTICATE_WEBAUTHN) |
| 347 |
} |
| 348 |
|
| 349 |
export async function reauthenticateUsingWebAuthn (credential) { |
| 350 |
return await request('POST', URLs.REAUTHENTICATE_WEBAUTHN, { credential }) |
| 351 |
} |
| 352 |
|
| 353 |
export async function authenticateUsingWebAuthn (credential) { |
| 354 |
return await request('POST', URLs.AUTHENTICATE_WEBAUTHN, { credential }) |
| 355 |
} |
| 356 |
|
| 357 |
export async function loginUsingWebAuthn (credential) { |
| 358 |
return await request('POST', URLs.LOGIN_WEBAUTHN, { credential }) |
| 359 |
} |
| 360 |
|
| 361 |
export async function getWebAuthnRequestOptionsForLogin () { |
| 362 |
return await request('GET', URLs.LOGIN_WEBAUTHN) |
| 363 |
} |
| 364 |
|
| 365 |
export async function getWebAuthnRequestOptionsForAuthentication () { |
| 366 |
return await request('GET', URLs.AUTHENTICATE_WEBAUTHN) |
| 367 |
} |
| 368 |
|
| 369 |
export function setup (client, baseUrl, withCredentials) { |
| 370 |
settings.client = client |
| 371 |
settings.baseUrl = baseUrl |
| 372 |
settings.withCredentials = withCredentials |
| 373 |
} |