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
213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/hub/github_api.rb', line 213 def apply_authentication req, url if req.path.index('/authorizations') == 0 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
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/hub/github_api.rb', line 225 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 |