From d925c1bd193b5c19cb23a246f04fc46d0429fc75 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Tue, 28 Apr 2020 21:52:18 -0700 Subject: [PATCH] fix(authentication-oauth): Add getEntity method to oAuth authentication and remove provider field for other calls (#1935) --- packages/authentication-oauth/src/strategy.ts | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/authentication-oauth/src/strategy.ts b/packages/authentication-oauth/src/strategy.ts index 0cbf047932..9d7e00508a 100644 --- a/packages/authentication-oauth/src/strategy.ts +++ b/packages/authentication-oauth/src/strategy.ts @@ -6,6 +6,7 @@ import { AuthenticationRequest, AuthenticationBaseStrategy, AuthenticationResult } from '@feathersjs/authentication'; import { Params } from '@feathersjs/feathers'; +import { NotAuthenticated } from '@feathersjs/errors'; const debug = Debug('@feathersjs/authentication-oauth/strategy'); @@ -126,8 +127,27 @@ export class OAuthStrategy extends AuthenticationBaseStrategy { return this.entityService.patch(id, data, params); } - async authenticate (authentication: AuthenticationRequest, params: Params) { + async getEntity (result: any, params: Params) { + const { entityService } = this; + const { entityId = entityService.id, entity } = this.configuration; + + if (!entityId || result[entityId] === undefined) { + throw new NotAuthenticated('Could not get oAuth entity'); + } + + if (!params.provider) { + return result; + } + + return entityService.get(result[entityId], { + ...params, + [entity]: result + }); + } + + async authenticate (authentication: AuthenticationRequest, originalParams: Params) { const entity: string = this.configuration.entity; + const { provider, ...params } = originalParams; const profile = await this.getProfile(authentication, params); const existingEntity = await this.findEntity(profile, params) || await this.getCurrentEntity(params); @@ -139,7 +159,7 @@ export class OAuthStrategy extends AuthenticationBaseStrategy { return { authentication: { strategy: this.name }, - [entity]: authEntity + [entity]: await this.getEntity(authEntity, originalParams) }; } }