2017-10-29 10:16:40 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
const passport = require('passport')
|
|
|
|
const Mattermost = require('mattermost')
|
|
|
|
const OAuthStrategy = require('passport-oauth2').Strategy
|
|
|
|
const config = require('../../../config')
|
2019-05-30 22:27:56 +00:00
|
|
|
const { setReturnToFromReferer, passportGeneralCallback } = require('../utils')
|
2017-10-29 10:16:40 +00:00
|
|
|
|
|
|
|
const mattermost = new Mattermost.Client()
|
|
|
|
|
|
|
|
let mattermostAuth = module.exports = Router()
|
|
|
|
|
|
|
|
let mattermostStrategy = new OAuthStrategy({
|
|
|
|
authorizationURL: config.mattermost.baseURL + '/oauth/authorize',
|
|
|
|
tokenURL: config.mattermost.baseURL + '/oauth/access_token',
|
|
|
|
clientID: config.mattermost.clientID,
|
|
|
|
clientSecret: config.mattermost.clientSecret,
|
2018-03-07 14:17:35 +00:00
|
|
|
callbackURL: config.serverURL + '/auth/mattermost/callback'
|
2017-10-29 10:16:40 +00:00
|
|
|
}, passportGeneralCallback)
|
|
|
|
|
|
|
|
mattermostStrategy.userProfile = (accessToken, done) => {
|
|
|
|
mattermost.setUrl(config.mattermost.baseURL)
|
|
|
|
mattermost.token = accessToken
|
|
|
|
mattermost.useHeaderToken()
|
|
|
|
mattermost.getMe(
|
2019-05-30 22:27:56 +00:00
|
|
|
(data) => {
|
|
|
|
done(null, data)
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
done(err)
|
|
|
|
}
|
2017-10-29 10:16:40 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
passport.use(mattermostStrategy)
|
|
|
|
|
|
|
|
mattermostAuth.get('/auth/mattermost', function (req, res, next) {
|
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('oauth2')(req, res, next)
|
|
|
|
})
|
|
|
|
|
|
|
|
// mattermost auth callback
|
|
|
|
mattermostAuth.get('/auth/mattermost/callback',
|
|
|
|
passport.authenticate('oauth2', {
|
2018-03-07 14:17:35 +00:00
|
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
|
|
failureRedirect: config.serverURL + '/'
|
2017-10-29 10:16:40 +00:00
|
|
|
})
|
|
|
|
)
|