Ccyun登录功能实现
This commit is contained in:
parent
b161b66d79
commit
cfff434545
|
@ -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?
|
||||
|
|
|
@ -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}"]
|
||||
|
|
|
@ -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
|
|
@ -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|
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -55,5 +55,9 @@ json.setting do
|
|||
json.array! @personal
|
||||
end
|
||||
|
||||
json.third_party do
|
||||
json.array! @third_party
|
||||
end
|
||||
|
||||
json.common @common
|
||||
end
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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']
|
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue