UNPKG

@adonisjs/auth

Version:

Official authentication provider for Adonis framework

116 lines (115 loc) 3.67 kB
import { n as E_UNAUTHORIZED_ACCESS } from "../../errors-sGy-K8pd.js"; import "../../symbols-BQLDWwuQ.js"; import { RuntimeException } from "@adonisjs/core/exceptions"; import { base64 } from "@adonisjs/core/helpers"; import auth from "basic-auth"; var BasicAuthGuard = class { #name; #ctx; #userProvider; #emitter; driverName = "basic_auth"; authenticationAttempted = false; isAuthenticated = false; user; constructor(name, ctx, emitter, userProvider) { this.#name = name; this.#ctx = ctx; this.#emitter = emitter; this.#userProvider = userProvider; } #authenticationFailed() { this.isAuthenticated = false; this.user = void 0; const error = new E_UNAUTHORIZED_ACCESS("Invalid basic auth credentials", { guardDriverName: this.driverName }); this.#emitter.emit("basic_auth:authentication_failed", { ctx: this.#ctx, guardName: this.#name, error }); return error; } #authenticationSucceeded(user) { this.isAuthenticated = true; this.user = user; this.#emitter.emit("basic_auth:authentication_succeeded", { ctx: this.#ctx, guardName: this.#name, user }); } getUserOrFail() { if (!this.user) throw new E_UNAUTHORIZED_ACCESS("Invalid basic auth credentials", { guardDriverName: this.driverName }); return this.user; } async authenticate() { if (this.authenticationAttempted) return this.getUserOrFail(); this.authenticationAttempted = true; this.#emitter.emit("basic_auth:authentication_attempted", { ctx: this.#ctx, guardName: this.#name }); const credentials = auth(this.#ctx.request.request); if (!credentials) throw this.#authenticationFailed(); const user = await this.#userProvider.verifyCredentials(credentials.name, credentials.pass); if (!user) throw this.#authenticationFailed(); this.#authenticationSucceeded(user.getOriginal()); return this.getUserOrFail(); } async check() { try { await this.authenticate(); return true; } catch (error) { if (error instanceof E_UNAUTHORIZED_ACCESS) return false; throw error; } } async authenticateAsClient(uid, password) { return { headers: { authorization: `Basic ${base64.encode(`${uid}:${password}`)}` } }; } }; var BasicAuthLucidUserProvider = class { model; constructor(options) { this.options = options; } async getModel() { if (this.model && !("hot" in import.meta)) return this.model; this.model = (await this.options.model()).default; return this.model; } async createUserForGuard(user) { const model = await this.getModel(); if (user instanceof model === false) throw new RuntimeException(`Invalid user object. It must be an instance of the "${model.name}" model`); return { getId() { if (!user.$primaryKeyValue) throw new RuntimeException(`Cannot use "${model.name}" model for authentication. The value of column "${model.primaryKey}" is undefined or null`); return user.$primaryKeyValue; }, getOriginal() { return user; } }; } async verifyCredentials(uid, password) { const model = await this.getModel(); try { const user = await model.verifyCredentials(uid, password); return this.createUserForGuard(user); } catch { return null; } } }; function basicAuthGuard(config) { return { async resolver(name, app) { const emitter = await app.container.make("emitter"); const provider = "resolver" in config.provider ? await config.provider.resolver(app) : config.provider; return (ctx) => new BasicAuthGuard(name, ctx, emitter, provider); } }; } function basicAuthUserProvider(config) { return new BasicAuthLucidUserProvider(config); } export { BasicAuthGuard, BasicAuthLucidUserProvider, basicAuthGuard, basicAuthUserProvider };