Module: Legion::Crypt::JWT
- Extended by:
- Logging::Helper
- Defined in:
- lib/legion/crypt/jwt.rb
Defined Under Namespace
Classes: DecodeError, Error, ExpiredTokenError, InvalidTokenError
Constant Summary collapse
- SUPPORTED_ALGORITHMS =
%w[HS256 RS256].freeze
Constants included from Logging::Helper
Class Method Summary collapse
- .decode(token) ⇒ Object
- .issue(payload, signing_key:, algorithm: 'HS256', ttl: 3600, issuer: 'legion') ⇒ Object
- .issue_identity_token(signing_key:, extra_claims: {}, algorithm: 'HS256', ttl: 3600, issuer: 'legion') ⇒ Object
- .verify(token, verification_key:, **opts) ⇒ Object
- .verify_with_jwks(token, jwks_url:, **opts) ⇒ Object
Methods included from Logging::Helper
Class Method Details
.decode(token) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/legion/crypt/jwt.rb', line 98 def self.decode(token) payload, _header = ::JWT.decode(token, nil, false) symbolize_keys(payload) rescue ::JWT::DecodeError => e handle_exception(e, level: :warn, operation: 'crypt.jwt.decode') raise DecodeError, "failed to decode token: #{e.}" rescue StandardError => e handle_exception(e, level: :error, operation: 'crypt.jwt.decode') raise end |
.issue(payload, signing_key:, algorithm: 'HS256', ttl: 3600, issuer: 'legion') ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/legion/crypt/jwt.rb', line 20 def self.issue(payload, signing_key:, algorithm: 'HS256', ttl: 3600, issuer: 'legion') validate_algorithm!(algorithm) now = Time.now.to_i claims = sanitize_payload(payload).merge( iss: issuer, iat: now, exp: now + ttl, jti: SecureRandom.uuid ) token = ::JWT.encode(claims, signing_key, algorithm) log.info "JWT issued: sub=#{claims[:sub]}, exp=#{Time.at(claims[:exp]).utc.iso8601}, alg=#{algorithm}" token rescue StandardError => e handle_exception(e, level: :error, operation: 'crypt.jwt.issue', algorithm: algorithm, issuer: issuer) raise end |
.issue_identity_token(signing_key:, extra_claims: {}, algorithm: 'HS256', ttl: 3600, issuer: 'legion') ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/legion/crypt/jwt.rb', line 39 def self.issue_identity_token(signing_key:, extra_claims: {}, algorithm: 'HS256', ttl: 3600, issuer: 'legion') unless defined?(Legion::Identity::Process) && Legion::Identity::Process.resolved? raise ArgumentError, 'Identity::Process not resolved' end identity = Legion::Identity::Process.identity_hash identity_fields = { sub: identity[:canonical_name], principal_id: identity[:id], canonical_name: identity[:canonical_name], kind: identity[:kind].to_s, mode: identity[:mode].to_s, groups: (identity[:groups] || [])[0, 50] } normalized_extra_claims = symbolize_keys(extra_claims || {}).reject do |key, _value| identity_fields.key?(key) end payload = normalized_extra_claims.merge(identity_fields) issue(payload, signing_key: signing_key, algorithm: algorithm, ttl: ttl, issuer: issuer) end |
.verify(token, verification_key:, **opts) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/legion/crypt/jwt.rb', line 62 def self.verify(token, verification_key:, **opts) algorithm = opts.fetch(:algorithm, 'HS256') verify_expiration = opts.fetch(:verify_expiration, true) verify_issuer = opts.fetch(:verify_issuer, true) issuer = opts.fetch(:issuer, 'legion') validate_algorithm!(algorithm) decode_opts = { algorithm: algorithm, verify_expiration: verify_expiration, verify_iss: verify_issuer } decode_opts[:iss] = issuer if verify_issuer payload, _header = ::JWT.decode(token, verification_key, true, decode_opts) result = symbolize_keys(payload) log.debug "JWT verify success: sub=#{result[:sub]}, jti=#{result[:jti]}" result rescue ::JWT::ExpiredSignature => e handle_exception(e, level: :warn, operation: 'crypt.jwt.verify.expired', algorithm: algorithm) log.warn 'JWT verify failed: token has expired' raise ExpiredTokenError, 'token has expired' rescue ::JWT::VerificationError, ::JWT::IncorrectAlgorithm => e handle_exception(e, level: :warn, operation: 'crypt.jwt.verify.signature', algorithm: algorithm) log.warn 'JWT verify failed: signature verification failed' raise InvalidTokenError, 'token signature verification failed' rescue ::JWT::DecodeError => e handle_exception(e, level: :warn, operation: 'crypt.jwt.verify.decode', algorithm: algorithm) log.warn "JWT verify failed: #{e.}" raise DecodeError, "failed to decode token: #{e.}" rescue StandardError => e handle_exception(e, level: :error, operation: 'crypt.jwt.verify', algorithm: algorithm) raise end |
.verify_with_jwks(token, jwks_url:, **opts) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/legion/crypt/jwt.rb', line 109 def self.verify_with_jwks(token, jwks_url:, **opts) header = decode_header(token) kid = header['kid'] algorithm = header['alg'] || 'RS256' raise InvalidTokenError, 'token header missing kid' unless kid validate_algorithm!(algorithm) public_key = Legion::Crypt::JwksClient.find_key(jwks_url, kid) verify_expiration = opts.fetch(:verify_expiration, true) issuers = opts[:issuers] audience = opts[:audience] validate_external_requirements!(issuers: issuers, audience: audience) decode_opts = { algorithm: algorithm, verify_expiration: verify_expiration, verify_iss: true, iss: issuers, verify_aud: true, aud: audience } payload, _header = ::JWT.decode(token, public_key, true, decode_opts) result = symbolize_keys(payload) log.info "JWT JWKS verify success: sub=#{result[:sub]}, kid=#{kid}" result rescue ::JWT::ExpiredSignature => e handle_exception(e, level: :warn, operation: 'crypt.jwt.verify_with_jwks.expired', jwks_url: jwks_url, kid: kid) log.warn "JWT JWKS verify failed: token has expired, kid=#{kid}" raise ExpiredTokenError, 'token has expired' rescue ::JWT::VerificationError, ::JWT::IncorrectAlgorithm => e handle_exception(e, level: :warn, operation: 'crypt.jwt.verify_with_jwks.signature', jwks_url: jwks_url, kid: kid) log.warn "JWT JWKS verify failed: signature verification failed, kid=#{kid}" raise InvalidTokenError, 'token signature verification failed' rescue ::JWT::InvalidIssuerError => e handle_exception(e, level: :warn, operation: 'crypt.jwt.verify_with_jwks.issuer', jwks_url: jwks_url, kid: kid) log.warn "JWT JWKS verify failed: issuer not allowed, kid=#{kid}" raise InvalidTokenError, 'token issuer not allowed' rescue ::JWT::InvalidAudError => e handle_exception(e, level: :warn, operation: 'crypt.jwt.verify_with_jwks.audience', jwks_url: jwks_url, kid: kid) log.warn "JWT JWKS verify failed: audience mismatch, kid=#{kid}" raise InvalidTokenError, 'token audience mismatch' rescue ::JWT::DecodeError => e handle_exception(e, level: :warn, operation: 'crypt.jwt.verify_with_jwks.decode', jwks_url: jwks_url, kid: kid) log.warn "JWT JWKS verify failed: #{e.}, kid=#{kid}" raise DecodeError, "failed to decode token: #{e.}" rescue StandardError => e handle_exception(e, level: :error, operation: 'crypt.jwt.verify_with_jwks', jwks_url: jwks_url, kid: kid) raise end |