Class: JWTSessions::StoreAdapters::MemoryStoreAdapter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ MemoryStoreAdapter

Returns a new instance of MemoryStoreAdapter.

Raises:

  • (ArgumentError)


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

def initialize(**options)
  raise ArgumentError, "Memory store doesn't support any options" if options.any?
  @storage = Hash.new do |h, k|
    h[k] = Hash.new { |hh, kk| hh[kk] = {} }
  end
end

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



6
7
8
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 6

def storage
  @storage
end

Instance Method Details

#all_refresh_tokens(namespace) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 58

def all_refresh_tokens(namespace)
  namespace_keys = namespace.nil? ? storage.keys : [namespace]

  namespace_keys.each_with_object({}) do |namespace_key, acc|
    select_keys(storage[namespace_key]["refresh"], acc)
  end
end

#destroy_access(uid) ⇒ Object



70
71
72
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 70

def destroy_access(uid)
  storage[""]["access"].delete(uid)
end

#destroy_refresh(uid, namespace) ⇒ Object



66
67
68
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 66

def destroy_refresh(uid, namespace)
  storage[namespace.to_s]["refresh"].delete(uid)
end

#fetch_access(uid) ⇒ Object



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

def fetch_access(uid)
  access_token = value_if_not_expired(uid, "access", "")
  access_token.empty? ? {} : { csrf: access_token[:csrf] }
end

#fetch_refresh(uid, namespace, first_match = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 25

def fetch_refresh(uid, namespace, first_match = false)
  if first_match
    storage.keys.each do |namespace_key|
      val = value_if_not_expired(uid, "refresh", namespace_key)
      return val unless val.empty?
    end
    {}
  else
    value_if_not_expired(uid, "refresh", namespace.to_s)
  end
end

#persist_access(uid, csrf, expiration) ⇒ Object



20
21
22
23
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 20

def persist_access(uid, csrf, expiration)
  access_token = { csrf: csrf, expiration: expiration }
  storage[""]["access"].store(uid, access_token)
end

#persist_refresh(uid:, access_expiration:, access_uid:, csrf:, expiration:, namespace: "") ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 37

def persist_refresh(uid:, access_expiration:, access_uid:, csrf:, expiration:, namespace: "")
  update_refresh_fields(
    uid,
    namespace.to_s,
    csrf: csrf,
    access_expiration: access_expiration,
    access_uid: access_uid,
    expiration: expiration
  )
end

#update_refresh(uid:, access_expiration:, access_uid:, csrf:, namespace: "") ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 48

def update_refresh(uid:, access_expiration:, access_uid:, csrf:, namespace: "")
  update_refresh_fields(
    uid,
    namespace.to_s,
    csrf: csrf,
    access_expiration: access_expiration,
    access_uid: access_uid
  )
end