go_sso 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dec95b352aa71a72cf9fd14c8add2dbdf8071d13ec890f3e1b5932f692f894f8
4
- data.tar.gz: 611c62890222269f6347118fdf4ac346a350e6bf89c896a322cfd2026b41f496
3
+ metadata.gz: f1a35e3822cedfdf02950daabb9e122a8a7e4b8bbac300b75101a8a397a2b1d0
4
+ data.tar.gz: 998f461530017c647a1cd8aa46eee90d80e7647b4c7190a5b1551dc75c694028
5
5
  SHA512:
6
- metadata.gz: 20800b3baa179f58fc6caef835e688b03a3933448e8937217f4b4e68887df690cd4b81f7e093c64dde8315e8d8266b8f809e57e198f4fd8dc706a9e74c6a624e
7
- data.tar.gz: 8f9e5fe488ac6067421271acc75fb5fe0d4f82e8051f6a8976d3725e452c7d0db42f0ba62cb610f3268ef1e063cbdc95c10775005366f41f272cea57af74d7d4
6
+ metadata.gz: 93b3d53c98dea6ddfe286a9253c428a75abfc48a0ecb88fc39ce3c147ea324a9dc98e751f6835f277eee7f27d82086c4a546c09b6c6a44531a4c302efd103454
7
+ data.tar.gz: 393499ac4f6cd7316f1b007c0acf350455bd9001da4b90657f263100347b8a686a952b423bf51319470a0b79d0383187af73f20e84a1595948197b97882ffa64
@@ -2,44 +2,8 @@ module GoSso
2
2
  class ApplicationController < ActionController::Base
3
3
  protect_from_forgery with: :exception
4
4
 
5
- before_action do
6
- GoSso::Current.host = request.base_url
7
- end
8
-
9
- def auth
10
- session[:go_sso_referrer] = params[:redirect_url] || request.referrer || request.base_url
11
- if GoSso.test_mode?
12
- redirect_to go_sso_callback_url
13
- else
14
- redirect_to GoSso.authorize_url
15
- end
16
- end
17
-
18
5
  def callback
19
- token = GoSso.test_mode? ? GoSso.generate_fake_token : GoSso.get_token(params[:code])
20
- set_sso_token(token.token, expires_at: token.expires_at)
21
- GoSso.after_login.call(self)
22
6
  redirect_to session.delete(:go_sso_referrer) || request.base_url
23
- rescue OAuth2::Error => error
24
- render json: {
25
- code: error.code,
26
- description: error.description
27
- }
28
- end
29
-
30
- def logout
31
- set_sso_token(nil)
32
- redirect_to params[:redirect_url] || request.referrer || request.base_url
33
- end
34
-
35
- private
36
- def set_sso_token(token, expires_at: nil)
37
- if token
38
- session[:go_sso_token] = token
39
- session[:go_sso_token_expires_at] = expires_at
40
- else
41
- session[:go_sso_token] = session[:go_sso_token_expires_at] = nil
42
- end
43
7
  end
44
8
  end
45
9
  end
data/config/routes.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  Rails.application.routes.draw do
2
- namespace :go_sso do
3
- get :auth, to: 'application#auth'
4
- get :callback, to: 'application#callback'
5
- delete :logout, to: 'application#logout'
2
+ scope GoSso.routes_prefix do
3
+ controller = GoSso.controller.to_s.underscore.delete_suffix("_controller")
4
+ get :auth, controller: controller, as: :go_sso_auth
5
+ get :callback, controller: controller, as: :go_sso_callback
6
+ delete :logout, controller: controller, as: :go_sso_logout
6
7
  end
7
8
  end
data/lib/go_sso/engine.rb CHANGED
@@ -4,6 +4,10 @@ module GoSso
4
4
  GoSso.set_default_options!
5
5
  end
6
6
 
7
+ initializer "go_sso.middleware" do |app|
8
+ app.middleware.use GoSso::Middleware
9
+ end
10
+
7
11
  initializer "go_sso.helpers" do
8
12
  ActiveSupport.on_load(:action_controller) do
9
13
  include Controllers::Helpers
