Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github - legacy token check support for GHE #1140

Open
wants to merge 2 commits into
base: enterprise-2.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/travis/config/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def logs_api_auth_token
sidekiq: { namespace: 'sidekiq', pool_size: 1 },
smtp: {},
email: {},
github: { api_url: 'https://api.github.com', token: 'travisbot-token' },
github: { api_url: 'https://api.github.com', token: 'travisbot-token', enterprise_legacy_oauth: true },
async: {},
notifications: [], # TODO rename to event.handlers
metrics: { reporter: 'librato' },
Expand Down
31 changes: 31 additions & 0 deletions lib/travis/github.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
require 'gh'
require 'core_ext/hash/compact'

GH::Remote.class_eval do
def http(verb, url, headers = {}, &block)
body = headers.delete :body
connection.run_request(verb, url, body, headers, &block)
rescue Exception => error
raise Error.new(error, nil, :verb => verb, :url => url, :headers => headers)
end
end

GH::TokenCheck.class_eval do

def check_token
return unless @check_token and client_id and client_secret and token
@check_token = false
auth_header = "Basic %s" % Base64.encode64("#{client_id}:#{client_secret}").gsub("\n", "")

if is_legacy?
http :head, path_for("/applications/#{client_id}/tokens/#{token}?client_id=#{client_id}&client_secret=#{client_secret}"), "Authorization" => auth_header
else
http :post, path_for("/applications/#{client_id}/token"), :body => "{\"access_token\": \"#{token}\"}", "Authorization" => auth_header
end
rescue GH::Error(:response_status => 404) => error
raise GH::TokenInvalid, error
end

def is_legacy?
Travis.config.github.enterprise_legacy_oauth
end

end

module Travis
module Github
class << self
Expand Down