Skip to content

Commit

Permalink
feat(runtime): Allow null return value in AuthProvider.getConfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
Embraser01 committed Feb 23, 2024
1 parent 9fda3f9 commit 611eab1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions packages/typoas-runtime/src/auth/api-key-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ export class ApiKeySecurityAuthentication implements SecurityAuthentication {

async applySecurityAuthentication(context: RequestContext): Promise<void> {
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);
Expand Down
2 changes: 1 addition & 1 deletion packages/typoas-runtime/src/auth/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export interface SecurityAuthentication {
}

export interface AuthProvider<T> {
getConfig(): Promise<T> | T;
getConfig(): Promise<T> | T | null;
}
7 changes: 6 additions & 1 deletion packages/typoas-runtime/src/auth/http-auth-basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export class HttpBasicSecurityAuthentication implements SecurityAuthentication {
async applySecurityAuthentication(context: RequestContext): Promise<void> {
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}`);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/typoas-runtime/src/auth/http-auth-bearer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
8 changes: 7 additions & 1 deletion packages/typoas-runtime/src/auth/oauth2-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ export class OAuth2SecurityAuthentication implements SecurityAuthentication {

async applySecurityAuthentication(context: RequestContext): Promise<void> {
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}`,
Expand Down

0 comments on commit 611eab1

Please sign in to comment.