go_sso 0.3.0 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- 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 +4 -3
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74e802c1f65aec69060e5b79aa378ab4d2470c0a4f3adf7466add39a2875c569
|
4
|
+
data.tar.gz: 60a13a8e3b6045086887e3c1687cf5ae7f03f677d47eee2fc630b19c28a91a01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17746ae23a5d433886c56c60b922cb7a43b0abcab8a4f8907badac7107000b2252a3066a951094540a2217515139c303f2dae8830c54fcbd346c46fda4289dca
|
7
|
+
data.tar.gz: 88136bf0a22d248db516aded9bd146efd6c0d3bfbe5458119144604e71e9f00b2d440e49fb1de0fdc706ed725ff1acd24096dbec5a0fa9f28a33ba607a98d19a
|
@@ -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
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
|
@@ -27,7 +28,7 @@ module GoSso
|
|
27
28
|
self.client_id = ENV['GO_SSO_CLIENT_ID']
|
28
29
|
self.client_secret = ENV['GO_SSO_CLIENT_SECRET']
|
29
30
|
self.site = ENV['GO_SSO_CLIENT_SITE']
|
30
|
-
self.main_app_module_name = if Rails::VERSION::MAJOR
|
31
|
+
self.main_app_module_name = if Rails::VERSION::MAJOR >= 6
|
31
32
|
Rails.application.class.module_parent.to_s
|
32
33
|
else
|
33
34
|
Rails.application.class.parent.to_s
|
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.2
|
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-11-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
|
@@ -152,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
153
|
- !ruby/object:Gem::Version
|
153
154
|
version: '0'
|
154
155
|
requirements: []
|
155
|
-
rubygems_version: 3.
|
156
|
+
rubygems_version: 3.1.2
|
156
157
|
signing_key:
|
157
158
|
specification_version: 4
|
158
159
|
summary: Summary of GoSso.
|