-
Notifications
You must be signed in to change notification settings - Fork 1
/
authManager.js
104 lines (82 loc) · 2.56 KB
/
authManager.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require('@ostro/support/helpers')
const Manager = require('@ostro/support/manager')
const InvalidArgumentException = require('@ostro/support/exceptions/invalidArgumentException')
const CreatesUserProvider = require('./createsUserProviders')
const SessionGuard = require('./sessionGuard')
const TokenGuard = require('./tokenGuard')
const AuthRequest = require('./authRequest')
class AuthManager extends implement(Manager, CreatesUserProvider) {
$type = 'auth';
constructor($app) {
super($app)
Object.defineProperties(this, {
$userResolver: {
value: function($guard = null) {
return this.guard($guard).user();
},
writable: true
}
})
}
guard($name = null) {
return this.driver($name)
}
resolve($name) {
let $config = this.getConfig($name);
if (!$config) {
throw new InvalidArgumentException(`Auth guard [${$name}] is not defined.`);
}
return super.resolve($name, $config)
}
start() {
return (request, response, next) => {
request.auth = new AuthRequest(request, this)
request.user = function($guard) {
return this.auth.guard($guard).user()
}
next()
}
}
createSessionDriver($config, $name) {
let $provider = this.createUserProvider($config['provider'] || null);
return ($request) => {
return new SessionGuard($name, $provider, $request);
}
}
createTokenDriver($config, $name) {
let $provider = this.createUserProvider($config['provider'] || null)
return ($request) => {
return new TokenGuard(
$provider,
$request,
$config['input_key'] || 'api_token',
$config['storage_key'] || 'api_token',
$config['hash'] || false
);
}
}
getConfig(name) {
return super.getConfig(`guards.${name}`);
}
getDefaultDriver() {
return super.getConfig(`defaults.guard`);
}
userResolver() {
return this.$userResolver;
}
resolveUsersUsing($userResolver) {
this.$userResolver = $userResolver;
return this;
}
provider($name, $callback) {
this.$customProviderCreators[$name] = $callback;
return this;
}
hasResolvedGuards() {
return count(this.$driver) > 0;
}
forgetGuards() {
return this.forgetDrivers()
}
}
module.exports = AuthManager