-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2.js
177 lines (144 loc) · 5.24 KB
/
oauth2.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const oauth2orize = require('oauth2orize')
const passport = require('passport')
const Promise = require('bluebird')
const config = require('./config')
const api = require('./api')
const logger = require('./logger')
const Client = api.models.Client
const User = api.models.User
const Token = api.models.Token
const RefreshToken = api.models.RefreshToken
// create OAuth 2.0 server
var server = oauth2orize.createServer()
server.serializeClient(function(client, done) {
return done(null, client.id)
})
server.deserializeClient(function(id, done) {
Client.findOne({ id })
.then((client) => {
done(null, client)
})
.catch((e) => done(e))
})
// Exchange username & password for an access token.
server.exchange(oauth2orize.exchange.password(function(client, username, password, scope, done) {
User.findOne({ username: username })
.then((user) => {
if (!user) {
return done(null, false)
}
return user.validPassword(password).then((valid) => {
if(!valid) {
return done(null, false)
}
return Promise.all([
RefreshToken.remove({ userId: user.id, clientId: client.id }),
Token.remove({ userId: user.id, clientId: client.id })
])
}).then(() => {
const token = new Token({
name: 'at',
clientId: client.id,
userId: user.id,
type: 'oauth2',
expires: Date.now() + (config.oauth2.ttl*1000)
})
const refreshToken = new RefreshToken({
clientId: client.id,
userId: user.id
})
return refreshToken.save().then(() => {
const info = { scope: '*' }
return token.save().then(() => {
done(null, token.token, refreshToken.token, { 'expires_in': config.oauth2.ttl })
return Promise.resolve()
})
})
})
})
.catch((e) => {
logger.warn('Password exchange error: %s', e.message)
logger.debug(e.stack)
done(e)
})
}))
server.exchange(oauth2orize.exchange.clientCredentials(function(client, scope, done) {
if(!client) return done(null, false)
Client.findOne({ id: client.id })
.then((client) => {
if (!client) {
return done(null, false)
}
if(!client.enabled) {
return done(null, false)
}
return Promise.all([
RefreshToken.remove({ userId: client.userId, clientId: client.id }),
Token.remove({ userId: client.userId, clientId: client.id })
])
}).then(() => {
const token = new Token({
name: 'at',
clientId: client.id,
userId: client.userId,
type: 'oauth2',
expires: Date.now() + (config.oauth2.ttl*1000)
})
const refreshToken = new RefreshToken({
clientId: client.id,
userId: client.userId
})
return refreshToken.save().then(() => {
return token.save().then(() => {
done(null, token.token, refreshToken.token, { 'expires_in': config.oauth2.ttl })
return Promise.resolve()
})
})
})
.catch((e) => {
logger.warn('Client credentials exchange error %s', e.message)
logger.debug(e.stack)
done(e)
})
}))
// Exchange refreshToken for an access token.
server.exchange(oauth2orize.exchange.refreshToken(function(client, refreshToken, scope, done) {
RefreshToken.findOne({ token: refreshToken }).then((token) => {
if (!token) {
done(null, false)
return Promise.resolve()
}
return api.models.User.findOne({ id: token.userId, enabled: true }).then((user) => {
if (!user) {
done(null, false)
return Promise.resolve()
}
return Promise.all([
RefreshToken.remove({ userId: user.id, clientId: client.id }),
Token.remove({ userId: user.id, clientId: client.id })
]).then(() => {
const token = new Token({
name: 'at',
clientId: client.id,
userId: user.id
})
const refreshToken = new RefreshToken({
clientId: client.id,
userId: user.id
})
return refreshToken.save().then(() => {
return token.save(function () {
done(null, token.token, refreshToken, { 'expires_in': config.oauth2.ttl })
})
})
})
})
})
.catch((e) => done(e))
}))
// token endpoint
module.exports.token = [
passport.authenticate(['client_basic', 'client_password'], { session: false }),
server.token(),
server.errorHandler()
]