@koziatek/http 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -0
- package/middleware/Auth.js +65 -0
- package/middleware/ErrorRender.js +2 -3
- package/package.json +4 -2
package/index.js
CHANGED
|
@@ -6,6 +6,7 @@ const { OpenReq } = require("./middleware/OpenReq");
|
|
|
6
6
|
const { ResData } = require("./middleware/ResData");
|
|
7
7
|
const { SendRes } = require("./middleware/SendRes");
|
|
8
8
|
const { Status } = require("./middleware/Status");
|
|
9
|
+
const { RequireAuth } = require("./middleware/Auth");
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
module.exports = {
|
|
@@ -18,6 +19,7 @@ module.exports = {
|
|
|
18
19
|
ResData,
|
|
19
20
|
SendRes,
|
|
20
21
|
Status,
|
|
22
|
+
RequireAuth
|
|
21
23
|
},
|
|
22
24
|
|
|
23
25
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const jwt = require('jsonwebtoken')
|
|
2
|
+
const jwksRsa = require('jwks-rsa')
|
|
3
|
+
|
|
4
|
+
const AUTH0_DOMAIN = 'character-quiz-ai.us.auth0.com'
|
|
5
|
+
const AUTH0_ISSUER = `https://${AUTH0_DOMAIN}/`
|
|
6
|
+
const AUTH0_AUDIENCE = 'https://api.characterquiz.ai'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* JWKS client (cached + rate limited)
|
|
10
|
+
*/
|
|
11
|
+
const jwksClient = jwksRsa({
|
|
12
|
+
jwksUri: `${AUTH0_ISSUER}.well-known/jwks.json`,
|
|
13
|
+
cache: true,
|
|
14
|
+
cacheMaxEntries: 5,
|
|
15
|
+
cacheMaxAge: 10 * 60 * 1000, // 10 minutes
|
|
16
|
+
rateLimit: true,
|
|
17
|
+
jwksRequestsPerMinute: 10
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
function getSigningKey (header, callback) {
|
|
21
|
+
jwksClient.getSigningKey(header.kid, (err, key) => {
|
|
22
|
+
if (err) return callback(err)
|
|
23
|
+
callback(null, key.getPublicKey())
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Express middleware
|
|
29
|
+
*/
|
|
30
|
+
function RequireAuth (req, res, next) {
|
|
31
|
+
const authHeader = req.headers.authorization
|
|
32
|
+
|
|
33
|
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
34
|
+
return res.status(401).json({ error: 'Missing Bearer token' })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const token = authHeader.split(' ')[1]
|
|
38
|
+
|
|
39
|
+
jwt.verify(
|
|
40
|
+
token,
|
|
41
|
+
getSigningKey,
|
|
42
|
+
{
|
|
43
|
+
algorithms: ['RS256'],
|
|
44
|
+
issuer: AUTH0_ISSUER,
|
|
45
|
+
audience: AUTH0_AUDIENCE
|
|
46
|
+
},
|
|
47
|
+
(err, decoded) => {
|
|
48
|
+
if (err) {
|
|
49
|
+
return res.status(401).json({
|
|
50
|
+
error: 'Invalid token',
|
|
51
|
+
details: err.message
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ✅ Attach claims
|
|
56
|
+
req.user = decoded
|
|
57
|
+
|
|
58
|
+
next()
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = {
|
|
64
|
+
RequireAuth
|
|
65
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { HttpStatusCodes } = require('../HttpStatusCodes.js');
|
|
2
|
-
|
|
2
|
+
|
|
3
3
|
|
|
4
4
|
function ErrorRenderer(err, req, res, next) {
|
|
5
5
|
let response;
|
|
@@ -12,10 +12,9 @@ function ErrorRenderer(err, req, res, next) {
|
|
|
12
12
|
else response = {
|
|
13
13
|
metadata: err?.metadata || false,
|
|
14
14
|
message: err?.message || false,
|
|
15
|
-
stack: err?.stack || false,
|
|
15
|
+
// stack: err?.stack || false,
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
Log.Debug(err);
|
|
19
18
|
res.status(statusCode).json(response);
|
|
20
19
|
}
|
|
21
20
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koziatek/http",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
"author": "",
|
|
14
14
|
"license": "ISC",
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@
|
|
16
|
+
"@koziatek/utils": "^1.0.0",
|
|
17
|
+
"jsonwebtoken": "^9.0.3",
|
|
18
|
+
"jwks-rsa": "^3.2.2"
|
|
17
19
|
},
|
|
18
20
|
"devDependencies": {
|
|
19
21
|
"jest": "^29.7.0"
|