From 0635c1e98b80dc1225695e5f405340bb1ecfc6cf Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 4 May 2016 14:05:46 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E7=82=B9?= =?UTF-8?q?=E7=99=BB=E5=BD=95controller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 +- app/controllers/sso_controller.rb | 37 +++++++++++++++++++++++++++++++ config/routes.rb | 2 ++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 app/controllers/sso_controller.rb diff --git a/Gemfile b/Gemfile index 0c3fdc880..0174d73ac 100644 --- a/Gemfile +++ b/Gemfile @@ -45,11 +45,11 @@ gem 'elasticsearch-rails' ### profile #gem 'oneapm_rpm' -#gem "query_reviewer", group: :development group :development do gem 'grape-swagger' gem 'better_errors', '~> 1.1.0' + gem "query_reviewer" # gem 'rack-mini-profiler', '~> 0.9.3' if RUBY_PLATFORM =~ /w32/ gem 'win32console' diff --git a/app/controllers/sso_controller.rb b/app/controllers/sso_controller.rb new file mode 100644 index 000000000..0ae7b7449 --- /dev/null +++ b/app/controllers/sso_controller.rb @@ -0,0 +1,37 @@ +#coding=utf-8 + +require 'base64' +require 'json' +require 'openssl' + +## 单点登录 <=> 北斗 +class SsoController < ApplicationController + + def index + options = parse(params[:auth]) + + logger.debug options + + ## 认证 + login(options[:id]) + + ## 选择性跳转 + + redirect_to Organization.find(82) + + end + + private + def parse(auth) + crypted_str = Base64.decode64(auth) + pkey = OpenSSL::PKey::RSA.new(File.new(File.join(Rails.root,"config/private.key"))) + content = pkey.private_decrypt(pwd,OpenSSL::PKey::RSA::NO_PADDING) + JSON.parser(content) + end + + def login(id) + + start_user_session(user) + end + +end diff --git a/config/routes.rb b/config/routes.rb index 9599c705a..0592041f6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1197,6 +1197,8 @@ RedmineApp::Application.routes.draw do end end + get '/sso', to: 'sso#index' + get '/:sub_dir_name', :to => 'org_subfields#show', :as => 'show_subfield_without_id' Dir.glob File.expand_path("plugins/*", Rails.root) do |plugin_dir| From e8adcf8cc7edf24cb0dd1087f9ecc8d26d0c5b34 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 4 May 2016 15:40:18 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0sso=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/sso_controller.rb | 4 ++-- app/models/sso.rb | 4 ++++ app/models/user.rb | 2 ++ db/migrate/20160504060751_create_ssos.rb | 16 +++++++++++++++ db/schema.rb | 25 ++++++++++++++---------- 5 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 app/models/sso.rb create mode 100644 db/migrate/20160504060751_create_ssos.rb diff --git a/app/controllers/sso_controller.rb b/app/controllers/sso_controller.rb index 0ae7b7449..2a7ac6db3 100644 --- a/app/controllers/sso_controller.rb +++ b/app/controllers/sso_controller.rb @@ -30,8 +30,8 @@ class SsoController < ApplicationController end def login(id) - - start_user_session(user) + sso = Sso.find(id) + start_user_session(sso.user) end end diff --git a/app/models/sso.rb b/app/models/sso.rb new file mode 100644 index 000000000..d7cf54dc3 --- /dev/null +++ b/app/models/sso.rb @@ -0,0 +1,4 @@ +class Sso < ActiveRecord::Base + belongs_to :user + attr_accessible :email, :name, :openid, :password, :school, :sex +end diff --git a/app/models/user.rb b/app/models/user.rb index dc7a3e741..9b8cb2b8d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -181,6 +181,8 @@ class User < Principal has_one :user_wechat + has_one :sso + # add by zjc has_one :level, :class_name => 'UserLevels', :dependent => :destroy has_many :memos , :foreign_key => 'author_id' diff --git a/db/migrate/20160504060751_create_ssos.rb b/db/migrate/20160504060751_create_ssos.rb new file mode 100644 index 000000000..a01a7fe35 --- /dev/null +++ b/db/migrate/20160504060751_create_ssos.rb @@ -0,0 +1,16 @@ +class CreateSsos < ActiveRecord::Migration + def change + create_table :ssos do |t| + t.references :user + t.string :openid + t.string :name + t.string :password + t.string :email + t.integer :sex + t.string :school + + t.timestamps + end + add_index :ssos, :user_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 3d3a54105..677b030df 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20160429030819) do +ActiveRecord::Schema.define(:version => 20160504060751) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -1673,15 +1673,6 @@ ActiveRecord::Schema.define(:version => 20160429030819) do t.datetime "updated_at", :null => false end - create_table "shield_wechats", :force => true do |t| - t.string "container_type" - t.integer "container_id" - t.string "shield_type" - t.integer "shield_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false - end - create_table "softapplications", :force => true do |t| t.string "name" t.text "description" @@ -1700,6 +1691,20 @@ ActiveRecord::Schema.define(:version => 20160429030819) do t.integer "project_id" end + create_table "ssos", :force => true do |t| + t.integer "user_id" + t.string "openid" + t.string "name" + t.string "password" + t.string "email" + t.integer "sex" + t.string "school" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "ssos", ["user_id"], :name => "index_ssos_on_user_id" + create_table "student_work_projects", :force => true do |t| t.integer "homework_common_id" t.integer "student_work_id" From ff23df4f12d86dce73ada7c7c6c39c0801b598a2 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 4 May 2016 16:21:53 +0800 Subject: [PATCH 03/10] =?UTF-8?q?oss=E4=B9=8B=E5=89=8D=E4=B8=8D=E7=94=A8?= =?UTF-8?q?=E6=A3=80=E9=AA=8C=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/sso_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/sso_controller.rb b/app/controllers/sso_controller.rb index 2a7ac6db3..b4b2866a9 100644 --- a/app/controllers/sso_controller.rb +++ b/app/controllers/sso_controller.rb @@ -7,6 +7,8 @@ require 'openssl' ## 单点登录 <=> 北斗 class SsoController < ApplicationController + skip_before_filter :check_if_login_required + def index options = parse(params[:auth]) From 250ed8c981d3aa78827566172e8f87f5b60add59 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 4 May 2016 16:33:56 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/private.key | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 config/private.key diff --git a/config/private.key b/config/private.key new file mode 100644 index 000000000..3c3adf666 --- /dev/null +++ b/config/private.key @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXQIBAAKBgQC3//sR2tXw0wrC2DySx8vNGlqt3Y7ldU9+LBLI6e1KS5lfc5jl +TGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2klBd6h4wrbbHA2XE1sq21ykja/ +Gqx7/IRia3zQfxGv/qEkyGOx+XALVoOlZqDwh76o2n1vP1D+tD3amHsK7QIDAQAB +AoGBAKH14bMitESqD4PYwODWmy7rrrvyFPEnJJTECLjvKB7IkrVxVDkp1XiJnGKH +2h5syHQ5qslPSGYJ1M/XkDnGINwaLVHVD3BoKKgKg1bZn7ao5pXT+herqxaVwWs6 +ga63yVSIC8jcODxiuvxJnUMQRLaqoF6aUb/2VWc2T5MDmxLhAkEA3pwGpvXgLiWL +3h7QLYZLrLrbFRuRN4CYl4UYaAKokkAvZly04Glle8ycgOc2DzL4eiL4l/+x/gaq +deJU/cHLRQJBANOZY0mEoVkwhU4bScSdnfM6usQowYBEwHYYh/OTv1a3SqcCE1f+ +qbAclCqeNiHajCcDmgYJ53LfIgyv0wCS54kCQAXaPkaHclRkQlAdqUV5IWYyJ25f +oiq+Y8SgCCs73qixrU1YpJy9yKA/meG9smsl4Oh9IOIGI+zUygh9YdSmEq0CQQC2 +4G3IP2G3lNDRdZIm5NZ7PfnmyRabxk/UgVUWdk47IwTZHFkdhxKfC8QepUhBsAHL +QjifGXY4eJKUBm3FpDGJAkAFwUxYssiJjvrHwnHFbg0rFkvvY63OSmnRxiL4X6EY +yI9lblCsyfpl25l7l5zmJrAHn45zAiOoBrWqpM5edu7c +-----END RSA PRIVATE KEY----- \ No newline at end of file From e9834f108a883a3d75b856d398480dc6cb201b4a Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 4 May 2016 16:35:39 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9pwd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/sso_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/sso_controller.rb b/app/controllers/sso_controller.rb index b4b2866a9..7a1433873 100644 --- a/app/controllers/sso_controller.rb +++ b/app/controllers/sso_controller.rb @@ -27,7 +27,8 @@ class SsoController < ApplicationController def parse(auth) crypted_str = Base64.decode64(auth) pkey = OpenSSL::PKey::RSA.new(File.new(File.join(Rails.root,"config/private.key"))) - content = pkey.private_decrypt(pwd,OpenSSL::PKey::RSA::NO_PADDING) + # content = pkey.private_decrypt(pwd,OpenSSL::PKey::RSA::NO_PADDING) + content = pkey.private_decrypt(crypted_str) JSON.parser(content) end From eaad006d17815a3562372d7b6b855bb6e11c3149 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 4 May 2016 16:54:12 +0800 Subject: [PATCH 06/10] safe base64 --- app/controllers/sso_controller.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/sso_controller.rb b/app/controllers/sso_controller.rb index 7a1433873..23da4678f 100644 --- a/app/controllers/sso_controller.rb +++ b/app/controllers/sso_controller.rb @@ -24,11 +24,16 @@ class SsoController < ApplicationController end private + def base64_safe(content) + content = content.gsub('-', '+') + content.gsub('_', '/') + end + def parse(auth) - crypted_str = Base64.decode64(auth) + crypted_str = Base64.decode64(base64_safe(auth)) pkey = OpenSSL::PKey::RSA.new(File.new(File.join(Rails.root,"config/private.key"))) - # content = pkey.private_decrypt(pwd,OpenSSL::PKey::RSA::NO_PADDING) - content = pkey.private_decrypt(crypted_str) + content = pkey.private_decrypt(crypted_str,OpenSSL::PKey::RSA::PKCS1_PADDING) + # content = pkey.private_decrypt(crypted_str) JSON.parser(content) end From df41f7328f550e8156c2014d09d58adc4b7fee37 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 4 May 2016 17:25:24 +0800 Subject: [PATCH 07/10] =?UTF-8?q?json=E8=A7=A3=E6=9E=90=E6=8D=A2=E6=88=90r?= =?UTF-8?q?ails=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/sso_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/sso_controller.rb b/app/controllers/sso_controller.rb index 23da4678f..73982ae9c 100644 --- a/app/controllers/sso_controller.rb +++ b/app/controllers/sso_controller.rb @@ -34,7 +34,7 @@ class SsoController < ApplicationController pkey = OpenSSL::PKey::RSA.new(File.new(File.join(Rails.root,"config/private.key"))) content = pkey.private_decrypt(crypted_str,OpenSSL::PKey::RSA::PKCS1_PADDING) # content = pkey.private_decrypt(crypted_str) - JSON.parser(content) + ActiveSupport::JSON.decode(content) end def login(id) From dfdd301f2fc9a6795b616be57e57d04cbe2a7c4e Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Thu, 5 May 2016 14:06:47 +0800 Subject: [PATCH 08/10] =?UTF-8?q?sso=E5=90=8C=E6=AD=A5=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 +- app/controllers/sso_controller.rb | 45 -------------------- app/controllers/ssos_controller.rb | 66 ++++++++++++++++++++++++++++++ app/models/sso.rb | 54 +++++++++++++++++++++++- app/views/ssos/show.html.erb | 64 +++++++++++++++++++++++++++++ config/routes.rb | 2 +- 6 files changed, 185 insertions(+), 48 deletions(-) delete mode 100644 app/controllers/sso_controller.rb create mode 100644 app/controllers/ssos_controller.rb create mode 100644 app/views/ssos/show.html.erb diff --git a/Gemfile b/Gemfile index 0174d73ac..f3b06c2d4 100644 --- a/Gemfile +++ b/Gemfile @@ -49,7 +49,7 @@ gem 'elasticsearch-rails' group :development do gem 'grape-swagger' gem 'better_errors', '~> 1.1.0' - gem "query_reviewer" + # gem "query_reviewer" # gem 'rack-mini-profiler', '~> 0.9.3' if RUBY_PLATFORM =~ /w32/ gem 'win32console' diff --git a/app/controllers/sso_controller.rb b/app/controllers/sso_controller.rb deleted file mode 100644 index 73982ae9c..000000000 --- a/app/controllers/sso_controller.rb +++ /dev/null @@ -1,45 +0,0 @@ -#coding=utf-8 - -require 'base64' -require 'json' -require 'openssl' - -## 单点登录 <=> 北斗 -class SsoController < ApplicationController - - skip_before_filter :check_if_login_required - - def index - options = parse(params[:auth]) - - logger.debug options - - ## 认证 - login(options[:id]) - - ## 选择性跳转 - - redirect_to Organization.find(82) - - end - - private - def base64_safe(content) - content = content.gsub('-', '+') - content.gsub('_', '/') - end - - def parse(auth) - crypted_str = Base64.decode64(base64_safe(auth)) - pkey = OpenSSL::PKey::RSA.new(File.new(File.join(Rails.root,"config/private.key"))) - content = pkey.private_decrypt(crypted_str,OpenSSL::PKey::RSA::PKCS1_PADDING) - # content = pkey.private_decrypt(crypted_str) - ActiveSupport::JSON.decode(content) - end - - def login(id) - sso = Sso.find(id) - start_user_session(sso.user) - end - -end diff --git a/app/controllers/ssos_controller.rb b/app/controllers/ssos_controller.rb new file mode 100644 index 000000000..ee48bea9c --- /dev/null +++ b/app/controllers/ssos_controller.rb @@ -0,0 +1,66 @@ +#coding=utf-8 + +require 'base64' +require 'json' +require 'openssl' + +## 单点登录 <=> 北斗 +class SsosController < ApplicationController + + skip_before_filter :check_if_login_required + layout false + + def show + begin + # suRh2nFEJd0Ai_TFbqZ-1yQXnGfIB-YD_f4KTA3O4dQGSBMiXfOMt-0mzizgXekWTjHKfn62nJ60iHM3_eY_KS0Qn8SF8vANfa46GhzZRt4T0iC5ZOSs4cWeK43OU0RoekQLZZAo5OyOVibxabmiPGzEFCnVVtdmRk9d7X_B0Is= + @auth = params[:auth] + @options = parse(params[:auth]) + + if params[:login].present? + @options["name"] = params[:login] + end + + logger.debug @options + ## 认证 + login(@options) + + logger.debug "login over" + + ## 选择性跳转 + + redirect_to Organization.find(1) + rescue => e + logger.error e + if e.message == "exist user" + render 'ssos/show', :layout => false + else + raise e + end + end + end + + ## 改用户名 + def create + show and return + end + + private + def base64_safe(content) + content = content.gsub('-', '+') + content.gsub('_', '/') + end + + def parse(auth) + crypted_str = Base64.decode64(base64_safe(auth)) + pkey = OpenSSL::PKey::RSA.new(File.new(File.join(Rails.root,"config/private.key"))) + content = pkey.private_decrypt(crypted_str,OpenSSL::PKey::RSA::PKCS1_PADDING) + # content = pkey.private_decrypt(crypted_str) + ActiveSupport::JSON.decode(content) + end + + def login(opt) + sso = Sso.sync_user(opt) + start_user_session(sso.user) + end + +end diff --git a/app/models/sso.rb b/app/models/sso.rb index d7cf54dc3..c6b57cd2d 100644 --- a/app/models/sso.rb +++ b/app/models/sso.rb @@ -1,4 +1,56 @@ class Sso < ActiveRecord::Base belongs_to :user - attr_accessible :email, :name, :openid, :password, :school, :sex + attr_accessible :email, :name, :openid, :password, :school, :sex, :user, :user_id + + validates :user_id, :user, :email, :openid, :presence => true + + def self.sync_user(opt) + sso = Sso.where(openid: opt["openid"]).first + return sso if sso + + sso = Sso.new + sso.name = opt["name"] + sso.openid = opt["openid"] + sso.email = opt["email"] + sso.password = opt["password"] + sso.school = opt["school"] + sso.sex = opt["sex"] + + + # 查邮箱 + user = User.where(mail: opt["email"]).first + + unless user + # 查用户名 + + user = User.where(login: opt["name"]).first + if user + # 跳到修改用户名 + raise "exist user" + end + + password = opt["password"] + if password.size < 8 + password = random_pwd + end + us = UsersService.new + user = us.register(login: opt["name"], mail: opt["email"], + password: password, + :should_confirmation_password => false) + if user.new_record? + raise user.errors.full_messages.first + end + end + + sso.user = user + sso.save! + return sso + end + + + private + def self.random_pwd + ('a'..'z').to_a.shuffle[0..7].join + end + end diff --git a/app/views/ssos/show.html.erb b/app/views/ssos/show.html.erb new file mode 100644 index 000000000..456cbe154 --- /dev/null +++ b/app/views/ssos/show.html.erb @@ -0,0 +1,64 @@ + + + + + 跳转页面 + + + + +
+<%= form_tag('/sso') do -%> +
+

