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 +1 -1
- data/config.ru +2 -2
- data/config/devise-proxy.yml.sample +4 -1
- data/devise-proxy.gemspec +2 -2
- data/lib/devise-proxy/proxy.rb +54 -38
- data/test/test_devise-proxy.rb +2 -2
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.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
|
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
|
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.
|
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-
|
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 = [
|
data/lib/devise-proxy/proxy.rb
CHANGED
@@ -11,15 +11,24 @@ module DeviseProxy
|
|
11
11
|
attr_accessor :port
|
12
12
|
attr_accessor :authenticator
|
13
13
|
|
14
|
-
def initialize(
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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 = {}
|
data/test/test_devise-proxy.rb
CHANGED
@@ -6,7 +6,7 @@ class TestDeviseProxy < Test::Unit::TestCase
|
|
6
6
|
|
7
7
|
setup do
|
8
8
|
@host = 'localhost'
|
9
|
-
@port =
|
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(
|
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.
|
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-
|
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:
|
118
|
+
hash: 1399324303159525473
|
119
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
120
|
none: false
|
121
121
|
requirements:
|