Permalink
Browse files

Reimplement with an isolated namespace

  • Loading branch information...
phlipper committed Sep 19, 2012
1 parent dbef975 commit 013a8b2b15fd4e899ea87443d17468484992399d
Showing with 1,788 additions and 0 deletions.
  1. +17 −0 Gemfile
  2. +20 −0 MIT-LICENSE
  3. +55 −0 README.md
  4. +40 −0 Rakefile
  5. 0 app/assets/images/thincloud/.gitkeep
  6. 0 app/assets/images/thincloud/authentication/.gitkeep
  7. +15 −0 app/assets/javascripts/thincloud/authentication/application.js
  8. +13 −0 app/assets/stylesheets/thincloud/authentication/application.css
  9. +58 −0 app/controllers/thincloud/authentication/application_controller.rb
  10. +69 −0 app/controllers/thincloud/authentication/registrations_controller.rb
  11. +23 −0 app/controllers/thincloud/authentication/sessions_controller.rb
  12. +9 −0 app/helpers/thincloud/authentication/registrations_helper.rb
  13. +80 −0 app/models/thincloud/authentication/identity.rb
  14. +14 −0 app/views/thincloud/authentication/layouts/application.html.erb
  15. +69 −0 app/views/thincloud/authentication/registrations/_registration_form.html.erb
  16. +1 −0 app/views/thincloud/authentication/registrations/new.html.erb
  17. +46 −0 app/views/thincloud/authentication/sessions/_login_form.html.erb
  18. +2 −0 app/views/thincloud/authentication/sessions/destroy.html.erb
  19. +25 −0 app/views/thincloud/authentication/sessions/new.html.erb
  20. +14 −0 config/routes.rb
  21. +19 −0 db/migrate/20120918233329_create_thincloud_authentication_identities.rb
  22. +4 −0 lib/tasks/thincloud-authentication_tasks.rake
  23. +6 −0 lib/thincloud-authentication.rb
  24. +17 −0 lib/thincloud/authentication/engine.rb
  25. +5 −0 lib/thincloud/authentication/version.rb
  26. +8 −0 script/rails
  27. +172 −0 test/controllers/thincloud/authentication/registrations_controller_test.rb
  28. +39 −0 test/controllers/thincloud/authentication/sessions_controller_test.rb
  29. +261 −0 test/dummy/README.rdoc
  30. +7 −0 test/dummy/Rakefile
  31. +15 −0 test/dummy/app/assets/javascripts/application.js
  32. +13 −0 test/dummy/app/assets/stylesheets/application.css
  33. +3 −0 test/dummy/app/controllers/application_controller.rb
  34. +2 −0 test/dummy/app/helpers/application_helper.rb
  35. 0 test/dummy/app/mailers/.gitkeep
  36. 0 test/dummy/app/models/.gitkeep
  37. +3 −0 test/dummy/app/models/user.rb
  38. +16 −0 test/dummy/app/views/layouts/application.html.erb
  39. +4 −0 test/dummy/config.ru
  40. +66 −0 test/dummy/config/application.rb
  41. +10 −0 test/dummy/config/boot.rb
  42. +25 −0 test/dummy/config/database.yml
  43. +5 −0 test/dummy/config/environment.rb
  44. +37 −0 test/dummy/config/environments/development.rb
  45. +67 −0 test/dummy/config/environments/production.rb
  46. +37 −0 test/dummy/config/environments/test.rb
  47. +7 −0 test/dummy/config/initializers/backtrace_silencers.rb
  48. +15 −0 test/dummy/config/initializers/inflections.rb
  49. +5 −0 test/dummy/config/initializers/mime_types.rb
  50. +7 −0 test/dummy/config/initializers/secret_token.rb
  51. +8 −0 test/dummy/config/initializers/session_store.rb
  52. +14 −0 test/dummy/config/initializers/wrap_parameters.rb
  53. +5 −0 test/dummy/config/locales/en.yml
  54. +25 −0 test/dummy/config/routes.rb
  55. +8 −0 test/dummy/db/migrate/20120919182522_create_users.rb
  56. +38 −0 test/dummy/db/schema.rb
  57. 0 test/dummy/lib/assets/.gitkeep
  58. 0 test/dummy/log/.gitkeep
  59. +26 −0 test/dummy/public/404.html
  60. +26 −0 test/dummy/public/422.html
  61. +25 −0 test/dummy/public/500.html
  62. 0 test/dummy/public/favicon.ico
  63. +6 −0 test/dummy/script/rails
  64. +18 −0 test/minitest_helper.rb
  65. +82 −0 test/models/identity_test.rb
  66. +13 −0 test/models/user_test.rb
  67. +8 −0 test/support/user.rb
  68. +7 −0 test/thincloud-authentication_test.rb
  69. +34 −0 thincloud-authentication.gemspec
