Skip to content

Commit 959a9a3

Browse files
ksvirkou-hubspotFornori
authored andcommitted
add OAuth2 to typescript template (OpenAPITools#9466)
* add OAuth to typescript * generate samples
1 parent 4695fb7 commit 959a9a3

File tree

6 files changed

+54
-24
lines changed

6 files changed

+54
-24
lines changed

modules/openapi-generator/src/main/resources/typescript/auth/auth.mustache

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ export class {{#lambda.pascalcase}}{{name}}{{/lambda.pascalcase}}Authentication
7575
public constructor({{#useInversify}}@inject(AbstractTokenProvider) @named("{{name}}") {{/useInversify}}private tokenProvider: TokenProvider) {}
7676
{{/isBasicBearer}}
7777
{{#isOAuth}}
78-
// TODO: How to handle oauth2 authentication!
79-
public constructor() {}
78+
/**
79+
* Configures OAuth2 with the necessary properties
80+
*
81+
* @param accessToken: The access token to be used for every request
82+
*/
83+
public constructor(private accessToken: string) {}
8084
{{/isOAuth}}
8185

8286
public getName(): string {
@@ -95,7 +99,7 @@ export class {{#lambda.pascalcase}}{{name}}{{/lambda.pascalcase}}Authentication
9599
context.setHeaderParam("Authorization", "Bearer " + await this.tokenProvider.getToken());
96100
{{/isBasicBearer}}
97101
{{#isOAuth}}
98-
// TODO
102+
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
99103
{{/isOAuth}}
100104
}
101105
}
@@ -119,7 +123,7 @@ export const authMethodServices = {
119123
export type ApiKeyConfiguration = string;
120124
export type HttpBasicConfiguration = { "username": string, "password": string };
121125
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
122-
export type OAuth2Configuration = string;
126+
export type OAuth2Configuration = { accessToken: string };
123127

124128
export type AuthMethodsConfiguration = {
125129
{{#authMethods}}
@@ -152,6 +156,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
152156
config["{{name}}"]["tokenProvider"]
153157
{{/isBasicBearer}}
154158
{{#isOAuth}}
159+
config["{{name}}"]["accessToken"]
155160
{{/isOAuth}}
156161
);
157162
}

samples/openapi3/client/petstore/typescript/builds/default/auth/auth.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ export class ApiKeyAuthentication implements SecurityAuthentication {
4848
* Applies oauth2 authentication to the request context.
4949
*/
5050
export class PetstoreAuthAuthentication implements SecurityAuthentication {
51-
// TODO: How to handle oauth2 authentication!
52-
public constructor() {}
51+
/**
52+
* Configures OAuth2 with the necessary properties
53+
*
54+
* @param accessToken: The access token to be used for every request
55+
*/
56+
public constructor(private accessToken: string) {}
5357

5458
public getName(): string {
5559
return "petstore_auth";
5660
}
5761

5862
public applySecurityAuthentication(context: RequestContext) {
59-
// TODO
63+
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
6064
}
6165
}
6266

@@ -69,7 +73,7 @@ export type AuthMethods = {
6973
export type ApiKeyConfiguration = string;
7074
export type HttpBasicConfiguration = { "username": string, "password": string };
7175
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
72-
export type OAuth2Configuration = string;
76+
export type OAuth2Configuration = { accessToken: string };
7377

7478
export type AuthMethodsConfiguration = {
7579
"api_key"?: ApiKeyConfiguration,
@@ -95,6 +99,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
9599

96100
if (config["petstore_auth"]) {
97101
authMethods["petstore_auth"] = new PetstoreAuthAuthentication(
102+
config["petstore_auth"]["accessToken"]
98103
);
99104
}
100105

samples/openapi3/client/petstore/typescript/builds/deno/auth/auth.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,19 @@ export class ApiKeyAuthentication implements SecurityAuthentication {
4646
* Applies oauth2 authentication to the request context.
4747
*/
4848
export class PetstoreAuthAuthentication implements SecurityAuthentication {
49-
// TODO: How to handle oauth2 authentication!
50-
public constructor() {}
49+
/**
50+
* Configures OAuth2 with the necessary properties
51+
*
52+
* @param accessToken: The access token to be used for every request
53+
*/
54+
public constructor(private accessToken: string) {}
5155

5256
public getName(): string {
5357
return "petstore_auth";
5458
}
5559

5660
public applySecurityAuthentication(context: RequestContext) {
57-
// TODO
61+
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
5862
}
5963
}
6064

@@ -67,7 +71,7 @@ export type AuthMethods = {
6771
export type ApiKeyConfiguration = string;
6872
export type HttpBasicConfiguration = { "username": string, "password": string };
6973
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
70-
export type OAuth2Configuration = string;
74+
export type OAuth2Configuration = { accessToken: string };
7175

7276
export type AuthMethodsConfiguration = {
7377
"api_key"?: ApiKeyConfiguration,
@@ -93,6 +97,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
9397

9498
if (config["petstore_auth"]) {
9599
authMethods["petstore_auth"] = new PetstoreAuthAuthentication(
100+
config["petstore_auth"]["accessToken"]
96101
);
97102
}
98103

samples/openapi3/client/petstore/typescript/builds/inversify/auth/auth.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,19 @@ export class ApiKeyAuthentication implements SecurityAuthentication {
5656
*/
5757
@injectable()
5858
export class PetstoreAuthAuthentication implements SecurityAuthentication {
59-
// TODO: How to handle oauth2 authentication!
60-
public constructor() {}
59+
/**
60+
* Configures OAuth2 with the necessary properties
61+
*
62+
* @param accessToken: The access token to be used for every request
63+
*/
64+
public constructor(private accessToken: string) {}
6165

6266
public getName(): string {
6367
return "petstore_auth";
6468
}
6569

6670
public applySecurityAuthentication(context: RequestContext) {
67-
// TODO
71+
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
6872
}
6973
}
7074

@@ -82,7 +86,7 @@ export const authMethodServices = {
8286
export type ApiKeyConfiguration = string;
8387
export type HttpBasicConfiguration = { "username": string, "password": string };
8488
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
85-
export type OAuth2Configuration = string;
89+
export type OAuth2Configuration = { accessToken: string };
8690

8791
export type AuthMethodsConfiguration = {
8892
"api_key"?: ApiKeyConfiguration,
@@ -108,6 +112,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
108112

109113
if (config["petstore_auth"]) {
110114
authMethods["petstore_auth"] = new PetstoreAuthAuthentication(
115+
config["petstore_auth"]["accessToken"]
111116
);
112117
}
113118

samples/openapi3/client/petstore/typescript/builds/jquery/auth/auth.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,19 @@ export class ApiKeyAuthentication implements SecurityAuthentication {
4646
* Applies oauth2 authentication to the request context.
4747
*/
4848
export class PetstoreAuthAuthentication implements SecurityAuthentication {
49-
// TODO: How to handle oauth2 authentication!
50-
public constructor() {}
49+
/**
50+
* Configures OAuth2 with the necessary properties
51+
*
52+
* @param accessToken: The access token to be used for every request
53+
*/
54+
public constructor(private accessToken: string) {}
5155

5256
public getName(): string {
5357
return "petstore_auth";
5458
}
5559

5660
public applySecurityAuthentication(context: RequestContext) {
57-
// TODO
61+
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
5862
}
5963
}
6064

@@ -67,7 +71,7 @@ export type AuthMethods = {
6771
export type ApiKeyConfiguration = string;
6872
export type HttpBasicConfiguration = { "username": string, "password": string };
6973
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
70-
export type OAuth2Configuration = string;
74+
export type OAuth2Configuration = { accessToken: string };
7175

7276
export type AuthMethodsConfiguration = {
7377
"api_key"?: ApiKeyConfiguration,
@@ -93,6 +97,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
9397

9498
if (config["petstore_auth"]) {
9599
authMethods["petstore_auth"] = new PetstoreAuthAuthentication(
100+
config["petstore_auth"]["accessToken"]
96101
);
97102
}
98103

samples/openapi3/client/petstore/typescript/builds/object_params/auth/auth.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ export class ApiKeyAuthentication implements SecurityAuthentication {
4848
* Applies oauth2 authentication to the request context.
4949
*/
5050
export class PetstoreAuthAuthentication implements SecurityAuthentication {
51-
// TODO: How to handle oauth2 authentication!
52-
public constructor() {}
51+
/**
52+
* Configures OAuth2 with the necessary properties
53+
*
54+
* @param accessToken: The access token to be used for every request
55+
*/
56+
public constructor(private accessToken: string) {}
5357

5458
public getName(): string {
5559
return "petstore_auth";
5660
}
5761

5862
public applySecurityAuthentication(context: RequestContext) {
59-
// TODO
63+
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
6064
}
6165
}
6266

@@ -69,7 +73,7 @@ export type AuthMethods = {
6973
export type ApiKeyConfiguration = string;
7074
export type HttpBasicConfiguration = { "username": string, "password": string };
7175
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
72-
export type OAuth2Configuration = string;
76+
export type OAuth2Configuration = { accessToken: string };
7377

7478
export type AuthMethodsConfiguration = {
7579
"api_key"?: ApiKeyConfiguration,
@@ -95,6 +99,7 @@ export function configureAuthMethods(config: AuthMethodsConfiguration | undefine
9599

96100
if (config["petstore_auth"]) {
97101
authMethods["petstore_auth"] = new PetstoreAuthAuthentication(
102+
config["petstore_auth"]["accessToken"]
98103
);
99104
}
100105

0 commit comments

Comments
 (0)