koa-passport
Advanced tools
Comparing version 1.2.0 to 2.0.0
# Changelog | ||
## 2.0.0 | ||
- use promises rather than generators for `koa@2.x` compatibility | ||
- use some es6 features | ||
## 1.2.0 | ||
- upgrade `passport` to `^0.3.0` | ||
## 1.1.5 | ||
@@ -4,0 +13,0 @@ |
/** | ||
* Module dependencies. | ||
*/ | ||
var passport = require('passport') | ||
var co = require('co') | ||
const passport = require('passport') | ||
@@ -10,4 +9,4 @@ /** | ||
*/ | ||
var _initialize = require('passport/lib/middleware/initialize') | ||
var _authenticate = require('passport/lib/middleware/authenticate') | ||
const _initialize = require('passport/lib/middleware/initialize') | ||
const _authenticate = require('passport/lib/middleware/authenticate') | ||
@@ -21,9 +20,7 @@ /** | ||
function initialize(passport) { | ||
var middleware = _initialize(passport) | ||
return function* passportInitialize(next) { | ||
var ctx = this | ||
const middleware = promisify(_initialize(passport)) | ||
return function passportInitialize(ctx, next) { | ||
// koa <-> connect compatibility: | ||
this.passport = {} | ||
var userProperty = passport._userProperty || 'user' | ||
ctx.passport = {} | ||
const userProperty = passport._userProperty || 'user' | ||
// check ctx.req has the userProperty | ||
@@ -42,10 +39,16 @@ if (!ctx.req.hasOwnProperty(userProperty)) { | ||
var req = createReqMock(ctx) | ||
// create mock object for express' req object | ||
const req = createReqMock(ctx) | ||
// add aliases for passport's request extensions to Koa's context | ||
var login = ctx.req.login | ||
var logout = ctx.req.logout | ||
const login = ctx.req.login | ||
const logout = ctx.req.logout | ||
ctx.login = ctx.logIn = function(user, options) { | ||
return login.bind(req, user, options) | ||
return new Promise((resolve, reject) => { | ||
login.call(req, user, options, err => { | ||
if (err) reject(err) | ||
else resolve() | ||
}) | ||
}) | ||
} | ||
@@ -57,4 +60,5 @@ ctx.req.login = ctx.req.logIn = login.bind(req) | ||
yield middleware.bind(middleware, req, ctx) | ||
yield next | ||
return middleware(req, ctx).then(function() { | ||
return next() | ||
}) | ||
} | ||
@@ -81,38 +85,34 @@ } | ||
if (callback) { | ||
if (callback.constructor.name !== 'GeneratorFunction') { | ||
throw TypeError('Your custom authentication callback must be a Generator Function') | ||
} | ||
// When the callback is set, neither `next`, `res.redirect` or `res.end` | ||
// are called. That is, a workaround to catch the `callback` is required. | ||
// The `passportAuthenticate()` method below will therefore set | ||
// `callback.done`. Then, once the authentication finishes, the modified | ||
// callback yields the original one and afterwards triggers `callback.done` | ||
// to inform `passportAuthenticate()` that we are ready. | ||
var _callback = callback | ||
callback = co.wrap(function*(err, user, info, status) { | ||
try { | ||
yield _callback(err, user, info, status) | ||
callback.done(null, false) | ||
} catch (err) { | ||
callback.done(err); | ||
// `callback.resolve` and `callback.reject`. Then, once the authentication | ||
// finishes, the modified callback calls the original one and afterwards | ||
// triggers either `callback.resolve` or `callback.reject` to inform | ||
// `passportAuthenticate()` that we are ready. | ||
const _callback = callback | ||
callback = function(err, user, info, status) { | ||
if (err) { | ||
callback.reject(err) | ||
} else { | ||
Promise.resolve(_callback(user, info, status)) | ||
.then(() => callback.resolve(false)) | ||
} | ||
}) | ||
} | ||
} | ||
var middleware = _authenticate(passport, name, options, callback) | ||
return function* passportAuthenticate(next) { | ||
var ctx = this | ||
const middleware = promisify(_authenticate(passport, name, options, callback)) | ||
return function passportAuthenticate(ctx, next) { | ||
// this functions wraps the connect middleware | ||
// to catch `next`, `res.redirect` and `res.end` calls | ||
var cont = yield function(done) { | ||
const p = new Promise((resolve, reject) => { | ||
// mock the `req` object | ||
var req = createReqMock(ctx) | ||
const req = createReqMock(ctx) | ||
// mock the `res` object | ||
var res = { | ||
const res = { | ||
redirect: function(url) { | ||
ctx.redirect(url) | ||
done(null, false) | ||
resolve(false) | ||
}, | ||
@@ -122,3 +122,3 @@ setHeader: ctx.set.bind(ctx), | ||
if (content) ctx.body = content | ||
done(null, false) | ||
resolve(false) | ||
}, | ||
@@ -133,15 +133,19 @@ set statusCode(status) { | ||
// update the custom callback above | ||
if (callback) { | ||
callback.done = done | ||
callback.resolve = resolve | ||
callback.reject = reject | ||
} | ||
// call the connect middleware | ||
middleware(req, res, done) | ||
} | ||
middleware(req, res).then(resolve, reject) | ||
}) | ||
// cont equals `false` when `res.redirect` or `res.end` got called | ||
// in this case, yield next to continue through Koa's middleware stack | ||
if (cont !== false) { | ||
yield next | ||
} | ||
// in this case, call next to continue through Koa's middleware stack | ||
return p.then(cont => { | ||
if (cont !== false) { | ||
return next() | ||
} | ||
}) | ||
} | ||
@@ -151,2 +155,18 @@ } | ||
/** | ||
* Passport's authorize middleware for Koa. | ||
* | ||
* @param {String|Array} name | ||
* @param {Object} options | ||
* @param {GeneratorFunction} callback | ||
* @return {GeneratorFunction} | ||
* @api private | ||
*/ | ||
function authorize(passport, name, options, callback) { | ||
options = options || {} | ||
options.assignProperty = 'account' | ||
return authenticate(passport, name, options, callback) | ||
} | ||
/** | ||
* Framework support for Koa. | ||
@@ -164,4 +184,5 @@ * | ||
return { | ||
initialize: initialize, | ||
authenticate: authenticate | ||
initialize: initialize, | ||
authenticate: authenticate, | ||
authorize: authorize | ||
} | ||
@@ -171,6 +192,17 @@ } | ||
// create request mock | ||
var properties = require('./request') | ||
const properties = require('./request') | ||
function createReqMock(ctx) { | ||
var req = Object.create(ctx.request, properties) | ||
const req = Object.create(ctx.request, properties) | ||
return req | ||
} | ||
function promisify(expressMiddleware) { | ||
return function(req, res) { | ||
return new Promise(function(resolve, reject) { | ||
expressMiddleware(req, res, function(err, result) { | ||
if (err) reject(err) | ||
else resolve(result) | ||
}) | ||
}) | ||
} | ||
} |
@@ -29,6 +29,7 @@ // Koa and Express are fundamental different in how they deal with extensions | ||
// Property/Method names to be delegated | ||
var keys = [ | ||
const keys = [ | ||
// passport | ||
'_passport', | ||
'user', | ||
'account', | ||
'login', | ||
@@ -73,3 +74,3 @@ 'logIn', | ||
// create a delegate for each key | ||
var properties = module.exports = { | ||
const properties = module.exports = { | ||
// mock express' .get('trust proxy') | ||
@@ -80,3 +81,3 @@ app: { | ||
get: function() { | ||
var ctx = this.ctx | ||
const ctx = this.ctx | ||
return { | ||
@@ -98,3 +99,3 @@ get: function(key) { | ||
get: function() { | ||
var obj = getObject(this.ctx, key) | ||
const obj = getObject(this.ctx, key) | ||
if (!obj) return undefined | ||
@@ -113,3 +114,3 @@ | ||
set: function(value) { | ||
var obj = getObject(this.ctx, key) || this.ctx.passport | ||
const obj = getObject(this.ctx, key) || this.ctx.passport | ||
obj[key] = value | ||
@@ -116,0 +117,0 @@ } |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Passport middleware for Koa", | ||
@@ -14,21 +14,20 @@ "keywords": [ | ||
"auth", | ||
"authentication" | ||
"authentication", | ||
"authorization" | ||
], | ||
"homepage": "https://github.com/rkusa/koa-passport", | ||
"license": "MIT", | ||
"main": "./lib", | ||
"dependencies": { | ||
"co": "^4.4.0", | ||
"passport": "^0.3.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.2", | ||
"co-supertest": "0.0.10", | ||
"gulp": "^3.8", | ||
"gulp-mocha": "^2.0", | ||
"koa": "^1.0", | ||
"koa-bodyparser": "^2.0", | ||
"koa-route": "^2.1", | ||
"mocha": "^2.2", | ||
"passport-local": "^1.0", | ||
"supertest": "^1.0" | ||
"chai": "^3.2.0", | ||
"koa": "^2.0.0-alpha.3", | ||
"koa-bodyparser": "^3.0.0", | ||
"koa-route": "^3.0.0", | ||
"mocha": "^2.2.0", | ||
"passport-local": "^1.0.0", | ||
"supertest": "^1.0.0", | ||
"supertest-as-promised": "^2.0.2" | ||
}, | ||
@@ -41,8 +40,7 @@ "bugs": "https://github.com/rkusa/koa-passport/issues", | ||
"scripts": { | ||
"test": "node --harmony `which gulp`", | ||
"coveralls": "node --harmony `which gulp` coverage" | ||
"test": "mocha --harmony" | ||
}, | ||
"engines": { | ||
"node": ">=0.11.2" | ||
"node": ">= 4" | ||
} | ||
} |
@@ -7,4 +7,6 @@ # koa-passport | ||
[![Dependency Status][dependencies]](https://david-dm.org/rkusa/koa-passport) | ||
[![Build Status][drone]](https://ci.rkusa.st/github.com/rkusa/koa-passport) | ||
[![Build Status][drone]](https://ci.rkusa.st/rkusa/koa-passport) | ||
**Notice: `koa-passport@2` support `koa@2`, for `koa@1` use `koa-passport@1`.** | ||
## Usage | ||
@@ -14,11 +16,12 @@ | ||
// body parser | ||
var bodyParser = require('koa-bodyparser') | ||
const bodyParser = require('koa-bodyparser') | ||
app.use(bodyParser()) | ||
// Sessions | ||
var session = require('koa-session') | ||
const convert = require('koa-convert') // necessary until koa-generic-session has been updated to support koa@2 | ||
const session = require('koa-generic-session') | ||
app.keys = ['secret'] | ||
app.use(session(app)) | ||
app.use(convert(session())) | ||
var passport = require('koa-passport') | ||
const passport = require('koa-passport') | ||
app.use(passport.initialize()) | ||
@@ -30,25 +33,8 @@ app.use(passport.session()) | ||
## MIT License | ||
## License | ||
Copyright (c) 2014 Markus Ast | ||
[MIT](LICENSE) | ||
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. | ||
[npm]: http://img.shields.io/npm/v/koa-passport.svg?style=flat-square | ||
[dependencies]: http://img.shields.io/david/rkusa/koa-passport.svg?style=flat-square | ||
[drone]: http://ci.rkusa.st/api/badge/github.com/rkusa/koa-passport/status.svg?branch=master&style=flat-square | ||
[drone]: http://ci.rkusa.st/api/badges/rkusa/koa-passport/status.svg?style=flat-square |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
8
302
14421
9
38