@adonisjs/auth 10.0.0-next.2 → 10.0.0-next.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,129 @@
1
+ import { n as E_UNAUTHORIZED_ACCESS } from "./errors-BQxhZmkE.js";
2
+ import { t as debug_default } from "./debug-Ckko95-M.js";
3
+ import { RuntimeException } from "@adonisjs/core/exceptions";
4
+ import { HttpContextFactory } from "@adonisjs/core/factories/http";
5
+ var Authenticator = class {
6
+ #config;
7
+ #guardsCache = {};
8
+ #authenticationAttemptedViaGuard;
9
+ #authenticatedViaGuard;
10
+ #ctx;
11
+ get defaultGuard() {
12
+ return this.#config.default;
13
+ }
14
+ get authenticatedViaGuard() {
15
+ return this.#authenticatedViaGuard;
16
+ }
17
+ get isAuthenticated() {
18
+ if (!this.#authenticationAttemptedViaGuard) return false;
19
+ return this.use(this.#authenticationAttemptedViaGuard).isAuthenticated;
20
+ }
21
+ get user() {
22
+ if (!this.#authenticationAttemptedViaGuard) return;
23
+ return this.use(this.#authenticationAttemptedViaGuard).user;
24
+ }
25
+ get authenticationAttempted() {
26
+ if (!this.#authenticationAttemptedViaGuard) return false;
27
+ return this.use(this.#authenticationAttemptedViaGuard).authenticationAttempted;
28
+ }
29
+ constructor(ctx, config) {
30
+ this.#ctx = ctx;
31
+ this.#config = config;
32
+ debug_default("creating authenticator. config %O", this.#config);
33
+ }
34
+ getUserOrFail() {
35
+ if (!this.#authenticationAttemptedViaGuard) throw new RuntimeException("Cannot access authenticated user. Please call \"auth.authenticate\" method first.");
36
+ return this.use(this.#authenticationAttemptedViaGuard).getUserOrFail();
37
+ }
38
+ use(guard) {
39
+ const guardToUse = guard || this.#config.default;
40
+ const cachedGuard = this.#guardsCache[guardToUse];
41
+ if (cachedGuard) {
42
+ debug_default("authenticator: using guard from cache. name: \"%s\"", guardToUse);
43
+ return cachedGuard;
44
+ }
45
+ const guardFactory = this.#config.guards[guardToUse];
46
+ debug_default("authenticator: creating guard. name: \"%s\"", guardToUse);
47
+ const guardInstance = guardFactory(this.#ctx);
48
+ this.#guardsCache[guardToUse] = guardInstance;
49
+ return guardInstance;
50
+ }
51
+ async authenticate() {
52
+ await this.authenticateUsing();
53
+ return this.getUserOrFail();
54
+ }
55
+ async check() {
56
+ this.#authenticationAttemptedViaGuard = this.defaultGuard;
57
+ const isAuthenticated = await this.use().check();
58
+ if (isAuthenticated) this.#authenticatedViaGuard = this.defaultGuard;
59
+ return isAuthenticated;
60
+ }
61
+ async authenticateUsing(guards, options) {
62
+ const guardsToUse = guards || [this.defaultGuard];
63
+ let lastUsedDriver;
64
+ for (let guardName of guardsToUse) {
65
+ debug_default("attempting to authenticate using guard \"%s\"", guardName);
66
+ this.#authenticationAttemptedViaGuard = guardName;
67
+ const guard = this.use(guardName);
68
+ lastUsedDriver = guard.driverName;
69
+ if (await guard.check()) {
70
+ this.#authenticatedViaGuard = guardName;
71
+ return this.getUserOrFail();
72
+ }
73
+ }
74
+ throw new E_UNAUTHORIZED_ACCESS("Unauthorized access", {
75
+ guardDriverName: lastUsedDriver,
76
+ redirectTo: options?.loginRoute
77
+ });
78
+ }
79
+ async checkUsing(guards = [this.defaultGuard]) {
80
+ for (const name of guards) {
81
+ this.#authenticationAttemptedViaGuard = name;
82
+ if (await this.use(name).check()) {
83
+ this.#authenticatedViaGuard = name;
84
+ return true;
85
+ }
86
+ }
87
+ return false;
88
+ }
89
+ };
90
+ var AuthenticatorClient = class {
91
+ #config;
92
+ #guardsCache = {};
93
+ get defaultGuard() {
94
+ return this.#config.default;
95
+ }
96
+ constructor(config) {
97
+ this.#config = config;
98
+ debug_default("creating authenticator client. config %O", this.#config);
99
+ }
100
+ use(guard) {
101
+ const guardToUse = guard || this.#config.default;
102
+ const cachedGuard = this.#guardsCache[guardToUse];
103
+ if (cachedGuard) {
104
+ debug_default("authenticator client: using guard from cache. name: \"%s\"", guardToUse);
105
+ return cachedGuard;
106
+ }
107
+ const guardFactory = this.#config.guards[guardToUse];
108
+ debug_default("authenticator client: creating guard. name: \"%s\"", guardToUse);
109
+ const guardInstance = guardFactory(new HttpContextFactory().create());
110
+ this.#guardsCache[guardToUse] = guardInstance;
111
+ return guardInstance;
112
+ }
113
+ };
114
+ var AuthManager = class {
115
+ get defaultGuard() {
116
+ return this.config.default;
117
+ }
118
+ constructor(config) {
119
+ this.config = config;
120
+ this.config = config;
121
+ }
122
+ createAuthenticator(ctx) {
123
+ return new Authenticator(ctx, this.config);
124
+ }
125
+ createAuthenticatorClient() {
126
+ return new AuthenticatorClient(this.config);
127
+ }
128
+ };
129
+ export { AuthenticatorClient as n, Authenticator as r, AuthManager as t };
@@ -0,0 +1,3 @@
1
+ import { debuglog } from "node:util";
2
+ var debug_default = debuglog("adonisjs:auth");
3
+ export { debug_default as t };
@@ -0,0 +1,128 @@
1
+ import "node:module";
2
+ import { Exception } from "@adonisjs/core/exceptions";
3
+ var __defProp = Object.defineProperty;
4
+ var __export = (all, symbols) => {
5
+ let target = {};
6
+ for (var name in all) __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: true
9
+ });
10
+ if (symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
11
+ return target;
12
+ };
13
+ var errors_exports = /* @__PURE__ */ __export({
14
+ E_INVALID_CREDENTIALS: () => E_INVALID_CREDENTIALS,
15
+ E_UNAUTHORIZED_ACCESS: () => E_UNAUTHORIZED_ACCESS
16
+ });
17
+ const E_UNAUTHORIZED_ACCESS = class extends Exception {
18
+ static status = 401;
19
+ static code = "E_UNAUTHORIZED_ACCESS";
20
+ redirectTo;
21
+ identifier = "errors.E_UNAUTHORIZED_ACCESS";
22
+ guardDriverName;
23
+ renderers = {
24
+ session: (message, error, ctx) => {
25
+ switch (ctx.request.accepts([
26
+ "html",
27
+ "application/vnd.api+json",
28
+ "json"
29
+ ])) {
30
+ case "html":
31
+ case null:
32
+ ctx.session.flashExcept(["_csrf"]);
33
+ ctx.session.flashErrors({ [error.code]: message });
34
+ ctx.response.redirect(error.redirectTo || "/", true);
35
+ break;
36
+ case "json":
37
+ ctx.response.status(error.status).send({ errors: [{ message }] });
38
+ break;
39
+ case "application/vnd.api+json":
40
+ ctx.response.status(error.status).send({ errors: [{
41
+ code: error.code,
42
+ title: message
43
+ }] });
44
+ break;
45
+ }
46
+ },
47
+ basic_auth: (message, _, ctx) => {
48
+ ctx.response.status(this.status).header("WWW-Authenticate", `Basic realm="Authenticate", charset="UTF-8"`).send(message);
49
+ },
50
+ access_tokens: (message, error, ctx) => {
51
+ switch (ctx.request.accepts([
52
+ "html",
53
+ "application/vnd.api+json",
54
+ "json"
55
+ ])) {
56
+ case "html":
57
+ case null:
58
+ ctx.response.status(error.status).send(message);
59
+ break;
60
+ case "json":
61
+ ctx.response.status(error.status).send({ errors: [{ message }] });
62
+ break;
63
+ case "application/vnd.api+json":
64
+ ctx.response.status(error.status).send({ errors: [{
65
+ code: error.code,
66
+ title: message
67
+ }] });
68
+ break;
69
+ }
70
+ }
71
+ };
72
+ getResponseMessage(error, ctx) {
73
+ if ("i18n" in ctx) return ctx.i18n.t(error.identifier, {}, error.message);
74
+ return error.message;
75
+ }
76
+ constructor(message, options) {
77
+ super(message, {});
78
+ this.guardDriverName = options.guardDriverName;
79
+ this.redirectTo = options.redirectTo;
80
+ }
81
+ async handle(error, ctx) {
82
+ const renderer = this.renderers[this.guardDriverName];
83
+ const message = error.getResponseMessage(error, ctx);
84
+ if (!renderer) return ctx.response.status(error.status).send(message);
85
+ return renderer(message, error, ctx);
86
+ }
87
+ };
88
+ const E_INVALID_CREDENTIALS = class extends Exception {
89
+ static status = 400;
90
+ static code = "E_INVALID_CREDENTIALS";
91
+ identifier = "errors.E_INVALID_CREDENTIALS";
92
+ getResponseMessage(error, ctx) {
93
+ if ("i18n" in ctx) return ctx.i18n.t(error.identifier, {}, error.message);
94
+ return error.message;
95
+ }
96
+ async handle(error, ctx) {
97
+ const message = this.getResponseMessage(error, ctx);
98
+ switch (ctx.request.accepts([
99
+ "html",
100
+ "application/vnd.api+json",
101
+ "json"
102
+ ])) {
103
+ case "html":
104
+ case null:
105
+ if (ctx.session) {
106
+ ctx.session.flashExcept([
107
+ "_csrf",
108
+ "_method",
109
+ "password",
110
+ "password_confirmation"
111
+ ]);
112
+ ctx.session.flashErrors({ [error.code]: message });
113
+ ctx.response.redirect("back", true);
114
+ } else ctx.response.status(error.status).send(message);
115
+ break;
116
+ case "json":
117
+ ctx.response.status(error.status).send({ errors: [{ message }] });
118
+ break;
119
+ case "application/vnd.api+json":
120
+ ctx.response.status(error.status).send({ errors: [{
121
+ code: error.code,
122
+ title: message
123
+ }] });
124
+ break;
125
+ }
126
+ }
127
+ };
128
+ export { __export as i, E_UNAUTHORIZED_ACCESS as n, errors_exports as r, E_INVALID_CREDENTIALS as t };
package/build/index.js CHANGED
@@ -1,109 +1,68 @@
1
- import {
2
- AuthManager,
3
- Authenticator,
4
- AuthenticatorClient
5
- } from "./chunk-MSPAYMZE.js";
6
- import "./chunk-2VRS2VHB.js";
7
- import {
8
- errors_exports
9
- } from "./chunk-S5G5RTJX.js";
10
- import {
11
- __export
12
- } from "./chunk-UXA4FHST.js";
13
-
14
- // configure.ts
1
+ import { r as errors_exports } from "./errors-BQxhZmkE.js";
2
+ import { t as symbols_exports } from "./symbols-Ct253Khf.js";
3
+ import "./debug-Ckko95-M.js";
4
+ import { n as AuthenticatorClient, r as Authenticator, t as AuthManager } from "./auth_manager-BMz_OJXq.js";
15
5
  import { presetAuth } from "@adonisjs/presets/auth";
6
+ import { configProvider } from "@adonisjs/core";
16
7
  async function configure(command) {
17
- const codemods = await command.createCodemods();
18
- let guard = command.parsedFlags.guard;
19
- if (guard === void 0) {
20
- guard = await command.prompt.choice(
21
- "Select the auth guard you want to use",
22
- [
23
- {
24
- name: "session",
25
- message: "Session"
26
- },
27
- {
28
- name: "access_tokens",
29
- message: "Opaque access tokens"
30
- },
31
- {
32
- name: "basic_auth",
33
- message: "Basic Auth"
34
- }
35
- ],
36
- {
37
- validate(value) {
38
- return !!value;
39
- }
40
- }
41
- );
42
- }
43
- if (!["session", "access_tokens", "basic_auth"].includes(guard)) {
44
- command.logger.error(
45
- `The selected guard "${guard}" is invalid. Select one from: session, access_tokens, basic_auth`
46
- );
47
- command.exitCode = 1;
48
- return;
49
- }
50
- await presetAuth(codemods, command.app, {
51
- guard,
52
- userProvider: "lucid"
53
- });
8
+ const codemods = await command.createCodemods();
9
+ let guard = command.parsedFlags.guard;
10
+ if (guard === void 0) guard = await command.prompt.choice("Select the auth guard you want to use", [
11
+ {
12
+ name: "session",
13
+ message: "Session"
14
+ },
15
+ {
16
+ name: "access_tokens",
17
+ message: "Opaque access tokens"
18
+ },
19
+ {
20
+ name: "basic_auth",
21
+ message: "Basic Auth"
22
+ }
23
+ ], { validate(value) {
24
+ return !!value;
25
+ } });
26
+ if (![
27
+ "session",
28
+ "access_tokens",
29
+ "basic_auth"
30
+ ].includes(guard)) {
31
+ command.logger.error(`The selected guard "${guard}" is invalid. Select one from: session, access_tokens, basic_auth`);
32
+ command.exitCode = 1;
33
+ return;
34
+ }
35
+ await presetAuth(codemods, command.app, {
36
+ guard,
37
+ userProvider: "lucid"
38
+ });
54
39
  }
55
-
56
- // src/symbols.ts
57
- var symbols_exports = {};
58
- __export(symbols_exports, {
59
- GUARD_KNOWN_EVENTS: () => GUARD_KNOWN_EVENTS,
60
- PROVIDER_REAL_USER: () => PROVIDER_REAL_USER
61
- });
62
- var PROVIDER_REAL_USER = Symbol.for("PROVIDER_REAL_USER");
63
- var GUARD_KNOWN_EVENTS = Symbol.for("GUARD_KNOWN_EVENTS");
64
-
65
- // src/define_config.ts
66
- import { configProvider } from "@adonisjs/core";
67
40
  function defineConfig(config) {
68
- return configProvider.create(async (app) => {
69
- const guardsList = Object.keys(config.guards);
70
- const guards = {};
71
- for (let guardName of guardsList) {
72
- const guard = config.guards[guardName];
73
- if (typeof guard === "function") {
74
- guards[guardName] = guard;
75
- } else {
76
- guards[guardName] = await guard.resolver(guardName, app);
77
- }
78
- }
79
- return {
80
- default: config.default,
81
- guards
82
- };
83
- });
41
+ return configProvider.create(async (app) => {
42
+ const guardsList = Object.keys(config.guards);
43
+ const guards = {};
44
+ for (let guardName of guardsList) {
45
+ const guard = config.guards[guardName];
46
+ if (typeof guard === "function") guards[guardName] = guard;
47
+ else guards[guardName] = await guard.resolver(guardName, app);
48
+ }
49
+ return {
50
+ default: config.default,
51
+ guards
52
+ };
53
+ });
84
54
  }
85
-
86
- // index.ts
87
55
  function isModuleInstalled(moduleName) {
88
- try {
89
- import.meta.resolve(moduleName);
90
- return true;
91
- } catch (e) {
92
- return false;
93
- }
56
+ try {
57
+ import.meta.resolve(moduleName);
58
+ return true;
59
+ } catch (e) {
60
+ return false;
61
+ }
94
62
  }
95
- var withAuthFinder;
63
+ let withAuthFinder;
96
64
  if (isModuleInstalled("@adonisjs/lucid")) {
97
- const { withAuthFinder: withAuthFinderFn } = await import("./src/mixins/lucid.js");
98
- withAuthFinder = withAuthFinderFn;
65
+ const { withAuthFinder: withAuthFinderFn } = await import("./src/mixins/lucid.js");
66
+ withAuthFinder = withAuthFinderFn;
99
67
  }
100
- export {
101
- AuthManager,
102
- Authenticator,
103
- AuthenticatorClient,
104
- configure,
105
- defineConfig,
106
- errors_exports as errors,
107
- symbols_exports as symbols,
108
- withAuthFinder
109
- };
68
+ export { AuthManager, Authenticator, AuthenticatorClient, configure, defineConfig, errors_exports as errors, symbols_exports as symbols, withAuthFinder };