Permalink
Browse files

Really add warden example app

  • Loading branch information...
1 parent 2cca7bb commit 6f9a8a6f2f9c985ac09775803de06e9af897ca14 @radar committed Sep 14, 2012
Showing with 830 additions and 1 deletion.
  1. +0 −1 warden
  2. +15 −0 warden/.gitignore
  3. +39 −0 warden/Gemfile
  4. +114 −0 warden/Gemfile.lock
  5. +122 −0 warden/README.markdown
  6. +7 −0 warden/Rakefile
  7. BIN warden/app/assets/images/rails.png
  8. +15 −0 warden/app/assets/javascripts/application.js
  9. +3 −0 warden/app/assets/javascripts/login.js.coffee
  10. +13 −0 warden/app/assets/stylesheets/application.css
  11. +3 −0 warden/app/assets/stylesheets/login.css.scss
  12. +3 −0 warden/app/controllers/application_controller.rb
  13. +11 −0 warden/app/controllers/login_controller.rb
  14. +2 −0 warden/app/helpers/application_helper.rb
  15. +2 −0 warden/app/helpers/login_helper.rb
  16. 0 warden/app/mailers/.gitkeep
  17. 0 warden/app/models/.gitkeep
  18. +8 −0 warden/app/models/user.rb
  19. +14 −0 warden/app/views/layouts/application.html.erb
  20. +13 −0 warden/app/views/login/new.html.erb
  21. +4 −0 warden/config.ru
  22. +78 −0 warden/config/application.rb
  23. +6 −0 warden/config/boot.rb
  24. +25 −0 warden/config/database.yml
  25. +5 −0 warden/config/environment.rb
  26. +37 −0 warden/config/environments/development.rb
  27. +67 −0 warden/config/environments/production.rb
  28. +37 −0 warden/config/environments/test.rb
  29. +7 −0 warden/config/initializers/backtrace_silencers.rb
  30. +15 −0 warden/config/initializers/inflections.rb
  31. +5 −0 warden/config/initializers/mime_types.rb
  32. 0 warden/config/initializers/password_strategy.rb
  33. +7 −0 warden/config/initializers/secret_token.rb
  34. +8 −0 warden/config/initializers/session_store.rb
  35. +14 −0 warden/config/initializers/wrap_parameters.rb
  36. +5 −0 warden/config/locales/en.yml
  37. +4 −0 warden/config/routes.rb
  38. +10 −0 warden/db/migrate/20120913200357_create_users.rb
  39. +23 −0 warden/db/schema.rb
  40. +1 −0 warden/db/seeds.rb
  41. 0 warden/lib/assets/.gitkeep
  42. 0 warden/lib/tasks/.gitkeep
  43. +26 −0 warden/public/404.html
  44. +26 −0 warden/public/422.html
  45. +25 −0 warden/public/500.html
  46. 0 warden/public/favicon.ico
  47. +5 −0 warden/public/robots.txt
  48. +6 −0 warden/script/rails
