Skip to content

Commit

Permalink
Attempt to fetch oauth token from git credential
Browse files Browse the repository at this point in the history
  • Loading branch information
sj26 committed Jul 19, 2017
1 parent de3e287 commit fc98513
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion lib/gist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,50 @@ def self.write(token)
end
end

module AuthTokenGitCredential
def self.uri
if ENV.key?(URL_ENV_NAME)
URI.parse(ENV[URL_ENV_NAME])
else
GITHUB_API_URL
end
end

def self.helpers
IO.popen(["git", "config", "--get-all", "credential.helper"], &:read).split.uniq
end

def self.read
helpers.each do |helper|
begin
IO.popen(["git", "credential-#{helper}", "get"], "r+") do |io|
io.puts "protocol=#{uri.scheme}"
io.puts "hostname=#{uri.host}"
io.puts "username=#{uri.user}" if uri.user
io.puts

until io.eof?
line = io.readline.chomp
next if line.empty?

key, value = line.split("=", 2)
return value if key == "password"
end
end
rescue
# Swallow, try next helper
end
end

return nil
end
end

# auth token for authentication
#
# @return [String] string value of access token or `nil`, if not found
def auth_token
@token ||= AuthTokenFile.read rescue nil
@token ||= AuthTokenFile.read rescue AuthTokenGitCredential.read rescue nil
end

# Upload a gist to https://gist.github.com
Expand Down

0 comments on commit fc98513

Please sign in to comment.