2017-06-27 19:08:05 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
const passport = require('passport')
|
2020-02-27 23:49:05 +08:00
|
|
|
|
2020-01-05 06:30:23 +08:00
|
|
|
const config = require('../../config')
|
2019-04-12 17:58:06 +08:00
|
|
|
const { setReturnToFromReferer, passportGeneralCallback } = require('../utils')
|
2020-02-27 23:49:05 +08:00
|
|
|
const { OAuth2CustomStrategy } = require('./strategy')
|
2017-06-27 19:08:05 +02:00
|
|
|
|
2019-08-02 00:59:38 +08:00
|
|
|
const oauth2Auth = module.exports = Router()
|
2017-06-27 19:08:05 +02:00
|
|
|
|
|
|
|
passport.use(new OAuth2CustomStrategy({
|
|
|
|
authorizationURL: config.oauth2.authorizationURL,
|
|
|
|
tokenURL: config.oauth2.tokenURL,
|
|
|
|
clientID: config.oauth2.clientID,
|
|
|
|
clientSecret: config.oauth2.clientSecret,
|
|
|
|
callbackURL: config.serverURL + '/auth/oauth2/callback',
|
2020-02-28 02:13:58 +08:00
|
|
|
userProfileURL: config.oauth2.userProfileURL,
|
2020-05-11 15:57:58 +02:00
|
|
|
state: config.oauth2.state,
|
2020-02-28 02:13:58 +08:00
|
|
|
scope: config.oauth2.scope
|
2017-06-27 19:08:05 +02:00
|
|
|
}, passportGeneralCallback))
|
|
|
|
|
|
|
|
oauth2Auth.get('/auth/oauth2', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('oauth2')(req, res, next)
|
|
|
|
})
|
|
|
|
|
|
|
|
// github auth callback
|
|
|
|
oauth2Auth.get('/auth/oauth2/callback',
|
|
|
|
passport.authenticate('oauth2', {
|
2018-11-27 22:13:18 +08:00
|
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
|
|
failureRedirect: config.serverURL + '/'
|
2017-06-27 19:08:05 +02:00
|
|
|
})
|
|
|
|
)
|