forked from loafle/openapi-generator-original
Added auth module
This commit is contained in:
parent
1a31c48ceb
commit
1cc6fb0421
@ -123,7 +123,8 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
|
||||
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts"));
|
||||
supportingFiles.add(new SupportingFile("middleware.mustache", "", "middleware.ts"));
|
||||
|
||||
supportingFiles.add(new SupportingFile("auth" + File.separator + "auth.mustache", "auth", "auth.ts"));
|
||||
|
||||
// models
|
||||
this.modelPackage = "";
|
||||
this.modelTemplateFiles.put("models/models.mustache", ".ts");
|
||||
|
62
modules/openapi-generator/src/main/resources/typescript/auth/auth.mustache
vendored
Normal file
62
modules/openapi-generator/src/main/resources/typescript/auth/auth.mustache
vendored
Normal 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));
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
// TODO: evaluate if we can easily get rid of this library
|
||||
import * as FormData from "form-data";
|
||||
// typings of url-parse are incorrect...
|
||||
// @ts-ignore
|
||||
import * as URLParse from "url-parse";
|
||||
|
||||
export enum HttpMethod {
|
||||
GET = "GET",
|
||||
@ -28,18 +31,20 @@ export class HttpException extends Error {
|
||||
export class RequestContext {
|
||||
private headers: { [key: string]: string } = {};
|
||||
private body: string | FormData = "";
|
||||
|
||||
public constructor(private url: string, private httpMethod: HttpMethod) {
|
||||
|
||||
private url: URLParse;
|
||||
|
||||
public constructor(url: string, private httpMethod: HttpMethod) {
|
||||
this.url = URLParse(url, true);
|
||||
}
|
||||
|
||||
public getUrl(): string {
|
||||
return this.url;
|
||||
return this.url.toString();
|
||||
}
|
||||
|
||||
public setUrl(url: string) {
|
||||
this.url = url;
|
||||
this.url = URLParse(url, true);
|
||||
}
|
||||
|
||||
|
||||
public setBody(body: string | FormData) {
|
||||
// HTTP-Spec 1.1 Section 4.3
|
||||
@ -66,6 +71,12 @@ export class RequestContext {
|
||||
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 {
|
||||
if (!this.headers["Cookie"]) {
|
||||
this.headers["Cookie"] = "";
|
||||
@ -73,7 +84,7 @@ export class RequestContext {
|
||||
this.headers["Cookie"] += name + "=" + value + "; ";
|
||||
}
|
||||
|
||||
public setHeader(key: string, value: string): void {
|
||||
public setHeaderParam(key: string, value: string): void {
|
||||
this.headers[key] = value;
|
||||
}
|
||||
}
|
||||
|
@ -20,9 +20,11 @@
|
||||
"dependencies": {
|
||||
"es6-promise": "^4.2.4",
|
||||
"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",
|
||||
"@types/form-data": "^2.2.1",
|
||||
"url-parse": "^1.4.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ts-node": "^7.0.0",
|
||||
|
Loading…
x
Reference in New Issue
Block a user