很简单 护照 linkedIn OAuth2 策略,与 lite profile 工作.
此分叉执行可选的回调函数, 用于通过客户端 ID 和 secret 每次在认证前直接访问 Oauth2 。 此函数称为 带有请求对象.
var LinkedInStrategy = require('@dmapper/passport-linkedin-oauth2').Strategy;
passport.use(new LinkedInStrategy({
getClient: async function(req) {
// some possible async operations
// ...
return {
id: '#####',
secret: '#####'
}
},
callbackURL: "http://127.0.0.1:3000/auth/linkedin/callback",
scope: ['r_emailaddress', 'r_liteprofile'],
},
function(accessToken, refreshToken, profile, cb) {
// To keep the example simple, the user's LinkedIn profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the LinkedIn account with a user record in your database,
// and return that user instead.
return done(null, profile);
}
));
====================================================================================
安装
npm 安装 @dmapper/ passport- connectionin-oaut2
使用量
登记战略
var LinkedInStrategy = require('@dmapper/passport-linkedin-oauth2').Strategy;
passport.use(new LinkedInStrategy({
clientID: LINKEDIN_KEY,
clientSecret: LINKEDIN_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/linkedin/callback",
scope: ['r_emailaddress', 'r_liteprofile'],
}, function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's LinkedIn profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the LinkedIn account with a user record in your database,
// and return that user instead.
return done(null, profile);
});
}));
然后认证为:
app.get('/auth/linkedin',
passport.authenticate('linkedin', { state: 'SOME STATE' }),
function(req, res){
// The request will be redirected to LinkedIn for authentication, so this
// function will not be called.
});
登录回调 :
app.get('/auth/linkedin/callback', passport.authenticate('linkedin', {
successRedirect: '/',
failureRedirect: '/login'
}));
见 这个 详细内容请访问 LinkedIn API.
自动处理 state 段落
这个 state param用于防止CSRF攻击,并且 aPI 链接。您可以要求护照处理发送和验证 state 通过参数 state: true 作为该战略的一个选择:
var LinkedInStrategy = require('@dmapper/passport-linkedin-oauth2').Strategy;
passport.use(new LinkedInStrategy({
clientID: LINKEDIN_KEY,
clientSecret: LINKEDIN_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/linkedin/callback",
scope: ['r_emailaddress', 'r_liteprofile'],
state: true
}, function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's LinkedIn profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the LinkedIn account with a user record in your database,
// and return that user instead.
return done(null, profile);
});
}));
然后认证为:
app.get('/auth/linkedin',
passport.authenticate('linkedin'),
function(req, res){
// The request will be redirected to LinkedIn for authentication, so this
// function will not be called.
});
问题报告
如果您找到错误或有特性请求, 请在此寄存器问题节中报告 。 请不要在公共 GitHub 问题跟踪器上报告安全漏洞 。 这个 负责任的披露程序 详细说明披露安全问题的程序.
作者
许可证
这个项目根据麻省理工学院的许可证获得许可。 见 自由主义 需要更多信息的文件.