@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 CHANGED
@@ -1,4 +1,4 @@
1
- import { Elysia } from 'elysia';
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
- * Middleware de autenticação JWT com verificação JWKS local
230
+ * Plugin principal de autenticação
231
231
  */
232
- function createAuthMiddleware(config, authClient) {
233
- return async ({ request, set, cookie }) => {
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
- // Buscar token no cookie ou header Authorization
241
- let token = cookie[config.cookieName]?.value;
235
+ // Instanciar cliente Auth
236
+ const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey);
242
237
 
243
- if (!token) {
244
- const authHeader = request.headers.get('authorization');
245
- if (authHeader?.startsWith('Bearer ')) {
246
- token = authHeader.substring(7);
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
- if (!token) {
251
- return config.onUnauthorized(set)
252
- }
246
+ // Buscar token no cookie ou header Authorization
247
+ let token = cookie[config.cookieName]?.value;
253
248
 
254
- try {
255
- // Verificação otimizada com JWKS local + fallback remoto
256
- const userSession = await authClient.verifySession(token);
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 (!userSession) {
259
- return config.onUnauthorized(set)
256
+ if (!token) {
257
+ return { user: null, authMeta: null }
260
258
  }
261
259
 
262
- // Adicionar usuário ao contexto da requisição
263
- request.user = userSession.user;
264
- request.authMeta = {
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
- * Plugin principal de autenticação
276
- */
277
- function authPlugin(userConfig = {}) {
278
- const config = { ...DEFAULT_CONFIG, ...userConfig };
264
+ if (!userSession) {
265
+ return { user: null, authMeta: null }
266
+ }
279
267
 
280
- // Instanciar cliente Auth
281
- const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey);
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
- return new Elysia({ name: 'auth-plugin', scoped: false })
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
- var elysia = require('elysia');
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
- * Middleware de autenticação JWT com verificação JWKS local
234
+ * Plugin principal de autenticação
235
235
  */
236
- function createAuthMiddleware(config, authClient) {
237
- return async ({ request, set, cookie }) => {
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
- // Buscar token no cookie ou header Authorization
245
- let token = cookie[config.cookieName]?.value;
239
+ // Instanciar cliente Auth
240
+ const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey);
246
241
 
247
- if (!token) {
248
- const authHeader = request.headers.get('authorization');
249
- if (authHeader?.startsWith('Bearer ')) {
250
- token = authHeader.substring(7);
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
- if (!token) {
255
- return config.onUnauthorized(set)
256
- }
250
+ // Buscar token no cookie ou header Authorization
251
+ let token = cookie[config.cookieName]?.value;
257
252
 
258
- try {
259
- // Verificação otimizada com JWKS local + fallback remoto
260
- const userSession = await authClient.verifySession(token);
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 (!userSession) {
263
- return config.onUnauthorized(set)
260
+ if (!token) {
261
+ return { user: null, authMeta: null }
264
262
  }
265
263
 
266
- // Adicionar usuário ao contexto da requisição
267
- request.user = userSession.user;
268
- request.authMeta = {
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
- * Plugin principal de autenticação
280
- */
281
- function authPlugin(userConfig = {}) {
282
- const config = { ...DEFAULT_CONFIG, ...userConfig };
268
+ if (!userSession) {
269
+ return { user: null, authMeta: null }
270
+ }
283
271
 
284
- // Instanciar cliente Auth
285
- const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey);
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
- return new elysia.Elysia({ name: 'auth-plugin', scoped: false })
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riligar/auth-elysia",
3
- "version": "1.6.0",
3
+ "version": "1.6.1",
4
4
  "type": "module",
5
5
  "description": "Auth SDK for ElysiaJS with JWT and JWKS",
6
6
  "main": "dist/index.js",
package/src/index.js CHANGED
@@ -226,66 +226,69 @@ async function fetchAuth(url, options = {}) {
226
226
  }
227
227
 
228
228
  /**
229
- * Middleware de autenticação JWT com verificação JWKS local
229
+ * Plugin principal de autenticação
230
230
  */
231
- function createAuthMiddleware(config, authClient) {
232
- return async ({ request, set, cookie }) => {
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
- // Buscar token no cookie ou header Authorization
240
- let token = cookie[config.cookieName]?.value
234
+ // Instanciar cliente Auth
235
+ const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey)
241
236
 
242
- if (!token) {
243
- const authHeader = request.headers.get('authorization')
244
- if (authHeader?.startsWith('Bearer ')) {
245
- token = authHeader.substring(7)
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
- try {
254
- // Verificação otimizada com JWKS local + fallback remoto
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 (!userSession) {
258
- return config.onUnauthorized(set)
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
- // Adicionar usuário ao contexto da requisição
262
- request.user = userSession.user
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
- * Plugin principal de autenticação
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
- // Instanciar cliente Auth
280
- const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey)
263
+ if (!userSession) {
264
+ return { user: null, authMeta: null }
265
+ }
281
266
 
282
- return new Elysia({ name: 'auth-plugin', scoped: false })
283
- .onBeforeHandle(createAuthMiddleware(config, authClient))
284
- .derive(({ request }) => ({
285
- user: request.user || null,
286
- authMeta: request.authMeta || null,
287
- }))
288
- .group(config.prefix, app =>
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