dev_SaaS
jingquan huang 6 years ago
parent c8e7166d02
commit 9cf02eaf28

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
// Place all the styles related to the ecloud controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

@ -0,0 +1,97 @@
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

@ -0,0 +1,2 @@
module EcloudHelper
end

@ -1,6 +1,7 @@
class EcloudService
def list
return {status: 0, message: "test"}
return {status: 0, message: "test", code: params[:code], params: params}
end
end

@ -32,6 +32,8 @@ 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
resources :ec_course_evaluations do
member do

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe EcloudController, :type => :controller do
end
Loading…
Cancel
Save