1 | ;
|
2 | /*
|
3 | * @adonisjs/auth
|
4 | *
|
5 | * (c) Harminder Virk <virk@adonisjs.com>
|
6 | *
|
7 | * For the full copyright and license information, please view the LICENSE
|
8 | * file that was distributed with this source code.
|
9 | */
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.Auth = void 0;
|
12 | /**
|
13 | * Auth class exposes the API to obtain guard instances for a given
|
14 | * HTTP request.
|
15 | */
|
16 | class Auth {
|
17 | constructor(manager, ctx) {
|
18 | this.manager = manager;
|
19 | this.ctx = ctx;
|
20 | /**
|
21 | * We keep a per request singleton instances for each instantiated mapping
|
22 | */
|
23 | this.mappingsCache = new Map();
|
24 | /**
|
25 | * The default guard is always the one defined inside the config, until
|
26 | * manually overwritten by the user
|
27 | */
|
28 | this.defaultGuard = this.manager.defaultGuard;
|
29 | }
|
30 | /**
|
31 | * Returns an instance of a named or the default mapping
|
32 | */
|
33 | use(mapping) {
|
34 | mapping = mapping || this.defaultGuard;
|
35 | if (!this.mappingsCache.has(mapping)) {
|
36 | this.ctx.logger.trace('instantiating auth mapping', { name: mapping });
|
37 | this.mappingsCache.set(mapping, this.manager.makeMapping(this.ctx, mapping));
|
38 | }
|
39 | return this.mappingsCache.get(mapping);
|
40 | }
|
41 | /**
|
42 | * Guard name for the default mapping
|
43 | */
|
44 | get name() {
|
45 | return this.use().name;
|
46 | }
|
47 | /**
|
48 | * Reference to the logged in user
|
49 | */
|
50 | get user() {
|
51 | return this.use().user;
|
52 | }
|
53 | /**
|
54 | * Reference to the default guard config
|
55 | */
|
56 | get config() {
|
57 | return this.use().config;
|
58 | }
|
59 | /**
|
60 | * Find if the user has been logged out in the current request
|
61 | */
|
62 | get isLoggedOut() {
|
63 | return this.use().isLoggedOut;
|
64 | }
|
65 | /**
|
66 | * A boolean to know if user is a guest or not. It is
|
67 | * always opposite of [[isLoggedIn]]
|
68 | */
|
69 | get isGuest() {
|
70 | return this.use().isGuest;
|
71 | }
|
72 | /**
|
73 | * A boolean to know if user is logged in or not
|
74 | */
|
75 | get isLoggedIn() {
|
76 | return this.use().isLoggedIn;
|
77 | }
|
78 | /**
|
79 | * A boolean to know if user is retrieved by authenticating
|
80 | * the current request or not.
|
81 | */
|
82 | get isAuthenticated() {
|
83 | return this.use().isAuthenticated;
|
84 | }
|
85 | /**
|
86 | * Whether or not the authentication has been attempted
|
87 | * for the current request
|
88 | */
|
89 | get authenticationAttempted() {
|
90 | return this.use().authenticationAttempted;
|
91 | }
|
92 | /**
|
93 | * Reference to the provider for looking up the user
|
94 | */
|
95 | get provider() {
|
96 | return this.use().provider;
|
97 | }
|
98 | /**
|
99 | * Verify user credentials.
|
100 | */
|
101 | async verifyCredentials(uid, password) {
|
102 | return this.use().verifyCredentials(uid, password);
|
103 | }
|
104 | /**
|
105 | * Attempt to verify user credentials and perform login
|
106 | */
|
107 | async attempt(uid, password, ...args) {
|
108 | return this.use().attempt(uid, password, ...args);
|
109 | }
|
110 | /**
|
111 | * Login a user without any verification
|
112 | */
|
113 | async login(user, ...args) {
|
114 | return this.use().login(user, ...args);
|
115 | }
|
116 | /**
|
117 | * Login a user using their id
|
118 | */
|
119 | async loginViaId(id, ...args) {
|
120 | return this.use().loginViaId(id, ...args);
|
121 | }
|
122 | /**
|
123 | * Attempts to authenticate the user for the current HTTP request. An exception
|
124 | * is raised when unable to do so
|
125 | */
|
126 | async authenticate() {
|
127 | return this.use().authenticate();
|
128 | }
|
129 | /**
|
130 | * Attempts to authenticate the user for the current HTTP request and supresses
|
131 | * exceptions raised by the [[authenticate]] method and returns a boolean
|
132 | */
|
133 | async check() {
|
134 | return this.use().check();
|
135 | }
|
136 | /**
|
137 | * Logout user
|
138 | */
|
139 | async logout(...args) {
|
140 | return this.use().logout(...args);
|
141 | }
|
142 | /**
|
143 | * Serialize toJSON
|
144 | */
|
145 | toJSON() {
|
146 | return {
|
147 | defaultGuard: this.defaultGuard,
|
148 | guards: [...this.mappingsCache.keys()].reduce((result, key) => {
|
149 | result[key] = this.mappingsCache.get(key).toJSON();
|
150 | return result;
|
151 | }, {}),
|
152 | };
|
153 | }
|
154 | }
|
155 | exports.Auth = Auth;
|