-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauthModel.js
75 lines (67 loc) · 2.32 KB
/
authModel.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
const Token = require('./models/auth/Token')
const Client = require('./models/auth/Client')
const AuthorizationCode = require('./models/auth/AuthorizationCode')
module.exports = {
getAccessToken: accessToken =>
Token.findOne({ accessToken })
.populate('user client'),
getAuthorizationCode: authCode =>
AuthorizationCode.findOne({ authorizationCode: authCode })
.populate('user client'),
getClient: (clientId, clientSecret) => {
const params = { _id: clientId }
if (clientSecret) params.secret = clientSecret
return Client.findOne(params)
},
getRefreshToken: refreshToken =>
Token.findOne({ refreshToken })
.populate('user client'),
// should return true if successful
revokeAuthorizationCode: authCode =>
AuthorizationCode.findOneAndRemove({ authorizationCode: authCode.authorizationCode })
.then(doc => !!doc),
// should return true if successful
revokeToken: token =>
Token.findOneAndRemove({ refreshToken: token.refreshToken })
.then(doc => !!doc),
saveAuthorizationCode: (code, client, user) => {
const authCode = {
authorizationCode: code.authorizationCode,
expiresAt: code.expiresAt,
redirectUri: code.redirectUri,
scope: code.scope,
client: client.id,
user: user.id
}
return AuthorizationCode.create(authCode)
.then(authCode => ({
authorizationCode: authCode.authorizationCode,
expiresAt: authCode.expiresAt,
redirectUri: authCode.redirectUri,
scope: authCode.scope,
client: {id: authCode.client},
user: {id: authCode.user}
}))
},
saveToken: (token, client, user) => {
const newToken = {
accessToken: token.accessToken,
accessTokenExpiresAt: token.accessTokenExpiresAt,
refreshToken: token.refreshToken,
refreshTokenExpiresAt: token.refreshTokenExpiresAt,
scope: token.scope,
client: client.id,
user: user.id
}
return Token.create(newToken)
.then(savedToken => ({
accessToken: savedToken.accessToken,
accessTokenExpiresAt: savedToken.accessTokenExpiresAt,
refreshToken: savedToken.refreshToken,
refreshTokenExpiresAt: savedToken.refreshTokenExpiresAt,
scope: savedToken.scope,
client: {id: savedToken.client},
user: {id: savedToken.user}
}))
}
}