@urga-panel/ur-panels-core 1.0.10 → 1.0.12
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/dist/services/abstract/authServices/AuthService.d.ts +4 -5
- package/dist/services/abstract/authServices/AuthService.js +74 -30
- package/dist/services/main/httpServices/RequestHandlerService.js +3 -5
- package/package.json +1 -1
- package/src/services/abstract/authServices/AuthService.ts +40 -9
- package/src/services/main/httpServices/RequestHandlerService.ts +4 -5
|
@@ -33,6 +33,7 @@ export declare abstract class AuthService extends Service {
|
|
|
33
33
|
id: number;
|
|
34
34
|
username: string;
|
|
35
35
|
password: string;
|
|
36
|
+
email?: string;
|
|
36
37
|
role?: string;
|
|
37
38
|
databaseId?: string;
|
|
38
39
|
databases?: string[];
|
|
@@ -52,16 +53,14 @@ export declare abstract class AuthService extends Service {
|
|
|
52
53
|
request: any;
|
|
53
54
|
url: any;
|
|
54
55
|
}): Promise<any>;
|
|
55
|
-
checkToken({
|
|
56
|
-
|
|
57
|
-
refreshToken: string;
|
|
56
|
+
checkToken({ panel_token, options }: {
|
|
57
|
+
panel_token: string;
|
|
58
58
|
options?: {
|
|
59
59
|
role?: string;
|
|
60
60
|
};
|
|
61
61
|
}): Promise<{
|
|
62
62
|
valid: boolean;
|
|
63
|
-
|
|
64
|
-
newRefreshToken?: string;
|
|
63
|
+
newPanelToken?: string;
|
|
65
64
|
user?: any;
|
|
66
65
|
}>;
|
|
67
66
|
deneme({ params, request, url }: {
|
|
@@ -101,6 +101,7 @@ export class AuthService extends Service {
|
|
|
101
101
|
id: result.user.id,
|
|
102
102
|
username: result.user.username,
|
|
103
103
|
role: result.user.role,
|
|
104
|
+
email: result.user.email, // include email
|
|
104
105
|
databaseId: result.user.databaseId,
|
|
105
106
|
databases: result.user.databases // databaseId eklendi
|
|
106
107
|
}, JWT_SECRET, { expiresIn: '15m' });
|
|
@@ -109,6 +110,7 @@ export class AuthService extends Service {
|
|
|
109
110
|
id: result.user.id,
|
|
110
111
|
username: result.user.username,
|
|
111
112
|
role: result.user.role,
|
|
113
|
+
email: result.user.email, // include email
|
|
112
114
|
databaseId: result.user.databaseId,
|
|
113
115
|
databases: result.user.databases // databaseId eklendi
|
|
114
116
|
}, JWT_REFRESH_SECRET, { expiresIn: '7d' });
|
|
@@ -133,9 +135,8 @@ export class AuthService extends Service {
|
|
|
133
135
|
return this.resposeHandler({ status: "error", message: "An error occurred during login" });
|
|
134
136
|
}
|
|
135
137
|
}
|
|
136
|
-
async checkToken({
|
|
137
|
-
this.log.OK("
|
|
138
|
-
this.log.OK("Refresh Token:", refreshToken);
|
|
138
|
+
async checkToken({ panel_token, options }) {
|
|
139
|
+
this.log.OK("panel_token Token:", panel_token);
|
|
139
140
|
//debugger;
|
|
140
141
|
try {
|
|
141
142
|
// 1. Access token'ı doğrula
|
|
@@ -143,74 +144,116 @@ export class AuthService extends Service {
|
|
|
143
144
|
if (typeof window === "undefined") {
|
|
144
145
|
jwt = (await import("jsonwebtoken")).default;
|
|
145
146
|
}
|
|
146
|
-
const decoded = jwt.verify(
|
|
147
|
+
const decoded = jwt.verify(panel_token, JWT_SECRET);
|
|
147
148
|
const res = await this.getUserInfo({
|
|
148
149
|
userId: decoded.id,
|
|
149
150
|
userName: decoded.username,
|
|
150
151
|
userRole: decoded.role || "user" // Varsayılan rol
|
|
151
152
|
});
|
|
153
|
+
const newPanelToken = jwt.sign({
|
|
154
|
+
id: decoded.id,
|
|
155
|
+
username: decoded.username,
|
|
156
|
+
role: decoded.role,
|
|
157
|
+
email: decoded.email, // include email
|
|
158
|
+
databases: decoded.databases // databases eklendi
|
|
159
|
+
}, JWT_REFRESH_SECRET, { expiresIn: '7d' });
|
|
160
|
+
/*
|
|
161
|
+
const newRefreshToken = jwt.sign(
|
|
162
|
+
{
|
|
163
|
+
id: user.id,
|
|
164
|
+
username: user.username,
|
|
165
|
+
role: user.role,
|
|
166
|
+
databases: user.databases // databases eklendi
|
|
167
|
+
},
|
|
168
|
+
JWT_REFRESH_SECRET,
|
|
169
|
+
{ expiresIn: '7d' }
|
|
170
|
+
);
|
|
171
|
+
*/
|
|
152
172
|
//debugger;
|
|
153
173
|
if (!res.success) {
|
|
154
174
|
return { valid: false, user: res.user };
|
|
155
175
|
}
|
|
156
176
|
// Admin ise onay ver
|
|
157
177
|
if (options?.role && res.user?.role == 'admin') {
|
|
158
|
-
return { valid: true, user: res.user };
|
|
178
|
+
return { valid: true, user: res.user, newPanelToken };
|
|
159
179
|
}
|
|
160
180
|
//Check role here
|
|
161
181
|
if (options?.role && res.user?.role !== options.role) {
|
|
162
182
|
return { valid: false };
|
|
163
183
|
}
|
|
164
|
-
return { valid: true, user: res.user };
|
|
184
|
+
return { valid: true, user: res.user, newPanelToken };
|
|
165
185
|
}
|
|
166
186
|
catch (err) {
|
|
187
|
+
return { valid: false };
|
|
188
|
+
/*
|
|
167
189
|
// Access token geçersiz veya süresi dolmuşsa
|
|
168
190
|
try {
|
|
169
|
-
let jwt;
|
|
191
|
+
let jwt: typeof import("jsonwebtoken") | undefined;
|
|
170
192
|
if (typeof window === "undefined") {
|
|
171
193
|
jwt = (await import("jsonwebtoken")).default;
|
|
172
194
|
}
|
|
173
195
|
// 2. Refresh token'ı doğrula
|
|
174
|
-
const decodedRefresh
|
|
196
|
+
const decodedRefresh: {
|
|
197
|
+
id: number;
|
|
198
|
+
username: string;
|
|
199
|
+
role: string; // Refresh token'da rol bilgisi varsa
|
|
200
|
+
databaseId?: string;
|
|
201
|
+
databases?: string[];
|
|
202
|
+
} = jwt.verify(refreshToken, JWT_REFRESH_SECRET) as unknown as any;
|
|
175
203
|
//debugger;
|
|
204
|
+
|
|
176
205
|
const res = await this.getUserInfo({
|
|
177
|
-
userId: decodedRefresh.id,
|
|
178
|
-
userName: decodedRefresh.username,
|
|
179
|
-
userRole: decodedRefresh.role || "user" // Varsayılan rol
|
|
206
|
+
userId: (decodedRefresh as any).id,
|
|
207
|
+
userName: (decodedRefresh as any).username,
|
|
208
|
+
userRole: (decodedRefresh as any).role || "user" // Varsayılan rol
|
|
180
209
|
});
|
|
210
|
+
|
|
181
211
|
if (!res.success) {
|
|
182
212
|
return { valid: false };
|
|
183
213
|
}
|
|
214
|
+
|
|
184
215
|
//Check role here
|
|
185
216
|
// Admin ise onay ver
|
|
217
|
+
|
|
186
218
|
// Admin ise devam etsin
|
|
187
219
|
if (options?.role && res.user?.role === 'admin') {
|
|
188
220
|
// admin ise role kontrolü atlanır, devam edilir
|
|
189
|
-
}
|
|
190
|
-
else if (options?.role && res.user?.role !== options.role) {
|
|
221
|
+
} else if (options?.role && res.user?.role !== options.role) {
|
|
191
222
|
return { valid: false };
|
|
192
223
|
}
|
|
224
|
+
|
|
193
225
|
// Refresh token geçerli, yeni access token üret
|
|
194
|
-
const newAccessToken = jwt.sign(
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
226
|
+
const newAccessToken = jwt.sign(
|
|
227
|
+
{
|
|
228
|
+
id: decodedRefresh.id,
|
|
229
|
+
username: decodedRefresh.username,
|
|
230
|
+
role: decodedRefresh.role,
|
|
231
|
+
email: decodedRefresh.email, // include email
|
|
232
|
+
databaseId: decodedRefresh.databaseId, // databaseId eklendi
|
|
233
|
+
databases: decodedRefresh.databases // databases eklendi
|
|
234
|
+
},
|
|
235
|
+
JWT_SECRET,
|
|
236
|
+
{ expiresIn: '15m' }
|
|
237
|
+
);
|
|
238
|
+
const newRefreshToken = jwt.sign(
|
|
239
|
+
{
|
|
240
|
+
id: decodedRefresh.id,
|
|
241
|
+
username: decodedRefresh.username,
|
|
242
|
+
role: decodedRefresh.role,
|
|
243
|
+
email: decodedRefresh.email, // include email
|
|
244
|
+
databaseId: decodedRefresh.databaseId, // databaseId eklendi
|
|
245
|
+
databases: decodedRefresh.databases // databases eklendi
|
|
246
|
+
},
|
|
247
|
+
JWT_REFRESH_SECRET,
|
|
248
|
+
{ expiresIn: '7d' }
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
|
|
208
252
|
return { valid: true, newAccessToken, newRefreshToken, user: res.user };
|
|
209
|
-
}
|
|
210
|
-
catch (refreshErr) {
|
|
253
|
+
} catch (refreshErr) {
|
|
211
254
|
// Refresh token da geçersiz
|
|
212
255
|
return { valid: false };
|
|
213
|
-
}
|
|
256
|
+
}*/
|
|
214
257
|
}
|
|
215
258
|
}
|
|
216
259
|
// async checkToken(accessToken: string, refreshToken): Promise<boolean> {
|
|
@@ -249,6 +292,7 @@ export class AuthService extends Service {
|
|
|
249
292
|
id: user?.id || null,
|
|
250
293
|
username: user?.username || null,
|
|
251
294
|
role: user?.role || null,
|
|
295
|
+
email: user?.email || null, // include email
|
|
252
296
|
databaseId: user?.databaseId || null, // databaseId eklendi
|
|
253
297
|
databases: user?.databases || null // databases eklendi
|
|
254
298
|
}
|
|
@@ -67,12 +67,10 @@ export class RequestHandlerService extends Service {
|
|
|
67
67
|
return this.resposeHandler({ status: "error", message: "Authentication service not available" });
|
|
68
68
|
}
|
|
69
69
|
const cookieHeader = request.headers.get('cookie');
|
|
70
|
-
const
|
|
71
|
-
const refreshToken = this.getCookie(cookieHeader, 'refreshToken');
|
|
70
|
+
const panel_token = this.getCookie(cookieHeader, 'panel_token');
|
|
72
71
|
// Check authentication and role
|
|
73
72
|
const isAuthenticated = await authService.checkToken({
|
|
74
|
-
|
|
75
|
-
refreshToken,
|
|
73
|
+
panel_token,
|
|
76
74
|
options: {
|
|
77
75
|
role: handler.options?.role
|
|
78
76
|
}
|
|
@@ -86,7 +84,7 @@ export class RequestHandlerService extends Service {
|
|
|
86
84
|
// Update cookies with new tokens
|
|
87
85
|
console.log("update token");
|
|
88
86
|
header = {
|
|
89
|
-
"Set-Cookie": `
|
|
87
|
+
"Set-Cookie": `panel_token=${isAuthenticated.newAccessToken}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=900, refreshToken=${isAuthenticated.newRefreshToken}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=604800`,
|
|
90
88
|
};
|
|
91
89
|
// const response = new Response(JSON.stringify({ status: "success", message: "Authenticated with new token" }), {
|
|
92
90
|
// status: 200,
|
package/package.json
CHANGED
|
@@ -77,6 +77,7 @@ export abstract class AuthService extends Service {
|
|
|
77
77
|
id: number;
|
|
78
78
|
username: string;
|
|
79
79
|
password: string; // Optional password field for internal use
|
|
80
|
+
email?: string; // Optional email field
|
|
80
81
|
role?: string; // Optional role field
|
|
81
82
|
databaseId?: string; // Optional database ID field
|
|
82
83
|
databases?: string[]; // Optional databases field
|
|
@@ -154,6 +155,7 @@ export abstract class AuthService extends Service {
|
|
|
154
155
|
id: result.user.id,
|
|
155
156
|
username: result.user.username,
|
|
156
157
|
role: result.user.role,
|
|
158
|
+
email: result.user.email, // include email
|
|
157
159
|
databaseId: result.user.databaseId,
|
|
158
160
|
databases: result.user.databases // databaseId eklendi
|
|
159
161
|
},
|
|
@@ -167,6 +169,7 @@ export abstract class AuthService extends Service {
|
|
|
167
169
|
id: result.user.id,
|
|
168
170
|
username: result.user.username,
|
|
169
171
|
role: result.user.role,
|
|
172
|
+
email: result.user.email, // include email
|
|
170
173
|
databaseId: result.user.databaseId,
|
|
171
174
|
databases: result.user.databases // databaseId eklendi
|
|
172
175
|
},
|
|
@@ -209,13 +212,12 @@ export abstract class AuthService extends Service {
|
|
|
209
212
|
return this.resposeHandler({ status: "error", message: "An error occurred during login" });
|
|
210
213
|
}
|
|
211
214
|
}
|
|
212
|
-
async checkToken({
|
|
213
|
-
|
|
215
|
+
async checkToken({ panel_token, options }: {
|
|
216
|
+
panel_token: string, options?: {
|
|
214
217
|
role?: string;
|
|
215
218
|
}
|
|
216
|
-
}): Promise<{ valid: boolean;
|
|
217
|
-
this.log.OK("
|
|
218
|
-
this.log.OK("Refresh Token:", refreshToken);
|
|
219
|
+
}): Promise<{ valid: boolean; newPanelToken?: string, user?: any }> {
|
|
220
|
+
this.log.OK("panel_token Token:", panel_token);
|
|
219
221
|
//debugger;
|
|
220
222
|
try {
|
|
221
223
|
// 1. Access token'ı doğrula
|
|
@@ -223,12 +225,36 @@ export abstract class AuthService extends Service {
|
|
|
223
225
|
if (typeof window === "undefined") {
|
|
224
226
|
jwt = (await import("jsonwebtoken")).default;
|
|
225
227
|
}
|
|
226
|
-
const decoded = jwt.verify(
|
|
228
|
+
const decoded: any = jwt.verify(panel_token, JWT_SECRET);
|
|
227
229
|
const res = await this.getUserInfo({
|
|
228
230
|
userId: (decoded as any).id,
|
|
229
231
|
userName: (decoded as any).username,
|
|
230
232
|
userRole: (decoded as any).role || "user" // Varsayılan rol
|
|
231
233
|
});
|
|
234
|
+
|
|
235
|
+
const newPanelToken = jwt.sign(
|
|
236
|
+
{
|
|
237
|
+
id: decoded.id,
|
|
238
|
+
username: decoded.username,
|
|
239
|
+
role: decoded.role,
|
|
240
|
+
email: decoded.email, // include email
|
|
241
|
+
databases: decoded.databases // databases eklendi
|
|
242
|
+
},
|
|
243
|
+
JWT_REFRESH_SECRET,
|
|
244
|
+
{ expiresIn: '7d' }
|
|
245
|
+
);
|
|
246
|
+
/*
|
|
247
|
+
const newRefreshToken = jwt.sign(
|
|
248
|
+
{
|
|
249
|
+
id: user.id,
|
|
250
|
+
username: user.username,
|
|
251
|
+
role: user.role,
|
|
252
|
+
databases: user.databases // databases eklendi
|
|
253
|
+
},
|
|
254
|
+
JWT_REFRESH_SECRET,
|
|
255
|
+
{ expiresIn: '7d' }
|
|
256
|
+
);
|
|
257
|
+
*/
|
|
232
258
|
//debugger;
|
|
233
259
|
|
|
234
260
|
if (!res.success) {
|
|
@@ -237,15 +263,17 @@ export abstract class AuthService extends Service {
|
|
|
237
263
|
|
|
238
264
|
// Admin ise onay ver
|
|
239
265
|
if (options?.role && res.user?.role == 'admin') {
|
|
240
|
-
return { valid: true, user: res.user };
|
|
266
|
+
return { valid: true, user: res.user, newPanelToken };
|
|
241
267
|
}
|
|
242
268
|
|
|
243
269
|
//Check role here
|
|
244
270
|
if (options?.role && res.user?.role !== options.role) {
|
|
245
271
|
return { valid: false };
|
|
246
272
|
}
|
|
247
|
-
return { valid: true, user: res.user };
|
|
273
|
+
return { valid: true, user: res.user, newPanelToken };
|
|
248
274
|
} catch (err) {
|
|
275
|
+
return { valid: false };
|
|
276
|
+
/*
|
|
249
277
|
// Access token geçersiz veya süresi dolmuşsa
|
|
250
278
|
try {
|
|
251
279
|
let jwt: typeof import("jsonwebtoken") | undefined;
|
|
@@ -288,6 +316,7 @@ export abstract class AuthService extends Service {
|
|
|
288
316
|
id: decodedRefresh.id,
|
|
289
317
|
username: decodedRefresh.username,
|
|
290
318
|
role: decodedRefresh.role,
|
|
319
|
+
email: decodedRefresh.email, // include email
|
|
291
320
|
databaseId: decodedRefresh.databaseId, // databaseId eklendi
|
|
292
321
|
databases: decodedRefresh.databases // databases eklendi
|
|
293
322
|
},
|
|
@@ -299,6 +328,7 @@ export abstract class AuthService extends Service {
|
|
|
299
328
|
id: decodedRefresh.id,
|
|
300
329
|
username: decodedRefresh.username,
|
|
301
330
|
role: decodedRefresh.role,
|
|
331
|
+
email: decodedRefresh.email, // include email
|
|
302
332
|
databaseId: decodedRefresh.databaseId, // databaseId eklendi
|
|
303
333
|
databases: decodedRefresh.databases // databases eklendi
|
|
304
334
|
},
|
|
@@ -311,7 +341,7 @@ export abstract class AuthService extends Service {
|
|
|
311
341
|
} catch (refreshErr) {
|
|
312
342
|
// Refresh token da geçersiz
|
|
313
343
|
return { valid: false };
|
|
314
|
-
}
|
|
344
|
+
}*/
|
|
315
345
|
}
|
|
316
346
|
}
|
|
317
347
|
|
|
@@ -352,6 +382,7 @@ export abstract class AuthService extends Service {
|
|
|
352
382
|
id: user?.id || null,
|
|
353
383
|
username: user?.username || null,
|
|
354
384
|
role: user?.role || null,
|
|
385
|
+
email: user?.email || null, // include email
|
|
355
386
|
databaseId: user?.databaseId || null, // databaseId eklendi
|
|
356
387
|
databases: user?.databases || null // databases eklendi
|
|
357
388
|
}
|
|
@@ -95,12 +95,11 @@ export class RequestHandlerService extends Service {
|
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
const cookieHeader = request.headers.get('cookie');
|
|
98
|
-
const
|
|
99
|
-
const refreshToken = this.getCookie(cookieHeader, 'refreshToken');
|
|
98
|
+
const panel_token = this.getCookie(cookieHeader, 'panel_token');
|
|
100
99
|
// Check authentication and role
|
|
100
|
+
|
|
101
101
|
const isAuthenticated = await authService.checkToken({
|
|
102
|
-
|
|
103
|
-
refreshToken,
|
|
102
|
+
panel_token,
|
|
104
103
|
options: {
|
|
105
104
|
role: handler.options?.role
|
|
106
105
|
}
|
|
@@ -118,7 +117,7 @@ export class RequestHandlerService extends Service {
|
|
|
118
117
|
console.log("update token");
|
|
119
118
|
header = {
|
|
120
119
|
"Set-Cookie":
|
|
121
|
-
`
|
|
120
|
+
`panel_token=${isAuthenticated.newAccessToken}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=900, refreshToken=${isAuthenticated.newRefreshToken}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=604800`,
|
|
122
121
|
}
|
|
123
122
|
// const response = new Response(JSON.stringify({ status: "success", message: "Authenticated with new token" }), {
|
|
124
123
|
// status: 200,
|