registrar 0.0.1.alpha → 0.0.2.alpha

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: 1aaaffc536d54dfded534286d5d7f0520f2e293f
4
- data.tar.gz: 8018ff192a4cceccc8d5340a3f8d3c1ff296f34e
3
+ metadata.gz: 9f413572ed2bac2448931df73018fadc01778795
4
+ data.tar.gz: 090638ae3e238888f75927ed70da14e891ff81d2
5
5
  SHA512:
6
- metadata.gz: 0bcd8c5426ae1b9181a26e9895e638111d3ad4c89b4edf8087b8b640620615ed752f24f90cd6ac57e9529082502fcabff9e808773a76c169e3bd69a0c25ec751
7
- data.tar.gz: ab2cb5cecbb5f1c1736ab80cfce3cddceed39bb1411fb479b302760cd1c4dbdaa5650467fc2e30161d473514cbbe502241e7c101f6bade5859c668aef5b45b06
6
+ metadata.gz: 41717567d87d943da6342e616257369ae6e2f43f91ffa31faf8e293fa90c01451df6224a642a32758d87f9488ccbb44e4ada0e50e95c3b023762826c2bfd7ea6
7
+ data.tar.gz: 1e82ad6b625df0038bf959334525d3909e1467b4e148f7c287eeb811b1a9bfc54a1f5d24f8e2c3b12346bb14dd40837dfc3f0f8251190c2741097497a59ef858
data/README.md CHANGED
@@ -1,5 +1,19 @@
1
+ [github]: https://github.com/JanOwiesniak/registrar
2
+ [doc]: http://rubydoc.info/github/JanOwiesniak/registrar/master/file/README.md
3
+ [gem]: https://rubygems.org/gems/registrar
4
+ [gem-badge]: https://img.shields.io/gem/v/registrar.svg
5
+ [rack-playground]: https://github.com/JanOwiesniak/rack-playground/blob/master/lib/app_builder.rb
6
+
1
7
  # Registrar
2
8
 
9
+ [![Gem Version][gem-badge]][gem]
10
+
11
+ [Gem][gem] |
12
+ [Source][github] |
13
+ [Documentation][doc]
14
+
15
+ # Description
16
+
3
17
  Registrar standardizes Authentication Responses through Rack Middleware and works well with common authentication mechanisms like OmniAuth.
4
18
 
5
19
  ## Installation
@@ -20,11 +34,26 @@ Or install it yourself as:
20
34
 
21
35
  ## Usage
22
36
 
23
- TODO: Write usage instructions here
37
+ Click [here][rack-playground] to see how to use the different components.
38
+
39
+ ## Short summary of the components
40
+
41
+ ### Gatekeeper
42
+
43
+ It normalizes the interface.
44
+
45
+ ## Adapter
46
+
47
+ It normalizes the response.
48
+
49
+ ## ProfileFactory
50
+
51
+ It passes the normalized response to a profile factory and stores a hash version
52
+ of the returned object in the env.
24
53
 
25
54
  ## Contributing
26
55
 
