summaryrefslogtreecommitdiff
path: root/lib/web/auth
diff options
context:
space:
mode:
authorChristoph Witzany2017-10-29 11:16:40 +0100
committerChristoph Witzany2017-10-31 10:34:51 +0100
commit5cda55086acfc1000f0a0062045db50ad415db59 (patch)
tree4cad35c71d521507013f33bcd730143847234fb6 /lib/web/auth
parentdad5798472406aad08b1b1c5433f314fdaa679e7 (diff)
Add mattermost authentication
Diffstat (limited to 'lib/web/auth')
-rw-r--r--lib/web/auth/index.js1
-rw-r--r--lib/web/auth/mattermost/index.js49
2 files changed, 50 insertions, 0 deletions
diff --git a/lib/web/auth/index.js b/lib/web/auth/index.js
index b5ca8434..4b618101 100644
--- a/lib/web/auth/index.js
+++ b/lib/web/auth/index.js
@@ -33,6 +33,7 @@ if (config.isFacebookEnable) authRouter.use(require('./facebook'))
if (config.isTwitterEnable) authRouter.use(require('./twitter'))
if (config.isGitHubEnable) authRouter.use(require('./github'))
if (config.isGitLabEnable) authRouter.use(require('./gitlab'))
+if (config.isMattermostEnable) authRouter.use(require('./mattermost'))
if (config.isDropboxEnable) authRouter.use(require('./dropbox'))
if (config.isGoogleEnable) authRouter.use(require('./google'))
if (config.isLDAPEnable) authRouter.use(require('./ldap'))
diff --git a/lib/web/auth/mattermost/index.js b/lib/web/auth/mattermost/index.js
new file mode 100644
index 00000000..9ccf3de5
--- /dev/null
+++ b/lib/web/auth/mattermost/index.js
@@ -0,0 +1,49 @@
+'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')
+const {setReturnToFromReferer, passportGeneralCallback} = require('../utils')
+
+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,
+ callbackURL: config.serverurl + '/auth/mattermost/callback'
+}, passportGeneralCallback)
+
+mattermostStrategy.userProfile = (accessToken, done) => {
+ mattermost.setUrl(config.mattermost.baseURL)
+ mattermost.token = accessToken
+ mattermost.useHeaderToken()
+ mattermost.getMe(
+ (data) => {
+ done(null, data)
+ },
+ (err) => {
+ done(err)
+ }
+ )
+}
+
+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', {
+ successReturnToOrRedirect: config.serverurl + '/',
+ failureRedirect: config.serverurl + '/'
+ })
+)