finish saml link after sso login

GitOrigin-RevId: 688ce78ddfb0cfd6a025985dc2dd6f62bce76523
This commit is contained in:
Ersun Warncke 2019-11-01 10:19:56 -04:00 committed by sharelatex
parent 9fa858bbd6
commit 8d44f44784
2 changed files with 182 additions and 168 deletions

View file

@ -1,21 +1,19 @@
const EmailHandler = require('../../../../app/src/Features/Email/EmailHandler') const APP_ROOT = '../../../../app/src'
const EmailHandler = require(`${APP_ROOT}/Features/Email/EmailHandler`)
const Errors = require('../Errors/Errors') const Errors = require('../Errors/Errors')
const logger = require('logger-sharelatex')
const { User } = require('../../models/User')
const settings = require('settings-sharelatex')
const _ = require('lodash') const _ = require('lodash')
const logger = require('logger-sharelatex')
const settings = require('settings-sharelatex')
const { User } = require(`${APP_ROOT}/models/User`)
const { promisifyAll } = require(`${APP_ROOT}/util/promises`)
const oauthProviders = settings.oauthProviders || {} const oauthProviders = settings.oauthProviders || {}
const ThirdPartyIdentityManager = (module.exports = { function getUser(providerId, externalUserId, callback) {
getUser(providerId, externalUserId, callback) {
if (providerId == null || externalUserId == null) { if (providerId == null || externalUserId == null) {
return callback(new Error('invalid arguments')) return callback(new Error('invalid arguments'))
} }
const query = ThirdPartyIdentityManager._getUserQuery( const query = _getUserQuery(providerId, externalUserId)
providerId,
externalUserId
)
User.findOne(query, function(err, user) { User.findOne(query, function(err, user) {
if (err != null) { if (err != null) {
return callback(err) return callback(err)
@ -25,9 +23,9 @@ const ThirdPartyIdentityManager = (module.exports = {
} }
callback(null, user) callback(null, user)
}) })
}, }
login(providerId, externalUserId, externalData, callback) { function login(providerId, externalUserId, externalData, callback) {
ThirdPartyIdentityManager.getUser(providerId, externalUserId, function( ThirdPartyIdentityManager.getUser(providerId, externalUserId, function(
err, err,
user user
@ -38,11 +36,8 @@ const ThirdPartyIdentityManager = (module.exports = {
if (!externalData) { if (!externalData) {
return callback(null, user) return callback(null, user)
} }
const query = ThirdPartyIdentityManager._getUserQuery( const query = _getUserQuery(providerId, externalUserId)
providerId, const update = _thirdPartyIdentifierUpdate(
externalUserId
)
const update = ThirdPartyIdentityManager._thirdPartyIdentifierUpdate(
user, user,
providerId, providerId,
externalUserId, externalUserId,
@ -50,37 +45,16 @@ const ThirdPartyIdentityManager = (module.exports = {
) )
User.findOneAndUpdate(query, update, { new: true }, callback) User.findOneAndUpdate(query, update, { new: true }, callback)
}) })
},
_getUserQuery(providerId, externalUserId) {
externalUserId = externalUserId.toString()
providerId = providerId.toString()
const query = {
'thirdPartyIdentifiers.externalUserId': externalUserId,
'thirdPartyIdentifiers.providerId': providerId
} }
return query
},
_thirdPartyIdentifierUpdate(user, providerId, externalUserId, externalData) { function link(
providerId = providerId.toString() userId,
// get third party identifier object from array providerId,
const thirdPartyIdentifier = user.thirdPartyIdentifiers.find( externalUserId,
tpi => externalData,
tpi.externalUserId === externalUserId && tpi.providerId === providerId callback,
) retry
// do recursive merge of new data over existing data ) {
_.merge(thirdPartyIdentifier.externalData, externalData)
const update = { 'thirdPartyIdentifiers.$': thirdPartyIdentifier }
return update
},
// register: () ->
// this should be implemented once we move to having v2 as the master
// but for now we need to register with v1 then call link once that
// is complete
link(userId, providerId, externalUserId, externalData, callback, retry) {
if (!oauthProviders[providerId]) { if (!oauthProviders[providerId]) {
return callback(new Error('Not a valid provider')) return callback(new Error('Not a valid provider'))
} }
@ -141,9 +115,9 @@ const ThirdPartyIdentityManager = (module.exports = {
}) })
} }
}) })
}, }
unlink(userId, providerId, callback) { function unlink(userId, providerId, callback) {
if (!oauthProviders[providerId]) { if (!oauthProviders[providerId]) {
return callback(new Error('Not a valid provider')) return callback(new Error('Not a valid provider'))
} }
@ -181,4 +155,42 @@ const ThirdPartyIdentityManager = (module.exports = {
} }
}) })
} }
})
function _getUserQuery(providerId, externalUserId) {
externalUserId = externalUserId.toString()
providerId = providerId.toString()
const query = {
'thirdPartyIdentifiers.externalUserId': externalUserId,
'thirdPartyIdentifiers.providerId': providerId
}
return query
}
function _thirdPartyIdentifierUpdate(
user,
providerId,
externalUserId,
externalData
) {
providerId = providerId.toString()
// get third party identifier object from array
const thirdPartyIdentifier = user.thirdPartyIdentifiers.find(
tpi =>
tpi.externalUserId === externalUserId && tpi.providerId === providerId
)
// do recursive merge of new data over existing data
_.merge(thirdPartyIdentifier.externalData, externalData)
const update = { 'thirdPartyIdentifiers.$': thirdPartyIdentifier }
return update
}
const ThirdPartyIdentityManager = {
getUser,
login,
link,
unlink
}
ThirdPartyIdentityManager.promises = promisifyAll(ThirdPartyIdentityManager)
module.exports = ThirdPartyIdentityManager

View file

@ -41,13 +41,15 @@ module.exports = class UserHelper {
this.user = null this.user = null
// cookie jar // cookie jar
this.jar = request.jar() this.jar = request.jar()
// create new request instance
this.request = request.defaults({})
// initialize request instance with default options // initialize request instance with default options
this.setRequestDefaults() this.setRequestDefaults()
} }
setRequestDefaults(defaults = {}) { setRequestDefaults(defaults = {}) {
// request-promise instance for making requests // request-promise instance for making requests
this.request = request.defaults({ this.request = this.request.defaults({
baseUrl: UserHelper.baseUrl(), baseUrl: UserHelper.baseUrl(),
followRedirect: false, followRedirect: false,
jar: this.jar, jar: this.jar,