Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): Allow null return value in AuthProvider.getConfig() #50

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading