Added auth module

This commit is contained in:
Tino Fuhrmann 2018-08-12 15:33:22 +02:00
parent 1a31c48ceb
commit 1cc6fb0421
4 changed files with 84 additions and 8 deletions

View File

@ -123,6 +123,7 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts")); supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts"));
supportingFiles.add(new SupportingFile("middleware.mustache", "", "middleware.ts")); supportingFiles.add(new SupportingFile("middleware.mustache", "", "middleware.ts"));
supportingFiles.add(new SupportingFile("auth" + File.separator + "auth.mustache", "auth", "auth.ts"));
// models // models
this.modelPackage = ""; this.modelPackage = "";

View File

@ -0,0 +1,62 @@
import {RequestContext} from '../http/http';
// typings for btoa are incorrect
//@ts-ignore
import * as btoa from "btoa";
export abstract class SecurityAuthentication {
public constructor(private name: string) {
}
/*
*
* @return returns the name of the security authentication as specified in OAI
*/
public getName(): string {
return this.name;
}
public abstract applySecurityAuthentication(context: RequestContext): void;
}
export class NoAuthentication extends SecurityAuthentication {
public constructor() {
super("_no_auth");
}
public applySecurityAuthentication(_context: RequestContext) {
}
}
export class APIKeyAuthentication extends SecurityAuthentication {
public constructor(authName: string, private paramName: string, private apiKey: string, private keyLocation: "query" | "header" | "cookie") {
super(authName);
}
public applySecurityAuthentication(context: RequestContext) {
if (this.keyLocation === "header") {
context.setHeaderParam(this.paramName, this.apiKey);
} else if (this.keyLocation === "cookie") {
context.addCookie(this.paramName, this.apiKey);
} else if (this.keyLocation === "query") {
context.setQueryParam(this.paramName, this.apiKey);
}
}
}
export class HttpBasicAuthentication extends SecurityAuthentication {
public constructor(authName: string, private username: string, private password: string) {
super(authName);
}
public applySecurityAuthentication(context: RequestContext) {
let comb = this.username + ":" + this.password;
context.setHeaderParam("Authentication", "Basic " + btoa(comb));
}
}

View File

@ -1,5 +1,8 @@
// TODO: evaluate if we can easily get rid of this library // TODO: evaluate if we can easily get rid of this library
import * as FormData from "form-data"; import * as FormData from "form-data";
// typings of url-parse are incorrect...
// @ts-ignore
import * as URLParse from "url-parse";
export enum HttpMethod { export enum HttpMethod {
GET = "GET", GET = "GET",
@ -28,19 +31,21 @@ export class HttpException extends Error {
export class RequestContext { export class RequestContext {
private headers: { [key: string]: string } = {}; private headers: { [key: string]: string } = {};
private body: string | FormData = ""; private body: string | FormData = "";
private url: URLParse;
public constructor(private url: string, private httpMethod: HttpMethod) { public constructor(url: string, private httpMethod: HttpMethod) {
this.url = URLParse(url, true);
} }
public getUrl(): string { public getUrl(): string {
return this.url; return this.url.toString();
} }
public setUrl(url: string) { public setUrl(url: string) {
this.url = url; this.url = URLParse(url, true);
} }
public setBody(body: string | FormData) { public setBody(body: string | FormData) {
// HTTP-Spec 1.1 Section 4.3 // HTTP-Spec 1.1 Section 4.3
if (this.httpMethod === HttpMethod.GET) { if (this.httpMethod === HttpMethod.GET) {
@ -66,6 +71,12 @@ export class RequestContext {
return this.body; return this.body;
} }
public setQueryParam(name: string, value: string) {
let queryObj = this.url.query;
queryObj[name] = value;
this.url.set("query", queryObj);
}
public addCookie(name: string, value: string): void { public addCookie(name: string, value: string): void {
if (!this.headers["Cookie"]) { if (!this.headers["Cookie"]) {
this.headers["Cookie"] = ""; this.headers["Cookie"] = "";
@ -73,7 +84,7 @@ export class RequestContext {
this.headers["Cookie"] += name + "=" + value + "; "; this.headers["Cookie"] += name + "=" + value + "; ";
} }
public setHeader(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
} }

View File

@ -20,9 +20,11 @@
"dependencies": { "dependencies": {
"es6-promise": "^4.2.4", "es6-promise": "^4.2.4",
"isomorphic-fetch": "^2.2.1", "isomorphic-fetch": "^2.2.1",
"@types/isomorphic-fetch": "0.0.34" "btoa": "^1.2.1",
"@types/isomorphic-fetch": "0.0.34",
"form-data": "^2.3.2", "form-data": "^2.3.2",
"@types/form-data": "^2.2.1", "@types/form-data": "^2.2.1",
"url-parse": "^1.4.3"
}, },
"devDependencies": { "devDependencies": {
"ts-node": "^7.0.0", "ts-node": "^7.0.0",