View
17 Gemfile
@@ -0,0 +1,17 @@
+source "http://rubygems.org"
+
+# Declare your gem's dependencies in thincloud-authentication.gemspec.
+# Bundler will treat runtime dependencies like base dependencies, and
+# development dependencies will be added by default to the :development group.
+gemspec
+
+# jquery-rails is used by the dummy application
+gem "jquery-rails"
+
+# Declare any dependencies that are still in development here instead of in
+# your gemspec. These might include edge Rails or gems from your path or
+# Git. Remember to move these dependencies to your gemspec before releasing
+# your gem to rubygems.org.
+
+# To use debugger
+# gem 'debugger'
View
@@ -0,0 +1,20 @@
+Copyright 2012 New Leaders
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,55 @@
+# Thincloud::Authentication
+
+## Description
+
+A Rails Engine to provide authentication for Thincloud applications.
+
+## Requirements
+
+This gem requires Rails 3.2+ and has been tested on the following versions:
+
+* 3.2
+
+This gem has been tested against the following Ruby versions:
+
+* 1.9.3
+
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+``` ruby
+gem "thincloud-authentication"
+```
+
+And then execute:
+
+```
+$ bundle
+```
+
+Or install it yourself as:
+
+```
+$ gem install thincloud-authentication
+```
+
+## Usage
+
+TODO
+
+## Contributing
+
+1. [Fork it](https://github.com/newleaders/thincloud-authentication/fork_select)
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. [Create a Pull Request](https://github.com/newleaders/thincloud-authentication/pull/new)
+
+
+## License
+
+* Freely distributable and licensed under the MIT-style license. See LICENSE file for details.
+* Copyright (c) 2012 New Leaders
+* [https://newleaders.com](https://newleaders.com)
View
@@ -0,0 +1,40 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+begin
+ require 'rdoc/task'
+rescue LoadError
+ require 'rdoc/rdoc'
+ require 'rake/rdoctask'
+ RDoc::Task = Rake::RDocTask
+end
+
+RDoc::Task.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Thincloud::Authentication'
+ rdoc.options << '--line-numbers'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
+load 'rails/tasks/engine.rake'
+
+
+
+Bundler::GemHelper.install_tasks
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+
+task :default => :test
No changes.
@@ -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,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,58 @@
+module Thincloud::Authentication
+ # Public: Primary controller settings and helpers for the engine.
+ class ApplicationController < ActionController::Base
+ layout "application"
+
+ protected
+
+ # Protected: The user that is currently logged in.
+ #
+ # This method is also available as a view helper.
+ #
+ # Returns: An instance of `User` or `nil`.
+ def current_user
+ return nil unless session[:uid].present?
+ @current_user ||= User.find(session[:uid])
+ end
+ helper_method :current_user
+
+ # Protected: Determine if the current request has a logged in user.
+ #
+ # This method is also available as a view helper.
+ #
+ # Returns: Boolean.
+ def logged_in?
+ current_user.present?
+ end
+ helper_method :logged_in?
+
+ # Protected: Require an authenticated user to perform an action.
+ #
+ # Use in a `before_filter`.
+ #
+ # Returns: Redirect if not logged in, otherwise `nil`.
+ def authenticate!
+ unless logged_in?
+ redirect_to login_url, alert: "You must be logged in to continue."
+ end
+ end
+
+ # Protected: Set the `current_user` to the provided `User` instance.
+ #
+ # user - An instance of `User` that has been authenticated.
+ #
+ # Returns: The `id` of the provided user.
+ def login_as(user)
+ reset_session # avoid session fixation
+ session[:uid] = user.id
+ end
+
+ # Protected: Clear the session of an authenticated user.
+ #
+ # Returns: A new empty session instance.
+ def logout
+ reset_session
+ end
+
+ end
+end
@@ -0,0 +1,69 @@
+require_dependency "thincloud/authentication/application_controller"
+
+module Thincloud::Authentication
+ # Public: Handle OmniAuth callbacks.
+ class RegistrationsController < ApplicationController
+ def new
+ @identity = Identity.new
+ end
+
+ def create
+ # identity exists
+ if omniauth && identity = Identity.find_omniauth(omniauth)
+ login_as identity.user
+ redirect_to root_url, notice: "You have been logged in."
+ # new identity for current_user
+ elsif current_user
+ current_user.identities.build.apply_omniauth(omniauth).save
+ redirect_to root_url, notice: "You have been logged in."
+ # failed identity login
+ elsif invalid_identity_credentials?
+ redirect_to auth_failure_url message: "invalid_credentials",
+ strategy: "identity"
+ # create a new identity
+ else
+ # params[:identity] exists when creating a local identity provider
+ @identity = Identity.new(params[:identity])
+ @identity.user = User.create
+
+ # omniauth exists if coming from a 3rd party provider like LinkedIn
+ if omniauth
+ @identity.apply_omniauth(omniauth)
+ else
+ flash[:alert] = "Welcome! Please check your email to verify your " <<
+ "registration."
+ end
+
+ if @identity.save
+ login_as @identity.user if omniauth
+ redirect_to root_url
+ else
+ render :new
+ end
+ end
+ end
+
+ def verify
+ identity = Identity.verify!(params[:token])
+ login_as identity.user
+ redirect_to root_url,
+ notice: "Thank you! Your registration has been verified."
+ end
+
+ private
+
+ # Private: Accessor for OmniAuth environment.
+ #
+ # Returns: An instance of `OmniAuth::InfoHash` or `nil`.
+ def omniauth
+ request.env["omniauth.auth"]
+ end
+
+ # Private: Determine if the request is from an invalid Identity login.
+ #
+ # Returns: Boolean.
+ def invalid_identity_credentials?
+ params[:provider] == "identity" && params[:auth_key].present?
+ end
+ end
+end
@@ -0,0 +1,23 @@
+require_dependency "thincloud/authentication/application_controller"
+
+module Thincloud::Authentication
+ # Public: Handle login/logout behavior.
+ class SessionsController < ApplicationController
+ before_filter :authenticate!, only: [:authenticated]
+
+ def new
+ redirect_to root_url if logged_in?
+ @identity = Identity.new
+ end
+
+ def destroy
+ logout
+ redirect_to root_url, notice: "You have been logged out."
+ end
+
+ def authenticated
+ # dummy method to test the :authenticate! before_filter
+ render text: "Authenticated!"
+ end
+ end
+end
@@ -0,0 +1,9 @@
+module Thincloud::Authentication
+ module RegistrationsHelper
+
+ def form_error_class_for(form, field)
+ "error" if form.object.errors[field].present?
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit 013a8b2

Please sign in to comment.