diff --git a/app/assets/javascripts/ecloud.js.coffee b/app/assets/javascripts/ecloud.js.coffee new file mode 100644 index 00000000..76156794 --- /dev/null +++ b/app/assets/javascripts/ecloud.js.coffee @@ -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/ diff --git a/app/assets/stylesheets/ecloud.css.scss b/app/assets/stylesheets/ecloud.css.scss new file mode 100644 index 00000000..2152c821 --- /dev/null +++ b/app/assets/stylesheets/ecloud.css.scss @@ -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/ diff --git a/app/controllers/ecloud_controller.rb b/app/controllers/ecloud_controller.rb new file mode 100644 index 00000000..a8f9a32a --- /dev/null +++ b/app/controllers/ecloud_controller.rb @@ -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 diff --git a/app/helpers/ecloud_helper.rb b/app/helpers/ecloud_helper.rb new file mode 100644 index 00000000..3a825b80 --- /dev/null +++ b/app/helpers/ecloud_helper.rb @@ -0,0 +1,2 @@ +module EcloudHelper +end diff --git a/app/services/ecloud_service.rb b/app/services/ecloud_service.rb index 7371b959..b35f05e6 100644 --- a/app/services/ecloud_service.rb +++ b/app/services/ecloud_service.rb @@ -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 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ff0b4ebc..3fe67be8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/ecloud_controller_spec.rb b/spec/controllers/ecloud_controller_spec.rb new file mode 100644 index 00000000..87513db2 --- /dev/null +++ b/spec/controllers/ecloud_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe EcloudController, :type => :controller do + +end