27
- 1. Fork it ( https://github.com/[my-github-username]/registrar/fork )
56
+ 1. Fork it ( https://github.com/JanOwiesniak/registrar/fork )
28
57
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
58
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
59
  4. Push to the branch (`git push origin my-new-feature`)
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rake/testtask'
2
3
 
4
+ Rake::TestTask.new(:specs) do |t|
5
+ t.libs << "spec"
6
+ t.test_files = FileList['spec/*_spec.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :specs
@@ -1,7 +1,7 @@
1
1
  require "registrar/version"
2
2
  require 'registrar/adapter'
3
- require 'registrar/profile_builder'
4
- require 'registrar/profile_gateway_stub'
3
+ require 'registrar/gatekeeper'
4
+ require 'registrar/profile_factory'
5
5
  require 'registrar/debug'
6
6
 
7
7
  module Registrar
@@ -13,52 +13,41 @@ module Registrar
13
13
  private
14
14
 
15
15
  def try_to_normalize_auth(env)
16
- if env["omniauth.auth"]
17
- env['registrar.auth'] = AuthNormalizer.normalized(env)
16
+ if auth = env['omniauth.auth']
17
+ env['registrar.auth'] = AuthNormalizer.normalized(auth)
18
18
  end
19
19
  end
20
20
 
21
21
  class AuthNormalizer
22
- def self.normalized(env)
23
- normalizer = new(env)
24
- normalizer.send(:normalize)
25
- normalizer.send(:normalized)
22
+ def self.normalized(auth)
23
+ new(auth).normalize
26
24
  end
27
25
 
28
- private
29
-
30
- attr_reader :auth, :normalized
31
-
32
- def initialize(env)
33
- @auth = env["omniauth.auth"]
34
- @normalized = {}
26
+ def initialize(auth)
27
+ @auth = auth
35
28
  end
36
29
 
37
30
  def normalize
38
- normalize_provider
39
- normalize_profile
31
+ normalized = {}
32
+ normalized['provider'] = normalize_provider
33
+ normalized['profile'] = normalize_profile
34
+ normalized
40
35
  end
41
36
 
42
- def normalize_provider
43
- provider_name = auth["provider"]
44
- provider_uid = auth["uid"]
37
+ private
38
+
39
+ attr_reader :auth
45
40
 
46
- normalized.merge!(
47
- 'provider' => {
48
- 'name' => provider_name,
49
- 'uid' => provider_uid
50
- }
51
- )
41
+ def normalize_provider
42
+ {
43
+ 'name' => auth['provider'],
44
+ 'uid' => auth['uid'],
45
+ 'access_token' => auth['credentials']['token']
46
+ }
52
47
  end
53
48
 
54
49
  def normalize_profile
55
- normalized.merge!(
56
- 'profile' => {}
57
- )
58
-
59
- auth["info"].to_hash.each_pair do |k, v|
60
- normalized['profile'][k] = v
61
- end
50
+ auth['info'].to_hash
62
51
  end
63
52
  end
64
53
  end
@@ -0,0 +1,82 @@
1
+ module Registrar
2
+ class Gatekeeper
3
+ def initialize(app, &block)
4
+ @app = app
5
+ @dispatcher = Dispatcher.new
6
+ try_to_eval_dispatcher(&block)
7
+ end
8
+
9
+ def call(env)
10
+ if dispatch?(env)
11
+ dispatch(env).tap do |d|
12
+ env["PATH_INFO"] = d.request_path
13
+ env["REQUEST_METHOD"] = d.request_method
14
+ end
15
+ end
16
+
17
+ @app.call(env)
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :dispatcher
23
+
24
+ def try_to_eval_dispatcher(&block)
25
+ if block_given?
26
+ dispatcher.instance_eval &block
27
+ end
28
+ end
29
+
30
+ def dispatch?(env)
31
+ !!dispatch(env)
32
+ end
33
+
34
+ def dispatch(env)
35
+ return @dispatch if @dispatch
36
+
37
+ request_path = env["PATH_INFO"]
38
+ request_method = env["REQUEST_METHOD"]
39
+ uid = "#{request_path}_#{request_method}"
40
+
41
+ @dispatch = dispatcher.dispatches[uid]
42
+ end
43
+
44
+ class Dispatcher
45
+ def initialize
46
+ @dispatches = {}
47
+ end
48
+
49
+ def get(from, to)
50
+ add_dispatch(from, to, "GET")
51
+ end
52
+
53
+ def post(from, to)
54
+ add_dispatch(from, to, "POST")
55
+ end
56
+
57
+ def dispatches
58
+ @dispatches
59
+ end
60
+
61
+ private
62
+
63
+ def add_dispatch(from, to, request_method)
64
+ lookup_key = "#{from}_#{request_method}"
65
+ @dispatches[lookup_key] = build_dispatch(to, request_method)
66
+ end
67
+
68
+ def build_dispatch(request_path, request_method)
69
+ Dispatch.new(request_path, request_method)
70
+ end
71
+
72
+ class Dispatch
73
+ attr_reader :request_path, :request_method
74
+
75
+ def initialize(request_path, request_method)
76
+ @request_path = request_path
77
+ @request_method = request_method
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -1,18 +1,18 @@
1
1
  module Registrar
2
- class ProfileBuilder
2
+ class ProfileFactory
3
3
  def initialize(app, callable)
4
4
  @app = app
5
5
  @callable = callable
6
6
  end
7
7
 
8
8
  def call(env)
9
- try_to_call(env)
9
+ build_profile(env)
10
10
  @app.call(env)
11
11
  end
12
12
 
13
13
  private
14
14
 
15
- def try_to_call(env)
15
+ def build_profile(env)
16
16
  if auth_hash = env['registrar.auth']
17
17
  profile = @callable.call(auth_hash)
18
18
  env['registrar.profile'] = profile.to_hash
@@ -1,3 +1,3 @@
1
1
  module Registrar
2
- VERSION = "0.0.1.alpha"
2
+ VERSION = "0.0.2.alpha"
3
3
  end
@@ -20,4 +20,8 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rack"
24
+ spec.add_development_dependency "rack-test"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "omniauth"
23
27
  end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ class GatekeeperSpec < Spec
4
+ it 'delegates get requests to the defined endpoints' do
5
+ get '/login/base'
6
+
7
+ assert_path '/auth/developer'
8
+ assert_method 'GET'
9
+ end
10
+
11
+ it 'delegates post requests to the defined endpoints' do
12
+ post '/login/base'
13
+
14
+ assert_path '/auth/developer/callback'
15
+ assert_method 'POST'
16
+ end
17
+
18
+ private
19
+
20
+ def assert_path(request_path)
21
+ assert_equal request_path, env["PATH_INFO"]
22
+ end
23
+
24
+ def assert_method(request_method)
25
+ assert_equal request_method, env["REQUEST_METHOD"]
26
+ end
27
+
28
+ def env
29
+ last_request.env
30
+ end
31
+
32
+ def response
33
+ last_response
34
+ end
35
+
36
+ def app
37
+ @app ||= build_app
38
+ end
39
+
40
+ def build_app
41
+ builder.to_app
42
+ end
43
+
44
+ def builder
45
+ Rack::Builder.new do
46
+ use Registrar::Gatekeeper do
47
+ get '/login/base', '/auth/developer'
48
+ post '/login/base', '/auth/developer/callback'
49
+ end
50
+
51
+ app = Proc.new do |env|
52
+ ['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
53
+ end
54
+
55
+ run app
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+ require 'omniauth'
3
+
4
+ class OmniAuthAdapterSpec < Spec
5
+ it 'normalizes OmniAuth Auth Hash Schema 1.0 and later' do
6
+ get '/'
7
+ assert_normalizes_auth env
8
+ end
9
+
10
+ private
11
+
12
+ def assert_normalizes_auth(env)
13
+ assert_normalizes_provider(env)
14
+ assert_normalizes_profile(env)
15
+ end
16
+
17
+ def assert_normalizes_provider(env)
18
+ assert_equal(
19
+ {
20
+ 'name' => 'facebook',
21
+ 'uid' => '100000100277322',
22
+ 'access_token' => 'CAACEdEose0cBAGxad8Y14t3tu3kMlA3SgnxZBfZCQcSyb9hnn1kNZCpBDzZBIpNNpYSsJTFDs4Ar4ZBZBoMRRAzzFJGhPW4mtM1Rmm62BsQiZCkpJpG1pAC8tslbD3s3BiSYEGdjhOZBt7QVHZB1Sea14ojiZAOZBhFWi1BZBfIGgRI3El6FmEYMKPQ2XgJR0ottKdTQBR1ia2NuZABxpsAMYY8Sb'
23
+ }, env['registrar.auth']['provider'])
24
+ end
25
+
26
+ def assert_normalizes_profile(env)
27
+ assert_equal(
28
+ {
29
+ "name" => "Jan Ow",
30
+ "first_name" => "Jan",
31
+ "last_name" => "Ow",
32
+ "image" => "http://graph.facebook.com/100000100277322/picture",
33
+ "urls" => {
34
+ "Facebook" => "http://www.facebook.com/100000100277322"
35
+ },
36
+ "verified" => true
37
+ }, env['registrar.auth']['profile'])
38
+ end
39
+
40
+ def env
41
+ last_request.env
42
+ end
43
+
44
+ def response
45
+ last_response
46
+ end
47
+
48
+ def app
49
+ @app ||= build_app
50
+ end
51
+
52
+ def build_app
53
+ builder.to_app
54
+ end
55
+
56
+ class OmniAuthFacebookStub
57
+ def initialize(app)
58
+ @app = app
59
+ end
60
+
61
+ def call(env)
62
+ env['omniauth.auth'] = stubbed_auth_response
63
+ @app.call(env)
64
+ end
65
+
66
+ private
67
+
68
+ def stubbed_auth_response
69
+ Marshal.load(auth_response_file)
70
+ end
71
+
72
+ def auth_response_file
73
+ File.read(auth_response_path)
74
+ end
75
+
76
+ def auth_response_path
77
+ "#{current_path}/fixtures/omniauth_1_0_auth_hash_schema"
78
+ end
79
+
80
+ def current_path
81
+ File.expand_path(File.dirname(__FILE__))
82
+ end
83
+ end
84
+
85
+ def builder
86
+ Rack::Builder.new do
87
+ use OmniAuthFacebookStub
88
+
89
+ use Registrar::Adapter::OmniAuth
90
+
91
+ app = Proc.new do |env|
92
+ ['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
93
+ end
94
+
95
+ run app
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ class ProfileFactorySpec < Spec
4
+ it 'passes auth hash to callable and stores callable#to_hash in the env' do
5
+ get '/'
6
+ assert_stores_profile env
7
+ end
8
+
9
+ private
10
+
11
+ def assert_stores_profile(env)
12
+ assert_equal(
13
+ {
14
+ 'internal_uid' => '1',
15
+ 'external_uid' => 'facebook_123',
16
+ 'name' => 'Bob'
17
+ },
18
+ env['registrar.profile'])
19
+ end
20
+
21
+ def env
22
+ last_request.env
23
+ end
24
+
25
+ def response
26
+ last_response
27
+ end
28
+
29
+ def app
30
+ @app ||= build_app
31
+ end
32
+
33
+ def build_app
34
+ builder.to_app
35
+ end
36
+
37
+ class RegistrarAuthStub
38
+ def initialize(app)
39
+ @app = app
40
+ end
41
+
42
+ def call(env)
43
+ auth_hash = {
44
+ 'provider' => {
45
+ 'name' => 'facebook',
46
+ 'uid' => '123'
47
+ },
48
+ 'profile' => {
49
+ 'name' => 'Bob'
50
+ }
51
+ }
52
+ env['registrar.auth'] = auth_hash
53
+ @app.call(env)
54
+ end
55
+
56
+ private
57
+
58
+ def stubbed_auth_response
59
+ Marshal.load(auth_response_file)
60
+ end
61
+
62
+ def auth_response_file
63
+ File.read(auth_response_path)
64
+ end
65
+
66
+ def auth_response_path
67
+ "#{current_path}/fixtures/omniauth_1_0_auth_hash_schema"
68
+ end
69
+
70
+ def current_path
71
+ File.expand_path(File.dirname(__FILE__))
72
+ end
73
+ end
74
+
75
+ class ProfileGatewayStub
76
+ def call(auth_hash)
77
+ Profile.new(auth_hash)
78
+ end
79
+
80
+ class Profile
81
+ def initialize(auth_hash)
82
+ provider_name = auth_hash['provider']['name']
83
+ uid = auth_hash['provider']['uid']
84
+
85
+ @internal_uid = '1'
86
+ @external_uid = "#{provider_name}_#{uid}"
87
+ @name = auth_hash['profile']['name']
88
+ end
89
+
90
+ def to_hash
91
+ {
92
+ 'internal_uid' => @internal_uid,
93
+ 'external_uid' => @external_uid,
94
+ 'name' => @name
95
+ }
96
+ end
97
+ end
98
+ end
99
+
100
+ def builder
101
+ Rack::Builder.new do
102
+ use RegistrarAuthStub
103
+
104
+ use Registrar::ProfileFactory, ProfileGatewayStub.new
105
+
106
+ app = Proc.new do |env|
107
+ ['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
108
+ end
109
+
110
+ run app
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,8 @@
1
+ require 'minitest/autorun'
2
+ require 'rack'
3
+ require 'rack/test'
4
+ require 'registrar'
5
+
6
+ class Spec < Minitest::Spec
7
+ include Rack::Test::Methods
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: registrar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha
4
+ version: 0.0.2.alpha
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Owiesniak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-09 00:00:00.000000000 Z
11
+ date: 2015-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,62 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: omniauth
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
41
97
  description: Registrar standardizes Authentication Responses through Rack Middleware
42
98
  and works well with common authentication mechanisms like OmniAuth.
43
99
  email:
@@ -55,10 +111,15 @@ files:
55
111
  - lib/registrar/adapter.rb
56
112
  - lib/registrar/adapter/omni_auth.rb
57
113
  - lib/registrar/debug.rb
58
- - lib/registrar/profile_builder.rb
59
- - lib/registrar/profile_gateway_stub.rb
114
+ - lib/registrar/gatekeeper.rb
115
+ - lib/registrar/profile_factory.rb
60
116
  - lib/registrar/version.rb
61
117
  - registrar.gemspec
118
+ - spec/fixtures/omniauth_1_0_auth_hash_schema
119
+ - spec/gatekeeper_spec.rb
120
+ - spec/omni_auth_adapter_spec.rb
121
+ - spec/profile_factory_spec.rb
122
+ - spec/spec_helper.rb
62
123
  homepage: ''
63
124
  licenses:
64
125
  - MIT
@@ -83,4 +144,9 @@ rubygems_version: 2.2.2
83
144
  signing_key:
84
145
  specification_version: 4
85
146
  summary: 'Registrar: Standardized Multi-Provider Registration'
86
- test_files: []
147
+ test_files:
148
+ - spec/fixtures/omniauth_1_0_auth_hash_schema
149
+ - spec/gatekeeper_spec.rb
150
+ - spec/omni_auth_adapter_spec.rb
151
+ - spec/profile_factory_spec.rb
152
+ - spec/spec_helper.rb
@@ -1,27 +0,0 @@
1
- module Registrar
2
- class ProfileGatewayStub
3
- def call(auth_hash)
4
- Profile.new(auth_hash)
5
- end
6
-
7
- class Profile
8
- def initialize(auth_hash)
9
- provider_name = auth_hash['provider']['name']
10
- uid = auth_hash['provider']['uid']
11
-
12
- @internal_uid = 1
13
- @external_uid = "#{provider_name}_#{uid}"
14
- @name = auth_hash['profile']['name']
15
- end
16
-
17
- def to_hash
18
- {
19
- :internal_uid => @internal_uid,
20
- :external_uid => @external_uid,
21
- :access_token => 'verified',
22
- :name => @name
23
- }
24
- end
25
- end
26
- end
27
- end