From 611eab115436d880edf20883dfe8d71b8029e875 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Fernandes Date: Fri, 23 Feb 2024 16:15:18 +0100 Subject: [PATCH] feat(runtime): Allow null return value in AuthProvider.getConfig() --- CHANGELOG.md | 10 ++++++++++ packages/typoas-runtime/src/auth/api-key-auth.ts | 5 +++++ packages/typoas-runtime/src/auth/base.ts | 2 +- packages/typoas-runtime/src/auth/http-auth-basic.ts | 7 ++++++- packages/typoas-runtime/src/auth/http-auth-bearer.ts | 4 ++++ packages/typoas-runtime/src/auth/oauth2-auth.ts | 8 +++++++- 6 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84128a3..a491603 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # CHANGELOG.md +## Unreleased + +- Allow null return value in AuthProvider.getConfig() [#50](https://github.com/Embraser01/typoas/pull/50) + +## 3.1.2 - 2024-01-26 + +> Only the runtime was published, the generator and cli are still at 3.1.5 + +- Fix AuthProviders configuration [#44](https://github.com/Embraser01/typoas/issues/44) + ## 3.1.5 - 2023-10-25 > Only the generator and cli were published, the runtime is still at 3.1.3 diff --git a/packages/typoas-runtime/src/auth/api-key-auth.ts b/packages/typoas-runtime/src/auth/api-key-auth.ts index 3d11191..e700f18 100644 --- a/packages/typoas-runtime/src/auth/api-key-auth.ts +++ b/packages/typoas-runtime/src/auth/api-key-auth.ts @@ -22,7 +22,12 @@ export class ApiKeySecurityAuthentication implements SecurityAuthentication { async applySecurityAuthentication(context: RequestContext): Promise { if (!this.provider) return; + const apiToken = await this.provider.getConfig(); + if (apiToken === null) { + return; + } + switch (this.in) { case 'query': return context.setQueryParam(this.name, apiToken); diff --git a/packages/typoas-runtime/src/auth/base.ts b/packages/typoas-runtime/src/auth/base.ts index 18c2608..7c4163f 100644 --- a/packages/typoas-runtime/src/auth/base.ts +++ b/packages/typoas-runtime/src/auth/base.ts @@ -13,5 +13,5 @@ export interface SecurityAuthentication { } export interface AuthProvider { - getConfig(): Promise | T; + getConfig(): Promise | T | null; } diff --git a/packages/typoas-runtime/src/auth/http-auth-basic.ts b/packages/typoas-runtime/src/auth/http-auth-basic.ts index 2f67ddb..6ff5f91 100644 --- a/packages/typoas-runtime/src/auth/http-auth-basic.ts +++ b/packages/typoas-runtime/src/auth/http-auth-basic.ts @@ -9,7 +9,12 @@ export class HttpBasicSecurityAuthentication implements SecurityAuthentication { async applySecurityAuthentication(context: RequestContext): Promise { if (!this.provider) return; - const { username, password } = await this.provider.getConfig(); + const res = await this.provider.getConfig(); + if (res === null) { + return; + } + + const { username, password } = res; const base64 = btoa(`${username}:${password}`); return context.setHeaderParam('Authorization', `Basic ${base64}`); } diff --git a/packages/typoas-runtime/src/auth/http-auth-bearer.ts b/packages/typoas-runtime/src/auth/http-auth-bearer.ts index aef12dc..8a2f59a 100644 --- a/packages/typoas-runtime/src/auth/http-auth-bearer.ts +++ b/packages/typoas-runtime/src/auth/http-auth-bearer.ts @@ -12,6 +12,10 @@ export class HttpBearerSecurityAuthentication if (!this.provider) return; const res = await this.provider.getConfig(); + if (res === null) { + return; + } + const token = typeof res === 'string' ? res : res.token; const prefix = typeof res === 'string' ? 'Bearer' : res.prefixName || 'Bearer'; diff --git a/packages/typoas-runtime/src/auth/oauth2-auth.ts b/packages/typoas-runtime/src/auth/oauth2-auth.ts index 5173f13..4a19676 100644 --- a/packages/typoas-runtime/src/auth/oauth2-auth.ts +++ b/packages/typoas-runtime/src/auth/oauth2-auth.ts @@ -44,7 +44,13 @@ export class OAuth2SecurityAuthentication implements SecurityAuthentication { async applySecurityAuthentication(context: RequestContext): Promise { if (!this.provider) return; - const { accessToken, tokenType } = await this.provider.getConfig(); + + const res = await this.provider.getConfig(); + if (res === null) { + return; + } + + const { accessToken, tokenType } = res; return context.setHeaderParam( 'Authorization', `${tokenType || 'Bearer'} ${accessToken}`,