From 77d2f3ac179d7583eca397cf13121e41472b8541 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Thu, 22 Nov 2018 14:55:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?oauth=E7=94=9F=E6=88=90token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/oauth_controller.rb | 37 +++++++++++++--- app/models/oauth.rb | 43 ++++++++++++++++++- config/routes.rb | 1 + .../20181122034752_add_user_id_to_oauths.rb | 7 +++ db/schema.rb | 9 ++-- 5 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20181122034752_add_user_id_to_oauths.rb diff --git a/app/controllers/oauth_controller.rb b/app/controllers/oauth_controller.rb index efc28bfa8..3dd9a85f5 100644 --- a/app/controllers/oauth_controller.rb +++ b/app/controllers/oauth_controller.rb @@ -1,6 +1,12 @@ class OauthController < ApplicationController + + include ApplicationHelper + before_filter :user_setup - before_filter :require_login, only: [:authorize, :token] + before_filter :require_login, only: [:authorize,:token] + + + skip_before_filter :verify_authenticity_token, only: [:token] # 客户端申请认证的URI,包含以下参数: # @@ -50,8 +56,7 @@ class OauthController < ApplicationController client_id = "88d893c5a345313e7b8c6fcf23d3d024ee08d5e41ce120c3448b6eea77d8de30" client_secret = "e9240cc5fc913741db5aea93f2986a8ea0631bb67f7c00e41e491b95d9619e64" redirect_uri = "http://localhost:3000/oauth/cb" - url = "http://127.0.0.1:3000/oauth/token?grant_type=authorization_code&code=#{params['code']}" - +"&redirect_uri=#{redirect_uri}&client_id=#{client_id}&client_secret=#{client_secret}" + url = "http://127.0.0.1:3000/oauth/token?grant_type=authorization_code&code=#{params['code']}&redirect_uri=#{redirect_uri}&client_id=#{client_id}&client_secret=#{client_secret}" render text: url end @@ -76,6 +81,8 @@ class OauthController < ApplicationController # refresh_token:表示更新令牌,用来获取下一次的访问令牌,可选项。 # scope:表示权限范围,如果与客户端申请的范围一致,此项可省略。 def token + + res = {} if params[:grant_type] == 'authorization_code' raise "code必传" unless params["code"] @@ -84,15 +91,16 @@ class OauthController < ApplicationController raise "code错误或已超时" unless Oauth.code_valid?(params["code"]) - oauth = Oauth.auth(params["code"], params["client_id"], params["client_secret"]) + oauth = Oauth.auth_code(params["code"], params["client_id"], params["client_secret"]) raise "认证不通过" unless oauth ## 生成 token # oauth.gen_token(User.current.id) + oauth.reload - { + res = { access_token: oauth.access_token, token_type: 'bearer', expires_in: oauth.token_expires_in, @@ -101,6 +109,25 @@ class OauthController < ApplicationController end + render json: res.to_json + end + + + def get_userinfo + user = Oauth.auth(params["access_token"]) + + user_info = {} + if user + user_info = { + token: user.id, + login: user.login, + avatar_url: "/images/"+url_to_avatar(user), + name: user.show_name, + email: user.mail + } + end + + render json: user_info.to_json end diff --git a/app/models/oauth.rb b/app/models/oauth.rb index 5b369be68..23f5f835c 100644 --- a/app/models/oauth.rb +++ b/app/models/oauth.rb @@ -1,7 +1,10 @@ require 'base64' class Oauth < ActiveRecord::Base - attr_accessible :client_id, :client_secret, :redirect_uri + attr_accessible :client_id, :client_secret, :redirect_uri, :access_token, + :refresh_token, :token_created_at,:token_expires_in, :user_id + + belongs_to :user def gen_code @@ -10,5 +13,43 @@ class Oauth < ActiveRecord::Base code end + def gen_token(user_id) + access_token = Digest::MD5.hexdigest "#{Time.now}-#{Random.new_seed}" + refresh_token = Digest::MD5.hexdigest "#{Random.new_seed}-#{Time.now}-#{Random.new_seed}" + + self.update_attributes(access_token: access_token, + refresh_token: refresh_token, + token_created_at: Time.now.to_i, + token_expires_in: Time.now.to_i + 24*60*60, + user_id: user_id + ) + end + + + def self.code_valid?(code) + # 1. 是否存在 + oauth = Oauth.where(code: code).order("ID desc").first + return false unless oauth + + # 2. 是否超过10分钟 + return false if Time.now.to_i - oauth.created_at.to_i > 10*60 + + # 3. 是否有使用过 + return false if oauth.access_token.present? + + return true + end + + + def self.auth_code(code, client_id, client_secret) + Oauth.where(code: code, client_id: client_id, client_secret: client_secret).order('id desc').first + end + + def self.auth(access_token) + oauth = self.find_by_access_token(access_token) + return nil unless oauth + oauth.user + end + end diff --git a/config/routes.rb b/config/routes.rb index b4a9efacc..7b6830322 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -562,6 +562,7 @@ RedmineApp::Application.routes.draw do match 'oauth/authorize', to: 'oauth#authorize', :via => [:get, :post] match 'oauth/token', to: 'oauth#token', :via => :post match 'oauth/cb', to: 'oauth#test_callback', :via => :get + match 'oauth/userinfo', to: 'oauth#get_userinfo', :via => :get # boards match 'boards/:board_id/topics/new', :to => 'messages#new', :via => [:get, :post], :as => 'new_board_message' diff --git a/db/migrate/20181122034752_add_user_id_to_oauths.rb b/db/migrate/20181122034752_add_user_id_to_oauths.rb new file mode 100644 index 000000000..a28d2fa4c --- /dev/null +++ b/db/migrate/20181122034752_add_user_id_to_oauths.rb @@ -0,0 +1,7 @@ +class AddUserIdToOauths < ActiveRecord::Migration + def change + add_column :oauths, :user_id, :integer, default: 0 + + add_index :oauths, :user_id + end +end diff --git a/db/schema.rb b/db/schema.rb index ba8c9e915..a8413268a 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 => 20181121071704) do +ActiveRecord::Schema.define(:version => 20181122034752) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -1520,10 +1520,13 @@ ActiveRecord::Schema.define(:version => 20181121071704) do t.string "refresh_token" t.integer "token_created_at" t.integer "token_expires_in" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + t.integer "user_id", :default => 0 end + add_index "oauths", ["user_id"], :name => "index_oauths_on_user_id" + create_table "onclick_times", :force => true do |t| t.integer "user_id" t.datetime "onclick_time" From ecbdfcf1ed2d805475f5358a77284073457e5cd2 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Thu, 22 Nov 2018 15:29:17 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=A1=A8=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migrate/20181121064652_create_oauth.rb | 18 ------------------ .../20181122034752_add_user_id_to_oauths.rb | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 18 deletions(-) delete mode 100644 db/migrate/20181121064652_create_oauth.rb diff --git a/db/migrate/20181121064652_create_oauth.rb b/db/migrate/20181121064652_create_oauth.rb deleted file mode 100644 index b54d00d6d..000000000 --- a/db/migrate/20181121064652_create_oauth.rb +++ /dev/null @@ -1,18 +0,0 @@ -class CreateOauth < ActiveRecord::Migration - def change - create_table :oauth do |t| - t.string :client_id - t.string :client_secret - t.string :code - t.string :redirect_uri - t.string :scope - - t.string :access_token - t.string :refresh_token - t.integer :token_created_at - t.integer :token_expires_in #过期时间 - - t.timestamps - end - end -end diff --git a/db/migrate/20181122034752_add_user_id_to_oauths.rb b/db/migrate/20181122034752_add_user_id_to_oauths.rb index a28d2fa4c..e919939f0 100644 --- a/db/migrate/20181122034752_add_user_id_to_oauths.rb +++ b/db/migrate/20181122034752_add_user_id_to_oauths.rb @@ -1,5 +1,22 @@ class AddUserIdToOauths < ActiveRecord::Migration def change + + create_table :oauths do |t| + t.string :client_id + t.string :client_secret + t.string :code + t.string :redirect_uri + t.string :scope + + t.string :access_token + t.string :refresh_token + t.integer :token_created_at + t.integer :token_expires_in #过期时间 + + t.timestampss + end + + add_column :oauths, :user_id, :integer, default: 0 add_index :oauths, :user_id From 5c2565f236fd1e00c13ec1e47fd137e97ae57974 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Thu, 22 Nov 2018 15:30:25 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=A1=A8=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migrate/20181122034752_add_user_id_to_oauths.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/migrate/20181122034752_add_user_id_to_oauths.rb b/db/migrate/20181122034752_add_user_id_to_oauths.rb index e919939f0..4e2ed5bc4 100644 --- a/db/migrate/20181122034752_add_user_id_to_oauths.rb +++ b/db/migrate/20181122034752_add_user_id_to_oauths.rb @@ -13,7 +13,7 @@ class AddUserIdToOauths < ActiveRecord::Migration t.integer :token_created_at t.integer :token_expires_in #过期时间 - t.timestampss + t.timestamps end