diff --git a/modules/openapi-generator/src/main/resources/typescript/auth/auth.mustache b/modules/openapi-generator/src/main/resources/typescript/auth/auth.mustache index 7d88980180e..90a7171b6f5 100644 --- a/modules/openapi-generator/src/main/resources/typescript/auth/auth.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/auth/auth.mustache @@ -33,11 +33,16 @@ export class NoAuthentication extends SecurityAuthentication { } export class APIKeyAuthentication extends SecurityAuthentication { + private apiKey: string; - public constructor(authName: string, private paramName: string, private apiKey: string, private keyLocation: "query" | "header" | "cookie") { + public constructor(authName: string, private paramName: string, private keyLocation: "query" | "header" | "cookie") { super(authName); } + public setApiKey(apiKey: string) { + this.apiKey = apiKey; + } + public applySecurityAuthentication(context: RequestContext) { if (this.keyLocation === "header") { context.setHeaderParam(this.paramName, this.apiKey); @@ -50,13 +55,28 @@ export class APIKeyAuthentication extends SecurityAuthentication { } export class HttpBasicAuthentication extends SecurityAuthentication { - - public constructor(authName: string, private username: string, private password: string) { + private username: string; + private password: string; + + public constructor(authName: string) { super(authName); } + public setUserNameAndPassword(username: string, password: string) { + this.username = username; + this.password = password; + } + public applySecurityAuthentication(context: RequestContext) { let comb = this.username + ":" + this.password; context.setHeaderParam("Authentication", "Basic " + btoa(comb)); } -} \ No newline at end of file +} + +// TODO: add oauth2 +// TODO: check ^last +export const authMethods = { + {{#authMethods}} + "{{name}}": {{#isApiKey}}new APIKeyAuthentication("{{name}}", "{{keyParamName}}", {{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}){{/isApiKey}}{{#isBasic}}new HttpBasicAuthentication("{{keyParamName}}"){{/isBasic}}{{#isOAuth}}null{{/isOAuth}}{{^last}},{{/last}} + {{/authMethods}} +} diff --git a/samples/client/petstore/typescript/builds/default/auth/auth.ts b/samples/client/petstore/typescript/builds/default/auth/auth.ts index 7d88980180e..17c80b1d75a 100644 --- a/samples/client/petstore/typescript/builds/default/auth/auth.ts +++ b/samples/client/petstore/typescript/builds/default/auth/auth.ts @@ -33,11 +33,16 @@ export class NoAuthentication extends SecurityAuthentication { } export class APIKeyAuthentication extends SecurityAuthentication { + private apiKey: string; - public constructor(authName: string, private paramName: string, private apiKey: string, private keyLocation: "query" | "header" | "cookie") { + public constructor(authName: string, private paramName: string, private keyLocation: "query" | "header" | "cookie") { super(authName); } + public setApiKey(apiKey: string) { + this.apiKey = apiKey; + } + public applySecurityAuthentication(context: RequestContext) { if (this.keyLocation === "header") { context.setHeaderParam(this.paramName, this.apiKey); @@ -50,13 +55,26 @@ export class APIKeyAuthentication extends SecurityAuthentication { } export class HttpBasicAuthentication extends SecurityAuthentication { - - public constructor(authName: string, private username: string, private password: string) { + private username: string; + private password: string; + + public constructor(authName: string) { super(authName); } + public setUserNameAndPassword(username: string, password: string) { + this.username = username; + this.password = password; + } + public applySecurityAuthentication(context: RequestContext) { let comb = this.username + ":" + this.password; context.setHeaderParam("Authentication", "Basic " + btoa(comb)); } -} \ No newline at end of file +} + +// TODO: add oauth2 +export const authMethods = { + "api_key": new APIKeyAuthentication("api_key", "api_key", "header"), + "petstore_auth": null, +} diff --git a/samples/client/petstore/typescript/builds/default/configuration.ts b/samples/client/petstore/typescript/builds/default/configuration.ts index 59f97141075..a9a25e5952d 100644 --- a/samples/client/petstore/typescript/builds/default/configuration.ts +++ b/samples/client/petstore/typescript/builds/default/configuration.ts @@ -7,6 +7,7 @@ export interface ConfigurationParameters { baseServer?: ServerConfiguration; httpApi?: HttpLibrary; // override for fetch implementation middleware?: Middleware[]; // middleware to apply before/after fetch requests + username?: string; // parameter for basic security password?: string; // parameter for basic security apiKey?: string | ((name: string) => string); // parameter for apiKey security diff --git a/samples/client/petstore/typescript/builds/default/servers.ts b/samples/client/petstore/typescript/builds/default/servers.ts index 7cc55b40db0..e91a7c80ff7 100644 --- a/samples/client/petstore/typescript/builds/default/servers.ts +++ b/samples/client/petstore/typescript/builds/default/servers.ts @@ -11,5 +11,5 @@ export class ServerConfiguration { } export const servers = [ - new ServerConfiguration("http://petstore.swagger.io/v2"), + new ServerConfiguration("/"), ]