rails_sso 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -1
- data/app/services/rails_sso/fetch_user.rb +19 -4
- data/config/locales/en.yml +5 -0
- data/lib/generators/rails_sso_generator.rb +13 -0
- data/lib/generators/templates/sso.rb +15 -0
- data/lib/rails_sso/client.rb +50 -0
- data/lib/rails_sso/helpers.rb +28 -5
- data/lib/rails_sso/response_error.rb +11 -0
- data/lib/rails_sso/version.rb +1 -1
- data/lib/rails_sso.rb +3 -1
- data/test/dummy/config/application.rb +12 -2
- data/test/dummy/config/environments/development.rb +2 -2
- data/test/dummy/config/environments/production.rb +1 -1
- data/test/dummy/config/environments/test.rb +1 -1
- data/test/dummy/config/initializers/sso.rb +1 -0
- data/test/dummy/log/development.log +4 -0
- data/test/dummy/log/test.log +1395 -0
- data/test/lib/rails_sso/helpers_test.rb +74 -0
- data/test/lib/rails_sso/response_error_test.rb +21 -0
- data/test/services/rails_sso/fetch_user_test.rb +36 -20
- data/test/test_helper.rb +2 -1
- metadata +13 -5
- data/lib/rails_sso/access_token.rb +0 -52
- data/test/dummy/db/test.sqlite3 +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 870097ffd20610b11d2b2818e02be874f32f7a6b
|
4
|
+
data.tar.gz: 0d13485c9589191716ae0f067ecfebfdd8a6785e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d06d4ed43b361abc3d727f5673c55f7cbcf94858993d5b3a9f349b63191fcddc7a509ca6ad1be8270c2adef37f164e1a326ae27bc29c2291061bce8f39cd590
|
7
|
+
data.tar.gz: 5c9b79893b2475cad864fa469b23205327b36c0601a1ff2515d1d0992042f58c6ec4c8a5bc7ff3537a68ded052a3d1992b66bf375204939f98723cc2266bee0f
|
data/README.md
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# SSO client Rails Engine
|
2
2
|
|
3
|
+
[![Circle CI](https://circleci.com/gh/monterail/rails_sso/tree/master.svg?style=shield&circle-token=237c44548fb2c2597bcd0bc7b1dd99c81329e574)](https://circleci.com/gh/monterail/rails_sso/tree/master)
|
3
4
|
[![Dependency Status](https://gemnasium.com/monterail/rails_sso.svg)](https://gemnasium.com/monterail/rails_sso)
|
4
5
|
[![Gem Version](https://badge.fury.io/rb/rails_sso.svg)](http://badge.fury.io/rb/rails_sso)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/monterail/rails_sso/badges/gpa.svg)](https://codeclimate.com/github/monterail/rails_sso)
|
7
|
+
[![Test Coverage](https://codeclimate.com/github/monterail/rails_sso/badges/coverage.svg)](https://codeclimate.com/github/monterail/rails_sso)
|
5
8
|
|
6
9
|
## About
|
7
10
|
|
@@ -18,12 +21,20 @@ gem 'omniauth-example'
|
|
18
21
|
gem 'rails_sso'
|
19
22
|
```
|
20
23
|
|
21
|
-
|
24
|
+
Install initializer and mount routes:
|
25
|
+
|
26
|
+
```bash
|
27
|
+
bin/rails generate rails_sso
|
28
|
+
```
|
29
|
+
|
30
|
+
Configure initializer:
|
22
31
|
|
23
32
|
```ruby
|
24
33
|
# conifg/initializers/sso.rb
|
25
34
|
|
26
35
|
RailsSso.configure do |config|
|
36
|
+
# url of entity provider
|
37
|
+
config.provider_url = 'https://example.com'
|
27
38
|
# name of oauth2 provider
|
28
39
|
config.provider_name = 'example'
|
29
40
|
# oauth keys for omniauth-example
|
@@ -1,15 +1,30 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
1
3
|
module RailsSso
|
2
4
|
class FetchUser
|
3
|
-
def initialize(
|
4
|
-
@
|
5
|
+
def initialize(client)
|
6
|
+
@client = client
|
5
7
|
end
|
6
8
|
|
7
9
|
def call
|
8
|
-
|
10
|
+
response = client.get(RailsSso.provider_profile_path)
|
11
|
+
|
12
|
+
case response.status
|
13
|
+
when 200
|
14
|
+
begin
|
15
|
+
JSON.parse(response.body)
|
16
|
+
rescue
|
17
|
+
response.body
|
18
|
+
end
|
19
|
+
when 401
|
20
|
+
raise ResponseError.new(:unauthenticated)
|
21
|
+
else
|
22
|
+
raise ResponseError.new(:unknown)
|
23
|
+
end
|
9
24
|
end
|
10
25
|
|
11
26
|
private
|
12
27
|
|
13
|
-
attr_reader :
|
28
|
+
attr_reader :client
|
14
29
|
end
|
15
30
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class RailsSsoGenerator < Rails::Generators::Base
|
2
|
+
source_root File.expand_path("../templates", __FILE__)
|
3
|
+
|
4
|
+
desc "Creates RailsSso initializer and mount sso routes."
|
5
|
+
|
6
|
+
def copy_initializer
|
7
|
+
template "sso.rb", "config/initializers/sso.rb"
|
8
|
+
end
|
9
|
+
|
10
|
+
def add_sso_routes
|
11
|
+
route "mount RailsSso::Engine => '/sso', as: 'sso'"
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
RailsSso.configure do |config|
|
2
|
+
# url of entity provider
|
3
|
+
config.provider_url = 'https://example.com'
|
4
|
+
# name of oauth2 provider
|
5
|
+
config.provider_name = 'example'
|
6
|
+
# oauth keys for omniauth-example
|
7
|
+
config.provider_key = ENV['PROVIDER_KEY']
|
8
|
+
config.provider_secret = ENV['PROVIDER_SECRET']
|
9
|
+
# path for fetching user data
|
10
|
+
config.provider_profile_path = '/api/v1/profile'
|
11
|
+
# set if you support single sign out
|
12
|
+
config.provider_sign_out_path = '/api/v1/session'
|
13
|
+
# enable cache (will use Rails.cache store)
|
14
|
+
config.use_cache = Rails.application.config.action_controller.perform_caching
|
15
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday-http-cache'
|
3
|
+
|
4
|
+
module RailsSso
|
5
|
+
class Client
|
6
|
+
def initialize(url, &block)
|
7
|
+
@connection = Faraday.new(url, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def token!(token)
|
11
|
+
@token = token
|
12
|
+
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def get(url, params = {})
|
17
|
+
request(:get, url, params)
|
18
|
+
end
|
19
|
+
|
20
|
+
def post(url, params = {})
|
21
|
+
request(:post, url, params)
|
22
|
+
end
|
23
|
+
|
24
|
+
def put(url, params = {})
|
25
|
+
request(:put, url, params)
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete(url, params = {})
|
29
|
+
request(:delete, url, params)
|
30
|
+
end
|
31
|
+
|
32
|
+
def patch(url, params = {})
|
33
|
+
request(:patch, url, params)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
attr_reader :connection, :token
|
39
|
+
|
40
|
+
def request(verb, url, params = {})
|
41
|
+
connection.send(verb) do |req|
|
42
|
+
req.headers['Authorization'] = "Bearer #{token}"
|
43
|
+
req.headers['Content-Type'] = 'application/json'
|
44
|
+
|
45
|
+
req.url(url)
|
46
|
+
req.body = params.to_json
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/rails_sso/helpers.rb
CHANGED
@@ -19,7 +19,9 @@ module RailsSso
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def access_token
|
22
|
-
|
22
|
+
OAuth2::AccessToken.new(oauth2_strategy.client, session[:access_token], {
|
23
|
+
refresh_token: session[:refresh_token]
|
24
|
+
})
|
23
25
|
end
|
24
26
|
|
25
27
|
def invalidate_access_token!
|
@@ -45,14 +47,35 @@ module RailsSso
|
|
45
47
|
|
46
48
|
private
|
47
49
|
|
50
|
+
def oauth2_strategy
|
51
|
+
oauth2_strategy_class.new(nil, RailsSso.provider_key, RailsSso.provider_secret)
|
52
|
+
end
|
53
|
+
|
54
|
+
def oauth2_strategy_class
|
55
|
+
"OmniAuth::Strategies::#{RailsSso.provider_name.camelize}".constantize
|
56
|
+
end
|
57
|
+
|
58
|
+
def provider_client
|
59
|
+
@provider_client ||= RailsSso::Client.new(RailsSso.provider_url) do |conn|
|
60
|
+
if RailsSso.use_cache
|
61
|
+
conn.use :http_cache,
|
62
|
+
store: Rails.cache,
|
63
|
+
logger: Rails.logger,
|
64
|
+
shared_cache: false
|
65
|
+
end
|
66
|
+
|
67
|
+
conn.adapter Faraday.default_adapter
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
48
71
|
def fetch_user_data
|
49
72
|
return unless session[:access_token]
|
50
73
|
|
51
|
-
RailsSso::FetchUser.new(access_token).call
|
52
|
-
rescue
|
74
|
+
RailsSso::FetchUser.new(provider_client.token!(session[:access_token])).call
|
75
|
+
rescue ResponseError => e
|
53
76
|
refresh_access_token! do
|
54
|
-
RailsSso::FetchUser.new(access_token).call
|
55
|
-
end
|
77
|
+
RailsSso::FetchUser.new(provider_client.token!(session[:access_token])).call
|
78
|
+
end if e.code == :unauthenticated
|
56
79
|
end
|
57
80
|
end
|
58
81
|
end
|
data/lib/rails_sso/version.rb
CHANGED
data/lib/rails_sso.rb
CHANGED
@@ -2,6 +2,7 @@ module RailsSso
|
|
2
2
|
mattr_accessor :application_controller
|
3
3
|
@@application_controller = 'ApplicationController'
|
4
4
|
|
5
|
+
mattr_accessor :provider_url
|
5
6
|
mattr_accessor :provider_name
|
6
7
|
mattr_accessor :provider_key
|
7
8
|
mattr_accessor :provider_secret
|
@@ -29,4 +30,5 @@ require 'omniauth-oauth2'
|
|
29
30
|
require 'rails_sso/version'
|
30
31
|
require 'rails_sso/engine'
|
31
32
|
require 'rails_sso/helpers'
|
32
|
-
require 'rails_sso/
|
33
|
+
require 'rails_sso/client'
|
34
|
+
require 'rails_sso/response_error'
|
@@ -1,6 +1,16 @@
|
|
1
1
|
require File.expand_path('../boot', __FILE__)
|
2
2
|
|
3
|
-
|
3
|
+
%w(
|
4
|
+
action_controller
|
5
|
+
action_view
|
6
|
+
rails/test_unit
|
7
|
+
sprockets
|
8
|
+
).each do |framework|
|
9
|
+
begin
|
10
|
+
require "#{framework}/railtie"
|
11
|
+
rescue LoadError
|
12
|
+
end
|
13
|
+
end
|
4
14
|
|
5
15
|
Bundler.require(*Rails.groups)
|
6
16
|
require "rails_sso"
|
@@ -20,7 +30,7 @@ module Dummy
|
|
20
30
|
# config.i18n.default_locale = :de
|
21
31
|
|
22
32
|
# Do not swallow errors in after_commit/after_rollback callbacks.
|
23
|
-
config.active_record.raise_in_transactional_callbacks = true
|
33
|
+
# config.active_record.raise_in_transactional_callbacks = true
|
24
34
|
end
|
25
35
|
end
|
26
36
|
|
@@ -14,13 +14,13 @@ Rails.application.configure do
|
|
14
14
|
config.action_controller.perform_caching = false
|
15
15
|
|
16
16
|
# Don't care if the mailer can't send.
|
17
|
-
config.action_mailer.raise_delivery_errors = false
|
17
|
+
# config.action_mailer.raise_delivery_errors = false
|
18
18
|
|
19
19
|
# Print deprecation notices to the Rails logger.
|
20
20
|
config.active_support.deprecation = :log
|
21
21
|
|
22
22
|
# Raise an error on page load if there are pending migrations.
|
23
|
-
config.active_record.migration_error = :page_load
|
23
|
+
# config.active_record.migration_error = :page_load
|
24
24
|
|
25
25
|
# Debug mode disables concatenation and preprocessing of assets.
|
26
26
|
# This option may cause significant delays in view rendering with a large
|
@@ -29,7 +29,7 @@ Rails.application.configure do
|
|
29
29
|
# Tell Action Mailer not to deliver emails to the real world.
|
30
30
|
# The :test delivery method accumulates sent emails in the
|
31
31
|
# ActionMailer::Base.deliveries array.
|
32
|
-
config.action_mailer.delivery_method = :test
|
32
|
+
# config.action_mailer.delivery_method = :test
|
33
33
|
|
34
34
|
# Randomize the order test cases are executed.
|
35
35
|
config.active_support.test_order = :random
|
@@ -0,0 +1,4 @@
|
|
1
|
+
[1m[36m (11.2ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar NOT NULL) [0m
|
2
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
3
|
+
[1m[36m (15.7ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
4
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|