Class: GithubAuthentication::Provider

Inherits:
Object
  • Object
show all
Includes:
Retriable
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.



12
13
14
15
16
17
# File 'lib/github_authentication/provider.rb', line 12

def initialize(generator:, cache:)
  @token = nil
  @generator = generator
  @cache = cache
  @mutex = Mutex.new
end

Instance Method Details

#inspectObject

prevent credential leak



48
49
50
# File 'lib/github_authentication/provider.rb', line 48

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

#reset_tokenObject



42
43
44
45
# File 'lib/github_authentication/provider.rb', line 42

def reset_token
  @token = nil
  @cache.clear
end

#token(seconds_ttl: 5 * 60) ⇒ Object



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

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

  with_retries(TokenGeneratorError) do
    @mutex.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