complete v2 collabratec api

* remove proxies to v1
* allow oauth tokens with user stubs
* add profile/unlink end points
* fix Makefile issue

GitOrigin-RevId: 9c75b657c11f98e3f927dd01a422157f53a5672d
This commit is contained in:
Ersun Warncke 2019-04-25 11:43:16 -04:00 committed by sharelatex
parent 935877222a
commit 65cbfbaeae
5 changed files with 32 additions and 13 deletions

View file

@ -254,9 +254,9 @@ test_acceptance_modules_run:
test_acceptance_module_run: $(MODULE_MAKEFILES)
@if [ -e $(MODULE)/test/acceptance ]; then \
COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME)_$(MODULE) $(DOCKER_COMPOSE) down -v -t 0; \
cd $(MODULE) && COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME)_$(MODULE) $(MAKE) test_acceptance; \
cd $(CURDIR) && COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME)_$(MODULE) $(DOCKER_COMPOSE) down -v -t 0; \
COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME)_$(MODULE) $(DOCKER_COMPOSE) down -v -t 0 \
&& cd $(MODULE) && COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME)_$(MODULE) $(MAKE) test_acceptance \
&& cd $(CURDIR) && COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME)_$(MODULE) $(DOCKER_COMPOSE) down -v -t 0; \
fi
ci:

View file

@ -186,7 +186,10 @@ module.exports = AuthenticationController =
return doRequest
requireOauth: () ->
# access tokens might be associated with user stubs if the user is
# not yet migrated to v2. if api can work with user stubs then set
# allowUserStub true when adding middleware to route.
requireOauth: (allowUserStub=false) ->
# require this here because module may not be included in some versions
Oauth2Server = require "../../../../modules/oauth2-server/app/js/Oauth2Server"
return (req, res, next = (error) ->) ->
@ -200,6 +203,7 @@ module.exports = AuthenticationController =
return AuthenticationController._requireOauthV1Fallback req, res, next if err.code == 401
# send all other errors
return res.status(err.code).json({error: err.name, error_description: err.message})
return res.sendStatus 401 if token.user.constructor.modelName == "UserStub" and !allowUserStub
req.oauth =
access_token: token.accessToken
req.oauth_token = token
@ -221,7 +225,6 @@ module.exports = AuthenticationController =
return res.status(401).json({error: "invalid_token"}) unless user?
req.oauth =
access_token: body.access_token
user.collabratec_id = body.collabratec_customer_id unless user.collabratec_id?
req.oauth_user = user
next()

View file

@ -100,4 +100,4 @@ module.exports = V1SubscriptionManager =
if response.statusCode == 404
return callback new NotFoundError("v1 user not found: #{userId}")
else
return callback new Error("non-success code from v1: #{response.statusCode}")
return callback new Error("non-success code from v1: #{response.statusCode} #{options.method} #{options.url(v1Id)}")

View file

@ -5,14 +5,20 @@ UserUpdater = require "./UserUpdater"
_ = require "lodash"
module.exports = ThirdPartyIdentityManager =
login: (providerId, externalUserId, externalData, callback) ->
getUser: (providerId, externalUserId, callback) ->
return callback(new Error "invalid arguments") unless providerId? and externalUserId?
query = ThirdPartyIdentityManager._loginQuery providerId, externalUserId
query = ThirdPartyIdentityManager._getUserQuery providerId, externalUserId
User.findOne query, (err, user) ->
return callback err if err?
return callback(new Errors.ThirdPartyUserNotFoundError()) unless user
callback null, user
login: (providerId, externalUserId, externalData, callback) ->
ThirdPartyIdentityManager.getUser providerId, externalUserId, (err, user) ->
return callback err if err?
return callback(null, user) unless externalData
update = ThirdPartyIdentityManager._loginUpdate user, providerId, externalUserId, externalData
query = ThirdPartyIdentityManager._getUserQuery providerId, externalUserId
update = ThirdPartyIdentityManager._thirdPartyIdentifierUpdate user, providerId, externalUserId, externalData
User.findOneAndUpdate query, update, {new: true}, callback
# attempt to login normally but check for user stub if user not found
@ -20,15 +26,15 @@ module.exports = ThirdPartyIdentityManager =
ThirdPartyIdentityManager.login providerId, externalUserId, externalData, (err, user) ->
return callback null, user unless err?
return callback err unless err.name == "ThirdPartyUserNotFoundError"
query = ThirdPartyIdentityManager._loginQuery providerId, externalUserId
query = ThirdPartyIdentityManager._getUserQuery providerId, externalUserId
UserStub.findOne query, (err, userStub) ->
return callback err if err?
return callback(new Errors.ThirdPartyUserNotFoundError()) unless userStub
return callback(null, userStub) unless externalData
update = ThirdPartyIdentityManager._loginUpdate userStub, providerId, externalUserId, externalData
update = ThirdPartyIdentityManager._thirdPartyIdentifierUpdate userStub, providerId, externalUserId, externalData
UserStub.findOneAndUpdate query, update, {new: true}, callback
_loginQuery: (providerId, externalUserId) ->
_getUserQuery: (providerId, externalUserId) ->
externalUserId = externalUserId.toString()
providerId = providerId.toString()
query =
@ -36,7 +42,7 @@ module.exports = ThirdPartyIdentityManager =
"thirdPartyIdentifiers.providerId": providerId
return query
_loginUpdate: (user, providerId, externalUserId, externalData) ->
_thirdPartyIdentifierUpdate: (user, providerId, externalUserId, externalData) ->
providerId = providerId.toString()
# get third party identifier object from array
thirdPartyIdentifier = user.thirdPartyIdentifiers.find (tpi) ->
@ -74,3 +80,12 @@ module.exports = ThirdPartyIdentityManager =
update = $pull: thirdPartyIdentifiers:
providerId: providerId
UserUpdater.updateUser user_id, update, callback
# attempt to unlink user but unlink user stub if not linked to user
unlinkUserStub: (user_id, providerId, callback) ->
ThirdPartyIdentityManager.unlink user_id, providerId, (err, res) ->
return callback err if err?
return callback null, res if res.nModified == 1
update = $pull: thirdPartyIdentifiers:
providerId: providerId
UserStub.update { _id: user_id }, update, callback

View file

@ -409,6 +409,7 @@ describe "AuthenticationController", ->
@res.sendStatus = sinon.stub()
@res.send = sinon.stub()
@res.status = sinon.stub().returns(@res)
@res.sendStatus = sinon.stub()
@middleware = @AuthenticationController.requireOauth()
describe "when Oauth2Server authenticates", ->