registrar 0.0.5.alpha → 0.0.6.alpha

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
  SHA1:
3
- metadata.gz: d091f6c83a4e7b416182b5910b8c90d8f9eb6f39
4
- data.tar.gz: e9e16fb29574cd722dc7f10ef79c52442c6d9bb8
3
+ metadata.gz: f158247d28c1d8ffca5dee9112ff36823492c3a4
4
+ data.tar.gz: cafdb99ac046bdfff54e1d2ca0b361d580e1763b
5
5
  SHA512:
6
- metadata.gz: 8ae1e9736eb9c868a187ed47d12ec25e75aa7f8460e233fcad9ad4f1060f07a65e520f7bbb6b5b12716e531b4d3aa284feeeb8e52e10638f41f49f9a1168da32
7
- data.tar.gz: d2b069b93d8ca6dd43a521e94c8325dbd9cac596470d3f120cbe77a1ada426022d52606ba918b2ad30b56dc65fb4ea67447aa1e0f0b49142b15c9e5b31d79d18
6
+ metadata.gz: 491145017a0c347b688c3422424c1e43600b3c4ff7a8c49397c9de654f8f9370897b583d7aabf3739efef9fdf9b60244767348db8237da7129d97355002af116
7
+ data.tar.gz: 055012c4c8af534797078740fc75ab109c267118d894d7affc0b10d42d826d19e8ecf086977e1976fe8e9f88df3ae2d6e96248064a66aa44359ed9215830f7d3
@@ -0,0 +1,121 @@
1
+ module Registrar
2
+ module AuthBuilder
3
+ class OmniAuth
4
+ def initialize(app, time = Time)
5
+ @app = app
6
+ @time = time
7
+ end
8
+
9
+ def call(env)
10
+ try_to_normalize_auth(env)
11
+ @app.call(env)
12
+ end
13
+
14
+ private
15
+
16
+ def try_to_normalize_auth(env)
17
+ if env['omniauth.auth']
18
+ env['registrar.auth'] = Builder.build(env, @time)
19
+ end
20
+ end
21
+
22
+ class Builder
23
+ def self.build(env, time)
24
+ new(env, time).build
25
+ end
26
+
27
+ def initialize(env, time)
28
+ @env = env
29
+ @time = time
30
+ end
31
+
32
+ def build
33
+ Hash.new.tap do |schema|
34
+ schema['provider'] = provider
35
+ schema['profile'] = profile
36
+ schema['trace'] = trace
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def provider
43
+ {
44
+ 'name' => provider_name,
45
+ 'uid' => provider_uid,
46
+ 'access_token' => provider_access_token
47
+ }
48
+ end
49
+
50
+ def provider_name
51
+ auth['provider']
52
+ end
53
+
54
+ def provider_uid
55
+ auth['uid']
56
+ end
57
+
58
+ def provider_access_token
59
+ auth['credentials']['token']
60
+ end
61
+
62
+ def profile
63
+ {
64
+ 'name' => profile_name,
65
+ 'email' => profile_email,
66
+ 'location' => profile_location,
67
+ 'image' => profile_image
68
+ }
69
+ end
70
+
71
+ def profile_name
72
+ info['name']
73
+ end
74
+
75
+ def profile_email
76
+ info['email']
77
+ end
78
+
79
+ def profile_location
80
+ info['location']
81
+ end
82
+
83
+ def profile_image
84
+ info['image']
85
+ end
86
+
87
+ def info
88
+ auth['info'].to_hash
89
+ end
90
+
91
+ def trace
92
+ {
93
+ 'ip' => ip,
94
+ 'user_agent' => user_agent,
95
+ 'timestamp' => now
96
+ }
97
+ end
98
+
99
+ def ip
100
+ env['REMOTE_ADDR']
101
+ end
102
+
103
+ def user_agent
104
+ env['HTTP_USER_AGENT']
105
+ end
106
+
107
+ def now
108
+ @time.now.to_i.to_s
109
+ end
110
+
111
+ def auth
112
+ @auth ||= env['omniauth.auth']
113
+ end
114
+
115
+ def env
116
+ @env
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,6 @@
1
+ require 'registrar/auth_builder/omni_auth'
2
+
3
+ module Registrar
4
+ module AuthBuilder
5
+ end
6
+ end
@@ -0,0 +1,20 @@
1
+ require 'registrar/mapper/params'
2
+
3
+ module Registrar
4
+ module Mapper
5
+ class OmniAuth < Params
6
+ def initialize(app)
7
+ super(app, mapping)
8
+ end
9
+
10
+ private
11
+
12
+ def mapping
13
+ {
14
+ "profile#name" => "name",
15
+ "profile#email" => "email"
16
+ }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,62 @@
1
+ module Registrar
2
+ module Mapper
3
+ class Params
4
+ def initialize(app, mapping)
5
+ @app = app
6
+ @mapping = mapping
7
+ end
8
+
9
+ def call(env)
10
+ builder = Builder.new(env, @mapping)
11
+ builder.build_registrar_params
12
+ builder.define_params
13
+
14
+ @app.call(env)
15
+ end
16
+
17
+ private
18
+
19
+ class Builder
20
+ def initialize(env, mapping)
21
+ @env = env
22
+ @mapping = mapping
23
+ end
24
+
25
+ def build_registrar_params
26
+ @mapping.each do |from, to|
27
+ namespace_from, attr_from = from.split('#')
28
+ value = request.params[namespace_from]
29
+
30
+ if value.class != String && attr_from
31
+ value = request.params[namespace_from][attr_from]
32
+ end
33
+
34
+ namespace_to, attr_to = to.split('#')
35
+
36
+ if namespace_to
37
+ if attr_to
38
+ params[namespace_to][attr_to] = value
39
+ else
40
+ params[namespace_to] = value
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def define_params
47
+ params.each do |namespace, values|
48
+ request.update_param(namespace, values)
49
+ end
50
+ end
51
+
52
+ def request
53
+ @request ||= Rack::Request.new(@env)
54
+ end
55
+
56
+ def params
57
+ @params ||= Hash.new {|h,k| h[k] = {}}
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,7 @@
1
+ require 'registrar/mapper/params'
2
+ require 'registrar/mapper/omni_auth'
3
+
4
+ module Registrar
5
+ module Mapper
6
+ end
7
+ end
@@ -0,0 +1,78 @@
1
+ require 'omniauth'
2
+
3
+ module Registrar
4
+ module Middleware
5
+ def self.configure(&blk)
6
+ yield config
7
+
8
+ convert_params_to_registrar_schema
9
+ convert_registrar_schema_to_omniauth_schema
10
+ add_omniauth_strategies
11
+ convert_omniauth_schema_to_registrar_schema
12
+ add_registrar_handler
13
+ end
14
+
15
+ private
16
+
17
+ def self.convert_params_to_registrar_schema
18
+ config.middleware.use Registrar::Mapper::Params, config.attributes
19
+ end
20
+
21
+ def self.convert_registrar_schema_to_omniauth_schema
22
+ config.middleware.use Registrar::Mapper::OmniAuth
23
+ end
24
+
25
+ def self.add_omniauth_strategies
26
+ strategies = config.strategies
27
+
28
+ if strategies.respond_to?(:each)
29
+ strategies.each do |strategy|
30
+ add_omniauth_strategy(strategy)
31
+ end
32
+ else
33
+ add_omniauth_strategy(strategies)
34
+ end
35
+ end
36
+
37
+ def self.add_omniauth_strategy(strategy)
38
+ filename = "omniauth-#{strategy.gsub('_','-')}"
39
+ provider_name = strategy.gsub('-','_')
40
+
41
+ require filename
42
+
43
+ config.middleware.use ::OmniAuth::Builder do
44
+ provider provider_name
45
+ end
46
+ end
47
+
48
+ def self.convert_omniauth_schema_to_registrar_schema
49
+ config.middleware.use Registrar::AuthBuilder::OmniAuth
50
+ end
51
+
52
+ def self.add_registrar_handler
53
+ config.middleware.use Registrar::ProfileBuilder, config.handler
54
+ end
55
+
56
+ def self.config
57
+ @config ||= Registrar::Middleware::Config.new
58
+ end
59
+
60
+ class Config
61
+ def strategies(*args)
62
+ @strategies ||= args
63
+ end
64
+
65
+ def attributes(arg = nil)
66
+ @attributes ||= arg
67
+ end
68
+
69
+ def handler(arg = nil)
70
+ @handler ||= arg
71
+ end
72
+
73
+ def middleware(arg = nil)
74
+ @middleware ||= arg
75
+ end
76
+ end
77
+ end
78
+ end
@@ -1,5 +1,5 @@
1
1
  module Registrar
