From fd8bf4dbff8e52e60153a75880820036a89b1a77 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Sun, 8 Jul 2018 11:16:45 -0400 Subject: [PATCH] feat: authentication module refactor + added CAS module --- package.json | 1 + server/core/auth.js | 2 +- server/db/models/authentication.js | 24 +++++++++++----- .../{auth0.js => auth0/authentication.js} | 8 ------ .../authentication/auth0/definition.yml | 8 ++++++ .../{azure.js => azure/authentication.js} | 15 ---------- .../authentication/azure/definition.yml | 13 +++++++++ .../authentication/cas/authentication.js | 24 ++++++++++++++++ .../modules/authentication/cas/definition.yml | 7 +++++ .../{discord.js => discord/authentication.js} | 7 ----- .../authentication/discord/definition.yml | 7 +++++ .../{dropbox.js => dropbox/authentication.js} | 7 ----- .../authentication/dropbox/definition.yml | 7 +++++ .../authentication.js} | 7 ----- .../authentication/facebook/definition.yml | 7 +++++ .../{github.js => github/authentication.js} | 7 ----- .../authentication/github/definition.yml | 7 +++++ .../{google.js => google/authentication.js} | 7 ----- .../authentication/google/definition.yml | 7 +++++ .../{ldap.js => ldap/authentication.js} | 27 ------------------ .../authentication/ldap/definition.yml | 22 ++++++++++++++ .../{local.js => local/authentication.js} | 4 --- .../authentication/local/definition.yml | 5 ++++ .../authentication.js} | 7 ----- .../authentication/microsoft/definition.yml | 7 +++++ .../{oauth2.js => oauth2/authentication.js} | 9 ------ .../authentication/oauth2/definition.yml | 9 ++++++ .../{slack.js => slack/authentication.js} | 7 ----- .../authentication/slack/definition.yml | 7 +++++ .../{twitch.js => twitch/authentication.js} | 7 ----- .../authentication/twitch/definition.yml | 7 +++++ yarn.lock | Bin 470935 -> 471970 bytes 32 files changed, 163 insertions(+), 127 deletions(-) rename server/modules/authentication/{auth0.js => auth0/authentication.js} (83%) create mode 100644 server/modules/authentication/auth0/definition.yml rename server/modules/authentication/{azure.js => azure/authentication.js} (74%) create mode 100644 server/modules/authentication/azure/definition.yml create mode 100644 server/modules/authentication/cas/authentication.js create mode 100644 server/modules/authentication/cas/definition.yml rename server/modules/authentication/{discord.js => discord/authentication.js} (85%) create mode 100644 server/modules/authentication/discord/definition.yml rename server/modules/authentication/{dropbox.js => dropbox/authentication.js} (85%) create mode 100644 server/modules/authentication/dropbox/definition.yml rename server/modules/authentication/{facebook.js => facebook/authentication.js} (85%) create mode 100644 server/modules/authentication/facebook/definition.yml rename server/modules/authentication/{github.js => github/authentication.js} (85%) create mode 100644 server/modules/authentication/github/definition.yml rename server/modules/authentication/{google.js => google/authentication.js} (84%) create mode 100644 server/modules/authentication/google/definition.yml rename server/modules/authentication/{ldap.js => ldap/authentication.js} (67%) create mode 100644 server/modules/authentication/ldap/definition.yml rename server/modules/authentication/{local.js => local/authentication.js} (93%) create mode 100644 server/modules/authentication/local/definition.yml rename server/modules/authentication/{microsoft.js => microsoft/authentication.js} (83%) create mode 100644 server/modules/authentication/microsoft/definition.yml rename server/modules/authentication/{oauth2.js => oauth2/authentication.js} (81%) create mode 100644 server/modules/authentication/oauth2/definition.yml rename server/modules/authentication/{slack.js => slack/authentication.js} (84%) create mode 100644 server/modules/authentication/slack/definition.yml rename server/modules/authentication/{twitch.js => twitch/authentication.js} (85%) create mode 100644 server/modules/authentication/twitch/definition.yml diff --git a/package.json b/package.json index 88ebf615..4d274c52 100644 --- a/package.json +++ b/package.json @@ -110,6 +110,7 @@ "passport": "0.4.0", "passport-auth0": "0.6.1", "passport-azure-ad-oauth2": "0.0.4", + "passport-cas": "0.1.1", "passport-discord": "0.1.3", "passport-dropbox-oauth2": "1.1.0", "passport-facebook": "2.1.1", diff --git a/server/core/auth.js b/server/core/auth.js index df7514f5..9d2ec1af 100644 --- a/server/core/auth.js +++ b/server/core/auth.js @@ -45,7 +45,7 @@ module.exports = { const stg = enabledStrategies[idx] if (!stg.isEnabled) { continue } - const strategy = require(`../modules/authentication/${stg.key}`) + const strategy = require(`../modules/authentication/${stg.key}/authentication.js`) stg.config.callbackURL = `${WIKI.config.host}/login/${stg.key}/callback` // TODO: config.host strategy.init(passport, stg.config) diff --git a/server/db/models/authentication.js b/server/db/models/authentication.js index a31d8f76..167746f7 100644 --- a/server/db/models/authentication.js +++ b/server/db/models/authentication.js @@ -1,7 +1,9 @@ const Model = require('objection').Model -const autoload = require('auto-load') +const fs = require('fs-extra') const path = require('path') const _ = require('lodash') +const yaml = require('js-yaml') +const commonHelper = require('../../helpers/common') /* global WIKI */ @@ -42,9 +44,17 @@ module.exports = class Authentication extends Model { static async refreshStrategiesFromDisk() { try { const dbStrategies = await WIKI.db.authentication.query() - const diskStrategies = autoload(path.join(WIKI.SERVERPATH, 'modules/authentication')) + + // -> Fetch definitions from disk + const authDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/authentication')) + let diskStrategies = [] + for (let dir of authDirs) { + const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/authentication', dir, 'definition.yml'), 'utf8') + diskStrategies.push(yaml.safeLoad(def)) + } + let newStrategies = [] - _.forOwn(diskStrategies, (strategy, strategyKey) => { + _.forEach(diskStrategies, strategy => { if (!_.some(dbStrategies, ['key', strategy.key])) { newStrategies.push({ key: strategy.key, @@ -54,8 +64,8 @@ module.exports = class Authentication extends Model { config: _.transform(strategy.props, (result, value, key) => { if (_.isPlainObject(value)) { let cfgValue = { - type: typeof value.type(), - value: !_.isNil(value.default) ? value.default : new value() // eslint-disable-line new-cap + type: value.type.toLowerCase(), + value: !_.isNil(value.default) ? value.default : commonHelper.getTypeDefaultValue(value.type) } if (_.isArray(value.enum)) { cfgValue.enum = value.enum @@ -63,8 +73,8 @@ module.exports = class Authentication extends Model { _.set(result, key, cfgValue) } else { _.set(result, key, { - type: typeof value(), - value: new value() // eslint-disable-line new-cap + type: value.toLowerCase(), + value: commonHelper.getTypeDefaultValue(value) }) } return result diff --git a/server/modules/authentication/auth0.js b/server/modules/authentication/auth0/authentication.js similarity index 83% rename from server/modules/authentication/auth0.js rename to server/modules/authentication/auth0/authentication.js index c1f6bd99..6351b9b2 100644 --- a/server/modules/authentication/auth0.js +++ b/server/modules/authentication/auth0/authentication.js @@ -7,14 +7,6 @@ const Auth0Strategy = require('passport-auth0').Strategy module.exports = { - key: 'auth0', - title: 'Auth0', - useForm: false, - props: { - domain: String, - clientId: String, - clientSecret: String - }, init (passport, conf) { passport.use('auth0', new Auth0Strategy({ diff --git a/server/modules/authentication/auth0/definition.yml b/server/modules/authentication/auth0/definition.yml new file mode 100644 index 00000000..7771c0a1 --- /dev/null +++ b/server/modules/authentication/auth0/definition.yml @@ -0,0 +1,8 @@ +key: auth0 +title: Auth0 +author: requarks.io +useForm: false +props: + domain: String + clientId: String + clientSecret: String diff --git a/server/modules/authentication/azure.js b/server/modules/authentication/azure/authentication.js similarity index 74% rename from server/modules/authentication/azure.js rename to server/modules/authentication/azure/authentication.js index 156d4def..23c9b124 100644 --- a/server/modules/authentication/azure.js +++ b/server/modules/authentication/azure/authentication.js @@ -7,21 +7,6 @@ const AzureAdOAuth2Strategy = require('passport-azure-ad-oauth2').Strategy module.exports = { - key: 'azure', - title: 'Azure Active Directory', - useForm: false, - props: { - clientId: String, - clientSecret: String, - resource: { - type: String, - default: '00000002-0000-0000-c000-000000000000' - }, - tenant: { - type: String, - default: 'YOUR_TENANT.onmicrosoft.com' - } - }, init (passport, conf) { const jwt = require('jsonwebtoken') passport.use('azure_ad_oauth2', diff --git a/server/modules/authentication/azure/definition.yml b/server/modules/authentication/azure/definition.yml new file mode 100644 index 00000000..38a37465 --- /dev/null +++ b/server/modules/authentication/azure/definition.yml @@ -0,0 +1,13 @@ +key: azure +title: Azure Active Directory +author: requarks.io +useForm: false +props: + clientId: String + clientSecret: String + resource: + type: String, + default: '00000002-0000-0000-c000-000000000000' + tenant: + type: String, + default: YOUR_TENANT.onmicrosoft.com diff --git a/server/modules/authentication/cas/authentication.js b/server/modules/authentication/cas/authentication.js new file mode 100644 index 00000000..9b5d02c4 --- /dev/null +++ b/server/modules/authentication/cas/authentication.js @@ -0,0 +1,24 @@ +/* global WIKI */ + +// ------------------------------------ +// CAS Account +// ------------------------------------ + +const CASStrategy = require('passport-cas').Strategy + +module.exports = { + init (passport, conf) { + passport.use('cas', + new CASStrategy({ + ssoBaseURL: conf.ssoBaseURL, + serverBaseURL: conf.serverBaseURL + }, (profile, cb) => { + WIKI.db.users.processProfile(profile).then((user) => { + return cb(null, user) || true + }).catch((err) => { + return cb(err, null) || true + }) + } + )) + } +} diff --git a/server/modules/authentication/cas/definition.yml b/server/modules/authentication/cas/definition.yml new file mode 100644 index 00000000..00e109eb --- /dev/null +++ b/server/modules/authentication/cas/definition.yml @@ -0,0 +1,7 @@ +key: cas +title: CAS +author: requarks.io +useForm: false +props: + ssoBaseURL: String + serverBaseURL: String diff --git a/server/modules/authentication/discord.js b/server/modules/authentication/discord/authentication.js similarity index 85% rename from server/modules/authentication/discord.js rename to server/modules/authentication/discord/authentication.js index b9c3e51a..43ee7cb5 100644 --- a/server/modules/authentication/discord.js +++ b/server/modules/authentication/discord/authentication.js @@ -7,13 +7,6 @@ const DiscordStrategy = require('passport-discord').Strategy module.exports = { - key: 'discord', - title: 'Discord', - useForm: false, - props: { - clientId: String, - clientSecret: String - }, init (passport, conf) { passport.use('discord', new DiscordStrategy({ diff --git a/server/modules/authentication/discord/definition.yml b/server/modules/authentication/discord/definition.yml new file mode 100644 index 00000000..edea0649 --- /dev/null +++ b/server/modules/authentication/discord/definition.yml @@ -0,0 +1,7 @@ +key: discord +title: Discord +author: requarks.io +useForm: false +props: + clientId: String + clientSecret: String diff --git a/server/modules/authentication/dropbox.js b/server/modules/authentication/dropbox/authentication.js similarity index 85% rename from server/modules/authentication/dropbox.js rename to server/modules/authentication/dropbox/authentication.js index 07cc43dc..1fca0763 100644 --- a/server/modules/authentication/dropbox.js +++ b/server/modules/authentication/dropbox/authentication.js @@ -7,13 +7,6 @@ const DropboxStrategy = require('passport-dropbox-oauth2').Strategy module.exports = { - key: 'dropbox', - title: 'Dropbox', - useForm: false, - props: { - clientId: String, - clientSecret: String - }, init (passport, conf) { passport.use('dropbox', new DropboxStrategy({ diff --git a/server/modules/authentication/dropbox/definition.yml b/server/modules/authentication/dropbox/definition.yml new file mode 100644 index 00000000..b687643d --- /dev/null +++ b/server/modules/authentication/dropbox/definition.yml @@ -0,0 +1,7 @@ +key: dropbox +title: Dropbox +author: requarks.io +useForm: false +props: + clientId: String + clientSecret: String diff --git a/server/modules/authentication/facebook.js b/server/modules/authentication/facebook/authentication.js similarity index 85% rename from server/modules/authentication/facebook.js rename to server/modules/authentication/facebook/authentication.js index f3818fb5..991664f6 100644 --- a/server/modules/authentication/facebook.js +++ b/server/modules/authentication/facebook/authentication.js @@ -7,13 +7,6 @@ const FacebookStrategy = require('passport-facebook').Strategy module.exports = { - key: 'facebook', - title: 'Facebook', - useForm: false, - props: { - clientId: String, - clientSecret: String - }, init (passport, conf) { passport.use('facebook', new FacebookStrategy({ diff --git a/server/modules/authentication/facebook/definition.yml b/server/modules/authentication/facebook/definition.yml new file mode 100644 index 00000000..0434181f --- /dev/null +++ b/server/modules/authentication/facebook/definition.yml @@ -0,0 +1,7 @@ +key: facebook +title: Facebook +author: requarks.io +useForm: false +props: + clientId: String + clientSecret: String diff --git a/server/modules/authentication/github.js b/server/modules/authentication/github/authentication.js similarity index 85% rename from server/modules/authentication/github.js rename to server/modules/authentication/github/authentication.js index 9f140953..8f25f5d9 100644 --- a/server/modules/authentication/github.js +++ b/server/modules/authentication/github/authentication.js @@ -7,13 +7,6 @@ const GitHubStrategy = require('passport-github2').Strategy module.exports = { - key: 'github', - title: 'GitHub', - useForm: false, - props: { - clientId: String, - clientSecret: String - }, init (passport, conf) { passport.use('github', new GitHubStrategy({ diff --git a/server/modules/authentication/github/definition.yml b/server/modules/authentication/github/definition.yml new file mode 100644 index 00000000..69c73a13 --- /dev/null +++ b/server/modules/authentication/github/definition.yml @@ -0,0 +1,7 @@ +key: github +title: GitHub +author: requarks.io +useForm: false +props: + clientId: String + clientSecret: String diff --git a/server/modules/authentication/google.js b/server/modules/authentication/google/authentication.js similarity index 84% rename from server/modules/authentication/google.js rename to server/modules/authentication/google/authentication.js index bffc8b0e..1edb755d 100644 --- a/server/modules/authentication/google.js +++ b/server/modules/authentication/google/authentication.js @@ -7,13 +7,6 @@ const GoogleStrategy = require('passport-google-oauth20').Strategy module.exports = { - key: 'google', - title: 'Google', - useForm: false, - props: { - clientId: String, - clientSecret: String - }, init (passport, conf) { passport.use('google', new GoogleStrategy({ diff --git a/server/modules/authentication/google/definition.yml b/server/modules/authentication/google/definition.yml new file mode 100644 index 00000000..043dd55a --- /dev/null +++ b/server/modules/authentication/google/definition.yml @@ -0,0 +1,7 @@ +key: google +title: Google +author: requarks.io +useForm: false +props: + clientId: String + clientSecret: String diff --git a/server/modules/authentication/ldap.js b/server/modules/authentication/ldap/authentication.js similarity index 67% rename from server/modules/authentication/ldap.js rename to server/modules/authentication/ldap/authentication.js index d55c8ac8..0f96c1a3 100644 --- a/server/modules/authentication/ldap.js +++ b/server/modules/authentication/ldap/authentication.js @@ -8,33 +8,6 @@ const LdapStrategy = require('passport-ldapauth').Strategy const fs = require('fs') module.exports = { - key: 'ldap', - title: 'LDAP / Active Directory', - useForm: true, - props: { - url: { - type: String, - default: 'ldap://serverhost:389' - }, - bindDn: { - type: String, - default: `cn='root'` - }, - bindCredentials: String, - searchBase: { - type: String, - default: 'o=users,o=example.com' - }, - searchFilter: { - type: String, - default: '(uid={{username}})' - }, - tlsEnabled: { - type: Boolean, - default: false - }, - tlsCertPath: String - }, init (passport, conf) { passport.use('ldapauth', new LdapStrategy({ diff --git a/server/modules/authentication/ldap/definition.yml b/server/modules/authentication/ldap/definition.yml new file mode 100644 index 00000000..b9ae68e7 --- /dev/null +++ b/server/modules/authentication/ldap/definition.yml @@ -0,0 +1,22 @@ +key: ldap +title: LDAP / Active Directory +author: requarks.io +useForm: true +props: + url: + type: String + default: 'ldap://serverhost:389' + bindDn: + type: String + default: cn='root' + bindCredentials: String + searchBase: + type: String + default: 'o=users,o=example.com' + searchFilter: + type: String + default: '(uid={{username}})' + tlsEnabled: + type: Boolean + default: false + tlsCertPath: String diff --git a/server/modules/authentication/local.js b/server/modules/authentication/local/authentication.js similarity index 93% rename from server/modules/authentication/local.js rename to server/modules/authentication/local/authentication.js index ec21550c..8d55201b 100644 --- a/server/modules/authentication/local.js +++ b/server/modules/authentication/local/authentication.js @@ -7,10 +7,6 @@ const LocalStrategy = require('passport-local').Strategy module.exports = { - key: 'local', - title: 'Local', - useForm: true, - props: {}, init (passport, conf) { passport.use('local', new LocalStrategy({ diff --git a/server/modules/authentication/local/definition.yml b/server/modules/authentication/local/definition.yml new file mode 100644 index 00000000..b621c628 --- /dev/null +++ b/server/modules/authentication/local/definition.yml @@ -0,0 +1,5 @@ +key: local +title: Local +author: requarks.io +useForm: true +props: {} diff --git a/server/modules/authentication/microsoft.js b/server/modules/authentication/microsoft/authentication.js similarity index 83% rename from server/modules/authentication/microsoft.js rename to server/modules/authentication/microsoft/authentication.js index 28e943f6..1b45f4a8 100644 --- a/server/modules/authentication/microsoft.js +++ b/server/modules/authentication/microsoft/authentication.js @@ -7,13 +7,6 @@ const WindowsLiveStrategy = require('passport-windowslive').Strategy module.exports = { - key: 'microsoft', - title: 'Microsoft Account', - useForm: false, - props: { - clientId: String, - clientSecret: String - }, init (passport, conf) { passport.use('microsoft', new WindowsLiveStrategy({ diff --git a/server/modules/authentication/microsoft/definition.yml b/server/modules/authentication/microsoft/definition.yml new file mode 100644 index 00000000..7d0958b1 --- /dev/null +++ b/server/modules/authentication/microsoft/definition.yml @@ -0,0 +1,7 @@ +key: microsoft +title: Microsoft Account +author: requarks.io +useForm: false +props: + clientId: String + clientSecret: String diff --git a/server/modules/authentication/oauth2.js b/server/modules/authentication/oauth2/authentication.js similarity index 81% rename from server/modules/authentication/oauth2.js rename to server/modules/authentication/oauth2/authentication.js index cbc03d27..5c29b692 100644 --- a/server/modules/authentication/oauth2.js +++ b/server/modules/authentication/oauth2/authentication.js @@ -7,15 +7,6 @@ const OAuth2Strategy = require('passport-oauth2').Strategy module.exports = { - key: 'oauth2', - title: 'OAuth2', - useForm: false, - props: { - clientId: String, - clientSecret: String, - authorizationURL: String, - tokenURL: String - }, init (passport, conf) { passport.use('oauth2', new OAuth2Strategy({ diff --git a/server/modules/authentication/oauth2/definition.yml b/server/modules/authentication/oauth2/definition.yml new file mode 100644 index 00000000..800a58c0 --- /dev/null +++ b/server/modules/authentication/oauth2/definition.yml @@ -0,0 +1,9 @@ +key: oauth2 +title: OAuth2 +author: requarks.io +useForm: false +props: + clientId: String + clientSecret: String + authorizationURL: String + tokenURL: String diff --git a/server/modules/authentication/slack.js b/server/modules/authentication/slack/authentication.js similarity index 84% rename from server/modules/authentication/slack.js rename to server/modules/authentication/slack/authentication.js index bc710c70..985d12c3 100644 --- a/server/modules/authentication/slack.js +++ b/server/modules/authentication/slack/authentication.js @@ -7,13 +7,6 @@ const SlackStrategy = require('passport-slack').Strategy module.exports = { - key: 'slack', - title: 'Slack', - useForm: false, - props: { - clientId: String, - clientSecret: String - }, init (passport, conf) { passport.use('slack', new SlackStrategy({ diff --git a/server/modules/authentication/slack/definition.yml b/server/modules/authentication/slack/definition.yml new file mode 100644 index 00000000..689b5b3b --- /dev/null +++ b/server/modules/authentication/slack/definition.yml @@ -0,0 +1,7 @@ +key: slack +title: Slack +author: requarks.io +useForm: false +props: + clientId: String + clientSecret: String diff --git a/server/modules/authentication/twitch.js b/server/modules/authentication/twitch/authentication.js similarity index 85% rename from server/modules/authentication/twitch.js rename to server/modules/authentication/twitch/authentication.js index da28eacc..e05bc99e 100644 --- a/server/modules/authentication/twitch.js +++ b/server/modules/authentication/twitch/authentication.js @@ -7,13 +7,6 @@ const TwitchStrategy = require('passport-twitch').Strategy module.exports = { - key: 'twitch', - title: 'Twitch', - useForm: false, - props: { - clientId: String, - clientSecret: String - }, init (passport, conf) { passport.use('twitch', new TwitchStrategy({ diff --git a/server/modules/authentication/twitch/definition.yml b/server/modules/authentication/twitch/definition.yml new file mode 100644 index 00000000..e331fc47 --- /dev/null +++ b/server/modules/authentication/twitch/definition.yml @@ -0,0 +1,7 @@ +key: twitch +title: Twitch +author: requarks.io +useForm: false +props: + clientId: String + clientSecret: String diff --git a/yarn.lock b/yarn.lock index 439a3ed42d3f576a38cc7d267a2ee748ba10b88f..919869ba16d0ac636dec5209dfef37e47f5be808 100644 GIT binary patch delta 642 zcmX|0v@6o$FxCZ$0{=t3%4q;ccsCTG6xodHv0stet@@CVGNM3ScFHWFME7ojc- z6`i&I0r3N}aU&?*iTDRxx~{*#lO{NeVa}Q1o#%aL;_t@0KaCgjWPWL0`J8SZSEti~ z>&3V4tNnQOSz6s&)}5@gKXyJ_t~1i+ZPGq`{QL$}+6O_v3n`Um&T3_)5=KC73#PRv zMqA)=ksPN_@1$0jy|`0W`tj%Ybn`BjsC%Hx@?bnU%&^P=g;&y!=BVbW71HOx3*rM1 zUgT7QQNcO~LK+=j?bNTm`I}V5lC0UfKx@^TIHvo~Pf9zU_+>k9!>ThFmx!PQJzwIWt7r!V>4Dq8^k9mIoRv1J5ii0w_U^ zTKs6#e!Y&TSJ33qr`mPEi!Y**hDk%fYU_N+8DoW!hH}qCkh$YRdo8Haa&h{xHX5YJ z*o!CkYyI0RU<6t%kO{$8=@UYL000A!n6;uDywRlKsxYFk1yxqWhu@Ozn`mZf87=-# z!Au83OsLt~?Gj|fc