Class: GithubAuthentication::Provider

Inherits:
Object
  • Object
show all
Includes:
Retriable, Mutex_m
Defined in:
lib/github_authentication/provider.rb

Constant Summary collapse

Error =
Class.new(StandardError)
TokenGeneratorError =
Class.new(Error)

Instance Method Summary collapse

Methods included from Retriable

#with_retries

Constructor Details

#initialize(generator:, cache:) ⇒ Provider

Returns a new instance of Provider.



14
15
16
17
18
19
# File 'lib/github_authentication/provider.rb', line 14

def initialize(generator:, cache:)
  super()
  @token = nil
  @generator = generator
  @cache = cache
end

Instance Method Details

#inspectObject

prevent credential leak



50
51
52
# File 'lib/github_authentication/provider.rb', line 50

def inspect
  "#<#{self.class.name}>"
end

#reset_tokenObject



44
45
46
47
# File 'lib/github_authentication/provider.rb', line 44

def reset_token
  @token = nil
  @cache.clear
end

#token(seconds_ttl: 5 * 60) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/github_authentication/provider.rb', line 21

def token(seconds_ttl: 5 * 60)
  return @token if @token&.valid_for?(seconds_ttl)

  with_retries(TokenGeneratorError) do
    mu_synchronize do
      return @token if @token&.valid_for?(seconds_ttl)

      if (@token = @cache.read)
        return @token if @token.valid_for?(seconds_ttl)
      end

      if (@token = @generator.generate)
        if @token.valid_for?(seconds_ttl)
          @cache.write(@token)
          return @token
        end
      end

      raise TokenGeneratorError, "Couldn't create a token with a TTL of #{seconds_ttl}"
    end
  end
end