forked from oauthjs/express-oauth-server
-
Notifications
You must be signed in to change notification settings - Fork 11
4.0.0 - Migrate to async/await #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b7fbc11
tests: update location header
jankapunkt ab7d763
tests: rewrite tests to es6
jankapunkt fd38098
refactor: rewrite to use native async/await
jankapunkt 0168c66
build(deps): update deps to latest, drop bluebird
jankapunkt 9a90004
build(core): bump minimal node to 16
jankapunkt 6b782da
ci: update node versions to minimum 16
jankapunkt 7eee7b0
docs: updated for next release
jankapunkt 7eb7b66
build(core): prepare 4.0.0-rc.0 release
jankapunkt 1390685
Merge branch 'master' of github.com:node-oauth/express-oauth-server
jankapunkt 1b88ef0
Merge branch 'master' into migrate-es-6
jankapunkt 32d13c0
tests: coverage 100%
jankapunkt 67ae088
refactor: move remaining functions into es6 class and add more compre…
jankapunkt ad43b57
docs: link OAuth2 server documentation to options section in README
jankapunkt 10d56f8
build(core): make express a peer dependency to avoid version conflicts
jankapunkt 8f2646c
refactor: move exxamples to own reppository
jankapunkt 79cbc8a
docs: add jsDoc-based API documentation
jankapunkt 9137839
fix: eslint config; ignore update+fix
jankapunkt 15d6271
docs: updated
jankapunkt fe5ec7c
ci: added lint and docs build to ci
jankapunkt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,144 +1,166 @@ | ||
| /** | ||
| * Module dependencies. | ||
| */ | ||
| const NodeOAuthServer = require('@node-oauth/oauth2-server'); | ||
| const { Request, Response } = require('@node-oauth/oauth2-server'); | ||
|
|
||
| const InvalidArgumentError = require('@node-oauth/oauth2-server/lib/errors/invalid-argument-error'); | ||
| const NodeOAuthServer = require('@node-oauth/oauth2-server'); | ||
| const Request = require('@node-oauth/oauth2-server').Request; | ||
| const Response = require('@node-oauth/oauth2-server').Response; | ||
| const UnauthorizedRequestError = require('@node-oauth/oauth2-server/lib/errors/unauthorized-request-error'); | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| class ExpressOAuthServer { | ||
| constructor(options) { | ||
| this.options = options || {}; | ||
|
|
||
| if (!this.options.model) { | ||
| /** | ||
| * @constructor | ||
| */ | ||
| constructor(options = {}) { | ||
| if (!options.model) { | ||
| throw new InvalidArgumentError('Missing parameter: `model`'); | ||
| } | ||
| this.useErrorHandler = this.options.useErrorHandler === true; | ||
| this.continueMiddleware = this.options.continueMiddleware === true; | ||
|
|
||
| delete this.options.useErrorHandler; | ||
| delete this.options.continueMiddleware; | ||
| this.useErrorHandler = !!options.useErrorHandler; | ||
| delete options.useErrorHandler; | ||
|
|
||
| this.continueMiddleware = !!options.continueMiddleware; | ||
| delete options.continueMiddleware; | ||
|
|
||
| this.server = new NodeOAuthServer(this.options); | ||
| this.server = new NodeOAuthServer(options); | ||
| } | ||
|
|
||
| /** | ||
| * Authentication Middleware. | ||
| * | ||
| * Returns a middleware that will validate a token. | ||
| * | ||
| * (See: https://tools.ietf.org/html/rfc6749#section-7) | ||
| */ | ||
| authenticate(options) { | ||
| return async (req, res, next) => { | ||
| const request = new Request(req); | ||
| const response = new Response(res); | ||
| let token; | ||
| try { | ||
| token = await this.server.authenticate(request, response, options); | ||
| } catch (err) { | ||
| this._handleError(res, null, err, next); | ||
| return; | ||
| } | ||
| res.locals.oauth = { token }; | ||
| return next(); | ||
| } | ||
|
|
||
| /** | ||
| * Authentication Middleware. | ||
| * | ||
| * Returns a middleware that will validate a token. | ||
| * | ||
| * (See: https://tools.ietf.org/html/rfc6749#section-7) | ||
| */ | ||
|
|
||
| ExpressOAuthServer.prototype.authenticate = function(options) { | ||
| const fn = async function(req, res, next) { | ||
| const request = new Request(req); | ||
| const response = new Response(res); | ||
|
|
||
| let token | ||
|
|
||
| try { | ||
| token = await this.server.authenticate(request, response, options); | ||
| } catch (e) { | ||
| return handleError.call(this, e, req, res, null, next); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Authorization Middleware. | ||
| * | ||
| * Returns a middleware that will authorize a client to request tokens. | ||
| * | ||
| * (See: https://tools.ietf.org/html/rfc6749#section-3.1) | ||
| */ | ||
| authorize(options) { | ||
| return async (req, res, next) => { | ||
| const request = new Request(req); | ||
| const response = new Response(res); | ||
| let code; | ||
| try { | ||
| code = await this.server.authorize(request, response, options); | ||
| } catch (err) { | ||
| this._handleError(res, response, err, next); | ||
| return; | ||
| } | ||
| res.locals.oauth = { code }; | ||
| if (this.continueMiddleware) { | ||
| next(); | ||
| } | ||
| return this._handleResponse(req, res, response); | ||
| res.locals.oauth = { token }; | ||
| next(); | ||
| }; | ||
| return fn.bind(this); | ||
| }; | ||
|
|
||
| /** | ||
| * Authorization Middleware. | ||
| * | ||
| * Returns a middleware that will authorize a client to request tokens. | ||
| * | ||
| * (See: https://tools.ietf.org/html/rfc6749#section-3.1) | ||
| */ | ||
|
|
||
| ExpressOAuthServer.prototype.authorize = function(options) { | ||
| const fn = async function(req, res, next) { | ||
| const request = new Request(req); | ||
| const response = new Response(res); | ||
|
|
||
| let code | ||
|
|
||
| try { | ||
| code = await this.server.authorize(request, response, options); | ||
| } catch (e) { | ||
| return handleError.call(this, e, req, res, response, next); | ||
| } | ||
| } | ||
|
|
||
| res.locals.oauth = { code: code }; | ||
| if (this.continueMiddleware) { | ||
| next(); | ||
| } | ||
|
|
||
| /** | ||
| * Authorization Middleware. | ||
| * | ||
| * Returns a middleware that will authorize a client to request tokens. | ||
| * | ||
| * (See: https://tools.ietf.org/html/rfc6749#section-3.1) | ||
| */ | ||
| token(options) { | ||
| return async (req, res, next) => { | ||
| const request = new Request(req); | ||
| const response = new Response(res); | ||
| let token; | ||
| try { | ||
| token = await this.server.token(request, response, options); | ||
| } catch (err) { | ||
| this._handleError(res, response, err, next); | ||
| return; | ||
| } | ||
| res.locals.oauth = { token }; | ||
| if (this.continueMiddleware) { | ||
| next(); | ||
| } | ||
| return this._handleResponse(req, res, response); | ||
| return handleResponse.call(this, req, res, response); | ||
| }; | ||
|
|
||
| return fn.bind(this); | ||
| }; | ||
|
|
||
| /** | ||
| * Grant Middleware. | ||
| * | ||
| * Returns middleware that will grant tokens to valid requests. | ||
| * | ||
| * (See: https://tools.ietf.org/html/rfc6749#section-3.2) | ||
| */ | ||
|
|
||
| ExpressOAuthServer.prototype.token = function(options) { | ||
| const fn = async function(req, res, next) { | ||
| const request = new Request(req); | ||
| const response = new Response(res); | ||
|
|
||
| let token | ||
|
|
||
| try { | ||
| token = await this.server.token(request, response, options); | ||
| } catch (e) { | ||
| return handleError.call(this, e, req, res, response, next); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Grant Middleware. | ||
| * | ||
| * Returns middleware that will grant tokens to valid requests. | ||
| * | ||
| * (See: https://tools.ietf.org/html/rfc6749#section-3.2) | ||
| */ | ||
| _handleResponse(req, res, oauthResponse) { | ||
| if (oauthResponse.status === 302) { | ||
| const location = oauthResponse.headers.location; | ||
| delete oauthResponse.headers.location; | ||
| res.set(oauthResponse.headers); | ||
| res.redirect(location); | ||
| return; | ||
| res.locals.oauth = { token: token }; | ||
| if (this.continueMiddleware) { | ||
| next(); | ||
| } | ||
| res.set(oauthResponse.headers); | ||
| res.status(oauthResponse.status).send(oauthResponse.body); | ||
|
|
||
| return handleResponse.call(this, req, res, response); | ||
| }; | ||
|
|
||
| return fn.bind(this); | ||
| }; | ||
|
|
||
| /** | ||
| * Handle response. | ||
| */ | ||
| const handleResponse = function(req, res, response) { | ||
| if (response.status === 302) { | ||
| const location = response.headers.location; | ||
| delete response.headers.location; | ||
| res.set(response.headers); | ||
| res.redirect(location); | ||
| } else { | ||
| res.set(response.headers); | ||
| res.status(response.status).send(response.body); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Handles errors depending on the options of `this.useErrorHandler`. | ||
| * Either calls `next()` with the error (so the application can handle it), or returns immediately a response with the error. | ||
| */ | ||
| _handleError(res, oauthResponse, error, next) { | ||
| if (this.useErrorHandler) { | ||
| return next(error); | ||
| } | ||
| /** | ||
| * Handle error. | ||
| */ | ||
|
|
||
| if (oauthResponse) { | ||
| res.set(oauthResponse.headers); | ||
| const handleError = function(e, req, res, response, next) { | ||
| if (this.useErrorHandler === true) { | ||
| next(e); | ||
| } else { | ||
| if (response) { | ||
| res.set(response.headers); | ||
| } | ||
|
|
||
| res.status(error.code || 500); | ||
| res.status(e.code); | ||
|
|
||
| if (error instanceof UnauthorizedRequestError) { | ||
| if (e instanceof UnauthorizedRequestError) { | ||
| return res.send(); | ||
| } | ||
|
|
||
| return res.send({ error: error.name, error_description: error.message }); | ||
| res.send({ error: e.name, error_description: e.message }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Export constructor. | ||
| */ | ||
|
|
||
| module.exports = ExpressOAuthServer; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.