@seip/blue-bird 0.4.3 → 0.4.5
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/LICENSE +21 -0
- package/backend/index.js +1 -1
- package/backend/routes/api.js +31 -31
- package/backend/routes/frontend.js +3 -3
- package/core/auth.js +61 -30
- package/core/cli/scaffolding-auth.js +1037 -0
- package/core/swagger.js +40 -25
- package/frontend/public/robots.txt +0 -0
- package/frontend/public/sitemap.xml +0 -0
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andres Paiva
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/backend/index.js
CHANGED
package/backend/routes/api.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import Router from "
|
|
2
|
-
import Validator from "
|
|
3
|
-
|
|
4
|
-
const routerApiExample = new Router("/api");
|
|
5
|
-
|
|
6
|
-
routerApiExample.get("/users", (req, res) => {
|
|
7
|
-
const users = [
|
|
8
|
-
{
|
|
9
|
-
name: "John Doe",
|
|
10
|
-
email: "john.doe@example.com",
|
|
11
|
-
},
|
|
12
|
-
{
|
|
13
|
-
name: "Jane Doe2",
|
|
14
|
-
email: "jane.doe2@example.com",
|
|
15
|
-
},
|
|
16
|
-
];
|
|
17
|
-
res.json(users);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const loginSchema = {
|
|
21
|
-
email: { required: true, email: true },
|
|
22
|
-
password: { required: true, min: 6 },
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const loginValidator = new Validator(loginSchema);
|
|
26
|
-
|
|
27
|
-
routerApiExample.post("/login", loginValidator.middleware(), (req, res) => {
|
|
28
|
-
res.json({ message: "Login successful", body: req.body });
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
export default routerApiExample;
|
|
1
|
+
import Router from "@seip/blue-bird/core/router.js";
|
|
2
|
+
import Validator from "@seip/blue-bird/core/validate.js";
|
|
3
|
+
|
|
4
|
+
const routerApiExample = new Router("/api");
|
|
5
|
+
|
|
6
|
+
routerApiExample.get("/users", (req, res) => {
|
|
7
|
+
const users = [
|
|
8
|
+
{
|
|
9
|
+
name: "John Doe",
|
|
10
|
+
email: "john.doe@example.com",
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
name: "Jane Doe2",
|
|
14
|
+
email: "jane.doe2@example.com",
|
|
15
|
+
},
|
|
16
|
+
];
|
|
17
|
+
res.json(users);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const loginSchema = {
|
|
21
|
+
email: { required: true, email: true },
|
|
22
|
+
password: { required: true, min: 6 },
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const loginValidator = new Validator(loginSchema);
|
|
26
|
+
|
|
27
|
+
routerApiExample.post("/login", loginValidator.middleware(), (req, res) => {
|
|
28
|
+
res.json({ message: "Login successful", body: req.body });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export default routerApiExample;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Router from "
|
|
2
|
-
import Template from "
|
|
3
|
-
import App from "
|
|
1
|
+
import Router from "@seip/blue-bird/core/router.js";
|
|
2
|
+
import Template from "@seip/blue-bird/core/template.js";
|
|
3
|
+
import App from "@seip/blue-bird/core/app.js";
|
|
4
4
|
import seoData from "./seo.js";
|
|
5
5
|
|
|
6
6
|
const routerFrontendExample = new Router();
|
package/core/auth.js
CHANGED
|
@@ -1,45 +1,82 @@
|
|
|
1
1
|
import jwt from "jsonwebtoken";
|
|
2
|
+
import crypto from "node:crypto";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
|
-
* Auth class to handle JWT generation, verification and protection.
|
|
5
|
+
* Auth class to handle JWT generation, verification and protection with AES-256-GCM encryption.
|
|
5
6
|
*/
|
|
6
7
|
class Auth {
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
+
* Encrypts a payload using AES-256-GCM.
|
|
10
|
+
* @param {Object} payload - The data to encrypt.
|
|
11
|
+
* @param {string} secret - The secret key for encryption.
|
|
12
|
+
* @returns {string} The encrypted string in format iv:tag:encrypted.
|
|
13
|
+
*/
|
|
14
|
+
static encrypt(payload, secret) {
|
|
15
|
+
const iv = crypto.randomBytes(12);
|
|
16
|
+
const key = crypto.createHash("sha256").update(secret).digest();
|
|
17
|
+
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
|
|
18
|
+
let encrypted = cipher.update(JSON.stringify(payload), "utf8", "hex");
|
|
19
|
+
encrypted += cipher.final("hex");
|
|
20
|
+
const tag = cipher.getAuthTag().toString("hex");
|
|
21
|
+
return `${iv.toString("hex")}:${tag}:${encrypted}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Decrypts a payload using AES-256-GCM.
|
|
26
|
+
* @param {string} data - The encrypted string in format iv:tag:encrypted.
|
|
27
|
+
* @param {string} secret - The secret key for decryption.
|
|
28
|
+
* @returns {Object|null} The decrypted object or null if failed.
|
|
29
|
+
*/
|
|
30
|
+
static decrypt(data, secret) {
|
|
31
|
+
try {
|
|
32
|
+
const [ivHex, tagHex, encryptedHex] = data.split(":");
|
|
33
|
+
if (!ivHex || !tagHex || !encryptedHex) return null;
|
|
34
|
+
|
|
35
|
+
const iv = Buffer.from(ivHex, "hex");
|
|
36
|
+
const tag = Buffer.from(tagHex, "hex");
|
|
37
|
+
const key = crypto.createHash("sha256").update(secret).digest();
|
|
38
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
|
|
39
|
+
decipher.setAuthTag(tag);
|
|
40
|
+
|
|
41
|
+
let decrypted = decipher.update(encryptedHex, "hex", "utf8");
|
|
42
|
+
decrypted += decipher.final("utf8");
|
|
43
|
+
return JSON.parse(decrypted);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Generates an encrypted JWT token.
|
|
9
51
|
* @param {Object} payload - The data to store in the token.
|
|
10
52
|
* @param {string} [secret=process.env.JWT_SECRET] - The secret key.
|
|
11
|
-
* @param {string
|
|
53
|
+
* @param {string} [expiresIn="24h"] - Expiration time.
|
|
12
54
|
* @returns {string} The generated token.
|
|
13
|
-
* @example
|
|
14
|
-
* const token = Auth.generateToken({ id: 1 });
|
|
15
|
-
* console.log(token);
|
|
16
|
-
*
|
|
17
55
|
*/
|
|
18
56
|
static generateToken(
|
|
19
57
|
payload,
|
|
20
58
|
secret = process.env.JWT_SECRET,
|
|
21
|
-
expiresIn = "24h"
|
|
59
|
+
expiresIn = "24h"
|
|
22
60
|
) {
|
|
23
61
|
if (!secret)
|
|
24
62
|
throw new Error("FATAL: JWT_SECRET environment variable is not defined.");
|
|
25
|
-
|
|
63
|
+
const encrypted = this.encrypt(payload, secret);
|
|
64
|
+
return jwt.sign({ data: encrypted }, secret, { expiresIn });
|
|
26
65
|
}
|
|
27
66
|
|
|
28
67
|
/**
|
|
29
|
-
* Verifies a JWT token.
|
|
68
|
+
* Verifies and decrypts a JWT token.
|
|
30
69
|
* @param {string} token - The token to verify.
|
|
31
70
|
* @param {string} [secret=process.env.JWT_SECRET] - The secret key.
|
|
32
|
-
* @returns {Object|null} The decoded payload or null if invalid.
|
|
33
|
-
* @example
|
|
34
|
-
* const token = Auth.generateToken({ id: 1 });
|
|
35
|
-
* const decoded = Auth.verifyToken(token);
|
|
36
|
-
* console.log(decoded);
|
|
71
|
+
* @returns {Object|null} The decoded and decrypted payload or null if invalid.
|
|
37
72
|
*/
|
|
38
73
|
static verifyToken(token, secret = process.env.JWT_SECRET) {
|
|
39
74
|
if (!secret)
|
|
40
75
|
throw new Error("FATAL: JWT_SECRET environment variable is not defined.");
|
|
41
76
|
try {
|
|
42
|
-
|
|
77
|
+
const decoded = jwt.verify(token, secret);
|
|
78
|
+
if (!decoded || !decoded.data) return null;
|
|
79
|
+
return this.decrypt(decoded.data, secret);
|
|
43
80
|
} catch (error) {
|
|
44
81
|
return null;
|
|
45
82
|
}
|
|
@@ -48,33 +85,27 @@ class Auth {
|
|
|
48
85
|
/**
|
|
49
86
|
* Middleware to protect routes. Checks for token in Cookies or Authorization header.
|
|
50
87
|
* @param {Object} options - Options for protection.
|
|
51
|
-
* @param {string} [options.redirect=null] - URL to redirect if not authenticated
|
|
88
|
+
* @param {string} [options.redirect=null] - URL to redirect if not authenticated.
|
|
52
89
|
* @param {string} [options.key="user"] - Key to store the decoded token in the request.
|
|
53
90
|
* @returns {Function} Express middleware.
|
|
54
|
-
* @example
|
|
55
|
-
* app.use(Auth.protect({ redirect: "/login" ,key:"user"}));
|
|
56
91
|
*/
|
|
57
|
-
static protect(options = {}) {
|
|
58
|
-
const { redirect = null, key = "user" } = options;
|
|
59
|
-
|
|
92
|
+
static protect(options = { redirect: null, key: "user" }) {
|
|
60
93
|
return (req, res, next) => {
|
|
61
94
|
const token =
|
|
62
|
-
req.cookies?.
|
|
95
|
+
req.cookies?.auth || req.headers.authorization?.split(" ")[1];
|
|
63
96
|
|
|
64
97
|
if (!token) {
|
|
65
|
-
if (redirect) return res.redirect(redirect);
|
|
66
|
-
return res
|
|
67
|
-
.status(401)
|
|
68
|
-
.json({ message: "Unauthorized: No token provided" });
|
|
98
|
+
if (options.redirect) return res.redirect(options.redirect);
|
|
99
|
+
return res.status(401).json({ message: "Unauthorized" });
|
|
69
100
|
}
|
|
70
101
|
|
|
71
102
|
const decoded = this.verifyToken(token);
|
|
72
103
|
if (!decoded) {
|
|
73
|
-
if (redirect) return res.redirect(redirect);
|
|
74
|
-
return res.status(401).json({ message: "Unauthorized
|
|
104
|
+
if (options.redirect) return res.redirect(options.redirect);
|
|
105
|
+
return res.status(401).json({ message: "Unauthorized" });
|
|
75
106
|
}
|
|
76
107
|
|
|
77
|
-
req[key] = decoded;
|
|
108
|
+
req[options.key || "user"] = decoded;
|
|
78
109
|
next();
|
|
79
110
|
};
|
|
80
111
|
}
|