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
233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/hub/github_api.rb', line 233 def apply_authentication req, url if (req.path =~ /\/authorizations$/) super else user = url.user || config.username(url.host) token = config.oauth_token(url.host, user) { obtain_oauth_token url.host, user } req['Authorization'] = "token #{token}" end end |
#obtain_oauth_token(host, user) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/hub/github_api.rb', line 245 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 |