feat: social login providers with dynamic instances

This commit is contained in:
NGPixel
2020-08-30 01:36:17 -04:00
parent a7ddafd4aa
commit 32d67adee1
28 changed files with 147 additions and 107 deletions

View File

@@ -13,10 +13,14 @@ module.exports = {
domain: conf.domain,
clientID: conf.clientId,
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL
}, async (accessToken, refreshToken, extraParams, profile, cb) => {
callbackURL: conf.callbackURL,
passReqToCallback: true
}, async (req, accessToken, refreshToken, extraParams, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({ profile, providerKey: 'auth0' })
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile
})
cb(null, user)
} catch (err) {
cb(err, null)

View File

@@ -18,18 +18,19 @@ module.exports = {
responseType: 'id_token',
responseMode: 'form_post',
scope: ['profile', 'email', 'openid'],
allowHttpForRedirectUrl: WIKI.IS_DEBUG
}, async (iss, sub, profile, cb) => {
allowHttpForRedirectUrl: WIKI.IS_DEBUG,
passReqToCallback: true
}, async (req, iss, sub, profile, cb) => {
const usrEmail = _.get(profile, '_json.email', null) || _.get(profile, '_json.preferred_username')
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
id: profile.oid,
displayName: profile.displayName,
email: usrEmail,
picture: ''
},
providerKey: 'azure'
}
})
cb(null, user)
} catch (err) {

View File

@@ -11,14 +11,19 @@ module.exports = {
passport.use('cas',
new CASStrategy({
ssoBaseURL: conf.ssoBaseURL,
serverBaseURL: conf.serverBaseURL
}, (profile, cb) => {
WIKI.models.users.processProfile(profile).then((user) => {
return cb(null, user) || true
}).catch((err) => {
return cb(err, null) || true
})
}
))
serverBaseURL: conf.serverBaseURL,
passReqToCallback: true
}, async (req, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile
})
cb(null, user)
} catch (err) {
cb(err, null)
}
})
)
}
}

View File

@@ -15,19 +15,20 @@ module.exports = {
clientSecret: conf.clientSecret,
authorizationURL: 'https://discord.com/api/oauth2/authorize?prompt=none',
callbackURL: conf.callbackURL,
scope: 'identify email guilds'
}, async (accessToken, refreshToken, profile, cb) => {
scope: 'identify email guilds',
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
if (conf.guildId && !_.some(profile.guilds, { id: conf.guildId })) {
if (conf.guildId && !_.some(profile.guilds, { id: conf.guildId })) {
throw new WIKI.Error.AuthLoginFailed()
}
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
displayName: profile.username,
picture: `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.png`
},
providerKey: 'discord'
}
})
cb(null, user)
} catch (err) {

View File

@@ -14,15 +14,16 @@ module.exports = {
apiVersion: '2',
clientID: conf.clientId,
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL
}, async (accessToken, refreshToken, profile, cb) => {
callbackURL: conf.callbackURL,
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
picture: _.get(profile, '_json.profile_photo_url', '')
},
providerKey: 'dropbox'
}
})
cb(null, user)
} catch (err) {

View File

@@ -15,15 +15,16 @@ module.exports = {
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL,
profileFields: ['id', 'displayName', 'email', 'photos'],
authType: 'reauthenticate'
}, async (accessToken, refreshToken, profile, cb) => {
authType: 'reauthenticate',
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
picture: _.get(profile, 'photos[0].value', '')
},
providerKey: 'facebook'
}
})
cb(null, user)
} catch (err) {

View File

@@ -1,28 +1,30 @@
/* global WIKI */
// ------------------------------------
// GitHub Account
// Firebase Account
// ------------------------------------
const GitHubStrategy = require('passport-github2').Strategy
// INCOMPLETE / TODO
const FirebaseStrategy = require('passport-github2').Strategy
const _ = require('lodash')
module.exports = {
init (passport, conf) {
passport.use('github',
new GitHubStrategy({
passport.use('firebase',
new FirebaseStrategy({
clientID: conf.clientId,
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL,
scope: ['user:email']
}, async (accessToken, refreshToken, profile, cb) => {
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
picture: _.get(profile, 'photos[0].value', '')
},
providerKey: 'github'
}
})
cb(null, user)
} catch (err) {

View File

@@ -13,7 +13,8 @@ module.exports = {
clientID: conf.clientId,
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL,
scope: ['user:email']
scope: ['user:email'],
passReqToCallback: true
}
if (conf.useEnterprise) {
@@ -24,14 +25,14 @@ module.exports = {
}
passport.use('github',
new GitHubStrategy(githubConfig, async (accessToken, refreshToken, profile, cb) => {
new GitHubStrategy(githubConfig, async (req, accessToken, refreshToken, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
picture: _.get(profile, 'photos[0].value', '')
},
providerKey: 'github'
}
})
cb(null, user)
} catch (err) {

View File

@@ -15,15 +15,16 @@ module.exports = {
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL,
baseURL: conf.baseUrl,
scope: ['read_user']
}, async (accessToken, refreshToken, profile, cb) => {
scope: ['read_user'],
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
picture: _.get(profile, 'avatarUrl', '')
},
providerKey: 'gitlab'
}
})
cb(null, user)
} catch (err) {

View File

@@ -13,15 +13,16 @@ module.exports = {
new GoogleStrategy({
clientID: conf.clientId,
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL
}, async (accessToken, refreshToken, profile, cb) => {
callbackURL: conf.callbackURL,
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
picture: _.get(profile, 'photos[0].value', '')
},
providerKey: 'google'
}
})
cb(null, user)
} catch (err) {

View File

@@ -12,28 +12,29 @@ module.exports = {
init (passport, conf) {
passport.use('keycloak',
new KeycloakStrategy({
authorizationURL: conf.authorizationURL,
userInfoURL: conf.userInfoURL,
authorizationURL: conf.authorizationURL,
userInfoURL: conf.userInfoURL,
tokenURL: conf.tokenURL,
host: conf.host,
host: conf.host,
realm: conf.realm,
clientID: conf.clientId,
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL
}, async (accessToken, refreshToken, profile, cb) => {
callbackURL: conf.callbackURL,
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, cb) => {
let displayName = profile.username
if (_.isString(profile.fullName) && profile.fullName.length > 0) {
displayName = profile.fullName
}
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
id: profile.keycloakId,
email: profile.email,
name: displayName,
picture: ''
},
providerKey: 'keycloak'
}
})
cb(null, user)
} catch (err) {

View File

@@ -28,7 +28,7 @@ module.exports = {
usernameField: 'email',
passwordField: 'password',
passReqToCallback: false
}, async (profile, cb) => {
}, async (req, profile, cb) => {
try {
const userId = _.get(profile, conf.mappingUID, null)
if (!userId) {
@@ -36,13 +36,13 @@ module.exports = {
}
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
id: userId,
email: String(_.get(profile, conf.mappingEmail, '')).split(',')[0],
displayName: _.get(profile, conf.mappingDisplayName, '???'),
picture: _.get(profile, conf.mappingPicture, '')
},
providerKey: 'ldap'
}
})
cb(null, user)
} catch (err) {

View File

@@ -14,16 +14,16 @@ module.exports = {
clientID: conf.clientId,
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL,
scope: ['User.Read', 'email', 'openid', 'profile']
}, async (accessToken, refreshToken, profile, cb) => {
console.info(profile)
scope: ['User.Read', 'email', 'openid', 'profile'],
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
picture: _.get(profile, 'photos[0].value', '')
},
providerKey: 'microsoft'
}
})
cb(null, user)
} catch (err) {

View File

@@ -14,13 +14,18 @@ module.exports = {
tokenURL: conf.tokenURL,
clientID: conf.clientId,
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL
}, (accessToken, refreshToken, profile, cb) => {
WIKI.models.users.processProfile(profile).then((user) => {
return cb(null, user) || true
}).catch((err) => {
return cb(err, null) || true
})
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)
}
})
)
}

