portarius 0.0.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.
@@ -0,0 +1 @@
1
+ require "portarius/railtie" if defined?(Rails)
@@ -0,0 +1,8 @@
1
+ require "rails"
2
+ require 'rack/portarius'
3
+
4
+ module Portarius
5
+ class Railtie < Rails::Railtie #:nodoc:
6
+ config.app_middleware.use "Rack::Portarius"
7
+ end
8
+ end
@@ -0,0 +1,27 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
5
+ <title>Sie sind für diesen Bereich nicht autorisiert</title>
6
+ <style type="text/css">
7
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
8
+ div.dialog {
9
+ width: 25em;
10
+ padding: 0 4em;
11
+ margin: 4em auto 0 auto;
12
+ border: 1px solid #ccc;
13
+ border-right-color: #999;
14
+ border-bottom-color: #999;
15
+ }
16
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
17
+ </style>
18
+ </head>
19
+
20
+ <body>
21
+ <div class="dialog">
22
+ <h1>Sie sind für diesen Bereich nicht autorisiert.</h1>
23
+ <p>Sie haben keinen Zugriff auf diesen Bereich.<br />
24
+ Bitte prüfen Sie, ob die Adresse korrekt ist.</p>
25
+ </div>
26
+ </body>
27
+ </html>
@@ -0,0 +1,23 @@
1
+ module Rack
2
+ class Portarius
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ return unauthorized unless env["REMOTE_USER"].nil? || env["REMOTE_USER"] == env["SERVER_NAME"].split(".").first
9
+ @app.call(env)
10
+ end
11
+
12
+ private
13
+
14
+ def unauthorized()
15
+ file_401 = ::File.expand_path('../401.html', __FILE__)
16
+ return [ 401,
17
+ { 'Content-Type' => 'text/html',
18
+ 'Content-Length' => ::File.size(file_401).to_s },
19
+ [::File.read(file_401)]
20
+ ]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require "rack/portarius"
3
+ require 'rack/auth/basic'
4
+ require 'rack/mock'
5
+
6
+ describe Rack::Portarius do
7
+ let(:customers) { %w(customer1 customer2) }
8
+
9
+ def unprotected_app
10
+ lambda { |env| [ 200, {'Content-Type' => 'text/plain'}, ["Hi #{env['REMOTE_USER']}"] ] }
11
+ end
12
+
13
+ def portarius_app
14
+ Rack::Portarius.new(unprotected_app)
15
+ end
16
+
17
+ def protected_app
18
+ Rack::Auth::Basic.new(portarius_app) { |username, password| customers.include?(username) }
19
+ end
20
+
21
+ before do
22
+ @request = Rack::MockRequest.new(protected_app)
23
+ end
24
+
25
+ def request_with_basic_auth(username, server_name, &block)
26
+ request({'SERVER_NAME' => server_name, 'HTTP_AUTHORIZATION' => 'Basic ' + ["#{username}:some_passwd"].pack("m*")}, &block)
27
+ end
28
+
29
+ def request(headers = {})
30
+ yield @request.get('/', headers)
31
+ end
32
+
33
+ it 'returns application output if subdomain matches username of authenticated user' do
34
+ request_with_basic_auth 'customer1', 'customer1.test.host' do |response|
35
+ response.status.should == 200
36
+ response.body.to_s.should == 'Hi customer1'
37
+ end
38
+ end
39
+
40
+ it 'returns application output if no user is authenticated' do
41
+ @request = Rack::MockRequest.new(portarius_app)
42
+ request 'SERVER_NAME' => 'customer1.test.host' do |response|
43
+ response.status.should == 200
44
+ response.body.to_s.should == 'Hi '
45
+ end
46
+ end
47
+
48
+ it 'denies access if subdomain does not match username of authenticated user' do
49
+ request_with_basic_auth 'customer2', 'customer1.test.host' do |response|
50
+ response.status.should == 401
51
+ response.body.to_s.should =~ /nicht autorisiert/
52
+ end
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: portarius
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Infopark AG
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-17 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rack-test
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 5
30
+ segments:
31
+ - 0
32
+ - 5
33
+ - 7
34
+ version: 0.5.7
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rack
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 27
46
+ segments:
47
+ - 1
48
+ - 2
49
+ - 2
50
+ version: 1.2.2
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rack
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 27
62
+ segments:
63
+ - 1
64
+ - 2
65
+ - 2
66
+ version: 1.2.2
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ description: A doorman for your Rack application. Checks if REMOTE_USER matches the subdomain. Requires some authentication means to set REMOTE_USER.
70
+ email: info@infopark.de
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files: []
76
+
77
+ files:
78
+ - lib/portarius.rb
79
+ - lib/portarius/railtie.rb
80
+ - lib/rack/401.html
81
+ - lib/rack/portarius.rb
82
+ - spec/portarius_spec.rb
83
+ has_rdoc: true
84
+ homepage: http://www.infopark.de
85
+ licenses: []
86
+
87
+ post_install_message:
88
+ rdoc_options:
89
+ - --charset=UTF-8
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.3.7
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Rack::Portarius middleware + initializer for Rails
117
+ test_files:
118
+ - spec/portarius_spec.rb