Skip to content

Commit 684b07c

Browse files
authored
1 parent 31a4acb commit 684b07c

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

lib/uaa/token_coder.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ def self.decode(token, options = {}, obsolete1 = nil, obsolete2 = nil)
129129
payload
130130
end
131131

132+
# Decodes a JWT token to extract its expiry time
133+
# @param [String] token A JWT token as returned by {TokenCoder.encode}
134+
# @return [Integer] exp expiry timestamp
135+
def self.decode_token_expiry(token)
136+
segments = token.split('.')
137+
raise InvalidTokenFormat, "Not enough or too many segments" unless [2,3].include? segments.length
138+
header_segment, payload_segment, crypto_segment = segments
139+
payload = Util.json_decode64(payload_segment, :sym)
140+
payload[:exp]
141+
end
142+
132143
# Takes constant time to compare 2 strings (HMAC digests in this case)
133144
# to avoid timing attacks while comparing the HMAC digests
134145
# @param [String] a: the first digest to compare

spec/token_coder_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ module CF::UAA
183183
info["id"].should_not be_nil
184184
info["email"].should == "olds@vmware.com"
185185
end
186+
187+
it "decodes only the expiry_at time" do
188+
exp = Time.now.to_i + 60
189+
tkn = subject.encode({'foo' => "bar", 'exp' => exp })
190+
TokenCoder.decode_token_expiry("bEaReR #{tkn}").should == exp
191+
end
186192
end
187193

188194
end

0 commit comments

Comments
 (0)