From 12b06fa0d07cd5ec480951437a873a884af54ba9 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Fri, 20 Dec 2024 15:33:21 +0800 Subject: [PATCH] chore: cleanup codes --- .../server/src/core/auth/controller.ts | 2 ++ .../backend/server/src/core/version/guard.ts | 2 +- .../server/src/plugins/oauth/controller.ts | 3 ++ packages/backend/server/tests/version.spec.ts | 28 +++++++++---------- packages/frontend/graphql/src/schema.ts | 11 +++++++- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/packages/backend/server/src/core/auth/controller.ts b/packages/backend/server/src/core/auth/controller.ts index 013bb61144919..9b95f0b3201bf 100644 --- a/packages/backend/server/src/core/auth/controller.ts +++ b/packages/backend/server/src/core/auth/controller.ts @@ -72,6 +72,7 @@ export class AuthController { } @Public() + @UseNamedGuard('version') @Post('/preflight') async preflight( @Body() params?: { email: string } @@ -236,6 +237,7 @@ export class AuthController { } @Public() + @UseNamedGuard('version') @Post('/magic-link') async magicLinkSignIn( @Req() req: Request, diff --git a/packages/backend/server/src/core/version/guard.ts b/packages/backend/server/src/core/version/guard.ts index c8205b0a9a741..916256a5973b4 100644 --- a/packages/backend/server/src/core/version/guard.ts +++ b/packages/backend/server/src/core/version/guard.ts @@ -35,6 +35,6 @@ export class VersionGuardProvider const version = req.headers['x-affine-version']; - return await this.version.checkVersion(version); + return this.version.checkVersion(version); } } diff --git a/packages/backend/server/src/plugins/oauth/controller.ts b/packages/backend/server/src/plugins/oauth/controller.ts index f38eaa2eb0992..fd9c59fe5a962 100644 --- a/packages/backend/server/src/plugins/oauth/controller.ts +++ b/packages/backend/server/src/plugins/oauth/controller.ts @@ -16,6 +16,7 @@ import { OauthAccountAlreadyConnected, OauthStateExpired, UnknownOauthProvider, + UseNamedGuard, } from '../../base'; import { AuthService, Public } from '../../core/auth'; import { UserService } from '../../core/user'; @@ -35,6 +36,7 @@ export class OAuthController { ) {} @Public() + @UseNamedGuard('version') @Post('/preflight') @HttpCode(HttpStatus.OK) async preflight( @@ -64,6 +66,7 @@ export class OAuthController { } @Public() + @UseNamedGuard('version') @Post('/callback') @HttpCode(HttpStatus.OK) async callback( diff --git a/packages/backend/server/tests/version.spec.ts b/packages/backend/server/tests/version.spec.ts index bb67c1397668e..0c5cf48ab10d9 100644 --- a/packages/backend/server/tests/version.spec.ts +++ b/packages/backend/server/tests/version.spec.ts @@ -108,11 +108,11 @@ test('should be able to prevent requests if version outdated', async t => { await runtime.set('version/allowedVersion', 'unknownVersion'); await t.notThrowsAsync( fetchWithVersion(app.getHttpServer(), undefined, HttpStatus.OK), - 'should not check version if invalid minVersion provided' + 'should not check version if invalid allowedVersion provided' ); await t.notThrowsAsync( fetchWithVersion(app.getHttpServer(), '0.0.1', HttpStatus.OK), - 'should not check version if invalid minVersion provided' + 'should not check version if invalid allowedVersion provided' ); await runtime.set('version/allowedVersion', '0.0.1'); @@ -121,7 +121,7 @@ test('should be able to prevent requests if version outdated', async t => { { message: 'Unsupported client version: 0.0.0, please upgrade to 0.0.1.', }, - 'should reject version if valid minVersion provided' + 'should reject version if valid allowedVersion provided' ); await runtime.set( @@ -130,7 +130,7 @@ test('should be able to prevent requests if version outdated', async t => { ); await t.notThrowsAsync( fetchWithVersion(app.getHttpServer(), '0.17.5', HttpStatus.OK), - 'should pass version if version satisfies minVersion' + 'should pass version if version satisfies allowedVersion' ); await t.throwsAsync( fetchWithVersion(app.getHttpServer(), '0.17.4', HttpStatus.FORBIDDEN), @@ -138,7 +138,7 @@ test('should be able to prevent requests if version outdated', async t => { message: 'Unsupported client version: 0.17.4, please upgrade to 0.18.0.', }, - 'should reject version if valid minVersion provided' + 'should reject version if valid allowedVersion provided' ); await t.throwsAsync( fetchWithVersion( @@ -150,7 +150,7 @@ test('should be able to prevent requests if version outdated', async t => { message: 'Unsupported client version: 0.17.6-nightly-f0d99f4, please upgrade to 0.18.0.', }, - 'should reject version if valid minVersion provided' + 'should reject version if valid allowedVersion provided' ); await t.notThrowsAsync( fetchWithVersion( @@ -158,11 +158,11 @@ test('should be able to prevent requests if version outdated', async t => { '0.18.0-nightly-cc9b38c', HttpStatus.OK ), - 'should pass version if version satisfies minVersion' + 'should pass version if version satisfies allowedVersion' ); await t.notThrowsAsync( fetchWithVersion(app.getHttpServer(), '0.18.1', HttpStatus.OK), - 'should pass version if version satisfies minVersion' + 'should pass version if version satisfies allowedVersion' ); } @@ -174,18 +174,18 @@ test('should be able to prevent requests if version outdated', async t => { await t.notThrowsAsync( fetchWithVersion(app.getHttpServer(), '0.0.1', HttpStatus.OK), - 'should pass version if version satisfies minVersion' + 'should pass version if version satisfies allowedVersion' ); await t.notThrowsAsync( fetchWithVersion(app.getHttpServer(), '0.1.2', HttpStatus.OK), - 'should pass version if version satisfies maxVersion' + 'should pass version if version satisfies allowedVersion' ); await t.throwsAsync( fetchWithVersion(app.getHttpServer(), '0.1.3', HttpStatus.FORBIDDEN), { message: 'Unsupported client version: 0.1.3, please upgrade to 0.3.0.', }, - 'should reject version if valid maxVersion provided' + 'should reject version if valid allowedVersion provided' ); await t.notThrowsAsync( @@ -194,7 +194,7 @@ test('should be able to prevent requests if version outdated', async t => { '0.2.0-nightly-cc9b38c', HttpStatus.OK ), - 'should pass version if version satisfies maxVersion' + 'should pass version if version satisfies allowedVersion' ); await t.throwsAsync( @@ -202,7 +202,7 @@ test('should be able to prevent requests if version outdated', async t => { { message: 'Unsupported client version: 0.2.0, please upgrade to 0.3.0.', }, - 'should reject version if valid maxVersion provided' + 'should reject version if valid allowedVersion provided' ); await t.throwsAsync( @@ -211,7 +211,7 @@ test('should be able to prevent requests if version outdated', async t => { message: 'Unsupported client version: 0.3.1, please downgrade to 0.3.0.', }, - 'should reject version if valid maxVersion provided' + 'should reject version if valid allowedVersion provided' ); } }); diff --git a/packages/frontend/graphql/src/schema.ts b/packages/frontend/graphql/src/schema.ts index a48adf3315d8b..e8ca4fbaec927 100644 --- a/packages/frontend/graphql/src/schema.ts +++ b/packages/frontend/graphql/src/schema.ts @@ -1,4 +1,4 @@ -/* eslint-disable */ + export type Maybe = T | null; export type InputMaybe = T | null; export type Exact = { @@ -285,6 +285,7 @@ export type ErrorDataUnion = | SubscriptionNotExistsDataType | SubscriptionPlanNotFoundDataType | UnknownOauthProviderDataType + | UnsupportedClientVersionDataType | UnsupportedSubscriptionPlanDataType | VersionRejectedDataType | WrongSignInCredentialsDataType; @@ -361,6 +362,7 @@ export enum ErrorNames { TOO_MANY_REQUEST = 'TOO_MANY_REQUEST', UNKNOWN_OAUTH_PROVIDER = 'UNKNOWN_OAUTH_PROVIDER', UNSPLASH_IS_NOT_CONFIGURED = 'UNSPLASH_IS_NOT_CONFIGURED', + UNSUPPORTED_CLIENT_VERSION = 'UNSUPPORTED_CLIENT_VERSION', UNSUPPORTED_SUBSCRIPTION_PLAN = 'UNSUPPORTED_SUBSCRIPTION_PLAN', USER_AVATAR_NOT_FOUND = 'USER_AVATAR_NOT_FOUND', USER_NOT_FOUND = 'USER_NOT_FOUND', @@ -1208,6 +1210,13 @@ export interface UnknownOauthProviderDataType { name: Scalars['String']['output']; } +export interface UnsupportedClientVersionDataType { + __typename?: 'UnsupportedClientVersionDataType'; + action: Scalars['String']['output']; + clientVersion: Scalars['String']['output']; + recommendedVersion: Scalars['String']['output']; +} + export interface UnsupportedSubscriptionPlanDataType { __typename?: 'UnsupportedSubscriptionPlanDataType'; plan: Scalars['String']['output'];