From ebf3de1d56d3dda916a5e74962137886ab6e7846 Mon Sep 17 00:00:00 2001 From: daffl Date: Mon, 3 Jun 2024 17:22:50 -0700 Subject: [PATCH 1/2] fix(authentication-oauth): Allow POST oauth callbacks --- packages/authentication-oauth/src/index.ts | 9 ++++++++- packages/authentication-oauth/src/service.ts | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/authentication-oauth/src/index.ts b/packages/authentication-oauth/src/index.ts index f079dc6344..b7b0194207 100644 --- a/packages/authentication-oauth/src/index.ts +++ b/packages/authentication-oauth/src/index.ts @@ -3,7 +3,7 @@ import { createDebug } from '@feathersjs/commons' import { resolveDispatch } from '@feathersjs/schema' import { OAuthStrategy, OAuthProfile } from './strategy' -import { redirectHook, OAuthService } from './service' +import { redirectHook, OAuthService, OAuthCallbackService } from './service' import { getGrantConfig, authenticationServiceOptions, OauthSetupSettings } from './utils' const debug = createDebug('@feathersjs/authentication-oauth') @@ -34,6 +34,7 @@ export const oauth = const grantConfig = getGrantConfig(authService) const serviceOptions = authenticationServiceOptions(authService, oauthOptions) const servicePath = `${grantConfig.defaults.prefix || 'oauth'}/:provider` + const callbackServicePath = `${servicePath}/callback` app.use(servicePath, new OAuthService(authService, oauthOptions), serviceOptions) @@ -43,6 +44,12 @@ export const oauth = around: { all: [resolveDispatch(), redirectHook()] } }) + app.use( + callbackServicePath, + new OAuthCallbackService(app.service(servicePath) as unknown as OAuthService), + serviceOptions + ) + if (typeof oauthService.publish === 'function') { app.service(servicePath).publish(() => null) } diff --git a/packages/authentication-oauth/src/service.ts b/packages/authentication-oauth/src/service.ts index c0f0c072b3..1ae575d6aa 100644 --- a/packages/authentication-oauth/src/service.ts +++ b/packages/authentication-oauth/src/service.ts @@ -178,10 +178,6 @@ export class OAuthService { async get(override: string, params: OAuthParams) { const result = await this.handler('GET', params, {}, override) - if (override === 'callback') { - return this.authenticate(params, result) - } - return result } @@ -189,3 +185,19 @@ export class OAuthService { return this.handler('POST', params, data) } } + +export class OAuthCallbackService { + constructor(public service: OAuthService) {} + + async find(params: OAuthParams) { + const result = await this.service.handler('GET', params, {}, 'callback') + + return this.service.authenticate(params, result) + } + + async create(data: any, params: OAuthParams) { + const result = await this.service.handler('POST', params, data, 'callback') + + return this.service.authenticate(params, result) + } +} From 41d7aa9c9801a469917418a03832df22ac7ec832 Mon Sep 17 00:00:00 2001 From: daffl Date: Tue, 4 Jun 2024 13:47:46 -0700 Subject: [PATCH 2/2] Add hooks and publishers to both services --- packages/authentication-oauth/src/index.ts | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/authentication-oauth/src/index.ts b/packages/authentication-oauth/src/index.ts index b7b0194207..9d25b6f6c1 100644 --- a/packages/authentication-oauth/src/index.ts +++ b/packages/authentication-oauth/src/index.ts @@ -35,22 +35,22 @@ export const oauth = const serviceOptions = authenticationServiceOptions(authService, oauthOptions) const servicePath = `${grantConfig.defaults.prefix || 'oauth'}/:provider` const callbackServicePath = `${servicePath}/callback` + const oauthService = new OAuthService(authService, oauthOptions) - app.use(servicePath, new OAuthService(authService, oauthOptions), serviceOptions) - - const oauthService = app.service(servicePath) - - oauthService.hooks({ + app.use(servicePath, oauthService, serviceOptions) + app.use(callbackServicePath, new OAuthCallbackService(oauthService), serviceOptions) + app.service(servicePath).hooks({ + around: { all: [resolveDispatch(), redirectHook()] } + }) + app.service(callbackServicePath).hooks({ around: { all: [resolveDispatch(), redirectHook()] } }) - app.use( - callbackServicePath, - new OAuthCallbackService(app.service(servicePath) as unknown as OAuthService), - serviceOptions - ) - - if (typeof oauthService.publish === 'function') { + if (typeof app.service(servicePath).publish === 'function') { app.service(servicePath).publish(() => null) } + + if (typeof app.service(callbackServicePath).publish === 'function') { + app.service(callbackServicePath).publish(() => null) + } }