rails_sso 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 293476ec400ac5ebb791ab9f9cbcd2295d01e615
4
- data.tar.gz: 6e7b5864d01cca76d4c9fc042b582fc06f5e75f0
3
+ metadata.gz: 297b750c108836e79da3b03ad4cbfb84341a4854
4
+ data.tar.gz: e9f74bbd907a1a149327e8bce747ef3454d78aaa
5
5
  SHA512:
6
- metadata.gz: f1745b2047f5ec91329f88d8a65a0b8ca16b6e0bfbf607fbb74260e6fe1433fc257cc803f7bab2fdc6f7ee5ed06674132c2bcde3c8cb3ea79b1696503552688e
7
- data.tar.gz: 83798cf03f473f3425733f420d123f768d2212f6ff59e9ad2be3f34c321e1e3c14315ae9c023ca1177d6405ddedbf10eb12c1fb5dd3feadb542df3d5421cec75
6
+ metadata.gz: 62d7687593e7775cfa865c9bc56fcb6d49cef544da2f11ba8f7a1edac0a3a6e767784a7845a87aad73071a668d3c58d0b81828fd85cb1dbbdc6892824a91991f
7
+ data.tar.gz: dd07477eb293f068fccf01886dd68d225f0efda7cc0456623a402de8b08824340eafdc2b56a1d5bcbd66d9b27857a2089b69ee895f74d0edb2c7ca392eccea07
data/README.md CHANGED
@@ -35,16 +35,6 @@ RailsSso.configure do |config|
35
35
  config.provider_sign_out_path = '/api/v1/session'
36
36
  # enable cache (will use Rails.cache store)
37
37
  config.use_cache = Rails.application.config.action_controller.perform_caching
38
-
39
- # user fields to synchronize from API
40
- config.user_fields = [
41
- :email,
42
- :name,
43
- :roles
44
- ]
45
-
46
- # user repository class name
47
- config.user_repository = 'UserRepository'
48
38
  end