请修改您的资料,以确保Trustie为您提供更便捷的服务

+ + + + + + + + + + + + + + + + + +
用户名: + " name="login"/> +
已存在相同用户名,请选择更合适的用户名
邮箱: + " disabled="disabled" /> +
+
+ +<%= hidden_field_tag 'auth', @auth %> +<% end -%> + + + diff --git a/config/routes.rb b/config/routes.rb index 0592041f6..e1c969b0f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1197,7 +1197,7 @@ RedmineApp::Application.routes.draw do end end - get '/sso', to: 'sso#index' + resource :sso, only: [:show, :create] get '/:sub_dir_name', :to => 'org_subfields#show', :as => 'show_subfield_without_id' From 17282083a673b3b407982a75e028eab583930ecb Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Thu, 5 May 2016 14:07:37 +0800 Subject: [PATCH 09/10] merge --- app/controllers/ssos_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/ssos_controller.rb b/app/controllers/ssos_controller.rb index ee48bea9c..f6396f7b3 100644 --- a/app/controllers/ssos_controller.rb +++ b/app/controllers/ssos_controller.rb @@ -28,7 +28,7 @@ class SsosController < ApplicationController ## 选择性跳转 - redirect_to Organization.find(1) + redirect_to Organization.find(82) rescue => e logger.error e if e.message == "exist user" From 72f5f5b795a9bf4e1b1f7b5d7b2977ae20a32af2 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Thu, 5 May 2016 14:52:10 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E7=BB=84=E7=BB=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/ssos_controller.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controllers/ssos_controller.rb b/app/controllers/ssos_controller.rb index f6396f7b3..1e1bd932b 100644 --- a/app/controllers/ssos_controller.rb +++ b/app/controllers/ssos_controller.rb @@ -22,13 +22,17 @@ class SsosController < ApplicationController logger.debug @options ## 认证 - login(@options) + sso = login(@options) - logger.debug "login over" + ## 加入组织 + @organization = Organization.find(82) + unless @organization.org_members.exists?(user_id: sso.user_id) + member = OrgMember.new(:user_id => sso.user_id) + @organization.org_members << member + end ## 选择性跳转 - - redirect_to Organization.find(82) + redirect_to @organization rescue => e logger.error e if e.message == "exist user" @@ -61,6 +65,7 @@ class SsosController < ApplicationController def login(opt) sso = Sso.sync_user(opt) start_user_session(sso.user) + sso end end