-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes eggjs/egg#38
- Loading branch information
Showing
23 changed files
with
840 additions
and
75 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -9,7 +9,6 @@ module.exports = { | |
], | ||
devdep: [ | ||
'egg', | ||
'egg-ci', | ||
'egg-bin', | ||
'autod', | ||
'eslint', | ||
|
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
'use strict'; | ||
|
||
const KoaPassport = require('./lib/passport'); | ||
const assert = require('assert'); | ||
const Passport = require('./lib/passport'); | ||
|
||
module.exports = app => { | ||
app.passport = new KoaPassport(); | ||
app.passport = new Passport(app); | ||
|
||
assert(app.config.coreMiddleware.includes('session'), '[egg-passport] session middleware must exists'); | ||
app.config.coreMiddleware.push('passportInitialize'); | ||
app.config.coreMiddleware.push('passportSession'); | ||
}; |
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,54 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
/** | ||
* mock an authenticated user | ||
* | ||
* @param {Object} [user] - mock user data | ||
*/ | ||
mockUser(user) { | ||
user = Object.assign({ | ||
provider: 'mock', | ||
id: '10086', | ||
name: 'mock_name', | ||
displayName: 'mock displayName', | ||
photo: 'https://tva2.sinaimg.cn/crop.0.0.180.180.180/61c56ebcjw1e8qgp5bmzyj2050050aa8.jpg', | ||
profile: { | ||
photos: [ | ||
{ value: 'http://tva2.sinaimg.cn/crop.0.0.180.180.180/61c56ebcjw1e8qgp5bmzyj2050050aa8.jpg' }, | ||
], | ||
_raw: '{}', | ||
_json: { | ||
id: '10086', | ||
screen_name: 'mock_name', | ||
displayName: 'mock displayName', | ||
}, | ||
}, | ||
}, user); | ||
|
||
const createContext = this.createContext; | ||
this.mm(this, 'createContext', (req, res) => { | ||
req.user = user; | ||
const ctx = createContext.call(this, req, res); | ||
return ctx; | ||
}); | ||
}, | ||
|
||
/** | ||
* mock a context instance with authenticated user | ||
* | ||
* @param {Object} [user] - mock user data | ||
* @return {Context} ctx - context instance | ||
*/ | ||
mockUserContext(user) { | ||
this.mockUser(user); | ||
const ctx = this.mockContext(); | ||
// ctx.req is not the http request | ||
// login, logout, isAuthenticated, isUnauthenticated | ||
ctx.req.login = () => Promise.resolve(); | ||
ctx.req.logout = () => {}; | ||
ctx.req.isAuthenticated = () => true; | ||
ctx.req.isUnauthenticated = () => false; | ||
return ctx; | ||
}, | ||
}; |
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,48 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
get user() { | ||
return this.req[this.app.passport._userProperty]; | ||
}, | ||
|
||
// https://github.com/jaredhanson/passport/blob/master/lib/http/request.js | ||
// proxy login, logout, isAuthenticated, isUnauthenticated to ctx.req | ||
|
||
/** | ||
* Initiate a login session for `user`. | ||
* | ||
* @param {Object} user - authenticated user | ||
* @param {Object} [options] - login options | ||
* - {Boolean} options.session - Save login state in session, defaults to true | ||
* @return {Promise} success or not promise instance | ||
* | ||
* @api public | ||
*/ | ||
login(user, options) { | ||
return new Promise((resolve, reject) => { | ||
this.req.login(user, options, err => { | ||
if (err) return reject(err); | ||
resolve(); | ||
}); | ||
}); | ||
}, | ||
|
||
/** | ||
* Terminate an existing login session. | ||
* | ||
* @api public | ||
*/ | ||
logout(...args) { | ||
this.req.logout(...args); | ||
}, | ||
|
||
/** | ||
* Test if request is authenticated. | ||
* | ||
* @return {Boolean} - if authenticated return true | ||
* @api public | ||
*/ | ||
isAuthenticated() { | ||
return this.req.isAuthenticated(); | ||
}, | ||
}; |
Oops, something went wrong.