@@ -0,0 +1,97 @@
1
+ module GoSso
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ @env = nil
6
+ end
7
+
8
+ def call(env)
9
+ dup.call!(env)
10
+ end
11
+
12
+ def call!(env)
13
+ @env = env
14
+
15
+ GoSso::Current.host = request.base_url
16
+ begin
17
+ return request_call if on_path?("#{GoSso.routes_prefix}/auth")
18
+ return logout_call if on_path?("#{GoSso.routes_prefix}/logout") && delete_request?
19
+ return callback_call if on_path?("#{GoSso.routes_prefix}/callback")
20
+ end
21
+
22
+ @app.call(env)
23
+ end
24
+
25
+ def session
26
+ @env['rack.session']
27
+ end
28
+
29
+ private
30
+
31
+ def delete_request?
32
+ request.request_method == "DELETE"
33
+ end
34
+
35
+ def request_call
36
+ session[:go_sso_referrer] = request.params[:redirect_url] || request.referrer || request.base_url
37
+ if GoSso.test_mode?
38
+ redirect callback_path
39
+ else
40
+ redirect GoSso.authorize_url
41
+ end
42
+ end
43
+
44
+ def logout_call
45
+ set_sso_token(nil)
46
+ redirect request.params[:redirect_url] || request.referrer || request.base_url
47
+ end
48
+
49
+ def callback_call
50
+ token = GoSso.test_mode? ? GoSso.generate_fake_token : GoSso.get_token(request.params[:code])
51
+ set_sso_token(token.token, expires_at: token.expires_at)
52
+ @app.call(@env)
53
+ rescue OAuth2::Error => error
54
+ fail!({ code: error.code, description: error.description })
55
+ end
56
+
57
+ def callback_path
58
+ "#{GoSso.routes_prefix}/callback"
59
+ end
60
+
61
+ def on_path?(path)
62
+ current_path.casecmp(path).zero?
63
+ end
64
+
65
+ CURRENT_PATH_REGEX = %r{/$}.freeze
66
+ EMPTY_STRING = ''.freeze
67
+ def current_path
68
+ @current_path ||= request.path.downcase.sub(CURRENT_PATH_REGEX, EMPTY_STRING)
69
+ end
70
+
71
+ def request
72
+ @request ||= Rack::Request.new(@env)
73
+ end
74
+
75
+ def redirect(uri)
76
+ r = Rack::Response.new
77
+
78
+ r.write("Redirecting to #{uri}...")
79
+ r.redirect(uri)
80
+
81
+ r.finish
82
+ end
83
+
84
+ def set_sso_token(token, expires_at: nil)
85
+ if token
86
+ session[:go_sso_token] = token
87
+ session[:go_sso_token_expires_at] = expires_at
88
+ else
89
+ session[:go_sso_token] = session[:go_sso_token_expires_at] = nil
90
+ end
91
+ end
92
+
93
+ def fail!(hash)
94
+ [200, { 'Content-Type' => 'application/json' }, [hash.to_json]]
95
+ end
96
+ end
97
+ end
@@ -1,3 +1,3 @@
1
1
  module GoSso
2
- VERSION = '0.2.0'
2
+ VERSION = '0.4.0'
3
3
  end
data/lib/go_sso.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  require "oauth2"
2
+ require "rack"
2
3
 
3
4
  module GoSso
4
5
  autoload :User, 'go_sso/user'
5
6
  autoload :Current, 'go_sso/current'
6
-
7
+ autoload :Middleware, 'go_sso/middleware'
7
8
  module Controllers
8
9
  autoload :Helpers, 'go_sso/controllers/helpers'
9
10
  end
@@ -16,7 +17,8 @@ module GoSso
16
17
  mattr_accessor :main_app_module_name
17
18
  mattr_accessor :host
18
19
  mattr_accessor :fake_user_json
19
- mattr_accessor :after_login, default: ->(ctx){}
20
+ mattr_accessor :routes_prefix, default: "/go_sso"
21
+ mattr_accessor :controller, default: "GoSso::ApplicationController"
20
22
 
21
23
  def self.setup
22
24
  yield self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: go_sso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yi Feng
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-28 00:00:00.000000000 Z
11
+ date: 2021-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2
@@ -124,13 +124,13 @@ files:
124
124
  - app/helpers/go_sso/application_helper.rb
125
125
  - app/jobs/go_sso/application_job.rb
126
126
  - app/mailers/go_sso/application_mailer.rb
127
- - app/models/go_sso/application_record.rb
128
127
  - app/views/layouts/go_sso/application.html.erb
129
128
  - config/routes.rb
130
129
  - lib/go_sso.rb
131
130
  - lib/go_sso/controllers/helpers.rb
132
131
  - lib/go_sso/current.rb
133
132
  - lib/go_sso/engine.rb
133
+ - lib/go_sso/middleware.rb
134
134
  - lib/go_sso/user.rb
135
135
  - lib/go_sso/version.rb
136
136
  - lib/tasks/go_sso_tasks.rake
@@ -138,7 +138,7 @@ homepage: https://github.com/yfxie/go_sso
138
138
  licenses:
139
139
  - MIT
140
140
  metadata: {}
141
- post_install_message:
141
+ post_install_message:
142
142
  rdoc_options: []
143
143
  require_paths:
144
144
  - lib
@@ -153,8 +153,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  - !ruby/object:Gem::Version
154
154
  version: '0'
155
155
  requirements: []
156
- rubygems_version: 3.0.3
157
- signing_key:
156
+ rubygems_version: 3.0.8
157
+ signing_key:
158
158
  specification_version: 4
159
159
  summary: Summary of GoSso.
160
160
  test_files: []
@@ -1,5 +0,0 @@
1
- module GoSso
2
- class ApplicationRecord < ActiveRecord::Base
3
- self.abstract_class = true
4
- end
5
- end