-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
200 lines (184 loc) · 5.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const express = require('express');
const app = express();
const swaggerUi = require('swagger-ui-express');
const swaggerJsdoc = require('swagger-jsdoc');
const PORT = 9999;
const mongoose = require('mongoose');
const CaptchaService = require('./captchaService');
const captcha = new CaptchaService();
const cors = require('cors')
// mongodb connection string
const DB_URI = 'mongodb+srv://user:user@cluster0.ocgwopd.mongodb.net/uncaptchable'
mongoose.connect(DB_URI)
.then((result) => {
app.listen(
PORT,
() => {
console.log(`API live on http://localhost:${PORT}`);
// captcha.initialize();
}
)
})
.catch((err) => console.log(err));
app.use(cors())
app.use(express.json());
// Serve the Swagger documentation
// Define Swagger options
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'UnCAPTCHAble',
version: '1.0.0',
description: 'An innovative CAPTCHA powerful against AIs.'
},
},
apis: ['./index.js'], // Replace with the actual path to your API file
};
// Generate the Swagger documentation
const swaggerDocs = swaggerJsdoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
/**
* @swagger
* /captcha:
* get:
* summary: Get a CAPTCHA
* description: Retrieve a random CAPTCHA
* responses:
* 200:
* description: A CAPTCHA object
*/
app.get('/captcha', (req, res) => {
captcha.getOne()
.then((requestedCaptcha) => {
res.status(200).send(requestedCaptcha);
})
.catch((err) => {
console.log(err);
res.status(400).send({ error: 'Bad request.' });
});
});
/**
* @swagger
* /captcha/list:
* get:
* summary: Get a list of aCAPTCHA IDs
* description: Retrieve a list of all available CAPTCHA IDs
* responses:
* 200:
* description: List of CAPTCHA IDs retrieved
*/
app.get('/captcha/list', (req, res) => {
captcha.getIdList()
.then((idList) => {
res.status(200).send(idList);
})
.catch((err) => {
console.log(err);
res.status(400).send({ error: 'Bad request.' });
});
});
/**
* @swagger
* /captcha/{id}:
* get:
* summary: Get a specific CAPTCHA
* description: Retrieve a specific CAPTCHA by ID
* parameters:
* - name: id
* in: path
* description: ID of the CAPTCHA to retrieve
* required: true
* schema:
* type: string
* responses:
* 200:
* description: CAPTCHA retrieved
* 404:
* description: The requested CAPTCHA does not exist
*/
app.get('/captcha/:id', (req, res) => {
const id = req.params.id;
captcha.get(id)
.then((requestedCaptcha) => {
res.status(200).send(requestedCaptcha);
})
.catch((err) => {
console.log(err);
res.status(404).send({ error: 'The requested CAPTCHA does not exist.' });
});
});
/**
* @swagger
* /captcha/{id}/validate:
* post:
* summary: Validate a CAPTCHA
* description: Validate the answers for a specific CAPTCHA
* parameters:
* - name: id
* in: path
* description: ID of the CAPTCHA to validate
* required: true
* schema:
* type: string
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* answers:
* type: array
* items:
* type: string
* example:
* answers: ['A', 'B', 'C']
* responses:
* 200:
* description: Validation successful
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* description: Success message
* 401:
* description: The CAPTCHA was not solved correctly
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* description: Error message
* 500:
* description: An error occurred during validation
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* description: Error message
*/
app.post('/captcha/:id/validate', (req, res) => {
const id = req.params.id;
const answers = req.body.answers;
captcha.validate(id, answers)
.then((validation) => {
if (validation) {
res.send({ message: 'You solved the CAPTCHA! Here\'s a cookie for you: 🍪' })
} else {
res.status(401).send({ message: 'The CAPTCHA was not solved correctly, try again.' })
}
})
.catch((err) => {
console.log(err)
res.status(500).send({ message: 'There was an error, try again.' })
})
});