mirror of
https://github.com/openstf/stf
synced 2025-10-05 10:39:25 +02:00
Add an OAuth2 auth provider.
This commit is contained in:
parent
2ff16baf7a
commit
af09fc084a
3 changed files with 175 additions and 0 deletions
51
lib/units/auth/oauth2/index.js
Normal file
51
lib/units/auth/oauth2/index.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
var http = require('http')
|
||||
|
||||
// @todo Figure something out
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
|
||||
|
||||
var express = require('express')
|
||||
var passport = require('passport')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
var urlutil = require('../../../util/urlutil')
|
||||
var jwtutil = require('../../../util/jwtutil')
|
||||
var Strategy = require('./strategy')
|
||||
|
||||
module.exports = function(options) {
|
||||
var log = logger.createLogger('auth-oauth2')
|
||||
, app = express()
|
||||
, server = http.createServer(app)
|
||||
|
||||
app.set('strict routing', true)
|
||||
app.set('case sensitive routing', true)
|
||||
|
||||
function verify(accessToken, refreshToken, profile, done) {
|
||||
done(null, profile)
|
||||
}
|
||||
|
||||
passport.use(new Strategy(options.oauth, verify))
|
||||
|
||||
app.use(passport.initialize())
|
||||
app.use(passport.authenticate('oauth2', {
|
||||
failureRedirect: '/auth/oauth/'
|
||||
, session: false
|
||||
}))
|
||||
|
||||
app.get(
|
||||
'/auth/oauth/callback'
|
||||
, function(req, res) {
|
||||
res.redirect(urlutil.addParams(options.appUrl, {
|
||||
jwt: jwtutil.encode({
|
||||
payload: {
|
||||
email: req.user.email
|
||||
, name: req.user.email.split('@', 1).join('')
|
||||
}
|
||||
, secret: options.secret
|
||||
})
|
||||
}))
|
||||
}
|
||||
)
|
||||
|
||||
server.listen(options.port)
|
||||
log.info('Listening on port %d', options.port)
|
||||
}
|
32
lib/units/auth/oauth2/strategy.js
Normal file
32
lib/units/auth/oauth2/strategy.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
var util = require('util')
|
||||
|
||||
var oauth2 = require('passport-oauth2')
|
||||
|
||||
function Strategy(options, verify) {
|
||||
oauth2.Strategy.call(this, options, verify)
|
||||
if (!options.authorizationURL) {
|
||||
throw new TypeError('OAuth2Strategy requires a userinfoURL option')
|
||||
}
|
||||
this._userinfoURL = options.userinfoURL
|
||||
this._oauth2.useAuthorizationHeaderforGET(true)
|
||||
}
|
||||
|
||||
util.inherits(Strategy, oauth2.Strategy)
|
||||
|
||||
Strategy.prototype.userProfile = function(accessToken, callback) {
|
||||
this._oauth2.get(this._userinfoURL, accessToken, function(err, data) {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
else {
|
||||
try {
|
||||
return callback(null, JSON.parse(data))
|
||||
}
|
||||
catch (err) {
|
||||
return callback(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = Strategy
|
Loading…
Add table
Add a link
Reference in a new issue