49
39
  ```
50
40
 
@@ -62,7 +52,7 @@ end
62
52
 
63
53
  Available helpers for controllers and views:
64
54
 
65
- * `current_user`
55
+ * `current_user_data`
66
56
  * `user_signed_in?`
67
57
 
68
58
  Available filters and helpers for controllers:
@@ -76,43 +66,6 @@ Available helpers for views:
76
66
  * `sso.sign_in_path`
77
67
  * `sso.sign_out_path`
78
68
 
79
- ## User Repository
80
-
81
- Required methods:
82
-
83
- * `find_by_sso_id(id)`
84
- * `create_with_sso_id(id, attrs)`
85
- * `update(record, attrs)`
86
-
87
- Example with `ActiveRecord` user model:
88
-
89
- ```ruby
90
- # app/repositories/user_repository.rb
91
-
92
- class UserRepository
93
- attr_accessor :adapter
94
-
95
- def initialize(adapter = User)
96
- self.adapter = adapter
97
- end
98
-
99
- def find_by_sso_id(id)
100
- adapter.find_by(sso_id: id)
101
- end
102
-
103
- def create_with_sso_id(id, attrs)
104
- adapter.new(attrs) do |user|
105
- user.sso_id = id
106
- user.save!
107
- end
108
- end
109
-
110
- def update(record, attrs)
111
- adapter.update(record.id, attrs)
112
- end
113
- end
114
- ```
115
-
116
69
  ## Contributing
117
70
 
118
71
  1. Fork it
@@ -5,15 +5,11 @@ module RailsSso
5
5
  end
6
6
 
7
7
  def call
8
- yield(get)
8
+ access_token.get(RailsSso.provider_profile_path).parsed
9
9
  end
10
10
 
11
11
  private
12
12
 
13
13
  attr_reader :access_token
14
-
15
- def get
16
- access_token.get(RailsSso.provider_profile_path).parsed
17
- end
18
14
  end
19
15
  end
@@ -26,9 +26,9 @@ module RailsSso
26
26
 
27
27
  private
28
28
 
29
- def client
29
+ def client(cache = RailsSso.use_cache)
30
30
  strategy.client.tap do |c|
31
- if RailsSso.use_cache
31
+ if cache
32
32
  c.options[:connection_build] = Proc.new do |conn|
33
33
  conn.use :http_cache,
34
34
  store: Rails.cache,
@@ -2,18 +2,16 @@ module RailsSso
2
2
  module Helpers
3
3
  def self.included(base)
4
4
  base.class_eval do
5
- helper_method :current_user, :user_signed_in?
5
+ helper_method :current_user_data, :user_signed_in?
6
6
  end
7
7
  end
8
8
 
9
- def current_user
10
- @current_user ||= fetch_user do |user|
11
- cache_user(user)
12
- end
9
+ def current_user_data
10
+ @current_user ||= fetch_user
13
11
  end
14
12
 
15
13
  def user_signed_in?
16
- !!current_user
14
+ !!current_user_data
17
15
  end
18
16
 
19
17
  def authenticate_user!
@@ -56,16 +54,5 @@ module RailsSso
56
54
  RailsSso::FetchUser.new(access_token).call(&block)
57
55
  end
58
56
  end
59
-
60
- def cache_user(data)
61
- RailsSso::UpdateUser.new(data, update_user_options).call
62
- end
63
-
64
- def update_user_options
65
- {
66
- fields: RailsSso.user_fields,
67
- repository: RailsSso.user_repository.new
68
- }
69
- end
70
57
  end
71
58
  end
@@ -1,3 +1,3 @@
1
1
  module RailsSso
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/rails_sso.rb CHANGED
@@ -9,11 +9,6 @@ module RailsSso
9
9
  mattr_accessor :provider_profile_path
10
10
  mattr_accessor :provider_sign_out_path
11
11
 
12
- mattr_accessor :user_repository
13
-
14
- mattr_accessor :user_fields
15
- @@user_fields = [:email]
16
-
17
12
  mattr_accessor :use_cache
18
13
  @@use_cache = false
19
14
 
@@ -5,9 +5,4 @@ RailsSso.configure do |config|
5
5
  config.provider_profile_path = '/api/v1/me'
6
6
  config.provider_sign_out_path = '/api/v1/me'
7
7
  config.use_cache = false
8
- config.user_fields = [
9
- :email,
10
- :name
11
- ]
12
- config.user_repository = 'UserRepository'
13
8
  end
@@ -2371,3 +2371,165 @@ Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
2371
2371
  RailsSsoTest: test_truth
2372
2372
  ------------------------
2373
2373
   (0.0ms) rollback transaction
2374
+  (0.1ms) begin transaction
2375
+ ------------------------------------------------------------------------------------------------
2376
+ RailsSso::SessionsControllerTest: test_create_should_save_access_token_and_redirect_to_root_path
2377
+ ------------------------------------------------------------------------------------------------
2378
+ Processing by RailsSso::SessionsController#create as HTML
2379
+ Parameters: {"provider"=>"developer"}
2380
+ nil
2381
+ Redirected to http://test.host/
2382
+ Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
2383
+  (0.1ms) rollback transaction
2384
+  (0.1ms) begin transaction
2385
+ -------------------------------------------------------------------------------------------------------
2386
+ RailsSso::SessionsControllerTest: test_destroy_should_invalidate_access_token_and_redirect_to_root_path
2387
+ -------------------------------------------------------------------------------------------------------
2388
+ Processing by RailsSso::SessionsController#destroy as HTML
2389
+ Redirected to http://test.host/
2390
+ Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
2391
+  (0.1ms) rollback transaction
2392
+  (0.1ms) begin transaction
2393
+ --------------------------------------------------------------------------------------------
2394
+ RailsSso::FetchUserTest: test_call_should_fetch_user_with_access_token_and_yield_parsed_data
2395
+ --------------------------------------------------------------------------------------------
2396
+  (0.1ms) rollback transaction
2397
+  (0.1ms) begin transaction
2398
+ --------------------------------------------------------------------------------------------
2399
+ RailsSso::UpdateUserTest: test_call_should_update_user_if_exists_in_repository_and_return_it
2400
+ --------------------------------------------------------------------------------------------
2401
+  (0.1ms) rollback transaction
2402
+  (0.0ms) begin transaction
2403
+ ------------------------------------------------------------------------------------------------
2404
+ RailsSso::UpdateUserTest: test_call_should_create_user_if_not_exists_in_repository_and_return_it
2405
+ ------------------------------------------------------------------------------------------------
2406
+  (0.1ms) rollback transaction
2407
+  (0.1ms) begin transaction
2408
+ ------------------------
2409
+ RailsSsoTest: test_truth
2410
+ ------------------------
2411
+  (0.1ms) rollback transaction
2412
+  (0.1ms) begin transaction
2413
+ ----------------------------------------------------
2414
+ SsoRoutesTest: test_should_route_/:provider/callback
2415
+ ----------------------------------------------------
2416
+  (0.1ms) rollback transaction
2417
+  (0.1ms) begin transaction
2418
+ ------------------------------------------
2419
+ SsoRoutesTest: test_should_route_/sign_out
2420
+ ------------------------------------------
2421
+  (0.0ms) rollback transaction
2422
+  (0.1ms) begin transaction
2423
+ --------------------------------------------------------------------------------------------
2424
+ RailsSso::FetchUserTest: test_call_should_fetch_user_with_access_token_and_yield_parsed_data
2425
+ --------------------------------------------------------------------------------------------
2426
+  (0.1ms) rollback transaction
2427
+  (0.1ms) begin transaction
2428
+ ------------------------------------------
2429
+ SsoRoutesTest: test_should_route_/sign_out
2430
+ ------------------------------------------
2431
+  (0.1ms) rollback transaction
2432
+  (0.0ms) begin transaction
2433
+ ----------------------------------------------------
2434
+ SsoRoutesTest: test_should_route_/:provider/callback
2435
+ ----------------------------------------------------
2436
+  (0.1ms) rollback transaction
2437
+  (0.1ms) begin transaction
2438
+ ------------------------------------------------------------------------------------------------
2439
+ RailsSso::SessionsControllerTest: test_create_should_save_access_token_and_redirect_to_root_path
2440
+ ------------------------------------------------------------------------------------------------
2441
+ Processing by RailsSso::SessionsController#create as HTML
2442
+ Parameters: {"provider"=>"developer"}
2443
+ nil
2444
+ Redirected to http://test.host/
2445
+ Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
2446
+  (0.1ms) rollback transaction
2447
+  (0.0ms) begin transaction
2448
+ -------------------------------------------------------------------------------------------------------
2449
+ RailsSso::SessionsControllerTest: test_destroy_should_invalidate_access_token_and_redirect_to_root_path
2450
+ -------------------------------------------------------------------------------------------------------
2451
+ Processing by RailsSso::SessionsController#destroy as HTML
2452
+ Redirected to http://test.host/
2453
+ Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
2454
+  (0.1ms) rollback transaction
2455
+  (0.0ms) begin transaction
2456
+ ------------------------
2457
+ RailsSsoTest: test_truth
2458
+ ------------------------
2459
+  (0.0ms) rollback transaction
2460
+  (0.1ms) begin transaction
2461
+ -------------------------------------------------------------------------------------------------------
2462
+ RailsSso::SessionsControllerTest: test_destroy_should_invalidate_access_token_and_redirect_to_root_path
2463
+ -------------------------------------------------------------------------------------------------------
2464
+ Processing by RailsSso::SessionsController#destroy as HTML
2465
+ Redirected to http://test.host/
2466
+ Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
2467
+  (0.1ms) rollback transaction
2468
+  (0.1ms) begin transaction
2469
+ ------------------------------------------------------------------------------------------------
2470
+ RailsSso::SessionsControllerTest: test_create_should_save_access_token_and_redirect_to_root_path
2471
+ ------------------------------------------------------------------------------------------------
2472
+ Processing by RailsSso::SessionsController#create as HTML
2473
+ Parameters: {"provider"=>"developer"}
2474
+ nil
2475
+ Redirected to http://test.host/
2476
+ Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
2477
+  (0.1ms) rollback transaction
2478
+  (0.0ms) begin transaction
2479
+ ---------------------------------------------------------------------------------------------
2480
+ RailsSso::FetchUserTest: test_call_should_fetch_user_with_access_token_and_return_parsed_data
2481
+ ---------------------------------------------------------------------------------------------
2482
+  (0.1ms) rollback transaction
2483
+  (0.1ms) begin transaction
2484
+ ----------------------------------------------------
2485
+ SsoRoutesTest: test_should_route_/:provider/callback
2486
+ ----------------------------------------------------
2487
+  (0.1ms) rollback transaction
2488
+  (0.1ms) begin transaction
2489
+ ------------------------------------------
2490
+ SsoRoutesTest: test_should_route_/sign_out
2491
+ ------------------------------------------
2492
+  (0.0ms) rollback transaction
2493
+  (0.0ms) begin transaction
2494
+ ------------------------
2495
+ RailsSsoTest: test_truth
2496
+ ------------------------
2497
+  (0.0ms) rollback transaction
2498
+  (0.1ms) begin transaction
2499
+ ------------------------
2500
+ RailsSsoTest: test_truth
2501
+ ------------------------
2502
+  (0.0ms) rollback transaction
2503
+  (0.1ms) begin transaction
2504
+ -------------------------------------------------------------------------------------------------------
2505
+ RailsSso::SessionsControllerTest: test_destroy_should_invalidate_access_token_and_redirect_to_root_path
2506
+ -------------------------------------------------------------------------------------------------------
2507
+ Processing by RailsSso::SessionsController#destroy as HTML
2508
+ Redirected to http://test.host/
2509
+ Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
2510
+  (0.1ms) rollback transaction
2511
+  (0.1ms) begin transaction
2512
+ ------------------------------------------------------------------------------------------------
2513
+ RailsSso::SessionsControllerTest: test_create_should_save_access_token_and_redirect_to_root_path
2514
+ ------------------------------------------------------------------------------------------------
2515
+ Processing by RailsSso::SessionsController#create as HTML
2516
+ Parameters: {"provider"=>"developer"}
2517
+ nil
2518
+ Redirected to http://test.host/
2519
+ Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
2520
+  (0.1ms) rollback transaction
2521
+  (0.1ms) begin transaction
2522
+ ---------------------------------------------------------------------------------------------
2523
+ RailsSso::FetchUserTest: test_call_should_fetch_user_with_access_token_and_return_parsed_data
2524
+ ---------------------------------------------------------------------------------------------
2525
+  (0.1ms) rollback transaction
2526
+  (0.1ms) begin transaction
2527
+ ----------------------------------------------------
2528
+ SsoRoutesTest: test_should_route_/:provider/callback
2529
+ ----------------------------------------------------
2530
+  (0.1ms) rollback transaction
2531
+  (0.0ms) begin transaction
2532
+ ------------------------------------------
2533
+ SsoRoutesTest: test_should_route_/sign_out
2534
+ ------------------------------------------
2535
+  (0.1ms) rollback transaction
@@ -15,17 +15,11 @@ class RailsSso::FetchUserTest < ActiveSupport::TestCase
15
15
  @access_token = AccessToken.new(setup_access_token_client)
16
16
  end
17
17
 
18
- test "call should fetch user with access token and yield parsed data" do
19
- called = false
18
+ test "call should fetch user with access token and return parsed data" do
19
+ data = RailsSso::FetchUser.new(@access_token).call
20
20
 
21
- RailsSso::FetchUser.new(@access_token).call do |data|
22
- called = true
23
-
24
- assert_equal data['name'], user_data['name']
25
- assert_equal data['email'], user_data['email']
26
- end
27
-
28
- assert called, 'RailsSso::FetchUser#call should yield fetched data'
21
+ assert_equal data['name'], user_data['name']
22
+ assert_equal data['email'], user_data['email']
29
23
  end
30
24
 
31
25
  def setup_access_token_client
@@ -39,8 +33,7 @@ class RailsSso::FetchUserTest < ActiveSupport::TestCase
39
33
  def user_data
40
34
  {
41
35
  'name' => 'Kowalski',
42
- 'email' => 'jan@kowalski.pl',
43
- 'key' => 'value'
36
+ 'email' => 'jan@kowalski.pl'
44
37
  }
45
38
  end
46
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_sso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Dudulski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-02 00:00:00.000000000 Z
11
+ date: 2015-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -106,7 +106,6 @@ files:
106
106
  - Rakefile
107
107
  - app/controllers/rails_sso/sessions_controller.rb
108
108
  - app/services/rails_sso/fetch_user.rb
109
- - app/services/rails_sso/update_user.rb
110
109
  - config/initializers/omniauth.rb
111
110
  - config/routes.rb
112
111
  - lib/rails_sso.rb
@@ -156,7 +155,6 @@ files:
156
155
  - test/rails_sso_test.rb
157
156
  - test/routes/sso_routes_test.rb
158
157
  - test/services/rails_sso/fetch_user_test.rb
159
- - test/services/rails_sso/update_user_test.rb
160
158
  - test/test_helper.rb
161
159
  homepage: https://github.com/monterail/rails_sso
162
160
  licenses:
@@ -187,7 +185,6 @@ test_files:
187
185
  - test/rails_sso_test.rb
188
186
  - test/test_helper.rb
189
187
  - test/services/rails_sso/fetch_user_test.rb
190
- - test/services/rails_sso/update_user_test.rb
191
188
  - test/dummy/README.rdoc
192
189
  - test/dummy/db/test.sqlite3
193
190
  - test/dummy/public/favicon.ico
@@ -1,29 +0,0 @@
1
- module RailsSso
2
- class UpdateUser
3
- def initialize(data, options = {})
4
- @id, @data = data['id'], data.except('id')
5
- @fields, @repository = options.values_at(:fields, :repository)
6
- end
7
-
8
- def call
9
- if user = repository.find_by_sso_id(id)
10
- repository.update(user, params)
11
- user
12
- else
13
- repository.create_with_sso_id(id, params)
14
- end
15
- end
16
-
17
- private
18
-
19
- attr_reader :id, :data, :repository
20
-
21
- def fields
22
- @fields.map(&:to_s)
23
- end
24
-
25
- def params
26
- data.slice(*fields)
27
- end
28
- end
29
- end
@@ -1,79 +0,0 @@
1
- require 'anima'
2
- require 'test_helper'
3
-
4
- class RailsSso::UpdateUserTest < ActiveSupport::TestCase
5
- class User
6
- include Anima.new('email', 'name')
7
-
8
- attr_accessor :id
9
- attr_writer :name, :email
10
- end
11
-
12
- class Repository
13
- attr_reader :storage
14
-
15
- def initialize
16
- @storage = {}
17
- end
18
-
19
- def find_by_sso_id(id)
20
- storage[id]
21
- end
22
-
23
- def create_with_sso_id(id, data)
24
- @storage[id] = User.new(data).tap do |user|
25
- user.id = id
26
- end
27
- end
28
-
29
- def update(user, params)
30
- params.each do |k, v|
31
- user.send("#{k}=", v)
32
- end
33
- end
34
- end
35
-
36
- def setup
37
- @repository = Repository.new
38
-
39
- @options = {
40
- repository: @repository,
41
- fields: [:name, :email]
42
- }
43
-
44
- @data = {
45
- 'id' => 1,
46
- 'name' => 'Kowalski',
47
- 'email' => 'jan@kowalski.pl',
48
- 'key' => 'value'
49
- }
50
- end
51
-
52
- test "call should create user if not exists in repository and return it" do
53
- output = RailsSso::UpdateUser.new(@data, @options).call
54
-
55
- assert_equal_user(@data, output)
56
-
57
- user = @repository.find_by_sso_id(@data['id'])
58
-
59
- assert_equal_user(@data, user)
60
- end
61
-
62
- test "call should update user if exists in repository and return it" do
63
- @repository.create_with_sso_id(@data['id'], { 'email' => 'test@example.com', 'name' => 'Nowak' })
64
-
65
- output = RailsSso::UpdateUser.new(@data, @options).call
66
-
67
- assert_equal_user(@data, output)
68
-
69
- user = @repository.find_by_sso_id(@data['id'])
70
-
71
- assert_equal_user(@data, user)
72
- end
73
-
74
- def assert_equal_user(data, user)
75
- assert_equal data['id'], user.id
76
- assert_equal data['email'], user.email
77
- assert_equal data['name'], user.name
78
- end
79
- end