@riligar/auth-elysia 1.6.0 → 1.6.2
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 +50 -50
- package/dist/index.js +50 -50
- package/package.json +1 -1
- package/src/index.js +48 -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,65 @@ 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
|
-
token = authHeader.substring(7);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
238
|
+
return app => {
|
|
239
|
+
app.derive(async ({ request, cookie }) => {
|
|
240
|
+
new URL(request.url).pathname;
|
|
249
241
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
}
|
|
242
|
+
// Buscar token no cookie ou header Authorization
|
|
243
|
+
let token = cookie[config.cookieName]?.value;
|
|
253
244
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
245
|
+
if (!token) {
|
|
246
|
+
const authHeader = request.headers.get('authorization');
|
|
247
|
+
if (authHeader?.startsWith('Bearer ')) {
|
|
248
|
+
token = authHeader.substring(7);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
257
251
|
|
|
258
|
-
if (!
|
|
259
|
-
return
|
|
252
|
+
if (!token) {
|
|
253
|
+
return { user: null, authMeta: null }
|
|
260
254
|
}
|
|
261
255
|
|
|
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
|
-
}
|
|
256
|
+
try {
|
|
257
|
+
// ⚡ Verificação otimizada com JWKS local + fallback remoto
|
|
258
|
+
const userSession = await authClient.verifySession(token);
|
|
273
259
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
function authPlugin(userConfig = {}) {
|
|
278
|
-
const config = { ...DEFAULT_CONFIG, ...userConfig };
|
|
260
|
+
if (!userSession) {
|
|
261
|
+
return { user: null, authMeta: null }
|
|
262
|
+
}
|
|
279
263
|
|
|
280
|
-
|
|
281
|
-
|
|
264
|
+
return {
|
|
265
|
+
user: userSession.user,
|
|
266
|
+
authMeta: {
|
|
267
|
+
verified_locally: userSession.verified_locally,
|
|
268
|
+
cached: userSession.cached,
|
|
269
|
+
},
|
|
270
|
+
}
|
|
271
|
+
} catch (error) {
|
|
272
|
+
console.warn('Auth verification error:', error.message);
|
|
273
|
+
return { user: null, authMeta: null }
|
|
274
|
+
}
|
|
275
|
+
}).onBeforeHandle(({ user, set, request }) => {
|
|
276
|
+
const path = new URL(request.url).pathname;
|
|
277
|
+
|
|
278
|
+
// Verificar se a rota deve ser excluída da autenticação
|
|
279
|
+
const isExcluded = config.excludePaths.some(excluded => path.startsWith(excluded));
|
|
280
|
+
if (isExcluded) return
|
|
281
|
+
|
|
282
|
+
// Bloquear se não houver usuário
|
|
283
|
+
if (!user) {
|
|
284
|
+
return config.onUnauthorized(set)
|
|
285
|
+
}
|
|
286
|
+
});
|
|
282
287
|
|
|
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 =>
|
|
288
|
+
return app.group(config.prefix, app =>
|
|
290
289
|
app
|
|
291
290
|
// Rota de login
|
|
292
291
|
.post('/login', async ({ body, set, cookie }) => {
|
|
@@ -412,6 +411,7 @@ function authPlugin(userConfig = {}) {
|
|
|
412
411
|
return { user }
|
|
413
412
|
})
|
|
414
413
|
)
|
|
414
|
+
}
|
|
415
415
|
}
|
|
416
416
|
|
|
417
417
|
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,65 @@ 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
|
-
token = authHeader.substring(7);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
242
|
+
return app => {
|
|
243
|
+
app.derive(async ({ request, cookie }) => {
|
|
244
|
+
new URL(request.url).pathname;
|
|
253
245
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
246
|
+
// Buscar token no cookie ou header Authorization
|
|
247
|
+
let token = cookie[config.cookieName]?.value;
|
|
257
248
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
249
|
+
if (!token) {
|
|
250
|
+
const authHeader = request.headers.get('authorization');
|
|
251
|
+
if (authHeader?.startsWith('Bearer ')) {
|
|
252
|
+
token = authHeader.substring(7);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
261
255
|
|
|
262
|
-
if (!
|
|
263
|
-
return
|
|
256
|
+
if (!token) {
|
|
257
|
+
return { user: null, authMeta: null }
|
|
264
258
|
}
|
|
265
259
|
|
|
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
|
-
}
|
|
260
|
+
try {
|
|
261
|
+
// ⚡ Verificação otimizada com JWKS local + fallback remoto
|
|
262
|
+
const userSession = await authClient.verifySession(token);
|
|
277
263
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
function authPlugin(userConfig = {}) {
|
|
282
|
-
const config = { ...DEFAULT_CONFIG, ...userConfig };
|
|
264
|
+
if (!userSession) {
|
|
265
|
+
return { user: null, authMeta: null }
|
|
266
|
+
}
|
|
283
267
|
|
|
284
|
-
|
|
285
|
-
|
|
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
|
+
|
|
282
|
+
// Verificar se a rota deve ser excluída da autenticação
|
|
283
|
+
const isExcluded = config.excludePaths.some(excluded => path.startsWith(excluded));
|
|
284
|
+
if (isExcluded) return
|
|
285
|
+
|
|
286
|
+
// Bloquear se não houver usuário
|
|
287
|
+
if (!user) {
|
|
288
|
+
return config.onUnauthorized(set)
|
|
289
|
+
}
|
|
290
|
+
});
|
|
286
291
|
|
|
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 =>
|
|
292
|
+
return app.group(config.prefix, app =>
|
|
294
293
|
app
|
|
295
294
|
// Rota de login
|
|
296
295
|
.post('/login', async ({ body, set, cookie }) => {
|
|
@@ -416,6 +415,7 @@ function authPlugin(userConfig = {}) {
|
|
|
416
415
|
return { user }
|
|
417
416
|
})
|
|
418
417
|
)
|
|
418
|
+
}
|
|
419
419
|
}
|
|
420
420
|
|
|
421
421
|
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -226,66 +226,65 @@ 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
|
-
token = authHeader.substring(7)
|
|
246
|
-
}
|
|
247
|
-
}
|
|
237
|
+
return app => {
|
|
238
|
+
app.derive(async ({ request, cookie }) => {
|
|
239
|
+
const path = new URL(request.url).pathname
|
|
248
240
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
241
|
+
// Buscar token no cookie ou header Authorization
|
|
242
|
+
let token = cookie[config.cookieName]?.value
|
|
252
243
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
244
|
+
if (!token) {
|
|
245
|
+
const authHeader = request.headers.get('authorization')
|
|
246
|
+
if (authHeader?.startsWith('Bearer ')) {
|
|
247
|
+
token = authHeader.substring(7)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
256
250
|
|
|
257
|
-
if (!
|
|
258
|
-
return
|
|
251
|
+
if (!token) {
|
|
252
|
+
return { user: null, authMeta: null }
|
|
259
253
|
}
|
|
260
254
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
255
|
+
try {
|
|
256
|
+
// ⚡ Verificação otimizada com JWKS local + fallback remoto
|
|
257
|
+
const userSession = await authClient.verifySession(token)
|
|
258
|
+
|
|
259
|
+
if (!userSession) {
|
|
260
|
+
return { user: null, authMeta: null }
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
user: userSession.user,
|
|
265
|
+
authMeta: {
|
|
266
|
+
verified_locally: userSession.verified_locally,
|
|
267
|
+
cached: userSession.cached,
|
|
268
|
+
},
|
|
269
|
+
}
|
|
270
|
+
} catch (error) {
|
|
271
|
+
console.warn('Auth verification error:', error.message)
|
|
272
|
+
return { user: null, authMeta: null }
|
|
266
273
|
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
274
|
+
}).onBeforeHandle(({ user, set, request }) => {
|
|
275
|
+
const path = new URL(request.url).pathname
|
|
272
276
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
export function authPlugin(userConfig = {}) {
|
|
277
|
-
const config = { ...DEFAULT_CONFIG, ...userConfig }
|
|
277
|
+
// Verificar se a rota deve ser excluída da autenticação
|
|
278
|
+
const isExcluded = config.excludePaths.some(excluded => path.startsWith(excluded))
|
|
279
|
+
if (isExcluded) return
|
|
278
280
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
+
// Bloquear se não houver usuário
|
|
282
|
+
if (!user) {
|
|
283
|
+
return config.onUnauthorized(set)
|
|
284
|
+
}
|
|
285
|
+
})
|
|
281
286
|
|
|
282
|
-
|
|
283
|
-
.onBeforeHandle(createAuthMiddleware(config, authClient))
|
|
284
|
-
.derive(({ request }) => ({
|
|
285
|
-
user: request.user || null,
|
|
286
|
-
authMeta: request.authMeta || null,
|
|
287
|
-
}))
|
|
288
|
-
.group(config.prefix, app =>
|
|
287
|
+
return app.group(config.prefix, app =>
|
|
289
288
|
app
|
|
290
289
|
// Rota de login
|
|
291
290
|
.post('/login', async ({ body, set, cookie }) => {
|
|
@@ -411,6 +410,7 @@ export function authPlugin(userConfig = {}) {
|
|
|
411
410
|
return { user }
|
|
412
411
|
})
|
|
413
412
|
)
|
|
413
|
+
}
|
|
414
414
|
}
|
|
415
415
|
|
|
416
416
|
// Exportar configuração padrão e utilitários
|