Skip to content

Commit eea700f

Browse files
committed
fix auth, spit warning when using github.token
1 parent 0b8698d commit eea700f

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

lib/gist.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ def write(files, private_gist = false, description = nil)
141141
req = Net::HTTP::Post.new(url.path)
142142
req.body = JSON.generate(data(files, private_gist, description))
143143

144-
if auth_header = auth()
145-
req.add_field('Authorization', auth_header)
144+
user, password = auth()
145+
if user && password
146+
req.basic_auth(user, password)
146147
end
147148

148149
response = http.start{|h| h.request(req) }
@@ -212,15 +213,22 @@ def data(files, private_gist, description)
212213

213214
# Returns a basic auth string of the user's GitHub credentials if set.
214215
# http://github.com/guides/local-github-config
216+
#
217+
# Returns an Array of Strings if auth is found: [user, password]
218+
# Returns nil if no auth is found.
215219
def auth
216220
user = config("github.user")
217221
password = config("github.password")
218222

223+
token = config("github.token")
224+
if password.to_s.empty? && !token.to_s.empty?
225+
abort "Please set GITHUB_PASSWORD or github.password instead of using a token."
226+
end
227+
219228
if user.to_s.empty? || password.to_s.empty?
220229
nil
221230
else
222-
auth_str = Base64.encode64("#{user}:#{password}")
223-
"Basic #{auth_str}"
231+
[ user, password ]
224232
end
225233
end
226234

0 commit comments

Comments
 (0)