Sha256: 1c924675a44c9e13d3eba403b82147d2647febca15feff9fe76f12dc04b17c3a

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true
require "mutex_m.rb"

require 'github_authentication/retriable'

module GithubAuthentication
  class Provider
    include Retriable
    include Mutex_m

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

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

    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

    def reset_token
      @token = nil
      @cache.clear
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
github-authentication-1.0.2 lib/github_authentication/provider.rb
github-authentication-1.0.1 lib/github_authentication/provider.rb
github-authentication-1.0.0 lib/github_authentication/provider.rb