2
- class ProfileFactory
2
+ class ProfileBuilder
3
3
  def initialize(app, callable)
4
4
  @app = app
5
5
  @callable = callable
@@ -1,3 +1,3 @@
1
1
  module Registrar
2
- VERSION = "0.0.5.alpha"
2
+ VERSION = "0.0.6.alpha"
3
3
  end
data/lib/registrar.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require "registrar/version"
2
- require 'registrar/adapter'
3
- require 'registrar/gatekeeper'
4
- require 'registrar/profile_factory'
2
+ require 'registrar/mapper'
3
+ require 'registrar/auth_builder'
4
+ require 'registrar/middleware'
5
+ require 'registrar/profile_builder'
5
6
  require 'registrar/debug'
6
7
 
7
8
  module Registrar
data/registrar.gemspec CHANGED
@@ -18,10 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "omniauth"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.7"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
23
25
  spec.add_development_dependency "rack"
24
26
  spec.add_development_dependency "rack-test"
25
27
  spec.add_development_dependency "minitest"
26
- spec.add_development_dependency "omniauth"
27
28
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require 'omniauth'
3
3
 
4
- class OmniAuthAdapterSpec < Spec
4
+ class OmniAuthAuthBuilderSpec < Spec
5
5
  it 'normalizes OmniAuth Auth Hash Schema 1.0 and later' do
