UNPKG

@adonisjs/auth

Version:

Official authentication provider for Adonis framework

130 lines (129 loc) 4.28 kB
import { n as E_UNAUTHORIZED_ACCESS } from "./errors-sGy-K8pd.js"; import { t as debug_default } from "./debug-Ckko95-M.js"; import { RuntimeException } from "@adonisjs/core/exceptions"; import { HttpContextFactory } from "@adonisjs/core/factories/http"; var Authenticator = class { #config; #guardsCache = {}; #authenticationAttemptedViaGuard; #authenticatedViaGuard; #ctx; get defaultGuard() { return this.#config.default; } get authenticatedViaGuard() { return this.#authenticatedViaGuard; } get isAuthenticated() { if (!this.#authenticationAttemptedViaGuard) return false; return this.use(this.#authenticationAttemptedViaGuard).isAuthenticated; } get user() { if (!this.#authenticationAttemptedViaGuard) return; return this.use(this.#authenticationAttemptedViaGuard).user; } get authenticationAttempted() { if (!this.#authenticationAttemptedViaGuard) return false; return this.use(this.#authenticationAttemptedViaGuard).authenticationAttempted; } constructor(ctx, config) { this.#ctx = ctx; this.#config = config; debug_default("creating authenticator. config %O", this.#config); } getUserOrFail() { if (!this.#authenticationAttemptedViaGuard) throw new RuntimeException("Cannot access authenticated user. Please call \"auth.authenticate\" method first."); return this.use(this.#authenticationAttemptedViaGuard).getUserOrFail(); } use(guard) { const guardToUse = guard || this.#config.default; const cachedGuard = this.#guardsCache[guardToUse]; if (cachedGuard) { debug_default("authenticator: using guard from cache. name: \"%s\"", guardToUse); return cachedGuard; } const guardFactory = this.#config.guards[guardToUse]; debug_default("authenticator: creating guard. name: \"%s\"", guardToUse); const guardInstance = guardFactory(this.#ctx); this.#guardsCache[guardToUse] = guardInstance; return guardInstance; } async authenticate() { await this.authenticateUsing(); return this.getUserOrFail(); } async check() { this.#authenticationAttemptedViaGuard = this.defaultGuard; const isAuthenticated = await this.use().check(); if (isAuthenticated) this.#authenticatedViaGuard = this.defaultGuard; return isAuthenticated; } async authenticateUsing(guards, options) { const guardsToUse = guards || [this.defaultGuard]; let lastUsedDriver; for (let guardName of guardsToUse) { debug_default("attempting to authenticate using guard \"%s\"", guardName); this.#authenticationAttemptedViaGuard = guardName; const guard = this.use(guardName); lastUsedDriver = guard.driverName; if (await guard.check()) { this.#authenticatedViaGuard = guardName; return this.getUserOrFail(); } } throw new E_UNAUTHORIZED_ACCESS("Unauthorized access", { guardDriverName: lastUsedDriver, redirectTo: options?.loginRoute }); } async checkUsing(guards = [this.defaultGuard]) { for (const name of guards) { this.#authenticationAttemptedViaGuard = name; if (await this.use(name).check()) { this.#authenticatedViaGuard = name; return true; } } return false; } }; var AuthenticatorClient = class { #config; #guardsCache = {}; get defaultGuard() { return this.#config.default; } constructor(config) { this.#config = config; debug_default("creating authenticator client. config %O", this.#config); } use(guard) { const guardToUse = guard || this.#config.default; const cachedGuard = this.#guardsCache[guardToUse]; if (cachedGuard) { debug_default("authenticator client: using guard from cache. name: \"%s\"", guardToUse); return cachedGuard; } const guardFactory = this.#config.guards[guardToUse]; debug_default("authenticator client: creating guard. name: \"%s\"", guardToUse); const guardInstance = guardFactory(new HttpContextFactory().create()); this.#guardsCache[guardToUse] = guardInstance; return guardInstance; } }; var AuthManager = class { get defaultGuard() { return this.config.default; } constructor(config) { this.config = config; this.config = config; } createAuthenticator(ctx) { return new Authenticator(ctx, this.config); } createAuthenticatorClient() { return new AuthenticatorClient(this.config); } }; export { AuthenticatorClient as n, Authenticator as r, AuthManager as t };