From defbbe1c8499197736e65b4d3d0876e0465357ce Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Tue, 12 Mar 2019 16:22:39 +0800 Subject: [PATCH] =?UTF-8?q?ecloud=20oauth2=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/ecloud_controller.rb | 40 +++++++++++++++------------- config/routes.rb | 2 +- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/app/controllers/ecloud_controller.rb b/app/controllers/ecloud_controller.rb index 82c7286e..20301409 100644 --- a/app/controllers/ecloud_controller.rb +++ b/app/controllers/ecloud_controller.rb @@ -17,9 +17,10 @@ class EcloudController < ApplicationController def trustie_login end - CLIENT_ID = 'e5da9855f89bc724a335d100cb63cf02a03a592bd3151bbc84acf7b2e222ddb8' - CLIENT_SECRET = '4f2f291fac1d3dae338c18a3e3544814be5a1c4ade9e72d62f45ceab914c89f5' + CLIENT_ID = '1022' + CLIENT_SECRET = '2112037a-6d7a-432b-9081-feb1153d8668' ROOT_URl = 'http://localhost:3000' + SERVER_URL = "https://221.176.54.92:9081/restful/services/" def oschina_login # 根据session,看看有没有存access_token,去刷新下。 @@ -29,6 +30,10 @@ class EcloudController < ApplicationController redirect_to "https://gitee.com/oauth/authorize?client_id=#{CLIENT_ID}&redirect_uri=#{ROOT_URl}/oschina/login_cb&response_type=code" end + + + + # 企业开通 # ecordercode 唯一标志一个企业的订购关系 def bs_new @@ -91,16 +96,12 @@ class EcloudController < ApplicationController def ecloud_login_callback #获取code - # - # - logger.debug params - url = "https://221.176.54.92:9081/restful/services/oauth2/authorization?grant_type=authorization_code" + + logger.info "oauth2 login_callback: #{params}" + url = "#{SERVER_URL}/oauth2/authorization?grant_type=authorization_code" + "&client_id=#{CLIENT_ID}&scope=&redirect_uri=&code=#{params[:code]}" - # url = "https://gitee.com/oauth/token?grant_type=authorization_code"+ - # "&code=#{params[:code]}&client_id=#{CLIENT_ID}&redirect_uri=#{ROOT_URl}/oschina/login_cb&client_secret=#{CLIENT_SECRET}" res = post(url) - logger.debug res + logger.info "oauth2 authorization resp: #{res}" body = decode(res) #{"access_token":"21a80f20ff736b54aecd002b60210943","token_type":"bearer","expires_in":86400,"refresh_token":"be92e2c137a8c6dd22f0d8c4a622b3aeceb054087a95d293130f04ec60fd3e3f","scope":"user_info","created_at":1542684088} @@ -110,17 +111,18 @@ class EcloudController < ApplicationController #获取此用户信息 # res = get("https://gitee.com/api/v5/user?access_token=#{body["access_token"]}") - res = get("https://221.176.54.92:9081/restful/services/user/info?access_token=#{body["access_token"]}&userid=%7bUSERID%7d") - logger.debug res + res = get("#{SERVER_URL}/user/info?access_token={ACCESS_TOKEN}&userid={USERID}") + logger.info "oauth2 get user info: #{res}" - info = decode(res) - - user = User.find_by_oschina_user_id(info["id"]) - unless user - user = User.create_with_oschina!(info) - end - - @current_user = user + # 同步用户 + # info = decode(res) + # + # user = User.find_by_oschina_user_id(info["id"]) + # unless user + # user = User.create_with_oschina!(info) + # end + # + # @current_user = user render :index end diff --git a/config/routes.rb b/config/routes.rb index 525290a5..e790aa4d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,7 +33,7 @@ RedmineApp::Application.routes.draw do ## oauth相关 match 'oauth/cb', to: 'oauth#test_callback', :via => :get match 'oauth/userinfo', to: 'oauth#get_userinfo', :via => :get - match 'ecloud/ecloud_login_callback', to: 'ecloud#ecloud_login_callback', :via => :get + match 'ecloud/ecloud_login', to: 'ecloud#ecloud_login_callback', :via => :get match 'ecloud/bs_new', to: 'ecloud#bs_new', :via => :post match 'ecloud/bs_edit', to: 'ecloud#bs_edit', :via => :post match 'ecloud/ps_new', to: 'ecloud#ps_new', :via => :post