6
6
  get '/'
7
7
  assert_normalizes_auth env
@@ -12,6 +12,7 @@ class OmniAuthAdapterSpec < Spec
12
12
  def assert_normalizes_auth(env)
13
13
  assert_normalizes_provider(env)
14
14
  assert_normalizes_profile(env)
15
+ assert_adds_trace(env)
15
16
  end
16
17
 
17
18
  def assert_normalizes_provider(env)
@@ -19,7 +20,7 @@ class OmniAuthAdapterSpec < Spec
19
20
  {
20
21
  'name' => 'facebook',
21
22
  'uid' => '100000100277322',
22
- 'access_token' => 'CAACEdEose0cBAGxad8Y14t3tu3kMlA3SgnxZBfZCQcSyb9hnn1kNZCpBDzZBIpNNpYSsJTFDs4Ar4ZBZBoMRRAzzFJGhPW4mtM1Rmm62BsQiZCkpJpG1pAC8tslbD3s3BiSYEGdjhOZBt7QVHZB1Sea14ojiZAOZBhFWi1BZBfIGgRI3El6FmEYMKPQ2XgJR0ottKdTQBR1ia2NuZABxpsAMYY8Sb'
23
+ 'access_token' => 'CAACEdEose0cBAJN79Dw0mRoZCGz6ZBBq8VrIkSjh5zQG5fWC156X1uhEUUcjk5bOTXfeeAPDYsFLN48WSpZA73q9D4BmQkD73PYDPYKkjhLH90SPSxctWAZBZB50DNh8TAgxZAJ4JmpEbRtpuex3ovEMGoB9tsQlmDQu1ZAj5Qy2WDSaB88nxLcuaClZAXk2ZCqcuLLjO5kiZAZCyYCET9FCyMlmfzh1HhabapyGSm5DQrDqGN8ZCpm7oUkmfpz3prmShOcZD'
23
24
  }, env['registrar.auth']['provider'])
