From cfff4345454db06a262234922fe5d658a8932b88 Mon Sep 17 00:00:00 2001 From: guotao Date: Fri, 14 May 2021 09:10:36 +0800 Subject: [PATCH] =?UTF-8?q?Ccyun=E7=99=BB=E5=BD=95=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/concerns/register_helper.rb | 5 +- app/controllers/oauth/base_controller.rb | 5 ++ app/controllers/oauth/ccyun_controller.rb | 87 +++++++++++++++++++++ app/controllers/settings_controller.rb | 9 +++ app/libs/ccyun_oauth.rb | 18 +++++ app/libs/ccyun_oauth/error.rb | 14 ++++ app/libs/ccyun_oauth/service.rb | 33 ++++++++ app/models/open_users/ccyun.rb | 27 +++++++ app/models/user.rb | 2 +- app/views/settings/show.json.jbuilder | 4 + config/configuration.yml.example | 6 ++ config/initializers/ccyun_oauth_init.rb | 16 ++++ config/routes.rb | 2 + 13 files changed, 225 insertions(+), 3 deletions(-) create mode 100644 app/controllers/oauth/ccyun_controller.rb create mode 100644 app/libs/ccyun_oauth.rb create mode 100644 app/libs/ccyun_oauth/error.rb create mode 100644 app/libs/ccyun_oauth/service.rb create mode 100644 app/models/open_users/ccyun.rb create mode 100644 config/initializers/ccyun_oauth_init.rb diff --git a/app/controllers/concerns/register_helper.rb b/app/controllers/concerns/register_helper.rb index 3a23a210..b5d42322 100644 --- a/app/controllers/concerns/register_helper.rb +++ b/app/controllers/concerns/register_helper.rb @@ -3,13 +3,14 @@ module RegisterHelper def autologin_register(username, email, password, platform= 'forge') result = {message: nil, user: nil} - user = User.new(admin: false, login: username, mail: email, type: "User") user.password = password user.platform = platform user.activate + + ##puts "register user ===== #{user.valid?}" - return unless user.valid? + #return unless user.valid? interactor = Gitea::RegisterInteractor.call({username: username, email: email, password: password}) if interactor.success? diff --git a/app/controllers/oauth/base_controller.rb b/app/controllers/oauth/base_controller.rb index 3fe349bb..2d89f3d4 100644 --- a/app/controllers/oauth/base_controller.rb +++ b/app/controllers/oauth/base_controller.rb @@ -12,6 +12,11 @@ class Oauth::BaseController < ActionController::Base private + # 异常提醒 + def tip_exception(status = -1, message) + raise Educoder::TipException.new(status, message) + end + def session_user_id # session[:user_id] session[:"#{default_yun_session}"] diff --git a/app/controllers/oauth/ccyun_controller.rb b/app/controllers/oauth/ccyun_controller.rb new file mode 100644 index 00000000..a104efe3 --- /dev/null +++ b/app/controllers/oauth/ccyun_controller.rb @@ -0,0 +1,87 @@ +class Oauth::CcyunController < Oauth::BaseController + include RegisterHelper + + # def bind + # begin + # login = params[:login] + # mail = params[:mail] || nil + # callback_url = params[:callback_url] + # token = params[:token] + # + # ::OauthEducoderForm.new({login: login, token: token, callback_url: callback_url}).validate! + # + # open_user= OpenUsers::CCyun.find_by(uid: login) || OpenUsers::CCyun.find_by(uid: mail) + # + # if open_user.present? && open_user.user.present? && open_user.user.email_binded? + # Rails.logger.info "######## open_user exist and open_user.user exsit and email is binded ok" + # successful_authentication(open_user.user) + # + # redirect_to callback_url + # else + # Rails.logger.info "######## open user not exits" + # user = User.find_by(login: login) || User.find_by(mail: mail) + # + # if user.is_a?(User) && !user.is_a?(AnonymousUser) + # OpenUsers::Educoder.create!(user: user, uid: login) + # successful_authentication(user) + # + # redirect_to callback_url + # else + # redirect_to oauth_register_path(login: login, mail: mail, callback_url: callback_url) + # end + # end + # rescue WechatOauth::Error => ex + # render_error(ex.message) + # end + # end + + # 需要educoder那边设置回调地址 + def create + begin + code = params['code'].to_s.strip + tip_exception("code不能为空") if code.blank? + + Rails.logger.info "信创登录 ==============》#{code}" + + new_user = false + result = CcyunOauth::Service.access_token(code) + result = CcyunOauth::Service.user_info(result[:access_token]) + uid = result["mobile"] + + # 存在该用户 + open_user = OpenUsers::Ccyun.find_by(uid: uid) + Rails.logger.info("open_user #{open_user}") + if open_user.present? && open_user.user.present? + successful_authentication(open_user.user) + else + if current_user.blank? || !current_user.logged? + new_user = true + + # 使用XC提供的身份ID进行注册 + # login = result['personalId'].to_s + login = User.generate_login('E') + + # 自动注册到平台 + reg_result = autologin_register(login,"#{login}@forge.com", "Ec#{login}2021#", 'ccyun') + + Rails.logger.info("reg_result=====> #{reg_result}") + + if reg_result[:message].blank? + open_user = OpenUsers::Ccyun.create!(user_id: reg_result[:user][:id], uid: uid, extra: result) + # autosync_register_trustie(login, "Ec#{login}2021#", "#{login}@forge.com") + successful_authentication(open_user.user) + else + render_error(reg_result[:message]) + end + else + OpenUsers::Ccyun.create!(user: current_user, uid: uid, extra: result) + end + end + + #redirect_to root_path(new_user: new_user) + render_ok({}) + rescue Exception => ex + render_error(ex.message) + end + end +end diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index b6662f66..26e88c5e 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -1,6 +1,7 @@ class SettingsController < ApplicationController def show @old_projects_url = nil + get_third_party get_add_menu get_common_menu get_personal_menu @@ -8,6 +9,14 @@ class SettingsController < ApplicationController end private + def get_third_party + @third_party = [] + @third_party << { + name: 'ccyun', + url: CcyunOauth.oauth_url + } + end + def get_add_menu @add = [] Site.add.select(:id, :name, :url, :key).to_a.map(&:serializable_hash).each do |site| diff --git a/app/libs/ccyun_oauth.rb b/app/libs/ccyun_oauth.rb new file mode 100644 index 00000000..51914894 --- /dev/null +++ b/app/libs/ccyun_oauth.rb @@ -0,0 +1,18 @@ +module CcyunOauth + class << self + attr_accessor :client_id, :client_secret, :base_url, :redirect_uri + + def logger + @_logger ||= STDOUT + end + + def logger=(l) + @_logger = l + end + + def oauth_url + "#{base_url}/oauth/authorize?client_id=#{client_id}&redirect_uri=#{URI.encode_www_form_component(redirect_uri)}&response_type=code&state=1" + end + + end +end \ No newline at end of file diff --git a/app/libs/ccyun_oauth/error.rb b/app/libs/ccyun_oauth/error.rb new file mode 100644 index 00000000..895c8389 --- /dev/null +++ b/app/libs/ccyun_oauth/error.rb @@ -0,0 +1,14 @@ +class CcyunOauth::Error < StandardError + attr_reader :code + + def initialize(code=-1, msg) + super(msg) + @code = code + end + + # def message + # I18n.t("oauth.ccyun.#{code}") + # rescue I18n::MissingTranslationData + # super + # end +end \ No newline at end of file diff --git a/app/libs/ccyun_oauth/service.rb b/app/libs/ccyun_oauth/service.rb new file mode 100644 index 00000000..be898358 --- /dev/null +++ b/app/libs/ccyun_oauth/service.rb @@ -0,0 +1,33 @@ +require 'oauth2' + +module CcyunOauth::Service + module_function + + def request(method, url, params) + begin + Rails.logger.info("[CcyunOauth] [#{method.to_s.upcase}] #{url} || #{params}") + + response = Faraday.get(url, params, nil) + Rails.logger.info("[CcyunOauth] [response] #{response.body}") + result = JSON.parse(response.body) + result + rescue Exception => e + raise CcyunOauth::Error.new(result['error'], result['error_description']) + end + end + + def access_token(code) + begin + Rails.logger.info("[CcyunOauth] [code] #{code} ") + client = OAuth2::Client.new(CcyunOauth.client_id, CcyunOauth.client_secret, site: CcyunOauth.base_url) + result = client.auth_code.get_token(code, redirect_uri: CcyunOauth.redirect_uri).to_hash + return result + rescue Exception => e + raise CcyunOauth::Error.new(e.message) + end + end + + def user_info(access_token) + request(:get, "#{CcyunOauth.base_url}/user-api/userInfo", {access_token: access_token}) + end +end \ No newline at end of file diff --git a/app/models/open_users/ccyun.rb b/app/models/open_users/ccyun.rb new file mode 100644 index 00000000..c49fa5eb --- /dev/null +++ b/app/models/open_users/ccyun.rb @@ -0,0 +1,27 @@ +# == Schema Information +# +# Table name: open_users +# +# id :integer not null, primary key +# user_id :integer +# type :string(255) +# uid :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# extra :text(65535) +# +# Indexes +# +# index_open_users_on_type_and_uid (type,uid) UNIQUE +# index_open_users_on_user_id (user_id) +# + +class OpenUsers::Ccyun < OpenUser + def nickname + extra&.[]('nickname') + end + + def en_type + 'ccyun' + end +end diff --git a/app/models/user.rb b/app/models/user.rb index e16e5ca0..25e6bcb4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -112,7 +112,7 @@ class User < Owner # trustie: 来自Trustie平台 # forge: 平台本身注册的用户 # military: 军科的用户 - enumerize :platform, in: [:forge, :educoder, :trustie, :military], default: :forge, scope: :shallow + enumerize :platform, in: [:forge, :educoder, :trustie, :military, :ccyun], default: :forge, scope: :shallow belongs_to :laboratory, optional: true has_many :composes, dependent: :destroy diff --git a/app/views/settings/show.json.jbuilder b/app/views/settings/show.json.jbuilder index 330966aa..58cd597a 100644 --- a/app/views/settings/show.json.jbuilder +++ b/app/views/settings/show.json.jbuilder @@ -55,5 +55,9 @@ json.setting do json.array! @personal end + json.third_party do + json.array! @third_party + end + json.common @common end diff --git a/config/configuration.yml.example b/config/configuration.yml.example index 823d8547..087062c7 100644 --- a/config/configuration.yml.example +++ b/config/configuration.yml.example @@ -43,6 +43,12 @@ default: &default cate_id: '-1' callback_url: 'callback_url' signature_key: 'test12345678' + ccyun: + client_id: 'trustie' + client_secret: 'trustie' + scope: 'read' + base_url: 'http://localhost:8080' + redirect_uri: 'http://localhost:3030/loginxc' gitea: access_key_id: 'root' diff --git a/config/initializers/ccyun_oauth_init.rb b/config/initializers/ccyun_oauth_init.rb new file mode 100644 index 00000000..9b846f9b --- /dev/null +++ b/config/initializers/ccyun_oauth_init.rb @@ -0,0 +1,16 @@ +oauth_config = {} +begin + config = Rails.application.config_for(:configuration) + oauth_config = config.dig('oauth', 'ccyun') + raise 'oauth ccyun config missing' if oauth_config.blank? +rescue => ex + raise ex if Rails.env.production? + + puts %Q{\033[33m [warning] ccyun oauth config or configuration.yml missing, + please add it or execute 'cp config/configuration.yml.example config/configuration.yml' \033[0m} +end + +CcyunOauth.client_id = oauth_config['client_id'] +CcyunOauth.client_secret = oauth_config['client_secret'] +CcyunOauth.base_url = oauth_config['base_url'] +CcyunOauth.redirect_uri = oauth_config['redirect_uri'] diff --git a/config/routes.rb b/config/routes.rb index bbcc2923..414c45c1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -314,6 +314,8 @@ Rails.application.routes.draw do get '/auth/qq/callback', to: 'oauth/qq#create' get '/auth/wechat/callback', to: 'oauth/wechat#create' + get '/auth/ccyun/callback', to: 'oauth/ccyun#create' + resource :bind_user, only: [:create] resources :hot_keywords, only: [:index]