go_sso 0.1.1 → 0.3.1

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: 141663866cb0c76ce7d600625d230153c747493dd4814ad5dd3a79e7d8cd8846
4
- data.tar.gz: 686176927a42cd377a4dc38bc9eca2125c812b70366b695603187365a58bc2ce
3
+ metadata.gz: ae7a310b964f3781f1d0e26f7162c4f75d31b2e257c361c102529275fa5090ab
4
+ data.tar.gz: 5c1d80f3c43a2e10b4913a706007702c0dfc3fc0b063a9b9068fe46fd4db73cd
5
5
  SHA512:
6
- metadata.gz: 92ebdb0a8e6ad0a81de62f66c15cd6891b1dc836a88203a1c3c251de604e3499a249f160b74937c0d3d6782144b15bfe5dd066d76cf15e93f49d982a2343a800
7
- data.tar.gz: c42f320ed2432412fe15ac7415f7b8d52f82a5f4a417d78ff76c6f6d3c5d2191b3d5e8edc88eb3fcb7f2147635cd14fef9abc8e2b15aa526a3ecbea126125bea
6
+ metadata.gz: 34709f152bfc2c0034875c305c422b21a79c5b9864316ee65cbd31a3efb7398f88e3471e26b080243a47e62bb282c5fe8f709449949cf041894324a24d9bb363
7
+ data.tar.gz: 7c09b572fed55d05d3453cc4dc7783a5120ab65fd7f987a7e5908d9c1dd78a0a970ff885b025ae43a7d23156b50943a9dc86db748a608e18090742f1122bceb8
data/README.md CHANGED
@@ -22,6 +22,7 @@ GoSso.setup do |config|
22
22
  config.user_cache_ttl # default 1.minute
23
23
  config.main_app_module_name # default is your host app module name
24
24
  config.host # set to your app host
25
+ config.fake_user_json # set this options for development or test environment only
25
26
  end
