Sha256: cf8594a137fa579db5c40c087b540f6b0b6bb3c39ffcb609925ae80587d4e770

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module GithubAuthentication
  class GitCredentialHelper
    def initialize(pem:, installation_id:, app_id:, storage: nil, stdin: $stdin)
      @pem = pem
      @installation_id = installation_id
      @app_id = app_id
      @storage = storage
      @stdin = stdin
    end

    def handle_get
      description = parse_stdin

      unless description['protocol'] == 'https' && description['host'] == 'github.com'
        warn("Unsupported description: #{description}")
        return 2
      end

      token = provider.token(seconds_ttl: min_cache_ttl)
      puts("password=#{token}")
      puts('username=api')

      0
    end

    private

    def min_cache_ttl
      # Tokens are valid for 60 minutes, allow a 10 minute buffer
      10 * 60
    end

    def parse_stdin
      # Credential description is written to STDIN in line delimited key=value form,
      # see https://git-scm.com/docs/git-credential#IOFMT
      @stdin.each_line.map { |line| line.split('=', 2).map(&:strip) }.to_h
    end

    def provider
      @provider ||= Provider.new(
        generator: generator,
        cache: Cache.new(storage: @storage || ObjectCache.new)
      )
    end

    def generator
      @generator ||= Generator::App.new(
        pem: @pem,
        app_id: @app_id,
        installation_id: @installation_id
      )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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