2017-04-11 21:41:14 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
const passport = require('passport')
|
|
|
|
const GithubStrategy = require('passport-github').Strategy
|
|
|
|
const config = require('../../../config')
|
2017-05-16 18:42:44 +00:00
|
|
|
const response = require('../../../response')
|
2019-04-12 09:58:06 +00:00
|
|
|
const { setReturnToFromReferer, passportGeneralCallback } = require('../utils')
|
2019-10-25 06:12:20 +00:00
|
|
|
const { URL } = require('url')
|
2017-04-11 21:41:14 +00:00
|
|
|
|
2019-08-01 16:58:52 +00:00
|
|
|
const githubAuth = module.exports = Router()
|
2017-04-11 21:41:14 +00:00
|
|
|
|
2019-10-25 06:12:20 +00:00
|
|
|
function githubUrl (path) {
|
|
|
|
return config.github.enterpriseURL && new URL(path, config.github.enterpriseURL).toString()
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:41:14 +00:00
|
|
|
passport.use(new GithubStrategy({
|
|
|
|
clientID: config.github.clientID,
|
|
|
|
clientSecret: config.github.clientSecret,
|
2019-10-25 06:12:20 +00:00
|
|
|
callbackURL: config.serverURL + '/auth/github/callback',
|
|
|
|
authorizationURL: githubUrl('login/oauth/authorize'),
|
|
|
|
tokenURL: githubUrl('login/oauth/access_token'),
|
|
|
|
userProfileURL: githubUrl('api/v3/user')
|
2017-04-11 21:41:14 +00:00
|
|
|
}, passportGeneralCallback))
|
|
|
|
|
|
|
|
githubAuth.get('/auth/github', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('github')(req, res, next)
|
|
|
|
})
|
|
|
|
|
|
|
|
// github auth callback
|
|
|
|
githubAuth.get('/auth/github/callback',
|
|
|
|
passport.authenticate('github', {
|
2018-03-07 14:17:35 +00:00
|
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
|
|
failureRedirect: config.serverURL + '/'
|
2017-04-11 21:41:14 +00:00
|
|
|
})
|
|
|
|
)
|
2017-05-16 18:42:44 +00:00
|
|
|
|
|
|
|
// github callback actions
|
|
|
|
githubAuth.get('/auth/github/callback/:noteId/:action', response.githubActions)
|