Skip to content

Commit e90a485

Browse files
committed
feature: support async model and handlers by OAuth2Server v5.next
1 parent 8e20cb5 commit e90a485

File tree

2 files changed

+84
-83
lines changed

2 files changed

+84
-83
lines changed

lib/oauth.js

Lines changed: 82 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ export class OAuth2Server {
6363
*/
6464
constructor ({ serverOptions = {}, model, routes, debug } = {}) {
6565
check(serverOptions, OptionsSchema.serverOptions)
66-
66+
if (debug) {
67+
console.debug('[OAuth2Server]: create new instance')
68+
console.debug('[OAuth2Server]: serveroptions', serverOptions)
69+
}
6770
this.instanceId = Random.id()
6871
this.config = {
6972
serverOptions: Object.assign({}, OAuth2ServerDefaults.serverOptions, serverOptions),
@@ -120,9 +123,8 @@ export class OAuth2Server {
120123
* @param secret
121124
* @returns {}
122125
*/
123-
registerClient ({ title, homepage, description, privacyLink, redirectUris, grants, clientId, secret }) {
124-
const self = this
125-
return Promise.await(self.model.createClient({
126+
async registerClient ({ title, homepage, description, privacyLink, redirectUris, grants, clientId, secret }) {
127+
return this.model.createClient({
126128
title,
127129
homepage,
128130
description,
@@ -131,45 +133,44 @@ export class OAuth2Server {
131133
grants,
132134
clientId,
133135
secret
134-
}))
136+
})
135137
}
136138

137139
authorizeHandler (options) {
138140
const self = this
139-
return function (req, res, next) {
141+
return async function (req, res, next) {
140142
const request = new Request(req)
141143
const response = new Response(res)
142-
return self.oauth.authorize(request, response, options)
143-
.then(function (code) {
144-
res.locals.oauth = { code: code }
145-
next()
146-
})
147-
.catch(function (err) {
148-
// handle error condition
149-
res.writeHead(500)
150-
res.end(err)
151-
})
144+
145+
try {
146+
const code = await self.oauth.authorize(request, response, options)
147+
res.locals.oauth = { code: code }
148+
next()
149+
} catch (err) {
150+
res.writeHead(500)
151+
res.end(err)
152+
}
152153
}
153154
}
154155

155156
authenticateHandler (options) {
156157
const self = this
157-
return function (req, res, next) {
158+
return async function (req, res, next) {
158159
const request = new Request(req)
159160
const response = new Response(res)
160-
return self.oauth.authenticate(request, response, options)
161-
.then(function (token) {
162-
req.data = Object.assign({}, req.data, token)
163-
next()
164-
})
165-
.catch(function (err) {
166-
return errorHandler(res, {
167-
status: err.status,
168-
error: err.name,
169-
description: err.message,
170-
debug: self.debug
171-
})
161+
162+
try {
163+
const token = await self.oauth.authenticate(request, response, options)
164+
req.data = Object.assign({}, req.data, token)
165+
next()
166+
} catch (err) {
167+
return errorHandler(res, {
168+
status: err.status,
169+
error: err.name,
170+
description: err.message,
171+
debug: self.debug
172172
})
173+
}
173174
}
174175
}
175176

@@ -214,10 +215,11 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
214215
return true
215216
}
216217

217-
const getValidatedClient = (req, res) => {
218+
const getValidatedClient = async (req, res) => {
218219
const clientId = req.method.toLowerCase() === 'get' ? req.query.client_id : req.body.client_id
219220
const secret = req.method.toLowerCase() === 'get' ? req.query.client_secret : req.body.client_secret
220-
const client = Promise.await(self.model.getClient(clientId, secret))
221+
const client = await self.model.getClient(clientId, secret)
222+
221223
if (!client) {
222224
// unauthorized_client - The client is not authorized to request an authorization code using this method.
223225
return errorHandler(res, {
@@ -228,6 +230,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
228230
debug: self.debug
229231
})
230232
}
233+
231234
return client
232235
}
233236

@@ -279,7 +282,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
279282
// If there is something wrong with the syntax of the request, such as the redirect_uri or client_id is invalid,
280283
// then it’s important not to redirect the user and instead you should show the error message directly.
281284
// This is to avoid letting your authorization server be used as an open redirector.
282-
route('get', authorizeUrl, function (req, res, next) {
285+
route('get', authorizeUrl, async function (req, res, next) {
283286
if (!validateParams(req.query, requiredAuthorizeGetParams, self.debug)) {
284287
return errorHandler(res, {
285288
status: 400,
@@ -293,7 +296,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
293296
const validResponseType = validateResponseType(req, res)
294297
if (!validResponseType) return
295298

296-
const client = getValidatedClient(req, res)
299+
const client = await getValidatedClient(req, res)
297300
if (!client) return
298301

299302
const redirectUri = getValidatedRedirectUri(req, res, client)
@@ -305,7 +308,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
305308
// STEP 2: ADD USER TO THE REQUEST
306309
// validate all inputs again, since all inputs
307310
// could have been manipulated within form
308-
route('post', authorizeUrl, function (req, res, next) {
311+
route('post', authorizeUrl, async function (req, res, next) {
309312
if (!validateParams(req.body, requiredAuthorizePostParams, self.debug)) {
310313
return errorHandler(res, {
311314
error: 'invalid_request',
@@ -316,7 +319,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
316319
})
317320
}
318321

319-
const client = getValidatedClient(req, res)
322+
const client = await getValidatedClient(req, res)
320323
if (!client) return
321324

322325
const validRedirectUri = getValidatedRedirectUri(req, res, client)
@@ -366,7 +369,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
366369
// - on allow, assign the client_id to the user's authorized clients
367370
// - on deny, ...?
368371
// - construct the redirect query and redirect to the redirect_uri
369-
route('post', authorizeUrl, function (req, res /*, next */) {
372+
route('post', authorizeUrl, async function (req, res /*, next */) {
370373
const request = new Request(req)
371374
const response = new Response(res)
372375
const authorizeOptions = {
@@ -377,37 +380,36 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
377380
}
378381
}
379382

380-
return self.oauth.authorize(request, response, authorizeOptions)
381-
.then(bind(function (code) {
382-
const query = new URLSearchParams({
383-
code: code.authorizationCode,
384-
user: req.user.id,
385-
state: req.body.state
386-
})
387-
388-
const finalRedirectUri = `${req.body.redirect_uri}?${query}`
383+
try {
384+
const code = await self.oauth.authorize(request, response, authorizeOptions)
385+
const query = new URLSearchParams({
386+
code: code.authorizationCode,
387+
user: req.user.id,
388+
state: req.body.state
389+
})
389390

390-
res.statusCode = 302
391-
res.setHeader('Location', finalRedirectUri)
392-
res.end()
393-
}))
394-
.catch(function (err) {
395-
errorHandler(res, {
396-
originalError: err,
397-
error: err.name,
398-
description: err.message,
399-
status: err.statusCode,
400-
state: req.body.state,
401-
debug: self.debug
402-
})
391+
const finalRedirectUri = `${req.body.redirect_uri}?${query}`
392+
393+
res.statusCode = 302
394+
res.setHeader('Location', finalRedirectUri)
395+
res.end()
396+
} catch (err) {
397+
errorHandler(res, {
398+
originalError: err,
399+
error: err.name,
400+
description: err.message,
401+
status: err.statusCode,
402+
state: req.body.state,
403+
debug: self.debug
403404
})
405+
}
404406
})
405407

406408
// STEP 4: GENERATE ACCESS TOKEN RESPONSE
407409
// - validate params
408410
// - validate authorization code
409411
// - issue accessToken and refreshToken
410-
route('post', accessTokenUrl, function (req, res, next) {
412+
route('post', accessTokenUrl, async function (req, res, /* next */) {
411413
if (!validateParams(req.body, requiredAccessTokenPostParams, self.debug)) {
412414
return errorHandler(res, {
413415
status: 400,
@@ -421,30 +423,29 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
421423
const request = new Request(req)
422424
const response = new Response(res)
423425

424-
return self.oauth.token(request, response)
425-
.then(function (token) {
426-
res.writeHead(200, {
427-
'Content-Type': 'application/json',
428-
'Cache-Control': 'no-store',
429-
Pragma: 'no-cache'
430-
})
431-
const body = JSON.stringify({
432-
access_token: token.accessToken,
433-
token_type: 'bearer',
434-
expires_in: token.accessTokenExpiresAt,
435-
refresh_token: token.refreshToken
436-
})
437-
res.end(body)
426+
try {
427+
const token = await self.oauth.token(request, response)
428+
res.writeHead(200, {
429+
'Content-Type': 'application/json',
430+
'Cache-Control': 'no-store',
431+
Pragma: 'no-cache'
438432
})
439-
.catch(function (err) {
440-
return errorHandler(res, {
441-
error: 'unauthorized_client',
442-
description: err.message,
443-
state: req.body.state,
444-
debug: self.debug,
445-
status: err.statusCode
446-
})
433+
const body = JSON.stringify({
434+
access_token: token.accessToken,
435+
token_type: 'bearer',
436+
expires_in: token.accessTokenExpiresAt,
437+
refresh_token: token.refreshToken
438+
})
439+
res.end(body)
440+
} catch (err) {
441+
return errorHandler(res, {
442+
error: 'unauthorized_client',
443+
description: err.message,
444+
state: req.body.state,
445+
debug: self.debug,
446+
status: err.statusCode
447447
})
448+
}
448449
})
449450

450451
route('use', fallbackUrl, function (req, res, next) {

package.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env meteor */
22
Package.describe({
33
name: 'leaonline:oauth2-server',
4-
version: '4.2.1',
4+
version: '5.0.0',
55
summary: 'Node OAuth2 Server (v4) with Meteor bindings',
66
git: 'https://github.com/leaonline/oauth2-server.git'
77
})
@@ -13,7 +13,7 @@ Package.onUse(function (api) {
1313
})
1414

1515
Npm.depends({
16-
'@node-oauth/oauth2-server': '4.2.0',
16+
'@node-oauth/oauth2-server': '5.0.0-rc.1',
1717
'body-parser': '1.20.0'
1818
})
1919

0 commit comments

Comments
 (0)