oath 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +3 -0
  5. data/Gemfile +3 -0
  6. data/Gemfile.lock +165 -0
  7. data/LICENSE.txt +22 -0
  8. data/NEWS.rdoc +118 -0
  9. data/README.md +384 -0
  10. data/Rakefile +6 -0
  11. data/lib/oath.rb +132 -0
  12. data/lib/oath/back_door.rb +53 -0
  13. data/lib/oath/configuration.rb +149 -0
  14. data/lib/oath/constraints/signed_in.rb +14 -0
  15. data/lib/oath/constraints/signed_out.rb +14 -0
  16. data/lib/oath/controller_helpers.rb +161 -0
  17. data/lib/oath/failure_app.rb +48 -0
  18. data/lib/oath/field_map.rb +56 -0
  19. data/lib/oath/param_transformer.rb +38 -0
  20. data/lib/oath/railtie.rb +11 -0
  21. data/lib/oath/services.rb +5 -0
  22. data/lib/oath/services/authentication.rb +40 -0
  23. data/lib/oath/services/password_reset.rb +27 -0
  24. data/lib/oath/services/sign_in.rb +25 -0
  25. data/lib/oath/services/sign_out.rb +24 -0
  26. data/lib/oath/services/sign_up.rb +42 -0
  27. data/lib/oath/strategies/password_strategy.rb +42 -0
  28. data/lib/oath/test/controller_helpers.rb +43 -0
  29. data/lib/oath/test/helpers.rb +24 -0
  30. data/lib/oath/version.rb +4 -0
  31. data/lib/oath/warden_setup.rb +47 -0
  32. data/oath.gemspec +30 -0
  33. data/spec/features/user/user_signs_in_spec.rb +14 -0
  34. data/spec/features/user/user_signs_in_through_back_door_spec.rb +11 -0
  35. data/spec/features/user/user_tries_to_access_constrained_routes_spec.rb +18 -0
  36. data/spec/features/user/user_tries_to_access_http_auth_page_spec.rb +9 -0
  37. data/spec/features/visitor/visitor_fails_to_sign_up_spec.rb +10 -0
  38. data/spec/features/visitor/visitor_is_unauthorized_spec.rb +8 -0
  39. data/spec/features/visitor/visitor_signs_in_via_invalid_form_spec.rb +11 -0
  40. data/spec/features/visitor/visitor_signs_up_spec.rb +40 -0
  41. data/spec/features/visitor/visitor_tries_to_access_constrained_routes_spec.rb +14 -0
  42. data/spec/features/visitor/visitor_uses_remember_token_spec.rb +13 -0
  43. data/spec/oath/configuration_spec.rb +11 -0
  44. data/spec/oath/controller_helpers_spec.rb +180 -0
  45. data/spec/oath/field_map_spec.rb +19 -0
  46. data/spec/oath/services/authentication_spec.rb +25 -0
  47. data/spec/oath/services/password_reset_spec.rb +24 -0
  48. data/spec/oath/services/sign_in_spec.rb +13 -0
  49. data/spec/oath/services/sign_out_spec.rb +13 -0
  50. data/spec/oath/services/sign_up_spec.rb +49 -0
  51. data/spec/oath/strategies/password_strategy_spec.rb +23 -0
  52. data/spec/oath/test_controller_helpers_spec.rb +63 -0
  53. data/spec/oath/test_helpers_spec.rb +97 -0
  54. data/spec/oath_spec.rb +27 -0
  55. data/spec/rails_app/Rakefile +7 -0
  56. data/spec/rails_app/app/assets/images/rails.png +0 -0
  57. data/spec/rails_app/app/assets/javascripts/application.js +13 -0
  58. data/spec/rails_app/app/assets/stylesheets/application.css +13 -0
  59. data/spec/rails_app/app/controllers/application_controller.rb +4 -0
  60. data/spec/rails_app/app/controllers/basic_auth_controller.rb +7 -0
  61. data/spec/rails_app/app/controllers/constrained_to_users_controller.rb +5 -0
  62. data/spec/rails_app/app/controllers/constrained_to_visitors_controller.rb +5 -0
  63. data/spec/rails_app/app/controllers/failures_controller.rb +5 -0
  64. data/spec/rails_app/app/controllers/invalid_sessions_controller.rb +2 -0
  65. data/spec/rails_app/app/controllers/posts_controller.rb +6 -0
  66. data/spec/rails_app/app/controllers/sessions_controller.rb +26 -0
  67. data/spec/rails_app/app/controllers/users_controller.rb +23 -0
  68. data/spec/rails_app/app/helpers/application_helper.rb +2 -0
  69. data/spec/rails_app/app/models/user.rb +10 -0
  70. data/spec/rails_app/app/views/invalid_sessions/new.html.erb +4 -0
  71. data/spec/rails_app/app/views/layouts/application.html.erb +18 -0
  72. data/spec/rails_app/app/views/posts/index.html.erb +1 -0
  73. data/spec/rails_app/app/views/sessions/new.html.erb +5 -0
  74. data/spec/rails_app/app/views/users/new.html.erb +5 -0
  75. data/spec/rails_app/config.ru +4 -0
  76. data/spec/rails_app/config/application.rb +58 -0
  77. data/spec/rails_app/config/boot.rb +6 -0
  78. data/spec/rails_app/config/database.yml +25 -0
  79. data/spec/rails_app/config/environment.rb +5 -0
  80. data/spec/rails_app/config/environments/development.rb +29 -0
  81. data/spec/rails_app/config/environments/production.rb +54 -0
  82. data/spec/rails_app/config/environments/test.rb +29 -0
  83. data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  84. data/spec/rails_app/config/initializers/inflections.rb +15 -0
  85. data/spec/rails_app/config/initializers/secret_token.rb +7 -0
  86. data/spec/rails_app/config/routes.rb +24 -0
  87. data/spec/rails_app/db/seeds.rb +7 -0
  88. data/spec/rails_app/public/404.html +26 -0
  89. data/spec/rails_app/public/422.html +26 -0
  90. data/spec/rails_app/public/500.html +25 -0
  91. data/spec/rails_app/public/favicon.ico +0 -0
  92. data/spec/rails_app/script/rails +6 -0
  93. data/spec/spec_helper.rb +37 -0
  94. metadata +325 -0
