From cf9d32af7c153569c3894f8479c8a8e8143e9c8d Mon Sep 17 00:00:00 2001 From: Vanessa Burroughs Date: Thu, 5 Dec 2024 11:47:58 -0600 Subject: [PATCH] feat: install ActiveSupport for jwks caching --- Gemfile | 1 + lib/passageidentity/auth.rb | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index b435074..42cf6bb 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ source 'https://rubygems.org' gemspec group :development do + gem 'activesupport', '~> 8.0' gem 'dotenv', '~> 3.1' gem 'prettier_print', '~> 1.2' gem 'rack', '~> 3.1' diff --git a/lib/passageidentity/auth.rb b/lib/passageidentity/auth.rb index f48058c..acb03ed 100644 --- a/lib/passageidentity/auth.rb +++ b/lib/passageidentity/auth.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'active_support' require 'openssl' require 'base64' require 'jwt' @@ -14,7 +15,7 @@ class Auth # rubocop:disable Metrics/AbcSize def initialize(app_id, api_key, auth_strategy) - @app_cache = {} + @app_cache = ActiveSupport::Cache::MemoryStore.new @app_id = app_id @api_key = api_key @auth_strategy = auth_strategy @@ -197,7 +198,7 @@ def fetch_jwks auth_gw_connection.get("/v1/apps/#{@app_id}/.well-known/jwks.json") @jwks = response.body - !cache(@app_id) && cache(key: @app_id, jwks: @jwks) + cache(key: @app_id, jwks: @jwks) end end @@ -220,11 +221,11 @@ def user_exists?(user_id) end def cache(key) - @app_cache[key] + @app_cache.read(key) end def cache=(key:, jwks:) - @app_cache[key] = jwks + @app_cache.write(key, jwks) end def jwk_exists(token)