2019-07-11 11:22:25 -04:00
|
|
|
const EmailHandler = require('../../../../app/src/Features/Email/EmailHandler')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Errors = require('../Errors/Errors')
|
2019-07-11 11:22:25 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
2019-05-29 05:21:06 -04:00
|
|
|
const { User } = require('../../models/User')
|
2019-07-11 11:22:25 -04:00
|
|
|
const settings = require('settings-sharelatex')
|
2019-05-29 05:21:06 -04:00
|
|
|
const _ = require('lodash')
|
|
|
|
|
2019-07-11 11:22:25 -04:00
|
|
|
const oauthProviders = settings.oauthProviders || {}
|
|
|
|
|
2019-06-06 17:14:16 -04:00
|
|
|
const ThirdPartyIdentityManager = (module.exports = {
|
2019-05-29 05:21:06 -04:00
|
|
|
getUser(providerId, externalUserId, callback) {
|
|
|
|
if (providerId == null || externalUserId == null) {
|
|
|
|
return callback(new Error('invalid arguments'))
|
|
|
|
}
|
|
|
|
const query = ThirdPartyIdentityManager._getUserQuery(
|
|
|
|
providerId,
|
|
|
|
externalUserId
|
|
|
|
)
|
2019-06-06 17:14:16 -04:00
|
|
|
User.findOne(query, function(err, user) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (!user) {
|
|
|
|
return callback(new Errors.ThirdPartyUserNotFoundError())
|
|
|
|
}
|
2019-06-06 17:14:16 -04:00
|
|
|
callback(null, user)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
login(providerId, externalUserId, externalData, callback) {
|
2019-06-06 17:14:16 -04:00
|
|
|
ThirdPartyIdentityManager.getUser(providerId, externalUserId, function(
|
|
|
|
err,
|
|
|
|
user
|
|
|
|
) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-06 17:14:16 -04:00
|
|
|
if (!externalData) {
|
|
|
|
return callback(null, user)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-06 17:14:16 -04:00
|
|
|
const query = ThirdPartyIdentityManager._getUserQuery(
|
|
|
|
providerId,
|
|
|
|
externalUserId
|
|
|
|
)
|
|
|
|
const update = ThirdPartyIdentityManager._thirdPartyIdentifierUpdate(
|
|
|
|
user,
|
|
|
|
providerId,
|
|
|
|
externalUserId,
|
|
|
|
externalData
|
|
|
|
)
|
|
|
|
User.findOneAndUpdate(query, update, { new: true }, callback)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_getUserQuery(providerId, externalUserId) {
|
|
|
|
externalUserId = externalUserId.toString()
|
|
|
|
providerId = providerId.toString()
|
|
|
|
const query = {
|
|
|
|
'thirdPartyIdentifiers.externalUserId': externalUserId,
|
|
|
|
'thirdPartyIdentifiers.providerId': providerId
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
},
|
|
|
|
|
|
|
|
_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
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2019-06-06 17:14:16 -04:00
|
|
|
link(userId, providerId, externalUserId, externalData, callback, retry) {
|
2019-07-11 11:22:25 -04:00
|
|
|
if (!oauthProviders[providerId]) {
|
|
|
|
return callback(new Error('Not a valid provider'))
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
const query = {
|
2019-06-06 17:14:16 -04:00
|
|
|
_id: userId,
|
2019-05-29 05:21:06 -04:00
|
|
|
'thirdPartyIdentifiers.providerId': {
|
|
|
|
$ne: providerId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const update = {
|
|
|
|
$push: {
|
|
|
|
thirdPartyIdentifiers: {
|
|
|
|
externalUserId,
|
|
|
|
externalData,
|
|
|
|
providerId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add new tpi only if an entry for the provider does not exist
|
2019-07-11 11:22:25 -04:00
|
|
|
// projection includes thirdPartyIdentifiers for tests
|
|
|
|
User.findOneAndUpdate(
|
|
|
|
query,
|
|
|
|
update,
|
|
|
|
{ projection: { email: 1, thirdPartyIdentifiers: 1 }, new: 1 },
|
|
|
|
(err, res) => {
|
|
|
|
if (err && err.code === 11000) {
|
|
|
|
callback(new Errors.ThirdPartyIdentityExistsError())
|
|
|
|
} else if (err != null) {
|
|
|
|
callback(err)
|
|
|
|
} else if (res) {
|
|
|
|
const emailOptions = {
|
|
|
|
to: res.email,
|
|
|
|
provider: oauthProviders[providerId].name
|
|
|
|
}
|
|
|
|
if (settings.oauthFallback) {
|
|
|
|
return callback(null, res)
|
|
|
|
} else {
|
|
|
|
EmailHandler.sendEmail(
|
|
|
|
'emailThirdPartyIdentifierLinked',
|
|
|
|
emailOptions,
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
logger.warn(error)
|
|
|
|
}
|
|
|
|
return callback(null, res)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else if (retry) {
|
|
|
|
// if already retried then throw error
|
|
|
|
callback(new Error('update failed'))
|
|
|
|
} else {
|
|
|
|
// attempt to clear existing entry then retry
|
|
|
|
ThirdPartyIdentityManager.unlink(userId, providerId, function(err) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
ThirdPartyIdentityManager.link(
|
|
|
|
userId,
|
|
|
|
providerId,
|
|
|
|
externalUserId,
|
|
|
|
externalData,
|
|
|
|
callback,
|
|
|
|
retry
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-07-11 11:22:25 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2019-06-06 17:14:16 -04:00
|
|
|
unlink(userId, providerId, callback) {
|
2019-07-11 11:22:25 -04:00
|
|
|
if (!oauthProviders[providerId]) {
|
|
|
|
return callback(new Error('Not a valid provider'))
|
|
|
|
}
|
|
|
|
const query = {
|
|
|
|
_id: userId
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
const update = {
|
|
|
|
$pull: {
|
|
|
|
thirdPartyIdentifiers: {
|
|
|
|
providerId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 11:22:25 -04:00
|
|
|
// projection includes thirdPartyIdentifiers for tests
|
|
|
|
User.findOneAndUpdate(
|
|
|
|
query,
|
|
|
|
update,
|
|
|
|
{ projection: { email: 1, thirdPartyIdentifiers: 1 }, new: 1 },
|
|
|
|
(err, res) => {
|
|
|
|
if (err != null) {
|
|
|
|
callback(err)
|
|
|
|
} else if (!res) {
|
|
|
|
callback(new Error('update failed'))
|
|
|
|
} else {
|
|
|
|
const emailOptions = {
|
|
|
|
to: res.email,
|
|
|
|
provider: oauthProviders[providerId].name
|
|
|
|
}
|
|
|
|
if (settings.oauthFallback) {
|
|
|
|
return callback(null, res)
|
|
|
|
} else {
|
|
|
|
EmailHandler.sendEmail(
|
|
|
|
'emailThirdPartyIdentifierUnlinked',
|
|
|
|
emailOptions,
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
logger.warn(error)
|
|
|
|
}
|
|
|
|
return callback(null, res)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-06 17:14:16 -04:00
|
|
|
})
|