Skip to content

Commit f6a8e4f

Browse files
committed
feat: add authorization in documentation
1 parent 274757b commit f6a8e4f

2 files changed

Lines changed: 73 additions & 23 deletions

File tree

app.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,6 @@ app.head("/", (request, response) => {
3737
response.status(200);
3838
});
3939

40-
app.use(function (req, res, next) {
41-
if (!process.env.NODE_NSFW_KEY) {
42-
next();
43-
} else if (!req.headers.authorization && !!process.env.NODE_NSFW_KEY) {
44-
//console.log("No auth");
45-
return res.status(401).json({
46-
error: "No Authorization Provided"
47-
});
48-
} else if (req.headers.authorization !== process.env.NODE_NSFW_KEY) {
49-
//console.log("invalid auth");
50-
return res.status(401).json({
51-
error: "Invalid Authorization"
52-
});
53-
} else {
54-
next();
55-
}
56-
});
5740

5841
app.use(helmet());
5942
app.use(compression());
@@ -108,12 +91,6 @@ app.use(
10891
app.use(cookieParser());
10992
/* End Cookie Settings */
11093

111-
/* Start of Routing Modules */
112-
const v3_router = require("./routes/v3_route");
113-
v3_router(app);
114-
115-
/* End of Routing Modules */
116-
11794

11895
const apiVersion = process.env.API_VERSION || "v3";
11996
const testURL = "/api/" + apiVersion + "/health";
@@ -128,6 +105,32 @@ let docs = require(docsFolder + 'docs.json');
128105
app.use("/docs", swaggerUi.serve, swaggerUi.setup(docs, {
129106
explorer: true,
130107
}));
108+
/* Begin Authorization */
109+
app.use(function (req, res, next) {
110+
if (!process.env.NODE_NSFW_KEY) {
111+
next();
112+
} else if (!req.headers.authorization && !!process.env.NODE_NSFW_KEY) {
113+
//console.log("No auth");
114+
return res.status(401).json({
115+
error: "No Authorization Provided"
116+
});
117+
} else if (req.headers.authorization !== process.env.NODE_NSFW_KEY) {
118+
//console.log("invalid auth");
119+
return res.status(401).json({
120+
error: "Invalid Authorization"
121+
});
122+
} else {
123+
next();
124+
}
125+
});
126+
/* End Authorization */
127+
/* Start of Routing Modules */
128+
const v3_router = require("./routes/v3_route");
129+
v3_router(app);
130+
131+
/* End of Routing Modules */
132+
133+
131134
app.get("*", function (req, res) {
132135
res.status(404);
133136

docs/docs.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
"/api/v3/health": {
1818
"get": {
1919
"summary": "Health Check",
20+
"security": [
21+
{
22+
"authorization": []
23+
}
24+
],
2025
"description": "Check if the API is up and running",
2126
"responses": {
2227
"200": {
@@ -41,6 +46,11 @@
4146
"/api/v3/meta/categories": {
4247
"get": {
4348
"summary": "Get Categories",
49+
"security": [
50+
{
51+
"authorization": []
52+
}
53+
],
4454
"description": "Get all categories, n1 is negative 1",
4555
"responses": {
4656
"200": {
@@ -62,6 +72,11 @@
6272
"/api/v3/meta/hosts": {
6373
"get": {
6474
"summary": "Get Hosts List",
75+
"security": [
76+
{
77+
"authorization": []
78+
}
79+
],
6580
"description": "Get all blocked and allowed hosts",
6681
"responses": {
6782
"200": {
@@ -83,6 +98,11 @@
8398
"/api/v3/meta/hosts/{host}": {
8499
"get": {
85100
"summary": "Check Host",
101+
"security": [
102+
{
103+
"authorization": []
104+
}
105+
],
86106
"description": "Check if a host is blocked or allowed",
87107
"parameters": [
88108
{
@@ -115,6 +135,11 @@
115135
"/api/v3/classification/{url}": {
116136
"get": {
117137
"summary": "Classify URL",
138+
"security": [
139+
{
140+
"authorization": []
141+
}
142+
],
118143
"description": "Classify an image from a URL",
119144
"parameters": [
120145
{
@@ -153,6 +178,11 @@
153178
"/api/v3/classification": {
154179
"post": {
155180
"summary": "Classify Images",
181+
"security": [
182+
{
183+
"authorization": []
184+
}
185+
],
156186
"description": "Classify images with uploaded files",
157187
"requestBody": {
158188
"content": {
@@ -195,6 +225,11 @@
195225
"/api/v3/hash/{hash}": {
196226
"get": {
197227
"summary": "Get Hash Data",
228+
"security": [
229+
{
230+
"authorization": []
231+
}
232+
],
198233
"description": "Check if a hash is in the database",
199234
"parameters": [
200235
{
@@ -233,6 +268,11 @@
233268
"/api/v3/hash": {
234269
"post": {
235270
"summary": "Get Hash Data",
271+
"security": [
272+
{
273+
"authorization": []
274+
}
275+
],
236276
"description": "Get hashes from uploaded files, won't classify images",
237277
"requestBody": {
238278
"content": {
@@ -766,6 +806,13 @@
766806
}
767807
}
768808
}
809+
},
810+
"securitySchemes": {
811+
"authorization": {
812+
"type": "apiKey",
813+
"in": "header",
814+
"name": "Authorization"
815+
}
769816
}
770817
}
771818
}

0 commit comments

Comments
 (0)