Class: GitMarkdown::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/git/markdown/credentials.rb

Class Method Summary collapse

Class Method Details

.from_envObject



16
17
18
# File 'lib/git/markdown/credentials.rb', line 16

def from_env
  ENV["GITHUB_TOKEN"] || ENV["GH_TOKEN"]
end

.from_fileObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/git/markdown/credentials.rb', line 57

def from_file
  credentials_file = File.join(
    Configuration::XDG_CONFIG_HOME,
    "git-markdown",
    "credentials"
  )

  return nil unless File.exist?(credentials_file)

  File.read(credentials_file).strip
end

.from_gh_cliObject



50
51
52
53
54
55
# File 'lib/git/markdown/credentials.rb', line 50

def from_gh_cli
  output = `gh auth token 2>/dev/null`
  output.strip if $?.success? && !output.strip.empty?
rescue
  nil
end

.from_git_credentialObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git/markdown/credentials.rb', line 20

def from_git_credential
  host = "github.com"
  protocol = "https"

  input = "protocol=#{protocol}\nhost=#{host}\n"
  output = nil
  env = {
    "GIT_TERMINAL_PROMPT" => "0",
    "GIT_ASKPASS" => "/bin/false",
    "SSH_ASKPASS" => "/bin/false"
  }

  IO.popen(env, "git credential fill 2>/dev/null", "r+") do |io|
    io.write(input)
    io.close_write
    output = io.read
  end

  return nil unless $?.success?

  output.each_line do |line|
    key, value = line.chomp.split("=", 2)
    return value if key == "password"
  end

  nil
rescue
  nil
end

.resolve(debug: false) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/git/markdown/credentials.rb', line 6

def resolve(debug: false)
  token = from_env || from_git_credential || from_gh_cli || from_file

  raise AuthenticationError, "No GitHub token found. Run `git-markdown setup` to configure." if token.nil?

  token.strip
rescue => e
  raise AuthenticationError, "Failed to resolve credentials: #{e.message}"
end