zuk-picnic 0.7.999.20090212

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