26
27
  ```
27
28
 
@@ -33,4 +34,14 @@ Pages with this hook will be protected.
33
34
  If `current_sso_user` is not present, redirect users to SSO to login.
34
35
  Users can access the protected page only if after login and their applications attribute contains `main_app_module_name`
35
36
 
36
- You can access current user in views or controllers via `current_sso_user`.
37
+ You can access current user in views or controllers via `current_sso_user`.
38
+
39
+ In a development environment, it is probably without SSO server support. When `fake_user_json` option is set, users will always login successfully and its user JSON will be `fake_user_json`:
40
+ ```
41
+ GoSso.setup do |config|
42
+ # other configurations...
43
+ if Rails.env.development?
44
+ config.fake_user_json = { uid: 1, email: 'yfxie@me.com' }
45
+ end
46
+ end
47
+ ```
@@ -8,12 +8,17 @@ module GoSso
8
8
 
9
9
  def auth
10
10
  session[:go_sso_referrer] = params[:redirect_url] || request.referrer || request.base_url
11
- redirect_to GoSso.authorize_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
12
16
  end
13
17
 
14
18
  def callback
15
- token = GoSso.get_token(params[:code])
19
+ token = GoSso.test_mode? ? GoSso.generate_fake_token : GoSso.get_token(params[:code])
16
20
  set_sso_token(token.token, expires_at: token.expires_at)
21
+ GoSso.after_login.call(self)
17
22
  redirect_to session.delete(:go_sso_referrer) || request.base_url
18
23
  rescue OAuth2::Error => error
19
24
  render json: {
data/config/routes.rb CHANGED
@@ -1,7 +1,7 @@
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
+ get :auth, to: 'go_sso/application#auth', as: :go_sso_auth
4
+ get :callback, to: 'go_sso/application#callback', as: :go_sso_callback
5
+ delete :logout, to: 'go_sso/application#logout', as: :go_sso_logout
6
6
  end
7
7
  end
@@ -17,7 +17,8 @@ module GoSso
17
17
  @current_sso_user ||= begin
18
18
  return nil unless session[:go_sso_token]
19
19
  return nil if sso_token_expired?
20
- GoSso::User.from_token(session[:go_sso_token])
20
+ user_json = GoSso.get_user_json(session[:go_sso_token])
21
+ GoSso::User.new(user_json)
21
22
  end
22
23
  rescue OAuth2::Error
23
24
  nil
@@ -29,7 +30,7 @@ module GoSso
29
30
  end
30
31
 
31
32
  unless current_sso_user.can_access?(GoSso.main_app_module_name)
32
- render json: { message: 'access denied' }, status: 401
33
+ return render json: { message: 'access denied' }, status: 401
33
34
  end
34
35
  end
35
36
 
data/lib/go_sso/user.rb CHANGED
@@ -19,15 +19,4 @@ class GoSso::User
19
19
  super
20
20
  end
21
21
  end
22
-
23
- def self.from_token(token)
24
- o_token = OAuth2::AccessToken.new(GoSso.client, token)
25
- json_str = Rails.cache.fetch([:go_sso_user, token], expires_in: GoSso.user_cache_ttl) do
26
- o_token.get(GoSso.user_json_url).body
27
- end
28
- attrs = JSON.parse(json_str)
29
- new(attrs)
30
- rescue Faraday::ConnectionFailed
31
- raise GoSso::FailedToOpenConnection
32
- end
33
22
  end
@@ -1,3 +1,3 @@
1
1
  module GoSso
2
- VERSION = '0.1.1'
2
+ VERSION = '0.3.1'
3
3
  end
data/lib/go_sso.rb CHANGED
@@ -15,6 +15,9 @@ module GoSso
15
15
  mattr_accessor :user_cache_ttl, default: 1.minute
16
16
  mattr_accessor :main_app_module_name
17
17
  mattr_accessor :host
18
+ mattr_accessor :fake_user_json
19
+ mattr_accessor :after_login, default: ->(ctx){}
20
+ mattr_accessor :routes_prefix, default: "/go_sso"
18
21
 
19
22
  def self.setup
20
23
  yield self
@@ -47,6 +50,25 @@ module GoSso
47
50
  host || Current.host
48
51
  end
49
52
 
53
+ def self.get_user_json(token = nil)
54
+ return fake_user_json.as_json if test_mode?
55
+ o_token = OAuth2::AccessToken.new(client, token)
56
+ json_str = Rails.cache.fetch([:go_sso_user, token], expires_in: user_cache_ttl) do
57
+ o_token.get(GoSso.user_json_url).body
58
+ end
59
+ JSON.parse(json_str)
60
+ rescue Faraday::ConnectionFailed
61
+ raise GoSso::FailedToOpenConnection
62
+ end
63
+
64
+ def self.test_mode?
65
+ fake_user_json.present?
66
+ end
67
+
68
+ def self.generate_fake_token
69
+ OAuth2::AccessToken.new(client, SecureRandom.urlsafe_base64, expires_in: 2.hours)
70
+ end
71
+
50
72
  class FailedToOpenConnection < Exception; end
51
73
  end
52
74
 
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.1.1
4
+ version: 0.3.1
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-24 00:00:00.000000000 Z
11
+ date: 2021-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2
@@ -124,7 +124,6 @@ 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
@@ -138,7 +137,7 @@ homepage: https://github.com/yfxie/go_sso
138
137
  licenses:
139
138
  - MIT
140
139
  metadata: {}
141
- post_install_message:
140
+ post_install_message:
142
141
  rdoc_options: []
143
142
  require_paths:
144
143
  - lib
@@ -153,8 +152,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
152
  - !ruby/object:Gem::Version
154
153
  version: '0'
155
154
  requirements: []
156
- rubygems_version: 3.0.3
157
- signing_key:
155
+ rubygems_version: 3.0.8
156
+ signing_key:
158
157
  specification_version: 4
159
158
  summary: Summary of GoSso.
160
159
  test_files: []
@@ -1,5 +0,0 @@
1
- module GoSso
2
- class ApplicationRecord < ActiveRecord::Base
3
- self.abstract_class = true
4
- end
5
- end