add OAuth2 to typescript template (#9466)

* add OAuth to typescript

* generate samples
This commit is contained in:
Kuzma 2021-05-17 19:22:18 +03:00 committed by GitHub
parent 1c733247b1
commit e1ef7d40b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 24 deletions

View File

@ -75,8 +75,12 @@ export class {{#lambda.pascalcase}}{{name}}{{/lambda.pascalcase}}Authentication
public constructor({{#useInversify}}@inject(AbstractTokenProvider) @named("{{name}}") {{/useInversify}}private tokenProvider: TokenProvider) {}
{{/isBasicBearer}}
{{#isOAuth}}
// TODO: How to handle oauth2 authentication!
public constructor() {}
/**
* Configures OAuth2 with the necessary properties
*
* @param accessToken: The access token to be used for every request
*/
public constructor(private accessToken: string) {}
{{/isOAuth}}
public getName(): string {
@ -95,7 +99,7 @@ export class {{#lambda.pascalcase}}{{name}}{{/lambda.pascalcase}}Authentication
context.setHeaderParam("Authorization", "Bearer " + await this.tokenProvider.getToken());
{{/isBasicBearer}}
{{#isOAuth}}
// TODO
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
{{/isOAuth}}
}
}
@ -119,7 +123,7 @@ export const authMethodServices = {
export type ApiKeyConfiguration = string;
export type HttpBasicConfiguration = { "username": string, "password": string };
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
export type OAuth2Configuration = string;
export type OAuth2Configuration = { accessToken: string };
export type AuthMethodsConfiguration = {
{{#authMethods}}
@ -152,6 +156,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
config["{{name}}"]["tokenProvider"]
{{/isBasicBearer}}
{{#isOAuth}}
config["{{name}}"]["accessToken"]
{{/isOAuth}}
);
}

View File

@ -48,15 +48,19 @@ export class ApiKeyAuthentication implements SecurityAuthentication {
* Applies oauth2 authentication to the request context.
*/
export class PetstoreAuthAuthentication implements SecurityAuthentication {
// TODO: How to handle oauth2 authentication!
public constructor() {}
/**
* Configures OAuth2 with the necessary properties
*
* @param accessToken: The access token to be used for every request
*/
public constructor(private accessToken: string) {}
public getName(): string {
return "petstore_auth";
}
public applySecurityAuthentication(context: RequestContext) {
// TODO
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
}
}
@ -69,7 +73,7 @@ export type AuthMethods = {
export type ApiKeyConfiguration = string;
export type HttpBasicConfiguration = { "username": string, "password": string };
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
export type OAuth2Configuration = string;
export type OAuth2Configuration = { accessToken: string };
export type AuthMethodsConfiguration = {
"api_key"?: ApiKeyConfiguration,
@ -95,6 +99,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
if (config["petstore_auth"]) {
authMethods["petstore_auth"] = new PetstoreAuthAuthentication(
config["petstore_auth"]["accessToken"]
);
}

View File

@ -46,15 +46,19 @@ export class ApiKeyAuthentication implements SecurityAuthentication {
* Applies oauth2 authentication to the request context.
*/
export class PetstoreAuthAuthentication implements SecurityAuthentication {
// TODO: How to handle oauth2 authentication!
public constructor() {}
/**
* Configures OAuth2 with the necessary properties
*
* @param accessToken: The access token to be used for every request
*/
public constructor(private accessToken: string) {}
public getName(): string {
return "petstore_auth";
}
public applySecurityAuthentication(context: RequestContext) {
// TODO
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
}
}
@ -67,7 +71,7 @@ export type AuthMethods = {
export type ApiKeyConfiguration = string;
export type HttpBasicConfiguration = { "username": string, "password": string };
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
export type OAuth2Configuration = string;
export type OAuth2Configuration = { accessToken: string };
export type AuthMethodsConfiguration = {
"api_key"?: ApiKeyConfiguration,
@ -93,6 +97,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
if (config["petstore_auth"]) {
authMethods["petstore_auth"] = new PetstoreAuthAuthentication(
config["petstore_auth"]["accessToken"]
);
}

View File

@ -56,15 +56,19 @@ export class ApiKeyAuthentication implements SecurityAuthentication {
*/
@injectable()
export class PetstoreAuthAuthentication implements SecurityAuthentication {
// TODO: How to handle oauth2 authentication!
public constructor() {}
/**
* Configures OAuth2 with the necessary properties
*
* @param accessToken: The access token to be used for every request
*/
public constructor(private accessToken: string) {}
public getName(): string {
return "petstore_auth";
}
public applySecurityAuthentication(context: RequestContext) {
// TODO
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
}
}
@ -82,7 +86,7 @@ export const authMethodServices = {
export type ApiKeyConfiguration = string;
export type HttpBasicConfiguration = { "username": string, "password": string };
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
export type OAuth2Configuration = string;
export type OAuth2Configuration = { accessToken: string };
export type AuthMethodsConfiguration = {
"api_key"?: ApiKeyConfiguration,
@ -108,6 +112,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
if (config["petstore_auth"]) {
authMethods["petstore_auth"] = new PetstoreAuthAuthentication(
config["petstore_auth"]["accessToken"]
);
}

View File

@ -46,15 +46,19 @@ export class ApiKeyAuthentication implements SecurityAuthentication {
* Applies oauth2 authentication to the request context.
*/
export class PetstoreAuthAuthentication implements SecurityAuthentication {
// TODO: How to handle oauth2 authentication!
public constructor() {}
/**
* Configures OAuth2 with the necessary properties
*
* @param accessToken: The access token to be used for every request
*/
public constructor(private accessToken: string) {}
public getName(): string {
return "petstore_auth";
}
public applySecurityAuthentication(context: RequestContext) {
// TODO
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
}
}
@ -67,7 +71,7 @@ export type AuthMethods = {
export type ApiKeyConfiguration = string;
export type HttpBasicConfiguration = { "username": string, "password": string };
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
export type OAuth2Configuration = string;
export type OAuth2Configuration = { accessToken: string };
export type AuthMethodsConfiguration = {
"api_key"?: ApiKeyConfiguration,
@ -93,6 +97,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
if (config["petstore_auth"]) {
authMethods["petstore_auth"] = new PetstoreAuthAuthentication(
config["petstore_auth"]["accessToken"]
);
}

View File

@ -48,15 +48,19 @@ export class ApiKeyAuthentication implements SecurityAuthentication {
* Applies oauth2 authentication to the request context.
*/
export class PetstoreAuthAuthentication implements SecurityAuthentication {
// TODO: How to handle oauth2 authentication!
public constructor() {}
/**
* Configures OAuth2 with the necessary properties
*
* @param accessToken: The access token to be used for every request
*/
public constructor(private accessToken: string) {}
public getName(): string {
return "petstore_auth";
}
public applySecurityAuthentication(context: RequestContext) {
// TODO
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
}
}
@ -69,7 +73,7 @@ export type AuthMethods = {
export type ApiKeyConfiguration = string;
export type HttpBasicConfiguration = { "username": string, "password": string };
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
export type OAuth2Configuration = string;
export type OAuth2Configuration = { accessToken: string };
export type AuthMethodsConfiguration = {
"api_key"?: ApiKeyConfiguration,
@ -95,6 +99,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
if (config["petstore_auth"]) {
authMethods["petstore_auth"] = new PetstoreAuthAuthentication(
config["petstore_auth"]["accessToken"]
);
}