@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 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,65 @@ 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);
247
- }
248
- }
238
+ return app => {
239
+ app.derive(async ({ request, cookie }) => {
240
+ new URL(request.url).pathname;
249
241
 
250
- if (!token) {
251
- return config.onUnauthorized(set)
252
- }
242
+ // Buscar token no cookie ou header Authorization
243
+ let token = cookie[config.cookieName]?.value;
253
244
 
254
- try {
255
- // Verificação otimizada com JWKS local + fallback remoto
256
- const userSession = await authClient.verifySession(token);
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 (!userSession) {
259
- return config.onUnauthorized(set)
252
+ if (!token) {
253
+ return { user: null, authMeta: null }
260
254
  }
261
255
 
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
- }
256
+ try {
257
+ // Verificação otimizada com JWKS local + fallback remoto
258
+ const userSession = await authClient.verifySession(token);
273
259
 
274
- /**
275
- * Plugin principal de autenticação
276
- */
277
- function authPlugin(userConfig = {}) {
278
- const config = { ...DEFAULT_CONFIG, ...userConfig };
260
+ if (!userSession) {
261
+ return { user: null, authMeta: null }
262
+ }
279
263
 
280
- // Instanciar cliente Auth
281
- const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey);
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
- 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 =>
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
- var elysia = require('elysia');
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
- * 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);
251
- }
252
- }
242
+ return app => {
243
+ app.derive(async ({ request, cookie }) => {
244
+ new URL(request.url).pathname;
253
245
 
254
- if (!token) {
255
- return config.onUnauthorized(set)
256
- }
246
+ // Buscar token no cookie ou header Authorization
247
+ let token = cookie[config.cookieName]?.value;
257
248
 
258
- try {
259
- // Verificação otimizada com JWKS local + fallback remoto
260
- 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
+ }
261
255
 
262
- if (!userSession) {
263
- return config.onUnauthorized(set)
256
+ if (!token) {
257
+ return { user: null, authMeta: null }
264
258
  }
265
259
 
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
- }
260
+ try {
261
+ // Verificação otimizada com JWKS local + fallback remoto
262
+ const userSession = await authClient.verifySession(token);
277
263
 
278
- /**
279
- * Plugin principal de autenticação
280
- */
281
- function authPlugin(userConfig = {}) {
282
- const config = { ...DEFAULT_CONFIG, ...userConfig };
264
+ if (!userSession) {
265
+ return { user: null, authMeta: null }
266
+ }
283
267
 
284
- // Instanciar cliente Auth
285
- 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
+
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
- 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 =>
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riligar/auth-elysia",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
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,65 @@ 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)
246
- }
247
- }
237
+ return app => {
238
+ app.derive(async ({ request, cookie }) => {
239
+ const path = new URL(request.url).pathname
248
240
 
249
- if (!token) {
250
- return config.onUnauthorized(set)
251
- }
241
+ // Buscar token no cookie ou header Authorization
242
+ let token = cookie[config.cookieName]?.value
252
243
 
253
- try {
254
- // Verificação otimizada com JWKS local + fallback remoto
255
- const userSession = await authClient.verifySession(token)
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 (!userSession) {
258
- return config.onUnauthorized(set)
251
+ if (!token) {
252
+ return { user: null, authMeta: null }
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
+ 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
- } catch (error) {
268
- return config.onUnauthorized(set)
269
- }
270
- }
271
- }
274
+ }).onBeforeHandle(({ user, set, request }) => {
275
+ const path = new URL(request.url).pathname
272
276
 
273
- /**
274
- * Plugin principal de autenticação
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
- // Instanciar cliente Auth
280
- const authClient = new RiLiGarAuthClient(config.apiUrl, config.secretKey)
281
+ // Bloquear se não houver usuário
282
+ if (!user) {
283
+ return config.onUnauthorized(set)
284
+ }
285
+ })
281
286
 
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 =>
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