Class: JWTSessions::RedisTokenStore

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt_sessions/redis_token_store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, prefix) ⇒ RedisTokenStore



29
30
31
32
# File 'lib/jwt_sessions/redis_token_store.rb', line 29

def initialize(store, prefix)
  @store  = store
  @prefix = prefix
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



27
28
29
# File 'lib/jwt_sessions/redis_token_store.rb', line 27

def prefix
  @prefix
end

#storeObject (readonly)

Returns the value of attribute store.



27
28
29
# File 'lib/jwt_sessions/redis_token_store.rb', line 27

def store
  @store
end

Class Method Details

.clearObject



15
16
17
18
# File 'lib/jwt_sessions/redis_token_store.rb', line 15

def clear
  @_tokens_store = nil
  @_token_prefix = nil
end

.instance(redis_url, prefix) ⇒ Object



8
9
10
11
12
13
# File 'lib/jwt_sessions/redis_token_store.rb', line 8

def instance(redis_url, prefix)
  @_tokens_store ||= Redis.new(url: redis_url)
  @_token_prefix ||= prefix

  new(@_tokens_store, @_token_prefix)
end

Instance Method Details

#all_in_namespace(namespace) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/jwt_sessions/redis_token_store.rb', line 68

def all_in_namespace(namespace)
  keys = store.keys(refresh_key('*', namespace))
  (keys || []).each_with_object({}) do |key, acc|
    uid = uid_from_key(key)
    acc[uid] = fetch_refresh(uid, namespace)
  end
end

#destroy_access(uid) ⇒ Object



80
81
82
# File 'lib/jwt_sessions/redis_token_store.rb', line 80

def destroy_access(uid)
  store.del(access_key(uid))
end

#destroy_refresh(uid, namespace) ⇒ Object



76
77
78
# File 'lib/jwt_sessions/redis_token_store.rb', line 76

def destroy_refresh(uid, namespace)
  store.del(refresh_key(uid, namespace))
end

#fetch_access(uid) ⇒ Object



34
35
36
37
38
# File 'lib/jwt_sessions/redis_token_store.rb', line 34

def fetch_access(uid)
  csrf = store.get(access_key(uid))
  return {} if csrf.nil?
  { csrf: csrf }
end

#fetch_refresh(uid, namespace) ⇒ Object



46
47
48
49
50
51
# File 'lib/jwt_sessions/redis_token_store.rb', line 46

def fetch_refresh(uid, namespace)
  keys   = i[csrf access_uid access_expiration expiration]
  values = store.hmget(refresh_key(uid, namespace), *keys).compact
  return {} if values.length != keys.length
  keys.each_with_index.each_with_object({}) { |(key, index), acc| acc[key] = values[index]; }
end

#persist_access(uid, csrf, expiration) ⇒ Object



40
41
42
43
44
# File 'lib/jwt_sessions/redis_token_store.rb', line 40

def persist_access(uid, csrf, expiration)
  key = access_key(uid)
  store.set(key, csrf)
  store.expireat(key, expiration)
end

#persist_refresh(uid, access_expiration, access_uid, csrf, expiration, namespace = nil) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/jwt_sessions/redis_token_store.rb', line 53

def persist_refresh(uid, access_expiration, access_uid, csrf, expiration, namespace = nil)
  ns = namespace || ''
  key = refresh_key(uid, ns)
  update_refresh(uid, access_expiration, access_uid, csrf, ns)
  store.hset(key, :expiration, expiration)
  store.expireat(key, expiration)
end

#update_refresh(uid, access_expiration, access_uid, csrf, namespace = nil) ⇒ Object



61
62
63
64
65
66
# File 'lib/jwt_sessions/redis_token_store.rb', line 61

def update_refresh(uid, access_expiration, access_uid, csrf, namespace = nil)
  store.hmset(refresh_key(uid, namespace),
              :csrf, csrf,
              :access_expiration, access_expiration,
              :access_uid, access_uid)
end