2017-10-29 10:16:40 +00:00
|
|
|
'use strict'
|
2019-04-15 05:03:13 +00:00
|
|
|
require('babel-polyfill')
|
2019-04-15 05:36:44 +00:00
|
|
|
require('isomorphic-fetch')
|
2017-10-29 10:16:40 +00:00
|
|
|
const Router = require('express').Router
|
|
|
|
const passport = require('passport')
|
2019-04-15 05:03:13 +00:00
|
|
|
const MattermostClient = require('mattermost-redux/client/client4').default
|
2017-10-29 10:16:40 +00:00
|
|
|
const OAuthStrategy = require('passport-oauth2').Strategy
|
2020-01-04 22:30:23 +00:00
|
|
|
const config = require('../../config')
|
2019-04-12 09:58:06 +00:00
|
|
|
const { setReturnToFromReferer, passportGeneralCallback } = require('../utils')
|
2017-10-29 10:16:40 +00:00
|
|
|
|
2019-08-01 16:59:29 +00:00
|
|
|
const mattermostAuth = module.exports = Router()
|
2017-10-29 10:16:40 +00:00
|
|
|
|
2019-04-15 05:03:13 +00:00
|
|
|
const mattermostClient = new MattermostClient()
|
|
|
|
|
2019-08-01 16:59:29 +00:00
|
|
|
const mattermostStrategy = new OAuthStrategy({
|
2017-10-29 10:16:40 +00:00
|
|
|
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) => {
|
2019-04-15 05:03:13 +00:00
|
|
|
mattermostClient.setUrl(config.mattermost.baseURL)
|
|
|
|
mattermostClient.setToken(accessToken)
|
|
|
|
mattermostClient.getMe()
|
|
|
|
.then((data) => done(null, data))
|
|
|
|
.catch((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
|
|
|
})
|
|
|
|
)
|