Updated auth

This commit is contained in:
Tino Fuhrmann 2018-08-14 22:41:28 +02:00
parent fe2cc24f4d
commit 894bddac1b
4 changed files with 48 additions and 9 deletions

View File

@ -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 {
private username: string;
private password: string;
public constructor(authName: 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));
}
}
// 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}}
}

View File

@ -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 {
private username: string;
private password: string;
public constructor(authName: 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));
}
}
// TODO: add oauth2
export const authMethods = {
"api_key": new APIKeyAuthentication("api_key", "api_key", "header"),
"petstore_auth": null,
}

View File

@ -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

View File

@ -11,5 +11,5 @@ export class ServerConfiguration {
}
export const servers = [
new ServerConfiguration("http://petstore.swagger.io/v2"),
new ServerConfiguration("/"),
]