1 warden
Submodule warden deleted from 693b28
View
@@ -0,0 +1,15 @@
+# See http://help.github.com/ignore-files/ for more about ignoring files.
+#
+# If you find yourself ignoring temporary files generated by your text editor
+# or operating system, you probably want to add a global ignore instead:
+# git config --global core.excludesfile ~/.gitignore_global
+
+# Ignore bundler config
+/.bundle
+
+# Ignore the default SQLite database.
+/db/*.sqlite3
+
+# Ignore all logfiles and tempfiles.
+/log/*.log
+/tmp
View
@@ -0,0 +1,39 @@
+source 'https://rubygems.org'
+
+gem 'rails', '3.2.8'
+gem 'warden', '1.2.1'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+gem 'sqlite3'
+
+
+# Gems used only for assets and not required
+# in production environments by default.
+group :assets do
+ gem 'sass-rails', '~> 3.2.3'
+ gem 'coffee-rails', '~> 3.2.1'
+
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
+ # gem 'therubyracer', :platforms => :ruby
+
+ gem 'uglifier', '>= 1.0.3'
+end
+
+gem 'jquery-rails'
+
+# To use ActiveModel has_secure_password
+# gem 'bcrypt-ruby', '~> 3.0.0'
+
+# To use Jbuilder templates for JSON
+# gem 'jbuilder'
+
+# Use unicorn as the app server
+# gem 'unicorn'
+
+# Deploy with Capistrano
+# gem 'capistrano'
+
+# To use debugger
+# gem 'debugger'
View
@@ -0,0 +1,114 @@
+GEM
+ remote: https://rubygems.org/
+ specs:
+ actionmailer (3.2.8)
+ actionpack (= 3.2.8)
+ mail (~> 2.4.4)
+ actionpack (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.4)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.3)
+ activemodel (3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ activerecord (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ activesupport (3.2.8)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ arel (3.0.2)
+ builder (3.0.3)
+ coffee-rails (3.2.2)
+ coffee-script (>= 2.2.0)
+ railties (~> 3.2.0)
+ coffee-script (2.2.0)
+ coffee-script-source
+ execjs
+ coffee-script-source (1.3.3)
+ erubis (2.7.0)
+ execjs (1.4.0)
+ multi_json (~> 1.0)
+ hike (1.2.1)
+ i18n (0.6.1)
+ journey (1.0.4)
+ jquery-rails (2.1.2)
+ railties (>= 3.1.0, < 5.0)
+ thor (~> 0.14)
+ json (1.7.5)
+ mail (2.4.4)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.19)
+ multi_json (1.3.6)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rails (3.2.8)
+ actionmailer (= 3.2.8)
+ actionpack (= 3.2.8)
+ activerecord (= 3.2.8)
+ activeresource (= 3.2.8)
+ activesupport (= 3.2.8)
+ bundler (~> 1.0)
+ railties (= 3.2.8)
+ railties (3.2.8)
+ actionpack (= 3.2.8)
+ activesupport (= 3.2.8)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (>= 0.14.6, < 2.0)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ sass (3.2.1)
+ sass-rails (3.2.5)
+ railties (~> 3.2.0)
+ sass (>= 3.1.10)
+ tilt (~> 1.3)
+ sprockets (2.1.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.6)
+ thor (0.16.0)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.33)
+ uglifier (1.3.0)
+ execjs (>= 0.3.0)
+ multi_json (~> 1.0, >= 1.0.2)
+ warden (1.2.1)
+ rack (>= 1.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ coffee-rails (~> 3.2.1)
+ jquery-rails
+ rails (= 3.2.8)
+ sass-rails (~> 3.2.3)
+ sqlite3
+ uglifier (>= 1.0.3)
+ warden (= 1.2.1)
@@ -0,0 +1,122 @@
+# Banana, A Warden Example Application
+
+cjz was all like "bros, teach me the way of the Warden". And we said unto him
+"lol, like use Devise". But cjz refused. Some say it was a gallant refusal.
+Others said he was crazy. The third group asked "who the fuck is cjz?". And
+then it was so.
+
+Thus, I spent some time battling the beast known as Warden to produce this
+application. Gaze unto it's soft, gooey center to understand how you too can
+use Warden by itself within a Rails app.
+
+## omg there is code and I don't know what it does
+
+*Everybody be cool.*
+
+Warden works by placing a thing called "middleware" in the "rack stack" for
+your application. There's a whole bunch of middleware that make up a Rails
+application, and you can view these by running the `rake middleware` command.
+A little known fact.
+
+A request headed for your application goes through this middleware stack piece
+by piece and those pieces of middleware can modify the request (or in Rack
+parlance, it's called an "environment") in certain ways by adding headers,
+modifying content and other fun things. They can even stop the request in its
+tracks and return a response immediately.
+
+This particular piece of middleware, `Warden::Manager`, is provided by the
+warden gem which you may know from such places as the `Gemfile` of this
+application, and as a major component in how Devise works. This middleware is
+configured like this inside `config/application.rb`:
+
+```ruby
+config.middleware.use Warden::Manager do |manager|
+ manager.default_strategies :password
+end
+```
+
+Warden uses a thing called *strategies* for authentication. They're like
+*battle plans* for authentication. In this situation, Warden's told that it
+should use the `password` strategy, which is also defined inside
+`config/application.rb`, but would probably be better placed into
+`config/initializers/warden/strategies/password.rb`... anyway. This strategy is
+defined like this:
+
+```ruby
+Warden::Strategies.add(:password) do
+
+ def valid?
+ params["username"] || params["password"]
+ end
+
+ def authenticate!
+ u = User.authenticate(params["username"], params["password"])
+ u.nil? ? fail! : success!(u)
+ end
+end
+```
+
+So there's the two parts to it that matter crucially: we've defined the
+`Warden::Manager` as being a middleware for the application and we've told it
+"pretty please use the password strategy whenever you feel like authenticating
+a user". Cool.
+
+## more code
+
+So then you have a login form at `app/views/login/new.html.erb` inside this
+application which contains a `username` and `password` field. Simple enough.
+This form posts to `/login`, and if you look in `config/routes.rb` you'll see
+the route is defined like this:
+
+```ruby
+post '/login', :to => "login#login"
+```
+
+So that means that any time this form is submitted it will (hopefully) go to
+the `login` action inside `LoginController`. That action looks like this:
+
+```ruby
+def login
+ # Reset session
+ env['warden'].logout
+ if env['warden'].authenticate
+ render :text => "success"
+ else
+ render :text => "failure"
+ end
+end
+```
+
+And here's your first real taste of what warden does. The `env['warden']`
+object is a warden proxy object that acts as the gateway to all things
+authentication within your application. If you tell it to logout, it'll logout
+the current session. If you tell it to authenticate, then it'll authenticate
+it.
+
+On the first (real) line of this controller, the aforementioned `env['warden'].logout` is called which will reset the warden session every time the login form is submitted.
+
+The `env['warden'].authenticate` call in this action is where the real magic
+happens though. The `authenticate` method will tell Warden to look through all
+its *valid* strategies and attempt to authenticate a user against each one until it
+works. If no strategy is valid, then you're out of luck. If no strategy allows
+the user to be authenticated, then you're out of luck again.
+
+By "valid", I mean that it passes the `valid?` method test for the defined
+strategy (which is inside `config/application.rb`, remember?). If it *does* pass
+that, then it'll call the `authenticate!` method on that strategy, running the
+code inside it.
+
+For the strategy that this app has, it calls the `User.authenticate` method,
+defined inside `app/models/user.rb`, and then if that returns a user, then it
+calls `success!` for the strategy and if there is no user then `fail!`.
+
+What will happen then is that if it's successful, `env['warden'].authenticate`
+will return the newly authenticated-and-signed-in `User` object which will mean
+then that the controller will render "success". If it isn't successful, then it
+will render "failure".
+
+### Fin.
+
+This has been another deliriously educational wall-o-text by me. I hope you
+enjoyed it.
+
View
@@ -0,0 +1,7 @@
+#!/usr/bin/env rake
+# Add your own tasks in files placed in lib/tasks ending in .rake,
+# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
+
+require File.expand_path('../config/application', __FILE__)
+
+Banana::Application.load_tasks
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,15 @@
+// This is a manifest file that'll be compiled into application.js, which will include all the files
+// listed below.
+//
+// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
+// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
+//
+// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
+// the compiled file.
+//
+// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
+// GO AFTER THE REQUIRES BELOW.
+//
+//= require jquery
+//= require jquery_ujs
+//= require_tree .
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,13 @@
+/*
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
+ * listed below.
+ *
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
+ *
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
+ * compiled file, but it's generally better to create a new file per style scope.
+ *
+ *= require_self
+ *= require_tree .
+ */
@@ -0,0 +1,3 @@
+// Place all the styles related to the login controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,3 @@
+class ApplicationController < ActionController::Base
+ protect_from_forgery
+end
@@ -0,0 +1,11 @@
+class LoginController < ApplicationController
+ def login
+ # Reset session
+ env['warden'].logout
+ if env['warden'].authenticate
+ render :text => "success"
+ else
+ render :text => "failure"
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module ApplicationHelper
+end
@@ -0,0 +1,2 @@
+module LoginHelper
+end
No changes.
No changes.
@@ -0,0 +1,8 @@
+class User < ActiveRecord::Base
+ attr_accessible :password, :username
+
+ def self.authenticate(username, password)
+ u = self.find_by_username(username)
+ return u if u && u.password == password
+ end
+end
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Banana</title>
+ <%= stylesheet_link_tag "application", :media => "all" %>
+ <%= javascript_include_tag "application" %>
+ <%= csrf_meta_tags %>
+</head>
+<body>
+
+<%= yield %>
+
+</body>
+</html>
Oops, something went wrong.

0 comments on commit 6f9a8a6

Please sign in to comment.