Class: OAuth2c::Cache::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2c/cache/store.rb

Defined Under Namespace

Classes: Bucket

Instance Method Summary collapse

Constructor Details

#initialize(backend, exp_leeway: 300) ⇒ Store

Returns a new instance of Store.



20
21
22
23
# File 'lib/oauth2c/cache/store.rb', line 20

def initialize(backend, exp_leeway: 300)
  @backend    = backend
  @exp_leeway = exp_leeway
end

Instance Method Details

#cached(key, scope: []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/oauth2c/cache/store.rb', line 29

def cached(key, scope: [])
  return nil if key.nil?

  cache = @backend.lookup(key)
  return nil if cache.nil?
  return nil unless scope.all? { |s| cache.scope.include?(s) }

  if cache.access_token.expires_at - @exp_leeway >= Time.now
    cache.access_token
  end
end

#cached?(key, scope: []) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/oauth2c/cache/store.rb', line 25

def cached?(key, scope: [])
  cached(key, scope: scope) ? true : false
end

#issue(key, scope:, &block) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/oauth2c/cache/store.rb', line 41

def issue(key, scope:, &block)
  cached = @backend.lookup(key) unless key.nil?
  scope  = cached[:scope] | scope if cached

  access_token = block.call(scope)
  @backend.store(key, Bucket.new(access_token, scope)) unless key.nil?
  access_token
end