View File

@@ -18,18 +18,19 @@ module.exports = {
clientSecret: conf.clientSecret,
issuer: conf.issuer,
userInfoURL: conf.userInfoURL,
callbackURL: conf.callbackURL
}, async (iss, sub, profile, cb) => {
callbackURL: conf.callbackURL,
passReqToCallback: true
}, async (req, iss, sub, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
email: _.get(profile, '_json.' + conf.emailClaim)
},
providerKey: 'oidc'
}
})
cb(null, user)
} catch(err) {
} catch (err) {
cb(err, null)
}
})

View File

@@ -16,15 +16,16 @@ module.exports = {
clientSecret: conf.clientSecret,
idp: conf.idp,
callbackURL: conf.callbackURL,
response_type: 'code'
}, async (accessToken, refreshToken, profile, cb) => {
response_type: 'code',
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
picture: _.get(profile, '_json.profile', '')
},
providerKey: 'okta'
}
})
cb(null, user)
} catch (err) {

View File

@@ -22,7 +22,8 @@ module.exports = {
forceAuthn: conf.forceAuthn,
providerName: conf.providerName,
skipRequestCompression: conf.skipRequestCompression,
authnRequestBinding: conf.authnRequestBinding
authnRequestBinding: conf.authnRequestBinding,
passReqToCallback: true
}
if (!_.isEmpty(conf.audience)) {
samlConfig.audience = conf.audience
@@ -37,7 +38,7 @@ module.exports = {
samlConfig.decryptionPvk = conf.decryptionPvk
}
passport.use('saml',
new SAMLStrategy(samlConfig, async (profile, cb) => {
new SAMLStrategy(samlConfig, async (req, profile, cb) => {
try {
const userId = _.get(profile, [conf.mappingUID], null) || _.get(profile, 'nameID', null)
if (!userId) {
@@ -45,13 +46,13 @@ module.exports = {
}
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
id: userId,
email: _.get(profile, conf.mappingEmail, ''),
displayName: _.get(profile, conf.mappingDisplayName, '???'),
picture: _.get(profile, conf.mappingPicture, '')
},
providerKey: 'saml'
}
})
cb(null, user)
} catch (err) {

View File

@@ -14,15 +14,16 @@ module.exports = {
clientID: conf.clientId,
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL,
team: conf.team
}, async (accessToken, scopes, team, extra, { user: userProfile }, cb) => {
team: conf.team,
passReqToCallback: true
}, async (req, accessToken, scopes, team, extra, { user: userProfile }, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...userProfile,
picture: _.get(userProfile, 'image_48', '')
},
providerKey: 'slack'
}
})
cb(null, user)
} catch (err) {

View File

@@ -13,15 +13,16 @@ module.exports = {
new TwitchStrategy({
clientID: conf.clientId,
clientSecret: conf.clientSecret,
callbackURL: conf.callbackURL
}, async (accessToken, refreshToken, profile, cb) => {
callbackURL: conf.callbackURL,
passReqToCallback: true
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
const user = await WIKI.models.users.processProfile({
providerKey: req.params.strategy,
profile: {
...profile,
picture: _.get(profile, 'avatar', '')
},
providerKey: 'twitch'
}
})
cb(null, user)
} catch (err) {