Class: RackJwtAegis::JwtValidator
- Inherits:
-
Object
- Object
- RackJwtAegis::JwtValidator
- Defined in:
- lib/rack_jwt_aegis/jwt_validator.rb
Overview
JWT token validation and payload verification
Handles JWT token decoding, signature verification, and payload validation including claims verification and type checking based on configuration.
Instance Method Summary collapse
-
#initialize(config) ⇒ JwtValidator
constructor
Initialize the JWT validator.
-
#validate(token) ⇒ Hash
Validate and decode a JWT token.
Constructor Details
#initialize(config) ⇒ JwtValidator
Initialize the JWT validator
32 33 34 |
# File 'lib/rack_jwt_aegis/jwt_validator.rb', line 32 def initialize(config) @config = config end |
Instance Method Details
#validate(token) ⇒ Hash
Validate and decode a JWT token
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rack_jwt_aegis/jwt_validator.rb', line 42 def validate(token) # Decode JWT with verification payload, _header = JWT.decode( token, @config.jwt_secret, true, # verify signature { algorithm: @config.jwt_algorithm, verify_expiration: true, verify_not_before: true, verify_iat: true, verify_aud: false, # Not validating audience by default verify_iss: false, # Not validating issuer by default verify_sub: false, # Not validating subject by default }, ) # Validate payload structure validate_payload_structure(payload) payload rescue JWT::ExpiredSignature raise AuthenticationError, 'JWT token has expired' rescue JWT::ImmatureSignature raise AuthenticationError, 'JWT token not yet valid' rescue JWT::InvalidIatError raise AuthenticationError, 'JWT token issued in the future' rescue JWT::VerificationError raise AuthenticationError, 'JWT signature verification failed' rescue JWT::DecodeError => e raise AuthenticationError, "Invalid JWT token: #{e.}" rescue StandardError => e raise AuthenticationError, "JWT validation error: #{e.}" end |