24
25
  end
25
26
 
@@ -27,16 +28,21 @@ class OmniAuthAdapterSpec < Spec
27
28
  assert_equal(
28
29
  {
29
30
  "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
31
+ "email" => "janowiesniak@gmx.de",
32
+ "location" => "Bochum, Germany",
33
+ "image" => "http://graph.facebook.com/100000100277322/picture"
37
34
  }, env['registrar.auth']['profile'])
38
35
  end
39
36
 
37
+ def assert_adds_trace(env)
38
+ assert_equal(
39
+ {
40
+ "ip" => "127.0.0.2",
41
+ "user_agent" => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.91 Safari/537.36",
42
+ "timestamp" => "1427789796"
43
+ }, env['registrar.auth']['trace'])
44
+ end
45
+
40
46
  def env
41
47
  last_request.env
42
48
  end
@@ -53,6 +59,36 @@ class OmniAuthAdapterSpec < Spec
53
59
  builder.to_app
54
60
  end
55
61
 
62
+ class TraceStub
63
+ def initialize(app)
64
+ @app = app
65
+ end
66
+
67
+ def call(env)
68
+ env['HTTP_USER_AGENT'] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.91 Safari/537.36"
69
+ env['REMOTE_ADDR'] = "127.0.0.2"
70
+ @app.call(env)
71
+ end
72
+ end
73
+
74
+ class TimeStub
75
+ def now
76
+ Time.at(1427789796)
77
+ end
78
+ end
79
+
80
+ def env
81
+ last_request.env
82
+ end
83
+
84
+ def response
85
+ last_response
86
+ end
87
+
88
+ def app
89
+ @app ||= build_app
90
+ end
91
+
56
92
  class OmniAuthFacebookStub
57
93
  def initialize(app)
58
94
  @app = app
@@ -84,9 +120,10 @@ class OmniAuthAdapterSpec < Spec
84
120
 
85
121
  def builder
86
122
  Rack::Builder.new do
123
+ use TraceStub
87
124
  use OmniAuthFacebookStub
88
125
 
89
- use Registrar::Adapter::OmniAuth
126
+ use Registrar::AuthBuilder::OmniAuth, TimeStub.new
90
127
 
91
128
  app = Proc.new do |env|
