Sha256: f936bfe22bc722669a6debed84f04e6b1061002ede3bd828c5e9a27f8e9d1f63

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true
require "mutex_m.rb"

require 'github/authentication/retriable'

module Github
  module Authentication
    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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
github-authentication-0.1.2 lib/github/authentication/provider.rb
github-authentication-0.1.0 lib/github/authentication/provider.rb