From b0f61d66055aefd6fc52069be2e4e8ad57cff9c1 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Sun, 20 Sep 2020 20:15:41 -0400 Subject: [PATCH] feat: rocket.chat auth module --- client/static/svg/auth-icon-rocketchat.svg | 1 + .../rocketchat/authentication.js | 64 +++++++++++++++++++ .../authentication/rocketchat/definition.yml | 29 +++++++++ 3 files changed, 94 insertions(+) create mode 100644 client/static/svg/auth-icon-rocketchat.svg create mode 100644 server/modules/authentication/rocketchat/authentication.js create mode 100644 server/modules/authentication/rocketchat/definition.yml diff --git a/client/static/svg/auth-icon-rocketchat.svg b/client/static/svg/auth-icon-rocketchat.svg new file mode 100644 index 00000000..5a72e53f --- /dev/null +++ b/client/static/svg/auth-icon-rocketchat.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/server/modules/authentication/rocketchat/authentication.js b/server/modules/authentication/rocketchat/authentication.js new file mode 100644 index 00000000..944d667c --- /dev/null +++ b/server/modules/authentication/rocketchat/authentication.js @@ -0,0 +1,64 @@ +const _ = require('lodash') + +/* global WIKI */ + +// ------------------------------------ +// Rocket.chat Account +// ------------------------------------ + +const OAuth2Strategy = require('passport-oauth2').Strategy + +module.exports = { + init (passport, conf) { + const siteURL = conf.siteURL.slice(-1) === '/' ? conf.siteURL.slice(0, -1) : conf.siteURL + + OAuth2Strategy.prototype.userProfile = function (accessToken, cb) { + this._oauth2.get(`${siteURL}/api/v1/me`, accessToken, (err, body, res) => { + if (err) { + WIKI.logger.warn('Rocket.chat - Failed to fetch user profile.') + return cb(err) + } + try { + const usr = JSON.parse(body) + cb(null, { + id: usr._id, + displayName: _.isEmpty(usr.name) ? usr.username : usr.name, + email: usr.email, + picture: usr.avatarUrl + }) + } catch (err) { + WIKI.logger.warn('Rocket.chat - Failed to parse user profile.') + cb(err) + } + }) + } + + passport.use('rocketchat', + new OAuth2Strategy({ + authorizationURL: `${siteURL}/oauth/authorize`, + tokenURL: `${siteURL}/oauth/token`, + clientID: conf.clientId, + clientSecret: conf.clientSecret, + callbackURL: conf.callbackURL, + passReqToCallback: true + }, async (req, accessToken, refreshToken, profile, cb) => { + try { + const user = await WIKI.models.users.processProfile({ + providerKey: req.params.strategy, + profile + }) + cb(null, user) + } catch (err) { + cb(err, null) + } + }) + ) + }, + logout (conf) { + if (!conf.logoutURL) { + return '/' + } else { + return conf.logoutURL + } + } +} diff --git a/server/modules/authentication/rocketchat/definition.yml b/server/modules/authentication/rocketchat/definition.yml new file mode 100644 index 00000000..a47af49c --- /dev/null +++ b/server/modules/authentication/rocketchat/definition.yml @@ -0,0 +1,29 @@ +key: rocketchat +title: Rocket.chat +description: Communicate and collaborate with your team, share files, chat in real-time, or switch to video/audio conferencing. +author: requarks.io +logo: https://static.requarks.io/logo/rocketchat.svg +color: red accent-3 +website: https://rocket.chat/ +isAvailable: true +useForm: false +scopes: + - openid + - profile + - email +props: + clientId: + type: String + title: Client ID + hint: Application Client ID + order: 1 + clientSecret: + type: String + title: Client Secret + hint: Application Client Secret + order: 2 + siteURL: + type: String + title: Rocket.chat Site URL + hint: The base URL of your Rocket.chat site (e.g. https://example.rocket.chat) + order: 3