92
129
  ['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'omniauth'
3
+
4
+ class ParamsMapperSpec < Spec
5
+ it 'normalizes params to registrar schema' do
6
+ get '/'
7
+ assert_normalizes_params Rack::Request.new(env).params
8
+ end
9
+
10
+ private
11
+
12
+ def assert_normalizes_params(params)
13
+ assert_equal '1', params['uid']
14
+ assert_equal 'facebook', params['provider']['name']
15
+ assert_equal 'jan@featurefabrik.de', params['contact']
16
+ assert_equal '221b Baker Street', params['info']['location']
17
+ end
18
+
19
+ def env
20
+ last_request.env
21
+ end
22
+
23
+ def response
24
+ last_response
25
+ end
26
+
27
+ def app
28
+ @app ||= build_app
29
+ end
30
+
31
+ def build_app
32
+ builder.to_app
33
+ end
34
+
35
+ class ParamsStub
36
+ def initialize(app)
37
+ @app = app
38
+ end
39
+
40
+ def call(env)
41
+ request = Rack::Request.new(env)
42
+ request.update_param('id', '1')
43
+ request.update_param('provider', 'facebook')
44
+ request.update_param('info', {
45
+ 'email' => 'jan@featurefabrik.de',
46
+ 'address' => '221b Baker Street'
47
+ }
48
+ )
49
+ @app.call(env)
50
+ end
51
+ end
52
+
53
+ def builder
54
+ Rack::Builder.new do
55
+ use ParamsStub
56
+
57
+ use Registrar::Mapper::Params, {
58
+ "id" => "uid",
59
+ "provider" => "provider#name",
60
+ "info#email" => "contact",
61
+ "info#address" => "info#location"
62
+ }
63
+
64
+ app = Proc.new do |env|
65
+ ['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
66
+ end
67
+
68
+ run app
69
+ end
70
+ end
71
+ end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- class ProfileFactorySpec < Spec
4
- it 'passes auth hash to callable and stores return value in the env' do
3
+ class ProfileBuilderSpec < Spec
4
+ it 'passes registrar auth hash to callable and stores return value in the env' do
5
5
  get '/'
6
6
  assert_stores_profile env
7
7
  end
@@ -94,7 +94,7 @@ class ProfileFactorySpec < Spec
94
94
  Rack::Builder.new do
95
95
  use RegistrarAuthStub
96
96
 
97
- use Registrar::ProfileFactory, ProfileGatewayStub.new
97
+ use Registrar::ProfileBuilder, ProfileGatewayStub.new
98
98
 
99
99
  app = Proc.new do |env|
100
100
  ['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: registrar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5.alpha
4
+ version: 0.0.6.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-19 00:00:00.000000000 Z
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -80,20 +94,6 @@ dependencies:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  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'
97
97
  description: Registrar standardizes Authentication Responses through Rack Middleware
98
98
  and works well with common authentication mechanisms like OmniAuth.
99
99
  email:
@@ -108,18 +108,20 @@ files:
108
108
  - README.md
109
109
  - Rakefile
110
110
  - lib/registrar.rb
111
- - lib/registrar/adapter.rb
112
- - lib/registrar/adapter/omni_auth.rb
113
- - lib/registrar/adapter/rails.rb
111
+ - lib/registrar/auth_builder.rb
112
+ - lib/registrar/auth_builder/omni_auth.rb
114
113
  - lib/registrar/debug.rb
115
- - lib/registrar/gatekeeper.rb
116
- - lib/registrar/profile_factory.rb
114
+ - lib/registrar/mapper.rb
115
+ - lib/registrar/mapper/omni_auth.rb
116
+ - lib/registrar/mapper/params.rb
117
+ - lib/registrar/middleware.rb
118
+ - lib/registrar/profile_builder.rb
117
119
  - lib/registrar/version.rb
118
120
  - registrar.gemspec
119
121
  - spec/fixtures/omniauth_1_0_auth_hash_schema
120
- - spec/gatekeeper_spec.rb
121
122
  - spec/omni_auth_adapter_spec.rb
122
- - spec/profile_factory_spec.rb
123
+ - spec/params_mapper_spec.rb
124
+ - spec/profile_builder_spec.rb
123
125
  - spec/spec_helper.rb
124
126
  homepage: ''
125
127
  licenses:
@@ -147,7 +149,7 @@ specification_version: 4
147
149
  summary: 'Registrar: Standardized Multi-Provider Registration'
148
150
  test_files:
149
151
  - spec/fixtures/omniauth_1_0_auth_hash_schema
150
- - spec/gatekeeper_spec.rb
151
152
  - spec/omni_auth_adapter_spec.rb
152
- - spec/profile_factory_spec.rb
153
+ - spec/params_mapper_spec.rb
154
+ - spec/profile_builder_spec.rb
153
155
  - spec/spec_helper.rb
@@ -1,67 +0,0 @@
1
- module Registrar
2
- module Adapter
3
- class OmniAuth
4
- def initialize(app)
5
- @app = app
6
- end
7
-
8
- def call(env)
9
- try_to_normalize_auth(env)
10
- @app.call(env)
11
- end
12
-
13
- private
14
-
15
- def try_to_normalize_auth(env)
16
- if auth = env['omniauth.auth']
17
- env['registrar.auth'] = AuthNormalizer.normalized(auth)
18
- end
19
- end
20
-
21
- class AuthNormalizer
22
- def self.normalized(auth)
23
- new(auth).normalize
24
- end
25
-
26
- def initialize(auth)
27
- @auth = auth
28
- end
29
-
30
- def normalize
31
- normalized = {}
32
- normalized['provider'] = normalize_provider
33
- normalized['profile'] = normalize_profile
34
- normalized
35
- end
36
-
37
- private
38
-
39
- attr_reader :auth
40
-
41
- def normalize_provider
42
- {
43
- 'name' => provider_name,
44
- 'uid' => provider_uid,
45
- 'access_token' => access_token
46
- }
47
- end
48
-
49
- def provider_name
50
- auth['provider']
51
- end
52
-
53
- def provider_uid
54
- auth['uid']
55
- end
56
-
57
- def access_token
58
- auth['credentials']['token']
59
- end
60
-
61
- def normalize_profile
62
- auth['info'].to_hash
63
- end
64
- end
65
- end
66
- end
67
- end
@@ -1,94 +0,0 @@
1
- module Registrar
2
- class Profile < Delegator
3
- def initialize(obj)
4
- super
5
- @delegate_sd_obj = obj
6
- end
7
-
8
- def __getobj__
9
- @delegate_sd_obj
10
- end
11
-
12
- def __setobj__(obj)
13
- @delegate_sd_obj = obj
14
- end
15
- end
16
-
17
- module Adapter
18
- module Rails
19
- def self.included(klass)
20
- klass.include InstanceMethods
21
- klass.before_action :try_to_store_registrar_profile
22
-
23
- klass.class_eval do
24
- helper_method :current_profile
25
- helper_method :current_profile?
26
- helper_method :logged_in?
27
-
28
- helper_method :registrar_profile
29
- helper_method :registrar_profile?
30
-
31
- helper_method :authentication_phase?
32
-
33
- helper_method :presentable_authentication
34
- end
35
- end
36
-
37
- module InstanceMethods
38
- REGISTRAR_PROFILE_KEY = 'registrar.profile'
39
-
40
- def try_to_store_registrar_profile
41
- if registrar_profile = request.env['registrar.profile']
42
- store_registrar_profile(registrar_profile)
43
- end
44
- end
45
-
46
- def store_registrar_profile(registrar_profile)
47
- session[REGISTRAR_PROFILE_KEY] = registrar_profile
48
- end
49
-
50
- def current_profile
51
- return @current_profile if @current_profile
52
- try_to_set_current_profile
53
- end
54
-
55
- def current_profile?
56
- !!current_profile
57
- end
58
- alias_method :logged_in?, :current_profile?
59
-
60
- def try_to_set_current_profile
61
- if registrar_profile?
62
- @current_user = build_profile(registrar_profile)
63
- end
64
- end
65
-
66
- def registrar_profile?
67
- !!registrar_profile
68
- end
69
-
70
- def registrar_profile
71
- session[REGISTRAR_PROFILE_KEY]
72
- end
73
-
74
- def build_profile(profile)
75
- ::Registrar::Profile.new(profile)
76
- end
77
-
78
- def authentication_phase?
79
- params[:controller] == 'authentication' && params[:action] = 'callback'
80
- end
81
-
82
- def presentable_authentication
83
- {
84
- 'env.omniauth.auth' => request.env['omniauth.auth'],
85
- 'env.registrar.auth' => request.env['registrar.auth'],
86
- 'env.registrar.profile' => request.env['registrar.profile'],
87
- 'session.registrar_profile' => registrar_profile,
88
- 'runtime.current_profile' => current_profile
89
- }
90
- end
91
- end
92
- end
93
- end
94
- end
@@ -1,7 +0,0 @@
1
- require 'registrar/adapter/omni_auth'
2
- require 'registrar/adapter/rails'
3
-
4
- module Registrar
5
- module Adapter
6
- end
7
- end
@@ -1,82 +0,0 @@
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,58 +0,0 @@
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