picnic 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +3 -0
- data/LICENSE.txt +165 -0
- data/Manifest.txt +28 -0
- data/README.txt +31 -0
- data/Rakefile +58 -0
- data/lib/picnic.rb +122 -0
- data/lib/picnic/authentication.rb +80 -0
- data/lib/picnic/cli.rb +112 -0
- data/lib/picnic/conf.rb +133 -0
- data/lib/picnic/controllers.rb +31 -0
- data/lib/picnic/postambles.rb +227 -0
- data/lib/picnic/service_control.rb +269 -0
- data/lib/picnic/utils.rb +36 -0
- data/lib/picnic/version.rb +9 -0
- data/setup.rb +1585 -0
- data/test/picnic_test.rb +11 -0
- data/test/test_helper.rb +2 -0
- data/vendor/camping-1.5.180/CHANGELOG +99 -0
- data/vendor/camping-1.5.180/COPYING +18 -0
- data/vendor/camping-1.5.180/README +119 -0
- data/vendor/camping-1.5.180/Rakefile +117 -0
- data/vendor/camping-1.5.180/lib/camping-unabridged.rb +762 -0
- data/vendor/camping-1.5.180/lib/camping.rb +55 -0
- data/vendor/camping-1.5.180/lib/camping/db.rb +78 -0
- data/vendor/camping-1.5.180/lib/camping/fastcgi.rb +244 -0
- data/vendor/camping-1.5.180/lib/camping/reloader.rb +163 -0
- data/vendor/camping-1.5.180/lib/camping/session.rb +123 -0
- data/vendor/camping-1.5.180/lib/camping/webrick.rb +65 -0
- metadata +77 -0
data/CHANGELOG.txt
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
CHANGELOG.txt
|
2
|
+
LICENSE.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
lib/picnic.rb
|
7
|
+
lib/picnic/authentication.rb
|
8
|
+
lib/picnic/cli.rb
|
9
|
+
lib/picnic/conf.rb
|
10
|
+
lib/picnic/controllers.rb
|
11
|
+
lib/picnic/postambles.rb
|
12
|
+
lib/picnic/service_control.rb
|
13
|
+
lib/picnic/utils.rb
|
14
|
+
lib/picnic/version.rb
|
15
|
+
setup.rb
|
16
|
+
test/picnic_test.rb
|
17
|
+
test/test_helper.rb
|
18
|
+
vendor/camping-1.5.180/CHANGELOG
|
19
|
+
vendor/camping-1.5.180/COPYING
|
20
|
+
vendor/camping-1.5.180/README
|
21
|
+
vendor/camping-1.5.180/Rakefile
|
22
|
+
vendor/camping-1.5.180/lib/camping-unabridged.rb
|
23
|
+
vendor/camping-1.5.180/lib/camping.rb
|
24
|
+
vendor/camping-1.5.180/lib/camping/db.rb
|
25
|
+
vendor/camping-1.5.180/lib/camping/fastcgi.rb
|
26
|
+
vendor/camping-1.5.180/lib/camping/reloader.rb
|
27
|
+
vendor/camping-1.5.180/lib/camping/session.rb
|
28
|
+
vendor/camping-1.5.180/lib/camping/webrick.rb
|
data/README.txt
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
= Picnic
|
2
|
+
|
3
|
+
<b>Picnic makes it easy(ier) to distribute and run Camping-based applications as
|
4
|
+
well-behaved stand-alone Linux servers (although it works on Windows too).</b>
|
5
|
+
|
6
|
+
*Author*:: Matt Zukowski (matt at roughest dot net)
|
7
|
+
*Copyright*:: Copyright (c) 2007 Urbacon Ltd.
|
8
|
+
*License*:: GNU Lesser General Public License Version 3
|
9
|
+
*Homepage*:: http://camping-picnic.googlecode.com
|
10
|
+
|
11
|
+
For detailed information and usage examples, please see Picnic's Google Code page at:
|
12
|
+
<b>http://camping-picnic.googlecode.com</b>
|
13
|
+
|
14
|
+
For info on the Camping microframework, see:
|
15
|
+
http://code.whytheluckystiff.net/camping and
|
16
|
+
http://camping.rubyforge.org/files/README.html
|
17
|
+
|
18
|
+
== License
|
19
|
+
|
20
|
+
picnic is free software; you can redistribute it and/or modify
|
21
|
+
it under the terms of the GNU Lesser General Public License as published
|
22
|
+
by the Free Software Foundation; either version 3 of the License, or
|
23
|
+
(at your option) any later version.
|
24
|
+
|
25
|
+
Reststop is distributed in the hope that it will be useful,
|
26
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
27
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
28
|
+
GNU General Public License for more details.
|
29
|
+
|
30
|
+
You should have received a copy of the GNU Lesser General Public License
|
31
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/rdoctask'
|
8
|
+
require 'rake/contrib/rubyforgepublisher'
|
9
|
+
require 'fileutils'
|
10
|
+
require 'hoe'
|
11
|
+
include FileUtils
|
12
|
+
require File.join(File.dirname(__FILE__), 'lib', 'picnic', 'version')
|
13
|
+
|
14
|
+
AUTHOR = "Matt Zukowski"
|
15
|
+
EMAIL = "matt@roughest.net"
|
16
|
+
DESCRIPTION = "Camping for sissies"
|
17
|
+
GEM_NAME = "picnic"
|
18
|
+
RUBYFORGE_PROJECT = "picnic"
|
19
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
20
|
+
|
21
|
+
|
22
|
+
NAME = "picnic"
|
23
|
+
REV = nil
|
24
|
+
#REV = `svn info`[/Revision: (\d+)/, 1] rescue nil
|
25
|
+
VERS = ENV['VERSION'] || (Picnic::VERSION::STRING + (REV ? ".#{REV}" : ""))
|
26
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
|
27
|
+
RDOC_OPTS = ['--quiet', '--title', "picnic #{VERS} documentation",
|
28
|
+
"--opname", "index.html",
|
29
|
+
"--line-numbers",
|
30
|
+
"--main", "README",
|
31
|
+
"--inline-source"]
|
32
|
+
|
33
|
+
class Hoe
|
34
|
+
def extra_deps
|
35
|
+
@extra_deps.reject { |x| Array(x).first == 'hoe' }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Generate all the Rake tasks
|
40
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
41
|
+
hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
42
|
+
p.author = AUTHOR
|
43
|
+
p.description = DESCRIPTION
|
44
|
+
p.email = EMAIL
|
45
|
+
p.summary = DESCRIPTION
|
46
|
+
p.url = HOMEPATH
|
47
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
48
|
+
p.test_globs = ["test/**/*_test.rb"]
|
49
|
+
p.clean_globs = CLEAN #An array of file patterns to delete on clean.
|
50
|
+
|
51
|
+
# == Optional
|
52
|
+
#p.changes - A description of the release's latest changes.
|
53
|
+
#p.extra_deps - An array of rubygem dependencies.
|
54
|
+
#p.spec_extras - A hash of extra values to set in the gemspec.
|
55
|
+
|
56
|
+
# we now package camping-1.5.0.180 in the vendor directory
|
57
|
+
#p.extra_deps = ['camping']
|
58
|
+
end
|
data/lib/picnic.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
$: << File.dirname(File.expand_path(__FILE__))
|
2
|
+
$: << File.dirname(File.expand_path(__FILE__))+"/../vendor/camping-1.5.180/lib"
|
3
|
+
|
4
|
+
require 'camping'
|
5
|
+
require 'camping/db'
|
6
|
+
require 'camping/session'
|
7
|
+
|
8
|
+
require 'active_support' unless Object.const_defined?(:ActiveSupport)
|
9
|
+
|
10
|
+
require 'picnic/utils'
|
11
|
+
require 'picnic/conf'
|
12
|
+
require 'picnic/postambles'
|
13
|
+
require 'picnic/controllers'
|
14
|
+
|
15
|
+
|
16
|
+
class Module
|
17
|
+
|
18
|
+
# Adds Picnic functionality to a Camping-enabled module.
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
#
|
22
|
+
# Camping.goes :Blog
|
23
|
+
# Blog.picnic!
|
24
|
+
#
|
25
|
+
# Your <tt>Blog</tt> Camping app now has Picnic functionality.
|
26
|
+
def picnic!
|
27
|
+
include Picnic
|
28
|
+
|
29
|
+
puts "Adding Picnic functionality to #{self} from #{File.dirname(File.expand_path(__FILE__))}..."
|
30
|
+
self.module_eval do
|
31
|
+
# Initialize your application's logger.
|
32
|
+
# This is automatically done for you when you call #picnic!
|
33
|
+
# The logger is initialized based on your <tt>:log</tt> configuration.
|
34
|
+
# See <tt>config.example.yml</tt> for info on configuring the logger.
|
35
|
+
def init_logger
|
36
|
+
puts "Initializing #{self} logger..."
|
37
|
+
$LOG = self::Utils::Logger.new(self::Conf.log[:file])
|
38
|
+
$LOG.level = "#{self}::Utils::Logger::#{self::Conf.log[:level]}".constantize
|
39
|
+
end
|
40
|
+
module_function :init_logger
|
41
|
+
|
42
|
+
# Initialize your application's database logger.
|
43
|
+
# If enabled, all SQL queries going through ActiveRecord will be logged here.
|
44
|
+
def init_db_logger
|
45
|
+
begin
|
46
|
+
if self::Conf.db_log
|
47
|
+
log_file = self::Conf.db_log[:file] || "#{self.to_s.downcase}_db.log"
|
48
|
+
self::Models::Base.logger = Logger.new(log_file)
|
49
|
+
self::Models::Base.logger.level = "#{self}::Utils::Logger::#{self::Conf.db_log[:level] || 'DEBUG'}".constantize
|
50
|
+
end
|
51
|
+
rescue Errno::EACCES => e
|
52
|
+
$LOG.warn "Can't write to database log file at '#{log_file}': #{e}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
module_function :init_db_logger
|
56
|
+
|
57
|
+
# Enable authentication for your app.
|
58
|
+
#
|
59
|
+
# For example:
|
60
|
+
#
|
61
|
+
# Camping.goes :Blog
|
62
|
+
# Blog.picnic!
|
63
|
+
#
|
64
|
+
# $CONF[:authentication] ||= {:username => 'admin', :password => 'picnic'}
|
65
|
+
# Blog.authenticate_using :basic if Blog::Conf[:authentication]
|
66
|
+
#
|
67
|
+
# Note that in the above example we use the authentication configuration from
|
68
|
+
# your app's conf file. We specify default credentials for when your
|
69
|
+
# conf file doesn't define them.
|
70
|
+
#
|
71
|
+
# Currently only HTTP Basic authentication is available. See Picnic::Authentication
|
72
|
+
# for more info.
|
73
|
+
def authenticate_using(mod)
|
74
|
+
require 'picnic/authentication'
|
75
|
+
mod = "#{self}::Authentication::#{mod.to_s.camelize}".constantize unless mod.kind_of? Module
|
76
|
+
|
77
|
+
$LOG.info("Enabling authentication for all requests using #{mod.inspect}.")
|
78
|
+
|
79
|
+
module_eval do
|
80
|
+
include mod
|
81
|
+
end
|
82
|
+
end
|
83
|
+
module_function :authenticate_using
|
84
|
+
|
85
|
+
# Launches the web server to run your Picnic app.
|
86
|
+
# This method will continue to run as long as your server is running.
|
87
|
+
def start_picnic
|
88
|
+
require 'picnic/postambles'
|
89
|
+
self.extend self::Postambles
|
90
|
+
|
91
|
+
if $PID_FILE && !(self::Conf.server.to_s == 'mongrel' || self::Conf.server.to_s == 'webrick')
|
92
|
+
$LOG.warn("Unable to create a pid file. You must use mongrel or webrick for this feature.")
|
93
|
+
end
|
94
|
+
|
95
|
+
puts "\nStarting with configuration: #{$CONF.to_yaml}"
|
96
|
+
puts
|
97
|
+
|
98
|
+
# begin
|
99
|
+
raise NoMethodError if self::Conf.server.nil?
|
100
|
+
send(self::Conf.server)
|
101
|
+
# rescue NoMethodError => e
|
102
|
+
# # FIXME: this rescue can sometime report the incorrect error messages due to other underlying problems
|
103
|
+
# # raising a NoMethodError
|
104
|
+
# if Fluxr::Conf.server
|
105
|
+
# raise e, "The server setting '#{Fluxr::Conf.server}' in your config.yml file is invalid."
|
106
|
+
# else
|
107
|
+
# raise e, "You must have a 'server' setting in your config.yml file. Please see the Fluxr documentation."
|
108
|
+
# end
|
109
|
+
# end
|
110
|
+
end
|
111
|
+
module_function :start_picnic
|
112
|
+
|
113
|
+
c = File.dirname(File.expand_path(__FILE__))+'/picnic/controllers.rb'
|
114
|
+
p = IO.read(c).gsub("Picnic", self.to_s)
|
115
|
+
eval p, TOPLEVEL_BINDING
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
self::Conf.load(self)
|
120
|
+
init_logger
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Picnic #:nodoc:
|
2
|
+
# These modules (currently only one module, but more in the future) provide authentication
|
3
|
+
# for your Camping app.
|
4
|
+
#
|
5
|
+
# This code is based on Camping::BasicAuth written by Manfred Stienstra
|
6
|
+
# (see http://www.fngtps.com/2006/05/basic-authentication-for-camping).
|
7
|
+
#
|
8
|
+
# ----
|
9
|
+
#
|
10
|
+
# Picnic::Authentication::Basic can be mixed into a camping application to get Basic Authentication
|
11
|
+
# support in the application. The module defines a <tt>service</tt> method that only continues
|
12
|
+
# the request chain when proper credentials are given.
|
13
|
+
#
|
14
|
+
# == Getting Started
|
15
|
+
#
|
16
|
+
# To activate Basic Authentication for your application:
|
17
|
+
#
|
18
|
+
# 1. Picnic-fy your Camping app (e.g: <tt>Camping.goes :your_app; YourApp.picnic!</tt>)
|
19
|
+
# 2. Call <tt>authenticate_using <module></tt> (e.g: <tt>YourApp.authenticate_using :basic</tt>)
|
20
|
+
# 3. Define an <tt>authenticate</tt> method on your application module that takes a hash.
|
21
|
+
# The hash contains credentials like <tt>:username</tt>, <tt>:password</tt>, and <tt>:hostname</tt>,
|
22
|
+
# although future authentication modules may submit other credentials.
|
23
|
+
# The <tt>authenticate</tt> method should return true when the credentials are valid.
|
24
|
+
# Examples:
|
25
|
+
#
|
26
|
+
# module Blog
|
27
|
+
# def authenticate(credentials)
|
28
|
+
# credentials[:username] == 'admin' &&
|
29
|
+
# credentials[:password] == 'flapper30'
|
30
|
+
# end
|
31
|
+
# module_function :authenticate
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# or
|
35
|
+
#
|
36
|
+
# module Wiki
|
37
|
+
# def authenticate(credentials)
|
38
|
+
# u = credentials[:username]
|
39
|
+
# p = credentials[:password]
|
40
|
+
# Models::User.find_by_username_and_password u, p
|
41
|
+
# end
|
42
|
+
# module_function :authenticate
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# 4. <tt>service</tt> sets <tt>@credentials</tt> to the credentials of the person who logged in.
|
46
|
+
module Authentication
|
47
|
+
module Basic
|
48
|
+
require 'base64'
|
49
|
+
|
50
|
+
# Reads the username and password from the headers and returns them.
|
51
|
+
def read_credentials
|
52
|
+
if d = %w{REDIRECT_X_HTTP_AUTHORIZATION X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION}.inject([]) \
|
53
|
+
{ |d,h| env.has_key?(h) ? env[h].to_s.split : d }
|
54
|
+
u,p = ::Base64.decode64(d[1]).split(':')[0..1] if d[0] == 'Basic'
|
55
|
+
return {:username => u, :password => p}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# The <tt>service</tt> method, when mixed into your application module, wraps around the
|
60
|
+
# <tt>service</tt> method defined by Camping. It halts execution of the controllers when
|
61
|
+
# your <tt>authenticate</tt> method returns false. See the module documentation how to
|
62
|
+
# define your own <tt>authenticate</tt> method.
|
63
|
+
def service(*a)
|
64
|
+
@credentials = read_credentials || {}
|
65
|
+
app = self.class.name.gsub(/^(\w+)::.+$/, '\1')
|
66
|
+
if Kernel.const_get(app).authenticate(@credentials)
|
67
|
+
s = super(*a)
|
68
|
+
else
|
69
|
+
@status = 401
|
70
|
+
@headers['Content-type'] = @headers['Content-type'] || 'text/plain'
|
71
|
+
@headers['Status'] = 'Unauthorized'
|
72
|
+
@headers['WWW-Authenticate'] = "Basic realm=\"#{app}\""
|
73
|
+
@body = 'Unauthorized'
|
74
|
+
s = self
|
75
|
+
end
|
76
|
+
s
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|