class JWTSessions::StoreAdapters::RedisStoreAdapter
Constants
- REFRESH_KEYS
Attributes
prefix[R]
storage[R]
Public Class Methods
new(token_prefix: JWTSessions.token_prefix, **options)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 10 def initialize(token_prefix: JWTSessions.token_prefix, **options) @prefix = token_prefix begin require "redis" @storage = configure_redis_client(**options) rescue LoadError => e msg = "Could not load the 'redis' 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
Public Instance Methods
all_refresh_tokens(namespace)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 64 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) acc[uid] = fetch_refresh(uid, namespace) end end
destroy_access(uid)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 77 def destroy_access(uid) storage.del(access_key(uid)) end
destroy_refresh(uid, namespace)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 72 def destroy_refresh(uid, namespace) key = full_refresh_key(uid, namespace) storage.del(key) end
fetch_access(uid)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 23 def fetch_access(uid) csrf = storage.get(access_key(uid)) csrf.nil? ? {} : { csrf: csrf } end
fetch_refresh(uid, namespace, first_match = false)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 34 def fetch_refresh(uid, namespace, first_match = false) key = first_match ? first_refresh_key(uid) : full_refresh_key(uid, namespace) values = storage.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] } end
persist_access(uid, csrf, expiration)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 28 def persist_access(uid, csrf, expiration) key = access_key(uid) storage.set(key, csrf) storage.expireat(key, expiration) end
persist_refresh(uid:, access_expiration:, access_uid:, csrf:, expiration:, namespace: nil)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 42 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.hset(key, :expiration, expiration) storage.expireat(key, expiration) end
update_refresh(uid:, access_expiration:, access_uid:, csrf:, namespace: nil)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 55 def update_refresh(uid:, access_expiration:, access_uid:, csrf:, namespace: nil) storage.hmset( full_refresh_key(uid, namespace), :csrf, csrf, :access_expiration, access_expiration, :access_uid, access_uid ) end
Private Instance Methods
access_key(uid)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 122 def access_key(uid) "#{prefix}_access_#{uid}" end
build_redis_url(redis_host: nil, redis_port: nil, redis_db_name: nil)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 97 def build_redis_url(redis_host: nil, redis_port: nil, redis_db_name: nil) redis_db_name ||= JWTSessions.redis_db_name return URI.join(JWTSessions.redis_url, redis_db_name).to_s if JWTSessions.redis_url redis_host ||= JWTSessions.redis_host redis_port ||= JWTSessions.redis_port redis_base_url = ENV["REDIS_URL"] || "redis://#{redis_host}:#{redis_port}" URI.join(redis_base_url, redis_db_name).to_s end
configure_redis_client(redis_url: nil, redis_host: nil, redis_port: nil, redis_db_name: nil, **options)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 83 def configure_redis_client(redis_url: nil, redis_host: nil, redis_port: nil, redis_db_name: nil, **options) if redis_url && (redis_host || redis_port || redis_db_name) raise ArgumentError, "redis_url cannot be passed along with redis_host, redis_port or redis_db_name options" end redis_url ||= build_redis_url( redis_host: redis_host, redis_port: redis_port, redis_db_name: redis_db_name ) Redis.new(options.merge(url: redis_url)) end
first_refresh_key(uid)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 112 def first_refresh_key(uid) key = full_refresh_key(uid, "*") (scan_keys(key) || []).first end
full_refresh_key(uid, namespace)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 108 def full_refresh_key(uid, namespace) "#{prefix}_#{namespace}_refresh_#{uid}" end
refresh_key(uid, namespace)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 117 def refresh_key(uid, namespace) namespace = "*" if namespace.to_s.empty? full_refresh_key(uid, namespace) end
scan_keys(key_pattern)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 130 def scan_keys(key_pattern) cursor = 0 all_keys = [] loop do cursor, keys = storage.scan(cursor, match: key_pattern, count: 1000) all_keys |= keys break if cursor == "0" end all_keys end
uid_from_key(key)
click to toggle source
# File lib/jwt_sessions/store_adapters/redis_store_adapter.rb, line 126 def uid_from_key(key) key.split("_").last end