go_sso 0.3.1 → 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 +4 -4
- data/app/controllers/go_sso/application_controller.rb +0 -36
- data/config/routes.rb +4 -3
- data/lib/go_sso/engine.rb +4 -0
- data/lib/go_sso/middleware.rb +97 -0
- data/lib/go_sso/version.rb +1 -1
- data/lib/go_sso.rb +3 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1a35e3822cedfdf02950daabb9e122a8a7e4b8bbac300b75101a8a397a2b1d0
|
4
|
+
data.tar.gz: 998f461530017c647a1cd8aa46eee90d80e7647b4c7190a5b1551dc75c694028
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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(host: GoSso::Current.host)
|
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
2
|
scope GoSso.routes_prefix do
|
3
|
-
|
4
|
-
get :
|
5
|
-
|
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
@@ -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
|
data/lib/go_sso/version.rb
CHANGED
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,8 +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
20
|
mattr_accessor :routes_prefix, default: "/go_sso"
|
21
|
+
mattr_accessor :controller, default: "GoSso::ApplicationController"
|
21
22
|
|
22
23
|
def self.setup
|
23
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.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yi Feng
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oauth2
|
@@ -130,6 +130,7 @@ files:
|
|
130
130
|
- lib/go_sso/controllers/helpers.rb
|
131
131
|
- lib/go_sso/current.rb
|
132
132
|
- lib/go_sso/engine.rb
|
133
|
+
- lib/go_sso/middleware.rb
|
133
134
|
- lib/go_sso/user.rb
|
134
135
|
- lib/go_sso/version.rb
|
135
136
|
- lib/tasks/go_sso_tasks.rake
|