zyket 1.0.44 → 1.0.45
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 +3 -1
- package/package.json +3 -1
- package/src/services/auth/auth.js +8 -0
- package/src/services/auth/index.js +140 -0
- package/src/templates/default/src/routes/[test]/message.js +6 -0
- package/src/templates/auth/src/db-models/User.js +0 -53
- package/src/templates/auth/src/db-models/hooks/User.js +0 -46
- package/src/templates/auth/src/handlers/auth/login.js +0 -21
- package/src/templates/auth/src/handlers/auth/logout.js +0 -22
- package/src/templates/auth/src/handlers/auth/register.js +0 -30
- package/src/templates/auth/src/middlewares/logged.js +0 -0
package/index.js
CHANGED
|
@@ -10,6 +10,7 @@ const Worker = require("./src/services/bullmq/Worker");
|
|
|
10
10
|
const BullBoardExtension = require("./src/extensions/bullboard");
|
|
11
11
|
const Extension = require("./src/extensions/Extension");
|
|
12
12
|
const InteractiveStorageExtension = require("./src/extensions/interactive-storage");
|
|
13
|
+
const AuthService = require("./src/services/auth");
|
|
13
14
|
|
|
14
15
|
|
|
15
16
|
module.exports = {
|
|
@@ -21,5 +22,6 @@ module.exports = {
|
|
|
21
22
|
Worker,
|
|
22
23
|
EnvManager,
|
|
23
24
|
BullBoardExtension, InteractiveStorageExtension,
|
|
24
|
-
Extension
|
|
25
|
+
Extension,
|
|
26
|
+
AuthService
|
|
25
27
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zyket",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.45",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"description": "",
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@bull-board/express": "^6.14.2",
|
|
17
|
+
"better-auth": "^1.4.18",
|
|
17
18
|
"bullmq": "^5.63.0",
|
|
18
19
|
"colors": "^1.4.0",
|
|
19
20
|
"cors": "^2.8.5",
|
|
@@ -25,6 +26,7 @@
|
|
|
25
26
|
"multer": "^2.0.2",
|
|
26
27
|
"node-cron": "^4.2.1",
|
|
27
28
|
"node-dependency-injection": "^3.2.4",
|
|
29
|
+
"pg": "^8.18.0",
|
|
28
30
|
"prompts": "^2.4.2",
|
|
29
31
|
"redis": "^5.9.0",
|
|
30
32
|
"sequelize": "^6.37.7",
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const { Service } = require('../Service');
|
|
2
|
+
const { toNodeHandler } = require('better-auth/node');
|
|
3
|
+
const { betterAuth } = require("better-auth");
|
|
4
|
+
const { admin, bearer, organization } = require("better-auth/plugins");
|
|
5
|
+
const { Pool } = require("pg");
|
|
6
|
+
|
|
7
|
+
module.exports = class AuthService extends Service {
|
|
8
|
+
#container;
|
|
9
|
+
client;
|
|
10
|
+
|
|
11
|
+
constructor(container) {
|
|
12
|
+
super('AuthService');
|
|
13
|
+
this.#container = container;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async boot() {
|
|
17
|
+
if(process.env.DATABASE_DIALECT !== 'postgresql') throw new Error("AuthService only supports PostgreSQL as database dialect");
|
|
18
|
+
this.client = this.auth;
|
|
19
|
+
const express = this.#container.get('express');
|
|
20
|
+
express.regiterRawAllRoutes("/api/*splat", toNodeHandler(this.auth));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get plugins() {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get socialProviders() {
|
|
28
|
+
return {}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get userAdditionalFields() {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async sendResetPasswordEmail({ user, url, token }, request) {
|
|
36
|
+
throw new Error("sendResetPasswordEmail method not implemented");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async sendVerificationEmail({ user, url, token }, request) {
|
|
40
|
+
throw new Error("sendVerificationEmail method not implemented");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async sendInvitationEmail(data) {
|
|
44
|
+
throw new Error("sendInvitationEmail method not implemented");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async allowUserToCreateOrganization(user) {
|
|
48
|
+
throw new Error("allowUserToCreateOrganization method not implemented");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get auth() {
|
|
52
|
+
const cache = this.#container.get('cache');
|
|
53
|
+
return betterAuth({
|
|
54
|
+
plugins: [
|
|
55
|
+
admin(),
|
|
56
|
+
bearer(),
|
|
57
|
+
organization({
|
|
58
|
+
allowUserToCreateOrganization: async (user) => {
|
|
59
|
+
return await this.allowUserToCreateOrganization(user);
|
|
60
|
+
},
|
|
61
|
+
async sendInvitationEmail(data) {
|
|
62
|
+
return await this.sendInvitationEmail(data);
|
|
63
|
+
}
|
|
64
|
+
}),
|
|
65
|
+
...this.plugins,
|
|
66
|
+
],
|
|
67
|
+
socialProviders: this.socialProviders,
|
|
68
|
+
database: new Pool({
|
|
69
|
+
connectionString: process.env.DATABASE_URL || null,
|
|
70
|
+
}),
|
|
71
|
+
advanced: {
|
|
72
|
+
crossSubDomainCookies: {
|
|
73
|
+
enabled: true,
|
|
74
|
+
},
|
|
75
|
+
cookie: {
|
|
76
|
+
sameSite: "none",
|
|
77
|
+
secure: true,
|
|
78
|
+
path: "/",
|
|
79
|
+
state: {
|
|
80
|
+
attributes: {
|
|
81
|
+
sameSite: "none",
|
|
82
|
+
secure: true,
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
defaultCookieAttributes: {
|
|
87
|
+
secure: true,
|
|
88
|
+
sameSite: "none",
|
|
89
|
+
},
|
|
90
|
+
cookies: {
|
|
91
|
+
state: {
|
|
92
|
+
attributes: {
|
|
93
|
+
sameSite: "none",
|
|
94
|
+
secure: true,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
secondaryStorage: {
|
|
100
|
+
get: async (key) => {
|
|
101
|
+
return await cache.get(key);
|
|
102
|
+
},
|
|
103
|
+
set: async (key, value, ttl) => {
|
|
104
|
+
await cache.set(key, value);
|
|
105
|
+
if(ttl) await cache.expire(key, ttl);
|
|
106
|
+
},
|
|
107
|
+
delete: async (key) => {
|
|
108
|
+
await cache.del(key);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
emailAndPassword: {
|
|
112
|
+
enabled: true,
|
|
113
|
+
requireEmailVerification: false,
|
|
114
|
+
sendResetPassword: async ({ user, url, token }, request) => this.sendResetPasswordEmail({ user, url, token }, request),
|
|
115
|
+
},
|
|
116
|
+
emailVerification: {
|
|
117
|
+
sendVerificationEmail: async ({ user, url, token }, request) => this.sendVerificationEmail({ user, url, token }, request),
|
|
118
|
+
sendOnSignIn: true,
|
|
119
|
+
autoSignInAfterVerification: true,
|
|
120
|
+
},
|
|
121
|
+
user: {
|
|
122
|
+
additionalFields: this.userAdditionalFields
|
|
123
|
+
},
|
|
124
|
+
account: {
|
|
125
|
+
accountLinking: { enabled: true },
|
|
126
|
+
skipStateCookieCheck: true,
|
|
127
|
+
},
|
|
128
|
+
session: {
|
|
129
|
+
expiresIn: 60 * 60 * 24 * 7,
|
|
130
|
+
updateAge: 60 * 60 * 24
|
|
131
|
+
},
|
|
132
|
+
secret: process.env.AUTH_SECRET || 'your-secret-key-change-in-production',
|
|
133
|
+
trustedOrigins: process.env.TRUSTED_ORIGINS?.split(',') || ['http://localhost:5173', 'http://localhost:6632']
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
get client() {
|
|
138
|
+
return this.client;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
const { Route } = require("zyket");
|
|
2
|
+
const DefaultMiddleware = require("../../middlewares/default");
|
|
2
3
|
|
|
3
4
|
module.exports = class DefaultRoute extends Route {
|
|
5
|
+
middlewares = {
|
|
6
|
+
post: [ new DefaultMiddleware() ],
|
|
7
|
+
get: [ new DefaultMiddleware() ]
|
|
8
|
+
}
|
|
9
|
+
|
|
4
10
|
async post({ container, request }) {
|
|
5
11
|
const { test } = request.params;
|
|
6
12
|
container.get("logger").info("Default route GET");
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
module.exports = ({sequelize, container, Sequelize}) => {
|
|
2
|
-
const User = sequelize.define('user', {
|
|
3
|
-
id: {
|
|
4
|
-
type: Sequelize.UUID,
|
|
5
|
-
defaultValue: Sequelize.UUIDV4,
|
|
6
|
-
allowNull: false,
|
|
7
|
-
primaryKey: true
|
|
8
|
-
},
|
|
9
|
-
email: {
|
|
10
|
-
type: Sequelize.STRING,
|
|
11
|
-
allowNull: false
|
|
12
|
-
},
|
|
13
|
-
password: {
|
|
14
|
-
type: Sequelize.STRING,
|
|
15
|
-
allowNull: false
|
|
16
|
-
},
|
|
17
|
-
}, {
|
|
18
|
-
timestamps: true,
|
|
19
|
-
createdAt: 'created_at',
|
|
20
|
-
updatedAt: 'updated_at',
|
|
21
|
-
indexes: [
|
|
22
|
-
{
|
|
23
|
-
unique: true,
|
|
24
|
-
fields: ['email']
|
|
25
|
-
}
|
|
26
|
-
]
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
User.associate = models => {
|
|
30
|
-
// User is associated to an application through ApplicationUser
|
|
31
|
-
User.belongsToMany(models.Application, {
|
|
32
|
-
through: models.ApplicationUser,
|
|
33
|
-
as: 'applications',
|
|
34
|
-
foreignKey: 'user_id',
|
|
35
|
-
otherKey: 'application_id'
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
User.addScope('defaultScope', {
|
|
39
|
-
attributes: { exclude: ['password'] },
|
|
40
|
-
include: [
|
|
41
|
-
{
|
|
42
|
-
model: models.Application,
|
|
43
|
-
as: 'applications',
|
|
44
|
-
through: { attributes: [] }
|
|
45
|
-
}
|
|
46
|
-
]
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
require('./hooks/User')(User, container);
|
|
51
|
-
|
|
52
|
-
return User;
|
|
53
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-async-promise-executor */
|
|
2
|
-
const crypto = require('crypto')
|
|
3
|
-
|
|
4
|
-
module.exports = (User, container) => {
|
|
5
|
-
User.beforeCreate(async (user, options) => {
|
|
6
|
-
const salt = process.env.ENCRYPTION_SALT || ''
|
|
7
|
-
user.password = crypto.pbkdf2Sync(user.password, salt, 1000, 64, 'sha512').toString('hex')
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Verify if the password is valid
|
|
12
|
-
* @param {string} password - The password to verify
|
|
13
|
-
* @returns {Boolean} - Boolean
|
|
14
|
-
*/
|
|
15
|
-
User.prototype.isValidPassword = async function (password) {
|
|
16
|
-
const { User } = container.get('database').models
|
|
17
|
-
const user = await User.findByPk(this.id, {
|
|
18
|
-
attributes: ['password']
|
|
19
|
-
})
|
|
20
|
-
const hash = crypto.pbkdf2Sync(password, process.env.ENCRYPTION_SALT, 1000, 64, 'sha512').toString('hex')
|
|
21
|
-
return user.password === hash
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
User.prototype.generateAuthToken = async function () {
|
|
25
|
-
const token = crypto.randomBytes(32).toString('hex')
|
|
26
|
-
const cache = container.get('cache')
|
|
27
|
-
await cache.set(`auth:${token}`, JSON.stringify({
|
|
28
|
-
...this.toJSON()
|
|
29
|
-
}))
|
|
30
|
-
await cache.expire(`auth:${token}`, 60 * 60 * 24 * 7)
|
|
31
|
-
return token
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
User.prototype.removeAuthToken = function (token) {
|
|
35
|
-
return new Promise((resolve, reject) => {
|
|
36
|
-
try {
|
|
37
|
-
const cache = container.get('cache')
|
|
38
|
-
cache.del(`auth:${token}`).then(() => {
|
|
39
|
-
resolve()
|
|
40
|
-
}).catch(err => reject(err))
|
|
41
|
-
} catch (err) {
|
|
42
|
-
reject(err)
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
const { Handler } = require("zyket");
|
|
2
|
-
|
|
3
|
-
module.exports = class LoginHandler extends Handler {
|
|
4
|
-
middleware = [];
|
|
5
|
-
|
|
6
|
-
async handle({ container, socket, data, io }) {
|
|
7
|
-
const {email, password} = data
|
|
8
|
-
const { User } = container.get('database').models
|
|
9
|
-
const user = await User.findOne({ where: { email } })
|
|
10
|
-
|
|
11
|
-
if(!user) return socket.emit("auth.login", { error: "User not found" })
|
|
12
|
-
if(!await user.isValidPassword(password)) return socket.emit("auth.login", { error: "Invalid password" })
|
|
13
|
-
|
|
14
|
-
const token = await user.generateAuthToken()
|
|
15
|
-
|
|
16
|
-
socket.user = user
|
|
17
|
-
socket.token = token
|
|
18
|
-
|
|
19
|
-
socket.emit("auth.login", { ...user.toJSON(), token })
|
|
20
|
-
}
|
|
21
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
const { Handler } = require("zyket");
|
|
2
|
-
|
|
3
|
-
module.exports = class LogoutHandler extends Handler {
|
|
4
|
-
middleware = [];
|
|
5
|
-
|
|
6
|
-
async handle({ container, socket, data, io }) {
|
|
7
|
-
if(!socket.user) return socket.emit('auth.logout', { error: 'Not authenticated' });
|
|
8
|
-
const { token } = socket;
|
|
9
|
-
if (!token) return socket.emit('auth.logout', { error: 'Not authenticated' });
|
|
10
|
-
|
|
11
|
-
const cache = container.get('cache');
|
|
12
|
-
const tokenData = await cache.get(`auth:${token}`);
|
|
13
|
-
if (!tokenData) return socket.emit('auth.logout', { error: 'Not authenticated' });
|
|
14
|
-
|
|
15
|
-
try {
|
|
16
|
-
await cache.del(`auth:${token}`);
|
|
17
|
-
socket.emit('auth.logout', { success: true });
|
|
18
|
-
} catch (error) {
|
|
19
|
-
socket.emit('auth.logout', { error: error.message });
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const { Handler } = require("zyket");
|
|
2
|
-
const validator = require('validator');
|
|
3
|
-
|
|
4
|
-
module.exports = class RegisterHandler extends Handler {
|
|
5
|
-
middleware = [];
|
|
6
|
-
|
|
7
|
-
async handle({ container, socket, data, io }) {
|
|
8
|
-
const { email, password } = data;
|
|
9
|
-
|
|
10
|
-
if(!email) return socket.emit('auth.register', { error: 'Email is required' });
|
|
11
|
-
if(validator.isEmpty(email)) return socket.emit('auth.register', { error: 'Email is invalid' });
|
|
12
|
-
if(!validator.isEmail(email)) return socket.emit('auth.register', { error: 'Email is invalid' });
|
|
13
|
-
|
|
14
|
-
if(!password) return socket.emit('auth.register', { error: 'Password is required' });
|
|
15
|
-
if(validator.isEmpty(password)) return socket.emit('auth.register', { error: 'Password is invalid' });
|
|
16
|
-
if(!validator.isLength(password, { min: 6 })) return socket.emit('auth.register', { error: 'Password must be at least 6 characters' });
|
|
17
|
-
|
|
18
|
-
const { User } = container.get('database').models;
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
await User.create({ email, password })
|
|
22
|
-
} catch (error) {
|
|
23
|
-
return socket.emit('auth.register', { error: 'Email already exists' });
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const user = await User.findOne({ where: { email } });
|
|
27
|
-
|
|
28
|
-
socket.emit('auth.register', { user });
|
|
29
|
-
}
|
|
30
|
-
};
|
|
File without changes
|