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



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

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



62
63
64
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 62

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

#destroy_refresh(uid, namespace) ⇒ Object



58
59
60
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 58

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
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 25

def fetch_refresh(uid, namespace, _first_match = false)
  value_if_not_expired(uid, "refresh", namespace.to_s)
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



29
30
31
32
33
34
35
36
37
38
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 29

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



40
41
42
43
44
45
46
47
48
# File 'lib/jwt_sessions/store_adapters/memory_store_adapter.rb', line 40

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