@rabstack/rab-api 1.12.0 → 1.13.0

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/index.cjs.js CHANGED
@@ -688,22 +688,28 @@ const controllerHandler = (controller, config)=>{
688
688
  };
689
689
 
690
690
  const authHandler = (isProtected, config)=>(req, res, next)=>{
691
- console.log('authHandler:', req.path, ':isProtected:', isProtected);
691
+ var _config_debug;
692
+ const debug = (_config_debug = config.debug) != null ? _config_debug : false;
693
+ if (debug) console.log('authHandler:', req.path, ':isProtected:', isProtected);
692
694
  const token = extractTokenFromHeader(req);
693
695
  // If not protected and no token, just continue
694
696
  if (!isProtected && !token) return next();
695
697
  // If no token but route is protected, throw error
696
698
  if (!token) {
697
- console.log('authHandler:UnauthorizedException:Token Not Found');
699
+ if (debug) console.log('authHandler:UnauthorizedException:Token Not Found');
698
700
  throw new UnauthorizedException('Unauthorized', config.errorCode);
699
701
  }
700
- // Token exists - verify it (must be valid regardless of protection)
702
+ // Token exists - verify it
701
703
  try {
702
- const payload = jwt.verify(token, config.jwt.secret_key);
704
+ const payload = jwt.verify(token, config.jwt.secret_key, {
705
+ algorithms: config.jwt.algorithms
706
+ });
703
707
  req['auth'] = payload;
704
708
  return next();
705
709
  } catch (err) {
706
- console.error('authHandler:JWT Error:', err.message);
710
+ // If route is not protected, silently continue without auth
711
+ if (!isProtected) return next();
712
+ if (debug) console.error('authHandler:JWT Error:', err.message);
707
713
  throw new UnauthorizedException('Unauthorized', config.errorCode);
708
714
  }
709
715
  };
@@ -1131,7 +1137,9 @@ class AtomExpressApp {
1131
1137
  //auth middleware
1132
1138
  if (this.options.auth) {
1133
1139
  var _config_isProtected;
1134
- allPipes.unshift(authHandler((_config_isProtected = config.isProtected) != null ? _config_isProtected : this.options.enforceRouteProtection, this.options.auth));
1140
+ allPipes.unshift(authHandler((_config_isProtected = config.isProtected) != null ? _config_isProtected : this.options.enforceRouteProtection, _extends({}, this.options.auth, {
1141
+ debug: this.options.debug
1142
+ })));
1135
1143
  }
1136
1144
  //add body validation to validate the schema and inject request context
1137
1145
  if (config.bodySchema && !config.disableBodyValidation) {
package/index.esm.d.ts CHANGED
@@ -78,6 +78,7 @@ export declare type AtomExpressOptions = {
78
78
  errorHandler?: (err: any, req: Request_2, res: Response_2, next: NextFunction) => any;
79
79
  enforceBodyValidation?: boolean;
80
80
  enforceRouteProtection?: boolean;
81
+ debug?: boolean;
81
82
  auth?: AuthHandlerOptions;
82
83
  openapi?: {
83
84
  enabled?: boolean;
@@ -124,6 +125,7 @@ export declare const authHandler: (isProtected: boolean, config: AuthHandlerOpti
124
125
 
125
126
  export declare type AuthHandlerOptions = {
126
127
  errorCode?: string;
128
+ debug?: boolean;
127
129
  jwt: {
128
130
  secret_key: string;
129
131
  algorithms: any;
package/index.esm.js CHANGED
@@ -686,22 +686,28 @@ const controllerHandler = (controller, config)=>{
686
686
  };
687
687
 
688
688
  const authHandler = (isProtected, config)=>(req, res, next)=>{
689
- console.log('authHandler:', req.path, ':isProtected:', isProtected);
689
+ var _config_debug;
690
+ const debug = (_config_debug = config.debug) != null ? _config_debug : false;
691
+ if (debug) console.log('authHandler:', req.path, ':isProtected:', isProtected);
690
692
  const token = extractTokenFromHeader(req);
691
693
  // If not protected and no token, just continue
692
694
  if (!isProtected && !token) return next();
693
695
  // If no token but route is protected, throw error
694
696
  if (!token) {
695
- console.log('authHandler:UnauthorizedException:Token Not Found');
697
+ if (debug) console.log('authHandler:UnauthorizedException:Token Not Found');
696
698
  throw new UnauthorizedException('Unauthorized', config.errorCode);
697
699
  }
698
- // Token exists - verify it (must be valid regardless of protection)
700
+ // Token exists - verify it
699
701
  try {
700
- const payload = jwt.verify(token, config.jwt.secret_key);
702
+ const payload = jwt.verify(token, config.jwt.secret_key, {
703
+ algorithms: config.jwt.algorithms
704
+ });
701
705
  req['auth'] = payload;
702
706
  return next();
703
707
  } catch (err) {
704
- console.error('authHandler:JWT Error:', err.message);
708
+ // If route is not protected, silently continue without auth
709
+ if (!isProtected) return next();
710
+ if (debug) console.error('authHandler:JWT Error:', err.message);
705
711
  throw new UnauthorizedException('Unauthorized', config.errorCode);
706
712
  }
707
713
  };
@@ -1129,7 +1135,9 @@ class AtomExpressApp {
1129
1135
  //auth middleware
1130
1136
  if (this.options.auth) {
1131
1137
  var _config_isProtected;
1132
- allPipes.unshift(authHandler((_config_isProtected = config.isProtected) != null ? _config_isProtected : this.options.enforceRouteProtection, this.options.auth));
1138
+ allPipes.unshift(authHandler((_config_isProtected = config.isProtected) != null ? _config_isProtected : this.options.enforceRouteProtection, _extends({}, this.options.auth, {
1139
+ debug: this.options.debug
1140
+ })));
1133
1141
  }
1134
1142
  //add body validation to validate the schema and inject request context
1135
1143
  if (config.bodySchema && !config.disableBodyValidation) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabstack/rab-api",
3
- "version": "1.12.0",
3
+ "version": "1.13.0",
4
4
  "description": "A TypeScript REST API framework built on Express.js with decorator-based routing, dependency injection, and built-in validation",
5
5
  "author": "Softin",
6
6
  "license": "MIT",