Skip to content

Commit

Permalink
Setup initial authorizatoion (#9)
Browse files Browse the repository at this point in the history
Adds api key authorization

 add api key authentication to specification

 make generator support authotization (see implement (apikey) authorization and better errors LuvDaSun/Skiffa#21)

 write authenticationhandler

 authorize some endpoints

 debugging

closes Update generator and add some more sensible endpoints #8
  • Loading branch information
elmerbulthuis committed Jan 26, 2024
1 parent 89e439e commit f86d80e
Show file tree
Hide file tree
Showing 17 changed files with 360 additions and 101 deletions.
4 changes: 2 additions & 2 deletions generated/nwd-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "^20.11.6",
"@types/node": "^20.11.7",
"goodrouter": "^2.1.2",
"oa42-lib": "^0.6.4"
"oa42-lib": "^0.7.0"
},
"devDependencies": {
"typescript": "^5.3.3",
Expand Down
2 changes: 1 addition & 1 deletion generated/nwd-api/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// ██║ ██║██╔═══╝ ██╔══╝ ██║╚██╗██║██╔══██║██╔═══╝ ██║╚════██║██╔═══╝
// ╚██████╔╝██║ ███████╗██║ ╚████║██║ ██║██║ ██║ ██║███████╗
// ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚══════╝
// v0.1.6 -- www.OpenApi42.org
// v0.2.1 -- www.OpenApi42.org
export * from "./types.js";
export * from "./validators.js";
export * from "./parsers.js";
Expand Down
33 changes: 24 additions & 9 deletions generated/nwd-api/src/client-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
// ██║ ██║██╔═══╝ ██╔══╝ ██║╚██╗██║██╔══██║██╔═══╝ ██║╚════██║██╔═══╝
// ╚██████╔╝██║ ███████╗██║ ╚████║██║ ██║██║ ██║ ██║███████╗
// ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚══════╝
// v0.1.6 -- www.OpenApi42.org
// v0.2.1 -- www.OpenApi42.org
import assert from "assert/strict";
import test from "node:test";
import * as main from "./main.js";
import * as http from "http";
type ServerAuthentication = {
apiToken: boolean,
};
test("get-main-categories 200 application/json", async () => {
const server = new main.Server({
const server = new main.Server<ServerAuthentication>({
validateIncomingParameters: false,
validateIncomingEntity: false,
validateOutgoingParameters: false,
Expand All @@ -27,6 +30,7 @@ contentType: "application/json",
entity: () => main.mockMainCategory200GetSchema(),
}
});
server.registerApiTokenAuthentication((credential) => credential === "super-secret-api-key")
let lastError: unknown;
const httpServer = http.createServer();
httpServer.addListener(
Expand All @@ -46,7 +50,9 @@ const operationResult = await main.getMainCategories(
contentType: null,
parameters: {},
},
{},
{
apiToken: "super-secret-api-key",
},
{
baseUrl,
validateIncomingParameters: false,
Expand All @@ -71,7 +77,7 @@ httpServer.close((error) => (error == null ? resolve() : reject(error))),
}
});
test("create-main-category application/json 201 application/json", async () => {
const server = new main.Server({
const server = new main.Server<ServerAuthentication>({
validateIncomingParameters: false,
validateIncomingEntity: false,
validateOutgoingParameters: false,
Expand All @@ -92,6 +98,7 @@ contentType: "application/json",
entity: () => main.mockMainCategoryPost201Schema(),
}
});
server.registerApiTokenAuthentication((credential) => credential === "super-secret-api-key")
let lastError: unknown;
const httpServer = http.createServer();
httpServer.addListener(
Expand All @@ -112,7 +119,9 @@ contentType: "application/json",
parameters: {},
entity: () => main.mockMainCategoryPostRequestBodySchema(),
},
{},
{
apiToken: "super-secret-api-key",
},
{
baseUrl,
validateIncomingParameters: false,
Expand All @@ -137,7 +146,7 @@ httpServer.close((error) => (error == null ? resolve() : reject(error))),
}
});
test("get-sub-categories 200 application/json", async () => {
const server = new main.Server({
const server = new main.Server<ServerAuthentication>({
validateIncomingParameters: false,
validateIncomingEntity: false,
validateOutgoingParameters: false,
Expand All @@ -157,6 +166,7 @@ contentType: "application/json",
entity: () => main.mockSubCategoryMainCategoryId200GetSchema(),
}
});
server.registerApiTokenAuthentication((credential) => credential === "super-secret-api-key")
let lastError: unknown;
const httpServer = http.createServer();
httpServer.addListener(
Expand All @@ -178,7 +188,9 @@ parameters: {
mainCategoryId: main.mockParametersSchema(),
},
},
{},
{
apiToken: "super-secret-api-key",
},
{
baseUrl,
validateIncomingParameters: false,
Expand All @@ -203,7 +215,7 @@ httpServer.close((error) => (error == null ? resolve() : reject(error))),
}
});
test("create-sub-category application/json 201 application/json", async () => {
const server = new main.Server({
const server = new main.Server<ServerAuthentication>({
validateIncomingParameters: false,
validateIncomingEntity: false,
validateOutgoingParameters: false,
Expand All @@ -229,6 +241,7 @@ contentType: "application/json",
entity: () => main.mockSubCategoryMainCategoryIdPost201Schema(),
}
});
server.registerApiTokenAuthentication((credential) => credential === "super-secret-api-key")
let lastError: unknown;
const httpServer = http.createServer();
httpServer.addListener(
Expand All @@ -251,7 +264,9 @@ mainCategoryId: main.mockParametersSchema(),
},
entity: () => main.mockSubCategoryMainCategoryIdPostRequestBodySchema(),
},
{},
{
apiToken: "super-secret-api-key",
},
{
baseUrl,
validateIncomingParameters: false,
Expand Down
Loading

0 comments on commit f86d80e

Please sign in to comment.