@riligar/auth-elysia 1.6.0 → 1.6.1
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/index.esm.js +53 -49
- package/dist/index.js +53 -49
- package/package.json +1 -1
- package/src/index.js +52 -48
package/dist/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import 'elysia';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @module @riligar/auth-elysia
|
|
@@ -227,66 +227,69 @@ async function fetchAuth(url, options = {}) {
|
|
|
227
227
|
}
|
|
228
228
|
|
|
229
229
|
/**
|
|
230
|
-
*
|
|
230
|
+
* Plugin principal de autenticação
|
|
231
231
|
*/
|
|
232
|
-
function
|
|
233
|
-
|
|
234
|
-
// Verificar se a rota deve ser excluída da autenticação
|
|
235
|
-
const path = new URL(request.url).pathname;
|
|
236
|
-
if (config.excludePaths.some(excluded => path.startsWith(excluded))) {
|
|
237
|
-
return
|
|
238
|
-
}
|
|
232
|
+
function authPlugin(userConfig = {}) {
|
|
233
|
+
const config = { ...DEFAULT_CONFIG, ...userConfig };
|
|
239
234
|
|
|
240
|
-
|
|
241
|
-
|
|
235
|
+
// Instanciar cliente Auth
|
|
236
|
+
const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey);
|
|
242
237
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
238
|
+
return app => {
|
|
239
|
+
app.derive(async ({ request, cookie }) => {
|
|
240
|
+
const path = new URL(request.url).pathname;
|
|
241
|
+
// Verificar se a rota deve ser excluída da autenticação
|
|
242
|
+
if (config.excludePaths.some(excluded => path.startsWith(excluded))) {
|
|
243
|
+
return { user: null, authMeta: null }
|
|
247
244
|
}
|
|
248
|
-
}
|
|
249
245
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
}
|
|
246
|
+
// Buscar token no cookie ou header Authorization
|
|
247
|
+
let token = cookie[config.cookieName]?.value;
|
|
253
248
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
249
|
+
if (!token) {
|
|
250
|
+
const authHeader = request.headers.get('authorization');
|
|
251
|
+
if (authHeader?.startsWith('Bearer ')) {
|
|
252
|
+
token = authHeader.substring(7);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
257
255
|
|
|
258
|
-
if (!
|
|
259
|
-
return
|
|
256
|
+
if (!token) {
|
|
257
|
+
return { user: null, authMeta: null }
|
|
260
258
|
}
|
|
261
259
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
verified_locally: userSession.verified_locally,
|
|
266
|
-
cached: userSession.cached,
|
|
267
|
-
};
|
|
268
|
-
} catch (error) {
|
|
269
|
-
return config.onUnauthorized(set)
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
260
|
+
try {
|
|
261
|
+
// ⚡ Verificação otimizada com JWKS local + fallback remoto
|
|
262
|
+
const userSession = await authClient.verifySession(token);
|
|
273
263
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
function authPlugin(userConfig = {}) {
|
|
278
|
-
const config = { ...DEFAULT_CONFIG, ...userConfig };
|
|
264
|
+
if (!userSession) {
|
|
265
|
+
return { user: null, authMeta: null }
|
|
266
|
+
}
|
|
279
267
|
|
|
280
|
-
|
|
281
|
-
|
|
268
|
+
return {
|
|
269
|
+
user: userSession.user,
|
|
270
|
+
authMeta: {
|
|
271
|
+
verified_locally: userSession.verified_locally,
|
|
272
|
+
cached: userSession.cached,
|
|
273
|
+
},
|
|
274
|
+
}
|
|
275
|
+
} catch (error) {
|
|
276
|
+
console.warn('Auth verification error:', error.message);
|
|
277
|
+
return { user: null, authMeta: null }
|
|
278
|
+
}
|
|
279
|
+
}).onBeforeHandle(({ user, set, request }) => {
|
|
280
|
+
const path = new URL(request.url).pathname;
|
|
281
|
+
// Verificar se a rota deve ser excluída da autenticação (guard redundante por segurança)
|
|
282
|
+
if (config.excludePaths.some(excluded => path.startsWith(excluded))) {
|
|
283
|
+
return
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Se não houver usuário e a rota não for excluída, bloquear
|
|
287
|
+
if (!user) {
|
|
288
|
+
return config.onUnauthorized(set)
|
|
289
|
+
}
|
|
290
|
+
});
|
|
282
291
|
|
|
283
|
-
|
|
284
|
-
.onBeforeHandle(createAuthMiddleware(config, authClient))
|
|
285
|
-
.derive(({ request }) => ({
|
|
286
|
-
user: request.user || null,
|
|
287
|
-
authMeta: request.authMeta || null,
|
|
288
|
-
}))
|
|
289
|
-
.group(config.prefix, app =>
|
|
292
|
+
return app.group(config.prefix, app =>
|
|
290
293
|
app
|
|
291
294
|
// Rota de login
|
|
292
295
|
.post('/login', async ({ body, set, cookie }) => {
|
|
@@ -412,6 +415,7 @@ function authPlugin(userConfig = {}) {
|
|
|
412
415
|
return { user }
|
|
413
416
|
})
|
|
414
417
|
)
|
|
418
|
+
}
|
|
415
419
|
}
|
|
416
420
|
|
|
417
421
|
export { DEFAULT_CONFIG, RiLiGarAuthClient, authPlugin, authPlugin as default, fetchAuth };
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
require('elysia');
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @module @riligar/auth-elysia
|
|
@@ -231,66 +231,69 @@ async function fetchAuth(url, options = {}) {
|
|
|
231
231
|
}
|
|
232
232
|
|
|
233
233
|
/**
|
|
234
|
-
*
|
|
234
|
+
* Plugin principal de autenticação
|
|
235
235
|
*/
|
|
236
|
-
function
|
|
237
|
-
|
|
238
|
-
// Verificar se a rota deve ser excluída da autenticação
|
|
239
|
-
const path = new URL(request.url).pathname;
|
|
240
|
-
if (config.excludePaths.some(excluded => path.startsWith(excluded))) {
|
|
241
|
-
return
|
|
242
|
-
}
|
|
236
|
+
function authPlugin(userConfig = {}) {
|
|
237
|
+
const config = { ...DEFAULT_CONFIG, ...userConfig };
|
|
243
238
|
|
|
244
|
-
|
|
245
|
-
|
|
239
|
+
// Instanciar cliente Auth
|
|
240
|
+
const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey);
|
|
246
241
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
242
|
+
return app => {
|
|
243
|
+
app.derive(async ({ request, cookie }) => {
|
|
244
|
+
const path = new URL(request.url).pathname;
|
|
245
|
+
// Verificar se a rota deve ser excluída da autenticação
|
|
246
|
+
if (config.excludePaths.some(excluded => path.startsWith(excluded))) {
|
|
247
|
+
return { user: null, authMeta: null }
|
|
251
248
|
}
|
|
252
|
-
}
|
|
253
249
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
250
|
+
// Buscar token no cookie ou header Authorization
|
|
251
|
+
let token = cookie[config.cookieName]?.value;
|
|
257
252
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
253
|
+
if (!token) {
|
|
254
|
+
const authHeader = request.headers.get('authorization');
|
|
255
|
+
if (authHeader?.startsWith('Bearer ')) {
|
|
256
|
+
token = authHeader.substring(7);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
261
259
|
|
|
262
|
-
if (!
|
|
263
|
-
return
|
|
260
|
+
if (!token) {
|
|
261
|
+
return { user: null, authMeta: null }
|
|
264
262
|
}
|
|
265
263
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
verified_locally: userSession.verified_locally,
|
|
270
|
-
cached: userSession.cached,
|
|
271
|
-
};
|
|
272
|
-
} catch (error) {
|
|
273
|
-
return config.onUnauthorized(set)
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
264
|
+
try {
|
|
265
|
+
// ⚡ Verificação otimizada com JWKS local + fallback remoto
|
|
266
|
+
const userSession = await authClient.verifySession(token);
|
|
277
267
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
function authPlugin(userConfig = {}) {
|
|
282
|
-
const config = { ...DEFAULT_CONFIG, ...userConfig };
|
|
268
|
+
if (!userSession) {
|
|
269
|
+
return { user: null, authMeta: null }
|
|
270
|
+
}
|
|
283
271
|
|
|
284
|
-
|
|
285
|
-
|
|
272
|
+
return {
|
|
273
|
+
user: userSession.user,
|
|
274
|
+
authMeta: {
|
|
275
|
+
verified_locally: userSession.verified_locally,
|
|
276
|
+
cached: userSession.cached,
|
|
277
|
+
},
|
|
278
|
+
}
|
|
279
|
+
} catch (error) {
|
|
280
|
+
console.warn('Auth verification error:', error.message);
|
|
281
|
+
return { user: null, authMeta: null }
|
|
282
|
+
}
|
|
283
|
+
}).onBeforeHandle(({ user, set, request }) => {
|
|
284
|
+
const path = new URL(request.url).pathname;
|
|
285
|
+
// Verificar se a rota deve ser excluída da autenticação (guard redundante por segurança)
|
|
286
|
+
if (config.excludePaths.some(excluded => path.startsWith(excluded))) {
|
|
287
|
+
return
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Se não houver usuário e a rota não for excluída, bloquear
|
|
291
|
+
if (!user) {
|
|
292
|
+
return config.onUnauthorized(set)
|
|
293
|
+
}
|
|
294
|
+
});
|
|
286
295
|
|
|
287
|
-
|
|
288
|
-
.onBeforeHandle(createAuthMiddleware(config, authClient))
|
|
289
|
-
.derive(({ request }) => ({
|
|
290
|
-
user: request.user || null,
|
|
291
|
-
authMeta: request.authMeta || null,
|
|
292
|
-
}))
|
|
293
|
-
.group(config.prefix, app =>
|
|
296
|
+
return app.group(config.prefix, app =>
|
|
294
297
|
app
|
|
295
298
|
// Rota de login
|
|
296
299
|
.post('/login', async ({ body, set, cookie }) => {
|
|
@@ -416,6 +419,7 @@ function authPlugin(userConfig = {}) {
|
|
|
416
419
|
return { user }
|
|
417
420
|
})
|
|
418
421
|
)
|
|
422
|
+
}
|
|
419
423
|
}
|
|
420
424
|
|
|
421
425
|
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -226,66 +226,69 @@ async function fetchAuth(url, options = {}) {
|
|
|
226
226
|
}
|
|
227
227
|
|
|
228
228
|
/**
|
|
229
|
-
*
|
|
229
|
+
* Plugin principal de autenticação
|
|
230
230
|
*/
|
|
231
|
-
function
|
|
232
|
-
|
|
233
|
-
// Verificar se a rota deve ser excluída da autenticação
|
|
234
|
-
const path = new URL(request.url).pathname
|
|
235
|
-
if (config.excludePaths.some(excluded => path.startsWith(excluded))) {
|
|
236
|
-
return
|
|
237
|
-
}
|
|
231
|
+
export function authPlugin(userConfig = {}) {
|
|
232
|
+
const config = { ...DEFAULT_CONFIG, ...userConfig }
|
|
238
233
|
|
|
239
|
-
|
|
240
|
-
|
|
234
|
+
// Instanciar cliente Auth
|
|
235
|
+
const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey)
|
|
241
236
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
237
|
+
return app => {
|
|
238
|
+
app.derive(async ({ request, cookie }) => {
|
|
239
|
+
const path = new URL(request.url).pathname
|
|
240
|
+
// Verificar se a rota deve ser excluída da autenticação
|
|
241
|
+
if (config.excludePaths.some(excluded => path.startsWith(excluded))) {
|
|
242
|
+
return { user: null, authMeta: null }
|
|
246
243
|
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (!token) {
|
|
250
|
-
return config.onUnauthorized(set)
|
|
251
|
-
}
|
|
252
244
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
const userSession = await authClient.verifySession(token)
|
|
245
|
+
// Buscar token no cookie ou header Authorization
|
|
246
|
+
let token = cookie[config.cookieName]?.value
|
|
256
247
|
|
|
257
|
-
if (!
|
|
258
|
-
|
|
248
|
+
if (!token) {
|
|
249
|
+
const authHeader = request.headers.get('authorization')
|
|
250
|
+
if (authHeader?.startsWith('Bearer ')) {
|
|
251
|
+
token = authHeader.substring(7)
|
|
252
|
+
}
|
|
259
253
|
}
|
|
260
254
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
request.authMeta = {
|
|
264
|
-
verified_locally: userSession.verified_locally,
|
|
265
|
-
cached: userSession.cached,
|
|
255
|
+
if (!token) {
|
|
256
|
+
return { user: null, authMeta: null }
|
|
266
257
|
}
|
|
267
|
-
} catch (error) {
|
|
268
|
-
return config.onUnauthorized(set)
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
258
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
export function authPlugin(userConfig = {}) {
|
|
277
|
-
const config = { ...DEFAULT_CONFIG, ...userConfig }
|
|
259
|
+
try {
|
|
260
|
+
// ⚡ Verificação otimizada com JWKS local + fallback remoto
|
|
261
|
+
const userSession = await authClient.verifySession(token)
|
|
278
262
|
|
|
279
|
-
|
|
280
|
-
|
|
263
|
+
if (!userSession) {
|
|
264
|
+
return { user: null, authMeta: null }
|
|
265
|
+
}
|
|
281
266
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
267
|
+
return {
|
|
268
|
+
user: userSession.user,
|
|
269
|
+
authMeta: {
|
|
270
|
+
verified_locally: userSession.verified_locally,
|
|
271
|
+
cached: userSession.cached,
|
|
272
|
+
},
|
|
273
|
+
}
|
|
274
|
+
} catch (error) {
|
|
275
|
+
console.warn('Auth verification error:', error.message)
|
|
276
|
+
return { user: null, authMeta: null }
|
|
277
|
+
}
|
|
278
|
+
}).onBeforeHandle(({ user, set, request }) => {
|
|
279
|
+
const path = new URL(request.url).pathname
|
|
280
|
+
// Verificar se a rota deve ser excluída da autenticação (guard redundante por segurança)
|
|
281
|
+
if (config.excludePaths.some(excluded => path.startsWith(excluded))) {
|
|
282
|
+
return
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Se não houver usuário e a rota não for excluída, bloquear
|
|
286
|
+
if (!user) {
|
|
287
|
+
return config.onUnauthorized(set)
|
|
288
|
+
}
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
return app.group(config.prefix, app =>
|
|
289
292
|
app
|
|
290
293
|
// Rota de login
|
|
291
294
|
.post('/login', async ({ body, set, cookie }) => {
|
|
@@ -411,6 +414,7 @@ export function authPlugin(userConfig = {}) {
|
|
|
411
414
|
return { user }
|
|
412
415
|
})
|
|
413
416
|
)
|
|
417
|
+
}
|
|
414
418
|
}
|
|
415
419
|
|
|
416
420
|
// Exportar configuração padrão e utilitários
|