bmarzolf-picnic 0.8.0.20090420

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/CHANGELOG.txt +1 -0
  2. data/History.txt +78 -0
  3. data/LICENSE.txt +165 -0
  4. data/Manifest.txt +45 -0
  5. data/README.txt +31 -0
  6. data/Rakefile +64 -0
  7. data/lib/picnic/authentication.rb +254 -0
  8. data/lib/picnic/cli.rb +165 -0
  9. data/lib/picnic/conf.rb +135 -0
  10. data/lib/picnic/controllers.rb +4 -0
  11. data/lib/picnic/logger.rb +41 -0
  12. data/lib/picnic/server.rb +99 -0
  13. data/lib/picnic/service_control.rb +274 -0
  14. data/lib/picnic/version.rb +9 -0
  15. data/lib/picnic.rb +11 -0
  16. data/setup.rb +1585 -0
  17. data/test/picnic_test.rb +11 -0
  18. data/test/test_helper.rb +2 -0
  19. data/vendor/camping-2.0.20090420/CHANGELOG +118 -0
  20. data/vendor/camping-2.0.20090420/COPYING +18 -0
  21. data/vendor/camping-2.0.20090420/README +82 -0
  22. data/vendor/camping-2.0.20090420/Rakefile +180 -0
  23. data/vendor/camping-2.0.20090420/bin/camping +97 -0
  24. data/vendor/camping-2.0.20090420/doc/camping.1.gz +0 -0
  25. data/vendor/camping-2.0.20090420/examples/README +5 -0
  26. data/vendor/camping-2.0.20090420/examples/blog.rb +375 -0
  27. data/vendor/camping-2.0.20090420/examples/campsh.rb +629 -0
  28. data/vendor/camping-2.0.20090420/examples/tepee.rb +242 -0
  29. data/vendor/camping-2.0.20090420/extras/Camping.gif +0 -0
  30. data/vendor/camping-2.0.20090420/extras/permalink.gif +0 -0
  31. data/vendor/camping-2.0.20090420/lib/camping/ar/session.rb +132 -0
  32. data/vendor/camping-2.0.20090420/lib/camping/ar.rb +78 -0
  33. data/vendor/camping-2.0.20090420/lib/camping/mab.rb +26 -0
  34. data/vendor/camping-2.0.20090420/lib/camping/reloader.rb +184 -0
  35. data/vendor/camping-2.0.20090420/lib/camping/server.rb +159 -0
  36. data/vendor/camping-2.0.20090420/lib/camping/session.rb +75 -0
  37. data/vendor/camping-2.0.20090420/lib/camping-unabridged.rb +630 -0
  38. data/vendor/camping-2.0.20090420/lib/camping.rb +52 -0
  39. data/vendor/camping-2.0.20090420/setup.rb +1551 -0
  40. data/vendor/camping-2.0.20090420/test/apps/env_debug.rb +65 -0
  41. data/vendor/camping-2.0.20090420/test/apps/forms.rb +95 -0
  42. data/vendor/camping-2.0.20090420/test/apps/misc.rb +86 -0
  43. data/vendor/camping-2.0.20090420/test/apps/sessions.rb +38 -0
  44. data/vendor/camping-2.0.20090420/test/test_camping.rb +54 -0
  45. metadata +140 -0
