Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching certificates on memory. #33

Merged
merged 3 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/firebase_id_token/signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,20 @@ def initialize(jwt_token, raise_error: false)

# @see Signature.verify
def verify
certificate = firebase_id_token_certificates.find(@kid, raise_error: @raise_error)
var_name = :_firebase_id_token_cert
Thread.current[var_name] ||= {
cert: nil,
expires_at: Time.now.utc - 1
}

if Thread.current[var_name][:expires_at] <= Time.now.utc
Thread.current[var_name] = {
cert: firebase_id_token_certificates.find(@kid, raise_error: @raise_error),
expires_at: Time.now.utc + firebase_id_token_certificates.ttl
}
end

certificate = Thread.current[var_name][:cert]
return unless certificate

payload = decode_jwt_payload(@jwt_token, certificate.public_key)
Expand Down
4 changes: 4 additions & 0 deletions lib/firebase_id_token/testing/certificates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def self.read_jwt_file
)
)
end

def self.ttl
10
end
end
end
end
2 changes: 1 addition & 1 deletion lib/firebase_id_token/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module FirebaseIdToken
VERSION = '2.4.0'
VERSION = '2.5.1'
end