Class: VectorMCP::Security::Strategies::JwtToken
- Inherits:
-
Object
- Object
- VectorMCP::Security::Strategies::JwtToken
- Defined in:
- lib/vector_mcp/security/strategies/jwt_token.rb
Overview
JWT Token authentication strategy Provides stateless authentication using JSON Web Tokens
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
readonly
Returns the value of attribute algorithm.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Class Method Summary collapse
-
.available? ⇒ Boolean
Check if JWT gem is available.
Instance Method Summary collapse
-
#authenticate(request) ⇒ Hash, false
Authenticate a request using JWT token.
-
#generate_token(payload, expires_in: 3600) ⇒ String
Generate a JWT token (utility method for testing/development).
-
#initialize(secret:, algorithm: "HS256", **options) ⇒ JwtToken
constructor
Initialize JWT strategy.
Constructor Details
#initialize(secret:, algorithm: "HS256", **options) ⇒ JwtToken
Initialize JWT strategy
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/vector_mcp/security/strategies/jwt_token.rb', line 21 def initialize(secret:, algorithm: "HS256", **) raise LoadError, "JWT gem is required for JWT authentication strategy" unless defined?(JWT) @secret = secret @algorithm = algorithm @options = { algorithm: @algorithm, verify_expiration: true, verify_iat: true, verify_iss: false, verify_aud: false }.merge() end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
Returns the value of attribute algorithm.
15 16 17 |
# File 'lib/vector_mcp/security/strategies/jwt_token.rb', line 15 def algorithm @algorithm end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/vector_mcp/security/strategies/jwt_token.rb', line 15 def @options end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
15 16 17 |
# File 'lib/vector_mcp/security/strategies/jwt_token.rb', line 15 def secret @secret end |
Class Method Details
.available? ⇒ Boolean
Check if JWT gem is available
74 75 76 |
# File 'lib/vector_mcp/security/strategies/jwt_token.rb', line 74 def self.available? defined?(JWT) end |
Instance Method Details
#authenticate(request) ⇒ Hash, false
Authenticate a request using JWT token
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/vector_mcp/security/strategies/jwt_token.rb', line 38 def authenticate(request) token = extract_token(request) return false unless token begin decoded = JWT.decode(token, @secret, true, @options) payload = decoded[0] # First element is the payload headers = decoded[1] # Second element is the headers # Return user info from JWT payload { **payload, strategy: "jwt", authenticated_at: Time.now, jwt_headers: headers } rescue JWT::ExpiredSignature, JWT::InvalidIssuerError, JWT::InvalidAudienceError, JWT::VerificationError, JWT::DecodeError, StandardError false # Token validation failed end end |
#generate_token(payload, expires_in: 3600) ⇒ String
Generate a JWT token (utility method for testing/development)
64 65 66 67 68 69 70 |
# File 'lib/vector_mcp/security/strategies/jwt_token.rb', line 64 def generate_token(payload, expires_in: 3600) exp_payload = payload.merge( exp: Time.now.to_i + expires_in, iat: Time.now.to_i ) JWT.encode(exp_payload, @secret, @algorithm) end |