-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Auth.js
83 lines (73 loc) · 2.05 KB
/
Auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict'
/*
* adonis-auth
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const debug = require('debug')('adonis:auth')
class Auth {
constructor (Config) {
const authenticator = Config.get('auth.authenticator')
this.scheme = Config.get(`auth.${authenticator}.scheme`, null)
}
/**
* Authenticate the user using one of the defined
* schemes or the default scheme
*
* @method handle
*
* @param {Object} options.auth
* @param {Function} next
*
* @return {void}
*/
async handle ({ auth }, next, schemes) {
let lastError = null
let authenticatedScheme = null
schemes = schemes instanceof Array === true ? schemes : [this.scheme]
debug('attempting to authenticate via %j scheme(s)', schemes)
/**
* Loop over all the defined schemes and wait until use is logged
* via anyone
*/
for (const scheme of schemes) {
try {
await auth.authenticator(scheme).check()
debug('authenticated using %s scheme', scheme)
authenticatedScheme = scheme
lastError = null
break
} catch (error) {
debug('authentication failed using %s scheme', scheme)
lastError = error
}
}
/**
* If there is an error from all the schemes
* then throw it back
*/
if (lastError) {
throw lastError
}
/**
* If user got logged then set the `current` property
* on auth, which is reference to the scheme via
* which user got authenticated.
*/
if (authenticatedScheme) {
/**
* If logged in scheme is same as the default scheme, the reference
* the actual authenticator instance, otherwise create a new
* one for the scheme via which user got authenticated
*/
auth.current = authenticatedScheme === this.scheme
? auth.authenticatorInstance
: auth.authenticator(authenticatedScheme)
}
await next()
}
}
module.exports = Auth