Module: Legion::Extensions::Github::Helpers::ScopeRegistry

Included in:
Client
Defined in:
lib/legion/extensions/github/helpers/scope_registry.rb

Instance Method Summary collapse

Instance Method Details

#credential_fingerprint(auth_type:, identifier:) ⇒ Object



10
11
12
# File 'lib/legion/extensions/github/helpers/scope_registry.rb', line 10

def credential_fingerprint(auth_type:, identifier:)
  Digest::SHA256.hexdigest("#{auth_type}:#{identifier}")[0, 16]
end

#invalidate_scope(fingerprint:, owner:, repo: nil) ⇒ Object



48
49
50
51
52
# File 'lib/legion/extensions/github/helpers/scope_registry.rb', line 48

def invalidate_scope(fingerprint:, owner:, repo: nil)
  key = repo ? "github:scope:#{fingerprint}:#{owner}/#{repo}" : "github:scope:#{fingerprint}:#{owner}"
  cache_delete(key) if cache_connected?
  local_cache_delete(key) if local_cache_connected?
end

#mark_rate_limited(fingerprint:, reset_at:) ⇒ Object



41
42
43
44
45
46
# File 'lib/legion/extensions/github/helpers/scope_registry.rb', line 41

def mark_rate_limited(fingerprint:, reset_at:)
  ttl = [(reset_at - Time.now).ceil, 1].max
  value = { reset_at: reset_at, remaining: 0 }
  cache_set("github:rate_limit:#{fingerprint}", value, ttl: ttl) if cache_connected?
  local_cache_set("github:rate_limit:#{fingerprint}", value, ttl: ttl) if local_cache_connected?
end

#rate_limited?(fingerprint:) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/legion/extensions/github/helpers/scope_registry.rb', line 34

def rate_limited?(fingerprint:)
  entry = scope_cache_get("github:rate_limit:#{fingerprint}")
  return false unless entry

  entry[:reset_at] > Time.now
end

#register_scope(fingerprint:, owner:, status:, repo: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/extensions/github/helpers/scope_registry.rb', line 23

def register_scope(fingerprint:, owner:, status:, repo: nil)
  key = repo ? "github:scope:#{fingerprint}:#{owner}/#{repo}" : "github:scope:#{fingerprint}:#{owner}"
  ttl = if status == :denied
          scope_denied_ttl
        else
          (repo ? scope_repo_ttl : scope_org_ttl)
        end
  cache_set(key, status, ttl: ttl) if cache_connected?
  local_cache_set(key, status, ttl: ttl) if local_cache_connected?
end

#scope_status(fingerprint:, owner:, repo: nil) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/legion/extensions/github/helpers/scope_registry.rb', line 14

def scope_status(fingerprint:, owner:, repo: nil)
  if repo
    status = scope_cache_get("github:scope:#{fingerprint}:#{owner}/#{repo}")
    return status if status
  end

  scope_cache_get("github:scope:#{fingerprint}:#{owner}") || :unknown
end