rack-private 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Thibaud Guillaume-Gentil
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ = Rack::Private
2
+
3
+ Rack::Private middleware purpose to protect you Rack application from anonymous via a secret token form.
4
+
5
+ == Installation
6
+
7
+ gem install rack-private
8
+
9
+ == Usage
10
+
11
+ require 'rack-private'
12
+ use Rack::Private :code => 'secret'
13
+
14
+ You can also define multiple codes.
15
+
16
+ use Rack::Private :codes => ['secret', 'super-secret']
17
+
18
+ And provide your own template.
19
+
20
+ use Rack::Private :code => 'secret', :template_path => Rails.root.join("app/templates/private.html")
21
+
22
+ == Note on Patches/Pull Requests
23
+
24
+ * Fork the project.
25
+ * Make your feature addition or bug fix.
26
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
27
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
28
+ * Send me a pull request. Bonus points for topic branches.
29
+
30
+ == Contributors
31
+
32
+ * {Thibaud Guillaume-Gentil}[http://github.com/thibaudgg]
@@ -0,0 +1,46 @@
1
+ module Rack
2
+ class Private
3
+
4
+ def initialize(app, options = {})
5
+ @app = app
6
+ @options = options
7
+ end
8
+
9
+ def call(env)
10
+ request = Rack::Request.new(env)
11
+
12
+ # Check code in session and return Rails call if is valid
13
+ return @app.call(env) if already_auth?(request)
14
+
15
+ # If post method check :code_param value
16
+ if request.post? && code_valid?(request.params["private_code"])
17
+ request.session[:private_code] = request.params["private_code"]
18
+ [301, { 'Content-Type' => 'text/plain', 'Location' => '/' }, ['Secret code is valid.']] # Redirect if code is valid
19
+ else
20
+ render_private_form
21
+ end
22
+ end
23
+
24
+ private
25
+ # Render staging html file
26
+ def render_private_form
27
+ [200, {'Content-Type' => 'application/html'}, [
28
+ ::File.open(html_template, 'rb').read
29
+ ]]
30
+ end
31
+
32
+ def html_template
33
+ @options[:template_path] || ::File.expand_path('../private/index.html', __FILE__)
34
+ end
35
+
36
+ # Validate code
37
+ def code_valid?(code)
38
+ [@options[:code] || @options[:codes]].flatten.include?(code)
39
+ end
40
+
41
+ def already_auth?(request)
42
+ code_valid?(request.session[:private_code])
43
+ end
44
+ end
45
+ end
46
+
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Private access</title>
5
+ </head>
6
+ <body onload='document.forms[0].elements[0].focus();' style='text-align: center; margin-top:100px'>
7
+ <h1>Private access</h1>
8
+ <form action="/" method="post">
9
+ <p>
10
+ <label for="private_code" style='display: none'>Secret code</label>
11
+ <input type="password" name="private_code" placeholder="Secret code" />
12
+ <input type="submit" />
13
+ </p>
14
+ </form>
15
+ </body>
16
+ </html>
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Private
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-private
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Thibaud Guillaume-Gentil
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-02 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: shoulda
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 37
46
+ segments:
47
+ - 2
48
+ - 11
49
+ - 3
50
+ version: 2.11.3
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: 31
62
+ segments:
63
+ - 1
64
+ - 2
65
+ - 0
66
+ version: 1.2.0
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ - 5
81
+ - 4
82
+ version: 0.5.4
83
+ type: :development
84
+ version_requirements: *id004
85
+ description: Rack::Private middleware purpose to protect you Rack application from anonymous via a secret token form.
86
+ email:
87
+ - thibaud@thibaud.me
88
+ executables: []
89
+
90
+ extensions: []
91
+
92
+ extra_rdoc_files: []
93
+
94
+ files:
95
+ - lib/rack/private/index.html
96
+ - lib/rack/private/version.rb
97
+ - lib/rack/private.rb
98
+ - LICENSE
99
+ - README.rdoc
100
+ has_rdoc: true
101
+ homepage: http://github.com/thibaudgg/rack-private
102
+ licenses: []
103
+
104
+ post_install_message:
105
+ rdoc_options: []
106
+
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: 23
124
+ segments:
125
+ - 1
126
+ - 3
127
+ - 6
128
+ version: 1.3.6
129
+ requirements: []
130
+
131
+ rubyforge_project: rack-private
132
+ rubygems_version: 1.3.7
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: Protect you Rack application from anonymous.
136
+ test_files: []
137
+