Class: Spiffe::Workload::JWTSVIDWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/spiffe/workload/jwt_svid.rb

Overview

Represents a JWT SVID

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spiffe_id:, token:, hint: nil) ⇒ JWTSVIDWrapper



15
16
17
18
19
20
# File 'lib/spiffe/workload/jwt_svid.rb', line 15

def initialize(spiffe_id:, token:, hint: nil)
  @spiffe_id = spiffe_id
  @token = token
  @hint = hint
  @claims = parse_claims
end

Instance Attribute Details

#claimsObject (readonly)

Returns the value of attribute claims.



10
11
12
# File 'lib/spiffe/workload/jwt_svid.rb', line 10

def claims
  @claims
end

#hintObject (readonly)

Returns the value of attribute hint.



10
11
12
# File 'lib/spiffe/workload/jwt_svid.rb', line 10

def hint
  @hint
end

#spiffe_idObject (readonly)

Returns the value of attribute spiffe_id.



10
11
12
# File 'lib/spiffe/workload/jwt_svid.rb', line 10

def spiffe_id
  @spiffe_id
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/spiffe/workload/jwt_svid.rb', line 10

def token
  @token
end

Class Method Details

.from_proto(proto_jwt) ⇒ JWTSVID

Parse JWT SVID from proto response



66
67
68
69
70
71
72
# File 'lib/spiffe/workload/jwt_svid.rb', line 66

def self.from_proto(proto_jwt)
  new(
    spiffe_id: proto_jwt.spiffe_id,
    token: proto_jwt.svid,
    hint: proto_jwt.hint.empty? ? nil : proto_jwt.hint
  )
end

Instance Method Details

#audienceArray<String>

Get audience claims



57
58
59
60
61
# File 'lib/spiffe/workload/jwt_svid.rb', line 57

def audience
  aud = @claims['aud']
  return [] unless aud
  aud.is_a?(Array) ? aud : [aud]
end

#expirationTime?

Get the expiration time



42
43
44
45
# File 'lib/spiffe/workload/jwt_svid.rb', line 42

def expiration
  return nil unless @claims['exp']
  Time.at(@claims['exp'])
end

#expired?Boolean

Check if the JWT is expired



49
50
51
52
53
# File 'lib/spiffe/workload/jwt_svid.rb', line 49

def expired?
  exp = expiration
  return false unless exp
  exp < Time.now
end

#parse_claimsHash

Parse JWT claims without validation



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/spiffe/workload/jwt_svid.rb', line 24

def parse_claims
  # JWT format: header.payload.signature
  parts = @token.split('.')
  raise Error, 'Invalid JWT format' unless parts.length == 3

  # Decode payload (base64url)
  payload = parts[1]
  # Add padding if necessary
  payload += '=' * (4 - payload.length % 4) if payload.length % 4 != 0
  
  decoded = Base64.urlsafe_decode64(payload)
  JSON.parse(decoded)
rescue StandardError => e
  raise Error, "Failed to parse JWT claims: #{e.message}"
end