data/CHANGELOG.txt ADDED
@@ -0,0 +1 @@
1
+ See History.txt
data/History.txt ADDED
@@ -0,0 +1,78 @@
1
+ === 0.8.0 :: 2009-03-18
2
+
3
+ * Overhauled for Camping 2.0. Major changes throughout. Some things to
4
+ watch out for:
5
+ * Your app's bin script should now specify :app_file instead of :app_path,
6
+ and :app_file should point to your Camping app's main .rb file.
7
+ * Postamples for Mongrel and WEBrick are gone, since everything is now
8
+ handled by Rack. It is also now possible to run Picnic apps on Phusion
9
+ Passenger (mod_rails/mod_rack).
10
+
11
+ === 0.7.1 :: 2008-11-10
12
+
13
+ * Fixed config file loading problems rooted in $APP_PATH inconsistencies.
14
+ $APP_PATH is now set to the root of the application installation, rather than
15
+ the bin or lib subdirectory.
16
+
17
+ === 0.7.0 :: 2008-10-28
18
+
19
+ * Can now configure the CLI aspect of an app to respond to additional command-
20
+ line flags. This is done by passing a block of OptionParser calls as
21
+ an :extra_cli_options parameter to the Cli initializer.
22
+ * activerecord is no longer a requirement. However you should make sure that
23
+ you take care of loading activerecord in your app if you intend to use
24
+ Camping's database functionality.
25
+
26
+ === 0.6.5 :: 2008-09-18
27
+
28
+ * Fixed compatibility with ActiveSupport 2.1.
29
+
30
+ === 0.6.4 :: 2008-05-26
31
+
32
+ * Patched bundled Camping library to allow setting expiry time on cookies.
33
+ * CLI initializer can now be fed an alternate module name for your application.
34
+ This will override the module that would have been guessed based on the
35
+ app's name.
36
+ * Fixed bug where HTTPS operation didn't work under Webrick. You should now
37
+ again be able to serve over HTTPS when using webrick by supplying the
38
+ ssl_cert configuration option.
39
+ See: http://code.google.com/p/rubycas-server/issues/detail?id=45
40
+ * Fixed bug where the log file was being truncuated whenever the server
41
+ was started in daemon mode.
42
+
43
+ === 0.6.3 :: 2008-03-14
44
+
45
+ * Fixed bug in mongrel postamble that prevented the server from starting when
46
+ the log level was set to DEBUG.
47
+
48
+ === 0.6.2 :: 2008-03-06
49
+
50
+ * Fixed some loading problems having to do with the new CAS authenticator
51
+ introduced in 0.6.0.
52
+
53
+ === 0.6.1 :: 2008-02-28
54
+
55
+ * Fixed bug introduced in 0.6.0 where webrick and mongrel postambles were
56
+ broken for apps that don't define any public directories.
57
+ * Each public directory mount is now logged during startup. Also resolved
58
+ some potential issues with the uri_path config option. Multiple /'s
59
+ in the path are now automatically removed.
60
+
61
+ === 0.6.0 :: 2008-02-26
62
+
63
+ * Added support for CAS authentication. See picnic/authentication.rb for
64
+ details.
65
+ * Webrick and Mongrel can now be made to bind to a specific IP address using
66
+ the :bind_address option. If no :bind_address is specified, the server will
67
+ listen on all addresses (i.e. '0.0.0.0').
68
+ * The Public controller for serving the '/public' directory is gone. It has
69
+ been replaced by respective Webrick and Mongrel mechanisms for serving
70
+ directory contents, since these are much faster. If you're using CGI/FastCGI,
71
+ you'll have to manually configure your web server (i.e. probably Apache)
72
+ to serve your public directory contents.
73
+ * The gem package now correctly recognizes markaby as a required
74
+ dependency.
75
+
76
+ === 0.5.0 :: 2007-12-20
77
+
78
+ * First public release.
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,45 @@
1
+ CHANGELOG.txt
2
+ History.txt
3
+ LICENSE.txt
4
+ Manifest.txt
5
+ README.txt
6
+ Rakefile
7
+ lib/picnic.rb
8
+ lib/picnic/authentication.rb
9
+ lib/picnic/cli.rb
10
+ lib/picnic/conf.rb
11
+ lib/picnic/controllers.rb
12
+ lib/picnic/logger.rb
13
+ lib/picnic/server.rb
14
+ lib/picnic/service_control.rb
15
+ lib/picnic/version.rb
16
+ picnic.gemspec
17
+ setup.rb
18
+ test/picnic_test.rb
19
+ test/test_helper.rb
20
+ vendor/camping-2.0.20090420/CHANGELOG
21
+ vendor/camping-2.0.20090420/COPYING
22
+ vendor/camping-2.0.20090420/README
23
+ vendor/camping-2.0.20090420/Rakefile
24
+ vendor/camping-2.0.20090420/bin/camping
25
+ vendor/camping-2.0.20090420/doc/camping.1.gz
26
+ vendor/camping-2.0.20090420/examples/README
27
+ vendor/camping-2.0.20090420/examples/blog.rb
28
+ vendor/camping-2.0.20090420/examples/campsh.rb
29
+ vendor/camping-2.0.20090420/examples/tepee.rb
30
+ vendor/camping-2.0.20090420/extras/Camping.gif
31
+ vendor/camping-2.0.20090420/extras/permalink.gif
32
+ vendor/camping-2.0.20090420/lib/camping-unabridged.rb
33
+ vendor/camping-2.0.20090420/lib/camping.rb
34
+ vendor/camping-2.0.20090420/lib/camping/ar.rb
35
+ vendor/camping-2.0.20090420/lib/camping/ar/session.rb
36
+ vendor/camping-2.0.20090420/lib/camping/mab.rb
37
+ vendor/camping-2.0.20090420/lib/camping/reloader.rb
38
+ vendor/camping-2.0.20090420/lib/camping/server.rb
39
+ vendor/camping-2.0.20090420/lib/camping/session.rb
40
+ vendor/camping-2.0.20090420/setup.rb
41
+ vendor/camping-2.0.20090420/test/apps/env_debug.rb
42
+ vendor/camping-2.0.20090420/test/apps/forms.rb
43
+ vendor/camping-2.0.20090420/test/apps/misc.rb
44
+ vendor/camping-2.0.20090420/test/apps/sessions.rb
45
+ vendor/camping-2.0.20090420/test/test_camping.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,64 @@
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
+ ENV['NODOT'] = '1'
22
+
23
+ NAME = "picnic"
24
+ REV = nil
25
+ #REV = Time.now.strftime('%Y%m%d%H%M')
26
+ #REV = `svn info`[/Revision: (\d+)/, 1] rescue nil
27
+ VERS = ENV['VERSION'] || (Picnic::VERSION::STRING + (REV ? ".#{REV}" : ""))
28
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config']
29
+ RDOC_OPTS = ['--quiet', '--title', "picnic #{VERS} documentation",
30
+ "--opname", "index.html",
31
+ "--line-numbers",
32
+ "--main", "README",
33
+ "--inline-source"]
34
+
35
+ class Hoe
36
+ def extra_deps
37
+ @extra_deps.reject { |x| Array(x).first == 'hoe' }
38
+ end
39
+ end
40
+
41
+ # Generate all the Rake tasks
42
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
43
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
44
+ p.author = AUTHOR
45
+ p.description = DESCRIPTION
46
+ p.email = EMAIL
47
+ p.summary = DESCRIPTION
48
+ p.url = HOMEPATH
49
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
50
+ p.test_globs = ["test/**/*_test.rb"]
51
+ p.clean_globs = CLEAN #An array of file patterns to delete on clean.
52
+
53
+ # == Optional
54
+ #p.changes - A description of the release's latest changes.
55
+ #p.extra_deps - An array of rubygem dependencies.
56
+ #p.spec_extras - A hash of extra values to set in the gemspec.
57
+
58
+ # note that camping-1.5.0.180 is now bundled with picnic in the vendor directory.
59
+ p.extra_deps = [
60
+ 'rack',
61
+ 'markaby',
62
+ 'activesupport'
63
+ ]
64
+ end
@@ -0,0 +1,254 @@
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
+ module Authentication
6
+
7
+ def self.included(base)
8
+ base.module_eval do
9
+ # Enable authentication for your app.
10
+ #
11
+ # For example:
12
+ #
13
+ # Camping.goes :Blog
14
+ # Blog.picnic!
15
+ #
16
+ # $CONF[:authentication] ||= {:username => 'admin', :password => 'picnic'}
17
+ # Blog.authenticate_using :basic
18
+ #
19
+ # module Blog
20
+ # def self.authenticate(credentials)
21
+ # credentials[:username] == Taskr::Conf[:authentication][:username] &&
22
+ # credentials[:password] == Taskr::Conf[:authentication][:password]
23
+ # end
24
+ # end
25
+ #
26
+ # Note that in the above example we use the authentication configuration from
27
+ # your app's conf file.
28
+ #
29
+ def authenticate_using(mod)
30
+ mod = Picnic::Authentication.const_get(mod.to_s.camelize) unless mod.kind_of? Module
31
+
32
+ $LOG.info("Enabling authentication for all requests using #{mod.inspect}.")
33
+
34
+ module_eval do
35
+ include mod
36
+ end
37
+ end
38
+ module_function :authenticate_using
39
+ end
40
+ end
41
+
42
+ # Picnic::Authentication::Basic provides Basic HTTP Authentication for your Camping app.
43
+ # The module defines a <tt>service</tt> method that only continues the request chain when
44
+ # proper credentials are provided by the client (browser).
45
+ #
46
+ # == Getting Started
47
+ #
48
+ # To activate Basic Authentication for your application:
49
+ #
50
+ # 1. Picnic-fy your Camping app (e.g: <tt>Camping.goes :your_app; YourApp.picnic!</tt>)
51
+ # 2. Call <tt>YourApp.authenticate_using :basic</tt>.
52
+ # 3. Define an <tt>authenticate</tt> method on your application module that takes a hash.
53
+ # The hash contains credentials like <tt>:username</tt>, <tt>:password</tt>, and <tt>:hostname</tt>,
54
+ # although future authentication modules may submit other credentials.
55
+ # The <tt>authenticate</tt> method should return true when the credentials are valid.
56
+ # Examples:
57
+ #
58
+ # module Blog
59
+ # def authenticate(credentials)
60
+ # credentials[:username] == 'admin' &&
61
+ # credentials[:password] == 'flapper30'
62
+ # end
63
+ # module_function :authenticate
64
+ # end
65
+ #
66
+ # or
67
+ #
68
+ # module Wiki
69
+ # def authenticate(credentials)
70
+ # u = credentials[:username]
71
+ # p = credentials[:password]
72
+ # Models::User.find_by_username_and_password u, p
73
+ # end
74
+ # module_function :authenticate
75
+ # end
76
+ #
77
+ # 4. <tt>service</tt> sets <tt>@credentials</tt> to the credentials of the person who logged in.
78
+ #
79
+ # ----
80
+ #
81
+ # This code is based on Camping::BasicAuth written by Manfred Stienstra
82
+ # (see http://www.fngtps.com/2006/05/basic-authentication-for-camping).
83
+ module Basic
84
+ require 'base64'
85
+
86
+ # Reads the username and password from the headers and returns them.
87
+ def read_credentials
88
+ if d = %w{REDIRECT_X_HTTP_AUTHORIZATION X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION}.inject([]) \
89
+ { |d,h| @env.has_key?(h) ? @env[h].to_s.split : d }
90
+ u,p = ::Base64.decode64(d[1]).split(':')[0..1] if d[0] == 'Basic'
91
+ return {:username => u, :password => p}
92
+ end
93
+ end
94
+
95
+ def service(*a)
96
+ app = Kernel.const_get self.class.name.gsub(/^(\w+)::.+$/, '\1')
97
+ unless app.respond_to? :authenticate
98
+ raise "Basic authentication is enabled but the 'authenticate' method has not been defined."
99
+ end
100
+
101
+ @credentials = read_credentials || {}
102
+
103
+ if app.authenticate(@credentials)
104
+ s = super(*a)
105
+ else
106
+ @status = 401
107
+ headers['Content-type'] = @headers['Content-type'] || 'text/plain'
108
+ #headers['Status'] = 'Unauthorized'
109
+ headers['WWW-Authenticate'] = "Basic realm=\"#{app}\""
110
+ @body = 'Unauthorized'
111
+ s = self
112
+ end
113
+ s
114
+ end
115
+ end
116
+
117
+
118
+ # Picnic::Authentication::Cas provides basic CAS (Central Authentication System) authentication
119
+ # for your Camping app.
120
+ #
121
+ # To learn more about CAS, see http://rubycas-client.googlecode.com and
122
+ # http://www.ja-sig.org/products/cas.
123
+ #
124
+ # The module defines a <tt>service</tt> method that intercepts every request to check for CAS
125
+ # authentication. If the user has already been authenticated, the request proceeds as normal
126
+ # and the authenticated user's username is made available under <tt>@state[:cas_username].
127
+ # Otherwise the request is redirected to your CAS server for authentication.
128
+ #
129
+ # == Getting Started
130
+ #
131
+ # To activate CAS authentication for your application:
132
+ #
133
+ # 1. Picnic-fy your Camping app (e.g: <tt>Camping.goes :your_app; YourApp.picnic!</tt>)
134
+ # 2. Call <tt>YourApp.authenticate_using :cas</tt>.
135
+ # 3. In your app's configuration YAML file add something like this:
136
+ # authentication:
137
+ # cas_base_url: https://login.example.com/cas
138
+ # Where the value for </tt>cas_base_url</tt> is the URL of your CAS server.
139
+ # 4. That's it. Now whenever a user tries to access any of your controller's actions,
140
+ # the request will be checked for CAS authentication. If the user is authenticated,
141
+ # their username is availabe in @state[:cas_username]. Note that there is currently
142
+ # no way to apply CAS authentication only to certain controllers or actions. When
143
+ # enabled, CAS authentication applies to your entire application, except for items
144
+ # placed in the /public subdirectory (CSS files, JavaScripts, images, etc.). The
145
+ # public directory does not require CAS authentication, so anyone can access its
146
+ # contents.
147
+ #
148
+ module Cas
149
+ # For some reason the Module#included callback is just not working for me, so I had
150
+ # to resort to overriding append_features(). If anyone has any ideas why, please
151
+ # let me know!
152
+ def self.append_features(mod)
153
+ super
154
+
155
+ require 'camping/db'
156
+ require 'camping/session'
157
+
158
+ $: << File.dirname(File.expand_path(__FILE__))+"/../../../rubycas-client2/lib" # for development
159
+ require 'rubycas-client'
160
+ end
161
+
162
+ # app = Kernel.const_get self.name.gsub(/^(\w+)::.+$/, '\1')
163
+ # raise "Cannot enable CAS authentication because your Camping app does not extend Camping::Session." unless
164
+ # app.ancestors.include?(Camping::Session)
165
+
166
+ # There must be a smarter way to do this... but for now, we just re-implement
167
+ # the Camping::Session method here to provide session support for CAS.
168
+ module Session
169
+ # This doesn't work :( MySQL connection is not carried over.
170
+ #define_method(:service, Camping::Session.instance_method(:service))
171
+
172
+ def service(*a)
173
+ Camping::Models::Session.create_schema
174
+
175
+ session = Camping::Models::Session.persist @cookies
176
+ app = self.class.name.gsub(/^(\w+)::.+$/, '\1')
177
+ @state = (session[app] ||= Camping::H[])
178
+ hash_before = Marshal.dump(@state).hash
179
+ s = super(*a)
180
+ if session
181
+ hash_after = Marshal.dump(@state).hash
182
+ unless hash_before == hash_after
183
+ session[app] = @state
184
+ session.save
185
+ end
186
+ end
187
+ s
188
+ end
189
+ end
190
+
191
+ def self.included(mod)
192
+ mod.module_eval do
193
+ include Cas::Session
194
+ end
195
+ end
196
+
197
+ def service(*a)
198
+ $LOG.debug "Running CAS filter for request #{a.inspect}..."
199
+
200
+ if @env['PATH_INFO'] =~ /^\/public\/.*/
201
+ $LOG.debug "Access to items in /public subdirectory does not require CAS authentication."
202
+ return super(*a)
203
+ end
204
+ if @state[:cas_username]
205
+ $LOG.debug "Local CAS session exists for user #{@state[:cas_username]}."
206
+ return super(*a)
207
+ end
208
+
209
+ client = CASClient::Client.new($CONF[:authentication].merge(:logger => $LOG))
210
+
211
+ ticket = @input[:ticket]
212
+
213
+ cas_login_url = client.add_service_to_login_url(read_service_url(@env))
214
+
215
+ if ticket
216
+ if ticket =~ /^PT-/
217
+ st = CASClient::ProxyTicket.new(ticket, read_service_url(@env), @input[:renew])
218
+ else
219
+ st = CASClient::ServiceTicket.new(ticket, read_service_url(@env), @input[:renew])
220
+ end
221
+
222
+ $LOG.debug "Got CAS ticket: #{st.inspect}"
223
+
224
+ client.validate_service_ticket(st)
225
+ if st.is_valid?
226
+ $LOG.info "CAS ticket #{st.ticket.inspect} is valid. Opening local CAS session for user #{st.response.user.inspect}."
227
+ @state[:cas_username] = st.response.user
228
+ return super(*a)
229
+ else
230
+ $LOG.warn "CAS ticket #{st.ticket.inspect} is INVALID. Redirecting back to CAS server at #{cas_login_url.inspect} for authentication."
231
+ @state[:cas_username] = nil
232
+ redirect cas_login_url
233
+ s = self
234
+ end
235
+ else
236
+ $LOG.info "User is unauthenticated and no CAS ticket found. Redirecting to CAS server at #{cas_login_url.inspect} for authentication."
237
+ @state[:cas_username] = nil
238
+ redirect cas_login_url
239
+ s = self
240
+ end
241
+ s
242
+ end
243
+
244
+ private
245
+ def read_service_url(env)
246
+ if $CONF[:authentication][:service_url]
247
+ $CONF[:authentication][:service_url]
248
+ else
249
+ env['REQUEST_URI'].gsub(/service=[^&]*[&]?/,'').gsub(/ticket=[^&]*[&]?/,'')
250
+ end
251
+ end
252
+ end
253
+ end
254
+ end