Class: JWTSessions::StoreAdapters::RedisStoreAdapter

Inherits:
AbstractStoreAdapter show all
Defined in:
lib/jwt_sessions/store_adapters/redis_store_adapter.rb

Constant Summary collapse

REFRESH_KEYS =
%i[csrf access_uid access_expiration expiration].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_prefix: JWTSessions.token_prefix, **options) ⇒ RedisStoreAdapter

Returns a new instance of RedisStoreAdapter.



10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#prefixObject (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

#storageObject (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



64
65
66
67
68
69
70
# File 'lib/jwt_sessions/store_adapters/redis_store_adapter.rb', line 64

def all_refresh_tokens(namespace)
  keys_in_namespace = storage.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) ⇒ Object



77
78
79
# 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) ⇒ Object



72
73
74
75
# 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) ⇒ Object



23
24
25
26
# 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) ⇒ Object



34
35
36
37
38
39
40
# 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) ⇒ Object



28
29
30
31
32
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



55
56
57
58
59
60
61
62
# 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