Module: Hub::GitHubAPI::OAuth
- Included in:
- Hub::GitHubAPI
- Defined in:
- lib/hub/github_api.rb
Instance Method Summary collapse
Instance Method Details
#apply_authentication(req, url) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/hub/github_api.rb', line 239 def apply_authentication req, url if (req.path =~ /\/authorizations$/) super else refresh = false user = url.user || config.username(url.host) token = config.oauth_token(url.host, user) { refresh = true obtain_oauth_token url.host, user } if refresh # get current user info user to persist correctly capitalized login name res = get "https://#{url.host}/user" res.error! unless res.success? config.update_username(url.host, user, res.data['login']) end req['Authorization'] = "token #{token}" end end |
#obtain_oauth_token(host, user) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/hub/github_api.rb', line 259 def obtain_oauth_token host, user # first try to fetch existing authorization res = get "https://#{user}@#{host}/authorizations" res.error! unless res.success? if found = res.data.find {|auth| auth['app']['url'] == oauth_app_url } found['token'] else # create a new authorization res = post "https://#{user}@#{host}/authorizations", :scopes => %w[repo], :note => 'hub', :note_url => oauth_app_url res.error! unless res.success? res.data['token'] end end |