forked from nextstrain/nextstrain.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
458 additions
and
186 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
================================================================================ | ||
The following copyright and license applies to the original copy of | ||
"lib/strategy.js" from the passport-http-bearer project into this project as | ||
"src/authn/bearer.js". Subsequent modifications to this project's copy are | ||
made under the same MIT license. | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
Copyright (c) 2011-2013 Jared Hanson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
================================================================================ |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Originally copied from "lib/strategy.js" in the passport-http-bearer project. | ||
* The original copy is licensed under the MIT license. See the | ||
* LICENSE.third-party file distributed alongside this project's own LICENSE file. | ||
* | ||
* Subsequent modifications have been made. These and any future modifications | ||
* are licensed under the same MIT license. | ||
*/ | ||
|
||
/* eslint-disable */ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
var passport = require('passport-strategy') | ||
, util = require('util'); | ||
|
||
|
||
/** | ||
* Creates an instance of `Strategy`. | ||
* | ||
* The HTTP Bearer authentication strategy authenticates requests based on | ||
* a bearer token contained in the `Authorization` header field. | ||
* | ||
* Applications must supply a `verify` callback, for which the function | ||
* signature is: | ||
* | ||
* function(token, done) { ... } | ||
* | ||
* `token` is the bearer token provided as a credential. The verify callback | ||
* is responsible for finding the user who posesses the token, and invoking | ||
* `done` with the following arguments: | ||
* | ||
* done(err, user, info); | ||
* | ||
* If the token is not valid, `user` should be set to `false` to indicate an | ||
* authentication failure. Additional token `info` can optionally be passed as | ||
* a third argument, which will be set by Passport at `req.authInfo`, where it | ||
* can be used by later middleware for access control. This is typically used | ||
* to pass any scope associated with the token. | ||
* | ||
* Options: | ||
* | ||
* - `realm` authentication realm, defaults to "Users" | ||
* - `scope` list of scope values indicating the required scope of the access | ||
* token for accessing the requested resource | ||
* - `passIfMissing` instead of failing, pass if no `Authorization` header was | ||
* present in the request (defaults to false) | ||
* | ||
* Examples: | ||
* | ||
* passport.use(new BearerStrategy( | ||
* function(token, done) { | ||
* User.findByToken({ token: token }, function (err, user) { | ||
* if (err) { return done(err); } | ||
* if (!user) { return done(null, false); } | ||
* return done(null, user, { scope: 'read' }); | ||
* }); | ||
* } | ||
* )); | ||
* | ||
* For further details on HTTP Bearer authentication, refer to [The OAuth 2.0 Authorization Protocol: Bearer Tokens](http://tools.ietf.org/html/draft-ietf-oauth-v2-bearer) | ||
* | ||
* @constructor | ||
* @param {Object} [options] | ||
* @param {Function} verify | ||
* @api public | ||
*/ | ||
function Strategy(options, verify) { | ||
if (typeof options == 'function') { | ||
verify = options; | ||
options = {}; | ||
} | ||
if (!verify) { throw new TypeError('HTTPBearerStrategy requires a verify callback'); } | ||
|
||
passport.Strategy.call(this); | ||
this.name = 'bearer'; | ||
this._verify = verify; | ||
this._realm = options.realm || 'Users'; | ||
if (options.scope) { | ||
this._scope = (Array.isArray(options.scope)) ? options.scope : [ options.scope ]; | ||
} | ||
this._passReqToCallback = options.passReqToCallback; | ||
this._passIfMissing = options.passIfMissing === undefined ? false : options.passIfMissing; | ||
} | ||
|
||
/** | ||
* Inherit from `passport.Strategy`. | ||
*/ | ||
util.inherits(Strategy, passport.Strategy); | ||
|
||
/** | ||
* Authenticate request based on the contents of a HTTP Bearer authorization | ||
* header, body parameter, or query parameter. | ||
* | ||
* @param {Object} req | ||
* @api protected | ||
*/ | ||
Strategy.prototype.authenticate = function(req) { | ||
var token; | ||
|
||
if (req.headers && req.headers.authorization) { | ||
var parts = req.headers.authorization.split(' '); | ||
if (parts.length == 2) { | ||
var scheme = parts[0] | ||
, credentials = parts[1]; | ||
|
||
if (/^Bearer$/i.test(scheme)) { | ||
token = credentials; | ||
} | ||
} else { | ||
return this.fail(400); | ||
} | ||
} | ||
else if (this._passIfMissing) { | ||
return this.pass(); | ||
} | ||
|
||
if (!token) { return this.fail(this._challenge()); } | ||
|
||
var self = this; | ||
|
||
function verified(err, user, info) { | ||
if (err) { return self.error(err); } | ||
if (!user) { | ||
if (typeof info == 'string') { | ||
info = { message: info } | ||
} | ||
info = info || {}; | ||
return self.fail(self._challenge('invalid_token', info.message)); | ||
} | ||
self.success(user, info); | ||
} | ||
|
||
if (self._passReqToCallback) { | ||
this._verify(req, token, verified); | ||
} else { | ||
this._verify(token, verified); | ||
} | ||
}; | ||
|
||
/** | ||
* Build authentication challenge. | ||
* | ||
* @api private | ||
*/ | ||
Strategy.prototype._challenge = function(code, desc, uri) { | ||
var challenge = 'Bearer realm="' + this._realm + '"'; | ||
if (this._scope) { | ||
challenge += ', scope="' + this._scope.join(' ') + '"'; | ||
} | ||
if (code) { | ||
challenge += ', error="' + code + '"'; | ||
} | ||
if (desc && desc.length) { | ||
challenge += ', error_description="' + desc + '"'; | ||
} | ||
if (uri && uri.length) { | ||
challenge += ', error_uri="' + uri + '"'; | ||
} | ||
|
||
return challenge; | ||
}; | ||
|
||
|
||
/** | ||
* Expose `Strategy`. | ||
*/ | ||
module.exports = Strategy; |
This file contains 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