remove user.overleaf.id dependencies

GitOrigin-RevId: ca495010a4f0ee462bde696dd0c442b2bd57bf30
This commit is contained in:
Ersun Warncke 2019-07-10 06:40:59 -04:00 committed by sharelatex
parent 65849456d0
commit a61a59be20
5 changed files with 75 additions and 153 deletions

View file

@ -15,9 +15,6 @@ const RedisWrapper = require('../../infrastructure/RedisWrapper')
const rclient = RedisWrapper.client('sudomode')
const logger = require('logger-sharelatex')
const AuthenticationManager = require('../Authentication/AuthenticationManager')
const Settings = require('settings-sharelatex')
const V1Handler = require('../V1/V1Handler')
const UserGetter = require('../User/UserGetter')
const TIMEOUT_IN_SECONDS = 60 * 60
@ -28,22 +25,9 @@ module.exports = SudoModeHandler = {
authenticate(email, password, callback) {
if (callback == null) {
callback = function(err, user) {}
}
if (Settings.overleaf != null) {
return V1Handler.authWithV1(email, password, function(
err,
isValid,
v1Profile
) {
if (!isValid) {
return callback(null, null)
}
return UserGetter.getUser({ 'overleaf.id': v1Profile.id }, callback)
})
} else {
return AuthenticationManager.authenticate({ email }, password, callback)
callback = function() {}
}
AuthenticationManager.authenticate({ email }, password, callback)
},
activateSudoMode(userId, callback) {

View file

@ -101,51 +101,46 @@ const ThirdPartyIdentityManager = (module.exports = {
}
// add new tpi only if an entry for the provider does not exist
// 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
}
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
)
})
User.findOneAndUpdate(query, update, { 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
}
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,
true
)
})
}
)
})
},
unlink(userId, providerId, callback) {
@ -163,32 +158,27 @@ const ThirdPartyIdentityManager = (module.exports = {
}
}
// 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
}
EmailHandler.sendEmail(
'emailThirdPartyIdentifierUnlinked',
emailOptions,
error => {
if (error != null) {
logger.warn(error)
}
return callback(null, res)
}
)
User.findOneAndUpdate(query, update, { 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
}
EmailHandler.sendEmail(
'emailThirdPartyIdentifierUnlinked',
emailOptions,
error => {
if (error != null) {
logger.warn(error)
}
return callback(null, res)
}
)
}
)
})
}
})

View file

@ -28,12 +28,6 @@ module.exports = UserGetter = {
if (query == null) {
return callback(new Error('no query provided'))
}
if ((query != null ? query.email : undefined) != null) {
return callback(
new Error("Don't use getUser to find user by email"),
null
)
}
if (arguments.length === 2) {
callback = projection
projection = {}

View file

@ -139,30 +139,21 @@ module.exports = UserPagesController = {
if (err != null) {
return next(err)
}
return UserPagesController._hasPassword(user, function(
err,
passwordPresent
) {
if (err) {
logger.err({ err }, 'error getting password status from v1')
}
return res.render('user/settings', {
title: 'account_settings',
user,
hasPassword: passwordPresent,
shouldAllowEditingDetails,
languages: Settings.languages,
accountSettingsTabActive: true,
oauthProviders: UserPagesController._translateProviderDescriptions(
oauthProviders,
req
),
oauthUseV2: Settings.oauthUseV2 || false,
ssoError: ssoError,
thirdPartyIds: UserPagesController._restructureThirdPartyIds(user),
previewOauth: req.query.prvw != null
})
res.render('user/settings', {
title: 'account_settings',
user,
hasPassword: !!user.hashedPassword,
shouldAllowEditingDetails,
languages: Settings.languages,
accountSettingsTabActive: true,
oauthProviders: UserPagesController._translateProviderDescriptions(
oauthProviders,
req
),
oauthUseV2: Settings.oauthUseV2 || false,
ssoError: ssoError,
thirdPartyIds: UserPagesController._restructureThirdPartyIds(user),
previewOauth: req.query.prvw != null
})
})
},
@ -186,32 +177,6 @@ module.exports = UserPagesController = {
)
},
_hasPassword(user, callback) {
return request.get(
{
url: `${Settings.apis.v1.url}/api/v1/sharelatex/has_password`,
auth: { user: Settings.apis.v1.user, pass: Settings.apis.v1.pass },
body: {
user_id: __guard__(
user != null ? user.overleaf : undefined,
x => x.id
)
},
timeout: 20 * 1000,
json: true
},
function(err, response, body) {
if (err) {
// for errors assume password and show password setting form
return callback(err, true)
} else if (body != null ? body.has_password : undefined) {
return callback(err, true)
}
return callback(err, false)
}
)
},
_restructureThirdPartyIds(user) {
// 3rd party identifiers are an array of objects
// this turn them into a single object, which

View file

@ -80,17 +80,6 @@ describe('UserGetter', function() {
})
})
it('should not allow email in query', function(done) {
return this.UserGetter.getUser(
{ email: 'foo@bar.com' },
{},
(error, user) => {
error.should.exist
return done()
}
)
})
it('should not allow null query', function(done) {
return this.UserGetter.getUser(null, {}, (error, user) => {
error.should.exist