From 3a64c87a7f76021dfd8021c3e63c13dae0371cfd Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Tue, 18 Sep 2012 19:15:12 +0200 Subject: [PATCH] send user data with post message --- lib/travis/api/app/endpoint.rb | 7 +++---- lib/travis/api/app/endpoint/authorization.rb | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/travis/api/app/endpoint.rb b/lib/travis/api/app/endpoint.rb index ebfeec22..c4cd2199 100644 --- a/lib/travis/api/app/endpoint.rb +++ b/lib/travis/api/app/endpoint.rb @@ -13,14 +13,13 @@ class Travis::Api::App private - def service(key) + def service(key, user = current_user) const = Travis.services[key] || raise("no service registered for #{key}") - const.new(current_user) + const.new(user) end def current_user - # TODO - User.where(:login => 'svenfuchs').first + env['travis.access_token'].user if env['travis.access_token'] end def redis diff --git a/lib/travis/api/app/endpoint/authorization.rb b/lib/travis/api/app/endpoint/authorization.rb index b7ed528b..fafa5ad5 100644 --- a/lib/travis/api/app/endpoint/authorization.rb +++ b/lib/travis/api/app/endpoint/authorization.rb @@ -92,8 +92,10 @@ class Travis::Api::App values[:code] = params[:code] values[:client_secret] = config.client_secret github_token = get_token(endpoint.to_s, values) - token = github_to_travis(github_token, app_id: 0) - post_message(token: token) + user = user_for_github_token(github_token) + token = generate_token(user: user, app_id: 0) + rendered_user = service(:user, user).find_one + post_message(token: token, user: rendered_user) else values[:state] = create_state endpoint.path = config.authorize_path @@ -120,14 +122,17 @@ class Travis::Api::App end def github_to_travis(token, options = {}) + generate_token options.merge(user: user_for_github_token(token)) + end + + def user_for_github_token(token) data = GH.with(token: token.to_s) { GH['user'] } scopes = parse_scopes data.headers['x-oauth-scopes'] user = User.find_by_login(data['login']) halt 403, 'not a Travis user' if user.nil? halt 403, 'insufficient access' unless acceptable? scopes - - generate_token options.merge(user: user) + user end def get_token(endoint, values) @@ -160,9 +165,9 @@ __END__ @@ post_message - -SENT \ No newline at end of file