@@ -0,0 +1,23 @@
1
+ class UsersController < ApplicationController
2
+ def new
3
+ @user = User.new
4
+ end
5
+
6
+ def create
7
+ @user = sign_up(user_params)
8
+
9
+ if @user.valid?
10
+ sign_in(@user)
11
+ redirect_to posts_path
12
+ else
13
+ render :new
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def user_params
20
+ params.require(:user).permit(:email, :password)
21
+ end
22
+ end
23
+
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,10 @@
1
+ require 'active_hash'
2
+ class User < ActiveHash::Base
3
+ include ActiveModel::Validations
4
+ attr_accessor :email, :password_digest, :password
5
+ validates :email, presence: true
6
+
7
+ def self.find_by(params)
8
+ where(params).first
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ <%= form_for :session do |f| %>
2
+ <%= f.text_field :password %>
3
+ <%= f.submit 'go' %>
4
+ <% end %>
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>RailsApp</title>
5
+ <%= stylesheet_link_tag "application", :media => "all" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <% if signed_in? %>
11
+ <%= link_to "Sign out", sign_out_path, method: :delete %>
12
+ <% else %>
13
+ <%= link_to "Sign in", sign_in_path %>
14
+ <% end %>
15
+ <%= yield %>
16
+
17
+ </body>
18
+ </html>
@@ -0,0 +1,5 @@
1
+ <%= form_for :session do |f| %>
2
+ <%= f.text_field :email %>
3
+ <%= f.text_field :password %>
4
+ <%= f.submit 'go' %>
5
+ <% end %>
@@ -0,0 +1,5 @@
1
+ <%= form_for @user, url: users_path do |f| %>
2
+ <%= f.text_field :email %>
3
+ <%= f.text_field :password %>
4
+ <%= f.submit 'go' %>
5
+ <% end %>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run RailsApp::Application
@@ -0,0 +1,58 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ # require "action_mailer/railtie"
7
+ # require "active_resource/railtie"
8
+ # require "sprockets/railtie"
9
+ # require "rails/test_unit/railtie"
10
+
11
+ if defined?(Bundler)
12
+ # If you precompile assets before deploying to production, use this line
13
+ Bundler.require(*Rails.groups(:assets => %w(development test)))
14
+ # If you want your assets lazily compiled in production, use this line
15
+ # Bundler.require(:default, :assets, Rails.env)
16
+ end
17
+
18
+ require 'oath'
19
+
20
+ module RailsApp
21
+ class Application < Rails::Application
22
+ # Settings in config/environments/* take precedence over those specified here.
23
+ # Application configuration should go into files in config/initializers
24
+ # -- all .rb files in that directory are automatically loaded.
25
+
26
+ # Custom directories with classes and modules you want to be autoloadable.
27
+ # config.autoload_paths += %W(#{config.root}/extras)
28
+
29
+ # Only load the plugins named here, in the order given (default is alphabetical).
30
+ # :all can be used as a placeholder for all plugins not explicitly named.
31
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
32
+
33
+ # Activate observers that should always be running.
34
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
35
+
36
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
37
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
38
+ # config.time_zone = 'Central Time (US & Canada)'
39
+
40
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
41
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
42
+ # config.i18n.default_locale = :de
43
+
44
+ # Configure the default encoding used in templates for Ruby 1.9.
45
+ config.encoding = "utf-8"
46
+
47
+ # Configure sensitive parameters which will be filtered from the log file.
48
+ config.filter_parameters += [:password]
49
+
50
+ # Enable escaping HTML in JSON.
51
+ config.active_support.escape_html_entities_in_json = true
52
+
53
+ # Use SQL instead of Active Record's schema dumper when creating the database.
54
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
55
+ # like if you have constraints or database-specific column types
56
+ # config.active_record.schema_format = :sql
57
+ end
58
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+
3
+ # Set up gems listed in the Gemfile.
4
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
5
+
6
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ development:
7
+ adapter: sqlite3
8
+ database: db/development.sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ # Warning: The database defined as "test" will be erased and
13
+ # re-generated from your development database when you run "rake".
14
+ # Do not set this db to the same as development or production.
15
+ test:
16
+ adapter: sqlite3
17
+ database: db/test.sqlite3
18
+ pool: 5
19
+ timeout: 5000
20
+
21
+ production:
22
+ adapter: sqlite3
23
+ database: db/production.sqlite3
24
+ pool: 5
25
+ timeout: 5000
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ RailsApp::Application.initialize!
@@ -0,0 +1,29 @@
1
+ RailsApp::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Print deprecation notices to the Rails logger
17
+ config.active_support.deprecation = :log
18
+
19
+ # Only use best-standards-support built into browsers
20
+ config.action_dispatch.best_standards_support = :builtin
21
+
22
+ # Raise exception on mass assignment protection for Active Record models
23
+ config.active_record.mass_assignment_sanitizer = :strict
24
+
25
+ # Log the query plan for queries taking more than this (works
26
+ # with SQLite, MySQL, and PostgreSQL)
27
+ config.active_record.auto_explain_threshold_in_seconds = 0.5
28
+
29
+ end
@@ -0,0 +1,54 @@
1
+ RailsApp::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # Code is not reloaded between requests
5
+ config.cache_classes = true
6
+
7
+ # Full error reports are disabled and caching is turned on
8
+ config.consider_all_requests_local = false
9
+ config.action_controller.perform_caching = true
10
+
11
+ # Disable Rails's static asset server (Apache or nginx will already do this)
12
+ config.serve_static_assets = false
13
+
14
+
15
+ # Specifies the header that your server uses for sending files
16
+ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
17
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
18
+
19
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
20
+ # config.force_ssl = true
21
+
22
+ # See everything in the log (default is :info)
23
+ # config.log_level = :debug
24
+
25
+ # Prepend all log lines with the following tags
26
+ # config.log_tags = [ :subdomain, :uuid ]
27
+
28
+ # Use a different logger for distributed setups
29
+ # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
30
+
31
+ # Use a different cache store in production
32
+ # config.cache_store = :mem_cache_store
33
+
34
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server
35
+ # config.action_controller.asset_host = "http://assets.example.com"
36
+
37
+
38
+ # Disable delivery errors, bad email addresses will be ignored
39
+ # config.action_mailer.raise_delivery_errors = false
40
+
41
+ # Enable threaded mode
42
+ # config.threadsafe!
43
+
44
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
45
+ # the I18n.default_locale when a translation can not be found)
46
+ config.i18n.fallbacks = true
47
+
48
+ # Send deprecation notices to registered listeners
49
+ config.active_support.deprecation = :notify
50
+
51
+ # Log the query plan for queries taking more than this (works
52
+ # with SQLite, MySQL, and PostgreSQL)
53
+ # config.active_record.auto_explain_threshold_in_seconds = 0.5
54
+ end
@@ -0,0 +1,29 @@
1
+ RailsApp::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+ config.middleware.insert_after Warden::Manager, Oath::BackDoor
4
+
5
+ # The test environment is used exclusively to run your application's
6
+ # test suite. You never need to work with it otherwise. Remember that
7
+ # your test database is "scratch space" for the test suite and is wiped
8
+ # and recreated between test runs. Don't rely on the data there!
9
+ config.cache_classes = true
10
+
11
+ # Configure static asset server for tests with Cache-Control for performance
12
+ config.serve_static_assets = true
13
+ config.static_cache_control = "public, max-age=3600"
14
+
15
+ # Show full error reports and disable caching
16
+ config.consider_all_requests_local = true
17
+ config.action_controller.perform_caching = false
18
+
19
+ # Raise exceptions instead of rendering exception templates
20
+ config.action_dispatch.show_exceptions = false
21
+
22
+ # Disable request forgery protection in test environment
23
+ config.action_controller.allow_forgery_protection = false
24
+
25
+ # Print deprecation notices to the stderr
26
+ config.active_support.deprecation = :stderr
27
+ config.eager_load = false
28
+ config.secret_key_base = "1"
29
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
11
+ #
12
+ # These inflection rules are supported but not enabled by default:
13
+ # ActiveSupport::Inflector.inflections do |inflect|
14
+ # inflect.acronym 'RESTful'
15
+ # end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ RailsApp::Application.config.secret_token = '840b3262c6147ca69157ebd545ff37817922cd1315d688d7ec3a864629ae89398604efeca261c8e40273b0306dbcf5c7f609f7b0dd4d12d213f1d6bd3dc169db'
@@ -0,0 +1,24 @@
1
+ require 'oath/constraints/signed_in'
2
+ require 'oath/constraints/signed_out'
3
+
4
+ RailsApp::Application.routes.draw do
5
+ constraints Oath::Constraints::SignedIn.new do
6
+ resource :constrained_to_users, only: [:show]
7
+ end
8
+
9
+ constraints Oath::Constraints::SignedOut.new do
10
+ resource :constrained_to_visitors, only: [:show]
11
+ end
12
+
13
+ resources :posts, only: [:index]
14
+ resources :users, only: [:create]
15
+ resource :failure, only: [:show]
16
+ root to: "users#new"
17
+ get "sign_in" => "sessions#new"
18
+ post "sign_in" => "sessions#create"
19
+ delete "sign_out" => "sessions#destroy"
20
+ get "sign_up" => "users#new"
21
+ get "invalid_sign_in" => "invalid_sessions#new"
22
+ post "invalid_sign_in" => "invalid_sessions#create"
23
+ get "basic_auth" => "basic_auth#show"
24
+ end
@@ -0,0 +1,7 @@
1
+ # This file should contain all the record creation needed to seed the database with its default values.
2
+ # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
3
+ #
4
+ # Examples:
5
+ #
6
+ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
7
+ # Mayor.create(name: 'Emanuel', city: cities.first)
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>