Class: JWTSessions::StoreAdapters::RedisStoreAdapter
- Inherits:
-
AbstractStoreAdapter
- Object
- AbstractStoreAdapter
- JWTSessions::StoreAdapters::RedisStoreAdapter
- Defined in:
- lib/jwt_sessions/store_adapters/redis_store_adapter.rb
Constant Summary collapse
- REFRESH_KEYS =
%i[csrf access_uid access_expiration expiration].freeze
- DEFAULT_POOL_SIZE =
5
Instance Attribute Summary collapse
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Instance Method Summary collapse
- #all_refresh_tokens(namespace) ⇒ Object
- #destroy_access(uid) ⇒ Object
- #destroy_refresh(uid, namespace) ⇒ Object
- #fetch_access(uid) ⇒ Object
- #fetch_refresh(uid, namespace, first_match = false) ⇒ Object
-
#initialize(token_prefix: JWTSessions.token_prefix, redis_client: nil, **options) ⇒ RedisStoreAdapter
constructor
A new instance of RedisStoreAdapter.
- #persist_access(uid, csrf, expiration) ⇒ Object
- #persist_refresh(uid:, access_expiration:, access_uid:, csrf:, expiration:, namespace: nil) ⇒ Object
- #update_refresh(uid:, access_expiration:, access_uid:, csrf:, namespace: nil) ⇒ Object
Constructor Details
#initialize(token_prefix: JWTSessions.token_prefix, redis_client: nil, **options) ⇒ RedisStoreAdapter
Returns a new instance of RedisStoreAdapter.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 11 def initialize(token_prefix: JWTSessions.token_prefix, redis_client: nil, **) @prefix = token_prefix if redis_client @storage = redis_client else begin require "redis-client" @storage = configure_redis_client(**) rescue LoadError => e msg = "Could not load the 'redis-client' gem, please add it to your gemfile or " \ "configure a different adapter (e.g. JWTSessions.store_adapter = :memory)" raise e.class, msg, e.backtrace end end end |
Instance Attribute Details
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
6 7 8 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 6 def prefix @prefix end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
6 7 8 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 6 def storage @storage end |
Instance Method Details
#all_refresh_tokens(namespace) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 74 def all_refresh_tokens(namespace) keys_in_namespace = scan_keys(refresh_key("*", namespace)) (keys_in_namespace || []).each_with_object({}) do |key, acc| uid = uid_from_key(key) # to be able to properly initialize namespaced tokens extract their namespaces # and pass down to fetch_refresh token_namespace = namespace.to_s.empty? ? namespace_from_key(key) : namespace token_attrs = fetch_refresh(uid, token_namespace) acc[uid] = token_attrs unless token_attrs.empty? end end |
#destroy_access(uid) ⇒ Object
91 92 93 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 91 def destroy_access(uid) storage.call("DEL", access_key(uid)) end |
#destroy_refresh(uid, namespace) ⇒ Object
86 87 88 89 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 86 def destroy_refresh(uid, namespace) key = full_refresh_key(uid, namespace) storage.call("DEL", key) end |
#fetch_access(uid) ⇒ Object
28 29 30 31 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 28 def fetch_access(uid) csrf = storage.call("GET", access_key(uid)) csrf.nil? ? {} : { csrf: csrf } end |
#fetch_refresh(uid, namespace, first_match = false) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 39 def fetch_refresh(uid, namespace, first_match = false) key = first_match ? first_refresh_key(uid) : full_refresh_key(uid, namespace) return {} if key.nil? values = storage.call("HMGET", key, *REFRESH_KEYS).compact return {} if values.length != REFRESH_KEYS.length REFRESH_KEYS .each_with_index .each_with_object({}) { |(key, index), acc| acc[key] = values[index] } .merge({ namespace: namespace }) end |
#persist_access(uid, csrf, expiration) ⇒ Object
33 34 35 36 37 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 33 def persist_access(uid, csrf, expiration) key = access_key(uid) storage.call("SET", key, csrf) storage.call("EXPIREAT", key, expiration) end |
#persist_refresh(uid:, access_expiration:, access_uid:, csrf:, expiration:, namespace: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 52 def persist_refresh(uid:, access_expiration:, access_uid:, csrf:, expiration:, namespace: nil) key = full_refresh_key(uid, namespace) update_refresh( uid: uid, access_expiration: access_expiration, access_uid: access_uid, csrf: csrf, namespace: namespace ) storage.call("HSET", key, :expiration, expiration) storage.call("EXPIREAT", key, expiration) end |
#update_refresh(uid:, access_expiration:, access_uid:, csrf:, namespace: nil) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 65 def update_refresh(uid:, access_expiration:, access_uid:, csrf:, namespace: nil) storage.call("HMSET", full_refresh_key(uid, namespace), :csrf, csrf, :access_expiration, access_expiration, :access_uid, access_uid ) end |