|
|
|
|
require 'net/http'
|
|
|
|
|
|
|
|
|
|
class EcloudController < ApplicationController
|
|
|
|
|
def index
|
|
|
|
|
|
|
|
|
|
render file: 'public/react-oschina/build/index.html', :layout => false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def trustie_login
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
CLIENT_ID = 'e5da9855f89bc724a335d100cb63cf02a03a592bd3151bbc84acf7b2e222ddb8'
|
|
|
|
|
CLIENT_SECRET = '4f2f291fac1d3dae338c18a3e3544814be5a1c4ade9e72d62f45ceab914c89f5'
|
|
|
|
|
ROOT_URl = 'http://localhost:3000'
|
|
|
|
|
|
|
|
|
|
def oschina_login
|
|
|
|
|
# 根据session,看看有没有存access_token,去刷新下。
|
|
|
|
|
# 1. 如果过期,则跳转
|
|
|
|
|
# 2. 未过期,直接用
|
|
|
|
|
|
|
|
|
|
redirect_to "https://gitee.com/oauth/authorize?client_id=#{CLIENT_ID}&redirect_uri=#{ROOT_URl}/oschina/login_cb&response_type=code"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ecloud_login_callback
|
|
|
|
|
#获取code
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
logger.debug params
|
|
|
|
|
url = "https://221.176.54.92:9081/restful/services/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
|
|
|
|
|
|
|
|
|
|
body = decode(res)
|
|
|
|
|
#{"access_token":"21a80f20ff736b54aecd002b60210943","token_type":"bearer","expires_in":86400,"refresh_token":"be92e2c137a8c6dd22f0d8c4a622b3aeceb054087a95d293130f04ec60fd3e3f","scope":"user_info","created_at":1542684088}
|
|
|
|
|
|
|
|
|
|
raise '登录失败' unless body["access_token"]
|
|
|
|
|
|
|
|
|
|
#获取此用户信息
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
def get(url)
|
|
|
|
|
uri = URI(url)
|
|
|
|
|
res = Net::HTTP.start(uri.host, uri.port, use_ssl: url.start_with?('https')) do |http|
|
|
|
|
|
req = Net::HTTP::Get.new(uri)
|
|
|
|
|
#req['Content-Type'] = 'application/json'
|
|
|
|
|
# The body needs to be a JSON string, use whatever you know to parse Hash to JSON
|
|
|
|
|
#req.body = {a: 1}.to_json
|
|
|
|
|
http.request(req)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
res.body
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def post(url)
|
|
|
|
|
uri = URI(url)
|
|
|
|
|
res = Net::HTTP.start(uri.host, uri.port, use_ssl: url.start_with?('https')) do |http|
|
|
|
|
|
req = Net::HTTP::Post.new(uri)
|
|
|
|
|
#req['Content-Type'] = 'application/json'
|
|
|
|
|
# The body needs to be a JSON string, use whatever you know to parse Hash to JSON
|
|
|
|
|
#req.body = {a: 1}.to_json
|
|
|
|
|
http.request(req)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
res.body
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def decode(s)
|
|
|
|
|
begin
|
|
|
|
|
obj = ActiveSupport::JSON.decode(s)
|
|
|
|
|
rescue ActiveSupport::JSON.parse_error
|
|
|
|
|
logger.error("Attempted to decode invalid JSON: #{s}")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|