From 899e19d19e37d5f03935d668d166d1bb29daa722 Mon Sep 17 00:00:00 2001 From: Murad Khateeb Date: Tue, 9 Jul 2024 14:44:04 +0500 Subject: [PATCH 1/2] Fix the Apple POST callback issue while authenticating --- packages/authentication-oauth/src/index.ts | 21 ++++++++++++------- packages/authentication-oauth/src/service.ts | 19 ++++++++++++++--- .../test/utils/provider.ts | 14 ++++++++----- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/packages/authentication-oauth/src/index.ts b/packages/authentication-oauth/src/index.ts index b4971674e5..d37dffd420 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,16 +34,23 @@ export const oauth = const grantConfig = getGrantConfig(authService) 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()] } }) - if (typeof oauthService.publish === 'function') { + if (typeof app.service(servicePath) === 'function') { app.service(servicePath).publish(() => null) } + + if (typeof app.service(callbackServicePath).publish === 'function') { + app.service(callbackServicePath).publish(() => null) + } } diff --git a/packages/authentication-oauth/src/service.ts b/packages/authentication-oauth/src/service.ts index 957814642a..11d1e1e8e7 100644 --- a/packages/authentication-oauth/src/service.ts +++ b/packages/authentication-oauth/src/service.ts @@ -166,9 +166,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 } @@ -177,3 +174,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) + } +} diff --git a/packages/authentication-oauth/test/utils/provider.ts b/packages/authentication-oauth/test/utils/provider.ts index 77eafa7a84..bedf81d3c1 100644 --- a/packages/authentication-oauth/test/utils/provider.ts +++ b/packages/authentication-oauth/test/utils/provider.ts @@ -4,6 +4,7 @@ import http from 'http' import _url from 'url' import qs from 'qs' +import { userInfo } from 'os' const buffer = (req: http.IncomingMessage, done: any) => { let data = '' @@ -157,11 +158,14 @@ const oauth2 = (port: number) => if (query.response_mode === 'form_post') { provider === 'apple' ? res.end( - qs.stringify({ - code: 'code', - user: { name: { firstName: 'jon', lastName: 'doe' }, email: 'jon@doe.com' } - }) - ) + JSON.stringify({ + body: { + access_token: 'token', + refresh_token: 'refresh', + expires_in: 3600 + } + }) + ) : res.end('code') return } From 67e5e8fe83959cd88d05b942bb3bec2c8cccb9ad Mon Sep 17 00:00:00 2001 From: Murad Khateeb Date: Tue, 16 Jul 2024 13:40:51 +0500 Subject: [PATCH 2/2] Apply changes pertaining to version 5.0.28 --- .../authentication-oauth/test/utils/provider.ts | 16 ++++++---------- packages/transport-commons/src/http.ts | 8 ++++---- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/packages/authentication-oauth/test/utils/provider.ts b/packages/authentication-oauth/test/utils/provider.ts index bedf81d3c1..b3f7694a6e 100644 --- a/packages/authentication-oauth/test/utils/provider.ts +++ b/packages/authentication-oauth/test/utils/provider.ts @@ -4,7 +4,6 @@ import http from 'http' import _url from 'url' import qs from 'qs' -import { userInfo } from 'os' const buffer = (req: http.IncomingMessage, done: any) => { let data = '' @@ -158,14 +157,11 @@ const oauth2 = (port: number) => if (query.response_mode === 'form_post') { provider === 'apple' ? res.end( - JSON.stringify({ - body: { - access_token: 'token', - refresh_token: 'refresh', - expires_in: 3600 - } - }) - ) + qs.stringify({ + code: 'code', + user: { name: { firstName: 'jon', lastName: 'doe' }, email: 'jon@doe.com' } + }) + ) : res.end('code') return } @@ -283,4 +279,4 @@ const on = { authorize: (_opts: any) => {}, access: (_opts: any) => {}, profile: (_opts: any) => {} -} +} \ No newline at end of file diff --git a/packages/transport-commons/src/http.ts b/packages/transport-commons/src/http.ts index 262dde2176..f7114b238f 100644 --- a/packages/transport-commons/src/http.ts +++ b/packages/transport-commons/src/http.ts @@ -62,10 +62,6 @@ export function getStatusCode(context: HookContext, body: any, location: string return http.status } - if (context.method === 'create') { - return statusCodes.created - } - if (location !== undefined) { return statusCodes.seeOther } @@ -74,6 +70,10 @@ export function getStatusCode(context: HookContext, body: any, location: string return statusCodes.noContent } + if (context.method === 'create') { + return statusCodes.created + } + return statusCodes.success }