devise-proxy 0.1.1 → 0.1.2

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/config.ru CHANGED
@@ -7,12 +7,12 @@ $LOAD_PATH.unshift lib_path
7
7
  require 'devise-proxy'
8
8
 
9
9
  # For development only.
10
- # use Rack::Reloader
10
+ # use Rack::Reloader
11
11
 
12
12
  config_file = File.join(current_dir, 'config', 'devise-proxy.yml')
13
13
  if File.exists? config_file
14
14
  conf = YAML::load(File.open(config_file))
15
- run DeviseProxy::Proxy.new(conf['backend']['hostname'], conf['backend']['port'])
15
+ run DeviseProxy::Proxy.new(conf)
16
16
  else
17
17
  puts "\nPlease create a config file prior to starting the proxy using the provided .sample file, at:\n\n\t#{config_file}\n\n"
18
18
  exit 1
@@ -1,3 +1,6 @@
1
- backend:
1
+ authentication:
2
2
  hostname: localhost
3
3
  port: 3000
4
+ forwarding:
5
+ hostname: localhost
6
+ port: 3000
data/devise-proxy.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "devise-proxy"
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Preston Lee"]
12
- s.date = "2012-10-29"
12
+ s.date = "2012-10-30"
13
13
  s.description = "A standalone Rack Middleware application that authenticates a username/password against a devise-powered backend application before forwarding the original HTTP request to the same application. Authentication is done on *every* request, so it is advisable to use devise-proxy sparingly."
14
14
  s.email = "conmotto@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -11,15 +11,24 @@ module DeviseProxy
11
11
  attr_accessor :port
12
12
  attr_accessor :authenticator
13
13
 
14
- def initialize(remote_host, remote_port)
15
- @host = remote_host
16
- @port = remote_port
17
- @authenticator = DeviseProxy::Authenticator.new(@host, port)
14
+ def initialize(options = {})
15
+ opts = {
16
+ 'authentication' => {
17
+ 'hostname' => 'localhost',
18
+ 'port' => 3000},
19
+ 'forwarding' => {
20
+ 'hostname' => 'localhost',
21
+ 'port' => 3000}
22
+ }.merge!(options)
23
+ @host = opts['forwarding']['hostname']
24
+ @port = opts['forwarding']['port']
25
+ # puts "CREDS: #{opts['authentication']['hostname']}:#{opts['authentication']['port']}"
26
+ @authenticator = DeviseProxy::Authenticator.new(opts['authentication']['hostname'], opts['authentication']['port'])
18
27
  end
19
28
 
20
29
  def call(env)
21
30
  req = Rack::Request.new(env)
22
- puts req.to_yaml
31
+ # puts req.to_yaml
23
32
 
24
33
  puts "User agent: #{env['HTTP_USER_AGENT']}"
25
34
 
@@ -35,40 +44,47 @@ module DeviseProxy
35
44
 
36
45
  puts "Client password credentials email: #{email}, password, #{password}"
37
46
 
38
- # Authenticate against the backend:
39
- auth = @authenticator.authenticate(email, password)
40
- allowed = auth[0].to_s == '201'
41
- puts "Authenticated #{allowed ? 'approved.' : 'denied!'}\n\n"
42
-
43
- result = [403, auth[1], [auth[2]]]
44
- if allowed
45
-
46
- method = req.request_method.downcase
47
- method[0..0] = method[0..0].upcase
48
-
49
- sub_request = Net::HTTP.const_get(method).new("#{req.path}#{"?" if req.query_string}#{req.query_string}")
50
-
51
- if sub_request.request_body_permitted? and req.body
52
- sub_request.body_stream = req.body
53
- sub_request.content_length = req.content_length
54
- sub_request.content_type = req.content_type
55
- end
56
-
57
- sub_request["X-Forwarded-For"] = (req.env["X-Forwarded-For"].to_s.split(/, +/) + [req.env['REMOTE_ADDR']]).join(", ")
58
- sub_request["Accept-Encoding"] = req.accept_encoding
59
- sub_request["Referer"] = req.referer
60
-
61
- sub_response = Net::HTTP.start(@host, @port) do |http|
62
- http.request(sub_request)
63
- end
64
-
65
- headers = {}
66
- sub_response.each_header do |k,v|
67
- headers[k] = v unless k.to_s =~ /cookie|content-length|transfer-encoding/i
68
- end
69
-
70
- result = [sub_response.code.to_i, headers, [sub_response.read_body]]
47
+ result = [502, {'Content-Type' => 'text/plain'}, ["HTTP 502 - Ah crap! The authenticating proxy seems to be having issues. :-/"]]
48
+ begin
49
+ # Authenticate against the backend:
50
+ auth = @authenticator.authenticate(email, password)
51
+ allowed = auth[0].to_s == '201'
52
+ puts "Authenticated #{allowed ? 'approved.' : 'denied!'}\n\n"
53
+
54
+ result = [403, auth[1], [auth[2]]]
55
+ if allowed
56
+
57
+ method = req.request_method.downcase
58
+ method[0..0] = method[0..0].upcase
59
+
60
+ sub_request = Net::HTTP.const_get(method).new("#{req.path}#{"?" if req.query_string}#{req.query_string}")
61
+
62
+ if sub_request.request_body_permitted? and req.body
63
+ sub_request.body_stream = req.body
64
+ sub_request.content_length = req.content_length
65
+ sub_request.content_type = req.content_type
66
+ end
67
+
68
+ sub_request["X-Forwarded-For"] = (req.env["X-Forwarded-For"].to_s.split(/, +/) + [req.env['REMOTE_ADDR']]).join(", ")
69
+ sub_request["Accept-Encoding"] = req.accept_encoding
70
+ sub_request["Referer"] = req.referer
71
+
72
+ sub_response = Net::HTTP.start(@host, @port) do |http|
73
+ http.request(sub_request)
74
+ end
75
+
76
+ headers = {}
77
+ sub_response.each_header do |k,v|
78
+ headers[k] = v unless k.to_s =~ /cookie|content-length|transfer-encoding/i
79
+ end
80
+
81
+ result = [sub_response.code.to_i, headers, [sub_response.read_body]]
82
+ end
83
+ rescue
84
+ # Probably an I/O issue connecting to the authentication service.
71
85
  end
86
+
87
+
72
88
  else
73
89
  # The client needs to retry, next time sending proxy credentials!
74
90
  headers = {}
@@ -6,7 +6,7 @@ class TestDeviseProxy < Test::Unit::TestCase
6
6
 
7
7
  setup do
8
8
  @host = 'localhost'
9
- @port = '3000'
9
+ @port = 3000
10
10
  end
11
11
 
12
12
  should "create multiple authenticators cleanly" do
@@ -16,7 +16,7 @@ class TestDeviseProxy < Test::Unit::TestCase
16
16
  end
17
17
 
18
18
  should 'create proxy cleanly' do
19
- proxy = DeviseProxy::Proxy.new(@host, @port)
19
+ proxy = DeviseProxy::Proxy.new({})
20
20
  assert_equal proxy.host, @host
21
21
  assert_equal proxy.port, @port
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-29 00:00:00.000000000 Z
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -115,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
115
  version: '0'
116
116
  segments:
117
117
  - 0
118
- hash: -4515847689756379363
118
+ hash: 1399324303159525473
119
119
  required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  none: false
121
121
  requirements: