google_authentication 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/.rvmrc +1 -0
  4. data/Gemfile +28 -0
  5. data/Guardfile +20 -0
  6. data/LICENSE.txt +20 -0
  7. data/README.markdown +150 -0
  8. data/Rakefile +45 -0
  9. data/VERSION +1 -0
  10. data/app/controllers/google_authentication/omniauth_callbacks_controller.rb +11 -0
  11. data/app/controllers/google_authentication/sessions_controller.rb +10 -0
  12. data/config/routes.rb +11 -0
  13. data/features/google_authentication.feature +27 -0
  14. data/features/step_definitions/google_authentication_steps.rb +19 -0
  15. data/features/support/env.rb +24 -0
  16. data/google_authentication.gemspec +155 -0
  17. data/lib/generators/google_authentication/google_authentication_generator.rb +46 -0
  18. data/lib/generators/google_authentication/install_generator.rb +27 -0
  19. data/lib/generators/templates/MODEL.warning +10 -0
  20. data/lib/generators/templates/README +11 -0
  21. data/lib/generators/templates/google_authentication.rb +14 -0
  22. data/lib/generators/templates/migration.rb +27 -0
  23. data/lib/google_authentication.rb +43 -0
  24. data/lib/google_authentication/acts_as_google_user.rb +77 -0
  25. data/lib/google_authentication/engine.rb +19 -0
  26. data/spec/dummy/Rakefile +7 -0
  27. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  28. data/spec/dummy/app/controllers/posts_controller.rb +83 -0
  29. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  30. data/spec/dummy/app/helpers/posts_helper.rb +2 -0
  31. data/spec/dummy/app/models/post.rb +2 -0
  32. data/spec/dummy/app/models/user.rb +5 -0
  33. data/spec/dummy/app/views/layouts/application.html.erb +23 -0
  34. data/spec/dummy/app/views/posts/_form.html.erb +25 -0
  35. data/spec/dummy/app/views/posts/edit.html.erb +6 -0
  36. data/spec/dummy/app/views/posts/index.html.erb +25 -0
  37. data/spec/dummy/app/views/posts/new.html.erb +5 -0
  38. data/spec/dummy/app/views/posts/show.html.erb +15 -0
  39. data/spec/dummy/config.ru +4 -0
  40. data/spec/dummy/config/application.rb +45 -0
  41. data/spec/dummy/config/boot.rb +10 -0
  42. data/spec/dummy/config/database.yml +22 -0
  43. data/spec/dummy/config/environment.rb +5 -0
  44. data/spec/dummy/config/environments/development.rb +26 -0
  45. data/spec/dummy/config/environments/production.rb +49 -0
  46. data/spec/dummy/config/environments/test.rb +35 -0
  47. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  48. data/spec/dummy/config/initializers/devise.rb +204 -0
  49. data/spec/dummy/config/initializers/inflections.rb +10 -0
  50. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  51. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  52. data/spec/dummy/config/initializers/session_store.rb +8 -0
  53. data/spec/dummy/config/locales/devise.en.yml +53 -0
  54. data/spec/dummy/config/locales/en.yml +5 -0
  55. data/spec/dummy/config/routes.rb +6 -0
  56. data/spec/dummy/db/migrate/20110630105039_create_posts.rb +14 -0
  57. data/spec/dummy/db/migrate/20110630111038_devise_create_users.rb +18 -0
  58. data/spec/dummy/db/schema.rb +34 -0
  59. data/spec/dummy/public/404.html +26 -0
  60. data/spec/dummy/public/422.html +26 -0
  61. data/spec/dummy/public/500.html +26 -0
  62. data/spec/dummy/public/favicon.ico +0 -0
  63. data/spec/dummy/public/javascripts/application.js +2 -0
  64. data/spec/dummy/public/javascripts/controls.js +965 -0
  65. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  66. data/spec/dummy/public/javascripts/effects.js +1123 -0
  67. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  68. data/spec/dummy/public/javascripts/rails.js +191 -0
  69. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  70. data/spec/dummy/public/stylesheets/scaffold.css +56 -0
  71. data/spec/dummy/script/rails +6 -0
  72. data/spec/generators/google_authentication_generator_spec.rb +39 -0
  73. data/spec/generators/install_generator_spec.rb +23 -0
  74. data/spec/google_authentication_spec.rb +28 -0
  75. data/spec/integration/navigation_spec.rb +22 -0
  76. data/spec/models/acts_as_google_user_spec.rb +67 -0
  77. data/spec/spec_helper.rb +37 -0
  78. metadata +289 -0
@@ -0,0 +1,46 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/devise/orm_helpers'
3
+
4
+ module GoogleAuthentication
5
+ module Generators # :nodoc:
6
+ class GoogleAuthenticationGenerator < ActiveRecord::Generators::Base # :nodoc:
7
+ namespace "google_authentication"
8
+ source_root File.expand_path("../../templates", __FILE__)
9
+
10
+ # include some helpers from devise
11
+ include Devise::Generators::OrmHelpers
12
+
13
+ # accept same arguments as devise generator with google tuned defaults
14
+ argument :attributes, :type => :array, :default => ['first_name:string', 'last_name:string'], :banner => "field:type field:type"
15
+
16
+ desc "Generates a model with the given NAME with google_authentication " <<
17
+ "configuration plus a migration file which create it."
18
+
19
+ # Generate the model without a migration file using the active_record generator
20
+ def generate_model
21
+ invoke "active_record:model", [name], :migration => false unless model_exists? && behavior == :invoke
22
+ end
23
+
24
+ # Build the migration files for the given model
25
+ def copy_devise_migration
26
+ migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
27
+ end
28
+
29
+ # Insert gem activation code into the selected class
30
+ def inject_google_authentication_content
31
+ inject_into_class(model_path, class_name) do
32
+ return nil unless model_exists?
33
+ " # You can add other devise modules here as arguments, as in devise calls. :omniauthable module is always added\n" <<
34
+ " # and forbidden devise modules are automatically removed (see GoogleAuthentication::ActsAsGoogleUser::FORBIDDEN_MODULES)\n" <<
35
+ " # if you change this line, remember to edit the generated migration\n" <<
36
+ " acts_as_google_user\n"
37
+ end
38
+ end
39
+
40
+ # Show a readme file with further instructions after installation
41
+ def show_readme
42
+ readme "MODEL.warning" if behavior == :invoke && class_name != 'User'
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,27 @@
1
+
2
+ module GoogleAuthentication
3
+ module Generators # :nodoc:
4
+ class InstallGenerator < Rails::Generators::Base # :nodoc:
5
+ source_root File.expand_path("../../templates", __FILE__)
6
+
7
+ desc "Create an initializer for GoogleAuthentication gem and install devise (replace devise:install)"
8
+
9
+ class_option :domain, :desc => 'Google(Apps) domain used for authentication', :default => 'gmail.com', :type => :string
10
+
11
+ # Copy the initializer template in the config/initializer directory
12
+ def copy_initializer
13
+ template "google_authentication.rb", "config/initializers/google_authentication.rb"
14
+ end
15
+
16
+ # Run devise:install generator
17
+ def install_devise
18
+ invoke "devise:install"
19
+ end
20
+
21
+ # Show the readme file with further instructions after installation
22
+ def show_readme
23
+ readme "README" if behavior == :invoke
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+
2
+ ===============================================================================
3
+
4
+ *** ATTENTION!!! You have specified a model different than User ***
5
+
6
+ You should edit the google_authentication initializer to change the following
7
+
8
+ config.model_name = :your-model-name
9
+
10
+ ===============================================================================
@@ -0,0 +1,11 @@
1
+
2
+ What to do next?
3
+
4
+ 1. Edit initializer (config/initializer/google_authentication.rb) and fill:
5
+ - your desired domain for authentication (default gmail.com)
6
+ - your model used as google user
7
+
8
+ 2.a Run rails g google_authentication MODEL in order to create a default model
9
+ 2.b (or) add acts_as_google_user to the model used for authentication
10
+
11
+ ===============================================================================
@@ -0,0 +1,14 @@
1
+ # Use this hook to configure GoogleAuthentication, values in the comments are defaults
2
+ GoogleAuthentication.setup do |config|
3
+
4
+ # ==> Domain configuration
5
+ # Configure here the domain used for the authentication
6
+ config.domain = '<%= options[:domain] %>'
7
+
8
+ # ==> Model configuration
9
+ # Configure here the model name, it should be the model you've generated
10
+ # using the provided generator. Don't change after first generation
11
+ # unless you know what you're doing
12
+ # config.model_name = :user
13
+
14
+ end
@@ -0,0 +1,27 @@
1
+ class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table(:<%= table_name %>) do |t|
4
+ t.string :email, :null => :false
5
+ t.string :omniauth_uid, :null => false
6
+
7
+ # t.rememberable
8
+ # t.trackable
9
+ # t.confirmable
10
+ # t.token_authenticatable
11
+
12
+ <% for attribute in attributes -%>
13
+ t.<%= attribute.type %> :<%= attribute.name %>
14
+ <% end -%>
15
+
16
+ t.timestamps
17
+ end
18
+
19
+ add_index :<%= table_name %>, :email, :unique => true
20
+ add_index :<%= table_name %>, :omniauth_uid, :unique => true
21
+ # add_index :<%= table_name %>, :authentication_token, :unique => true
22
+ end
23
+
24
+ def self.down
25
+ drop_table :<%= table_name %>
26
+ end
27
+ end
@@ -0,0 +1,43 @@
1
+ require 'google_authentication/acts_as_google_user'
2
+
3
+ # Main module for the gem
4
+ module GoogleAuthentication
5
+
6
+ # domain configuration
7
+ mattr_accessor :domain
8
+ # default value for google domain used for authentication
9
+ @@domain = "gmail.com"
10
+
11
+ # model name configuration
12
+ mattr_accessor :model_name
13
+ # default model used (singular name)
14
+ @@model_name = :user
15
+
16
+ # Allows config in initializer
17
+ # @yield [self] Allows config in initializer using the same syntax as Devise
18
+ # @yieldparam [GoogleAuthentication] config the module itself
19
+ # @example Changing the authentication setup
20
+ # GoogleAuthentication.setup do |config|
21
+ # config.domain = 'your-google-apps-domain.com'
22
+ # config.model_name = :account
23
+ # end
24
+ def self.setup
25
+ yield self
26
+ end
27
+
28
+ # Used in the routes file to decide wheter to add routes
29
+ # to the application
30
+ # @return [bool] true iff the GoogleAuthentication model is already defined
31
+ def self.define_routes?
32
+ Object.const_defined?(model_name.to_s.camelize)
33
+ end
34
+
35
+ # Return a symbol which is used to build devise routes
36
+ # @return [Symbol] a symbol representing the table name used by devise
37
+ def self.devise_table
38
+ model_name.to_s.pluralize.to_sym
39
+ end
40
+
41
+ # require the engine if rails is defined
42
+ require 'google_authentication/engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
43
+ end
@@ -0,0 +1,77 @@
1
+ module GoogleAuthentication
2
+
3
+ # enclose AR methods used to give a model the ability to authenticate as a Google User
4
+ # using devise + omniauth combo
5
+ module ActsAsGoogleUser
6
+
7
+ # Devise module to include in included classes
8
+ mattr_accessor :devise_modules_to_include
9
+ # default devise modules
10
+ @@devise_modules_to_include = [:omniauthable]
11
+
12
+ # Devise forbidden modules, useless in this context
13
+ FORBIDDEN_MODULES = [:database_authenticable, :recoverable, :registrable, :encryptable, :lockable, :validatable, :confirmable]
14
+
15
+ # Devise allowed modules
16
+ ALLOWED_MODULES = [:omniauthable, :token_authenticable, :trackable, :timeoutable, :rememberable]
17
+
18
+ # Configure a model to be used with devise and google authentication
19
+ # @param [Array] modules a list of symbols used with a devise call
20
+ def acts_as_google_user *modules
21
+ # assign devise modules to module variable
22
+ if modules.empty?
23
+ self.devise_modules_to_include = [:omniauthable]
24
+ else
25
+ # restrict modules given to devise
26
+ self.devise_modules_to_include = (modules + [:omniauthable] - FORBIDDEN_MODULES) & ALLOWED_MODULES
27
+ end
28
+ # include model methods
29
+ include ActsAsGoogleUser::Model
30
+ end
31
+
32
+ # Models method added to an AR class which calls acts_as_google_user
33
+ module Model
34
+
35
+ extend ActiveSupport::Concern
36
+
37
+ # send devise methods and attr_accessible to the base class
38
+ included do
39
+ # include devise modules
40
+ devise *(ActsAsGoogleUser.devise_modules_to_include)
41
+ # Setup accessible (or protected) attributes for your model
42
+ attr_accessible :email
43
+ end
44
+
45
+ module ClassMethods # :nodoc:
46
+ # Find omniauth given user or create it
47
+ # @param [Hash] omniauth_data omniauth returned hash
48
+ def find_or_create_by_omniauth_impl omniauth_data
49
+ # use custom implementation if given by the user
50
+ respond_to?(:find_or_create_by_omniauth) ?
51
+ send(:find_or_create_by_omniauth, omniauth_data) :
52
+ find_or_create_by_omniauth_default_impl(omniauth_data)
53
+ end
54
+
55
+ private
56
+
57
+ # default implementation of find_or_create_by_omniauth_uid
58
+ # find the user with the given omniauth_uid or create it
59
+ # assign all properties found in user_info hash to instance
60
+ # and return it
61
+ # @param [Hash] omniauth_data omniauth returned hash
62
+ # @return [ActiveRecord::Base] an instance of the base class
63
+ def find_or_create_by_omniauth_default_impl omniauth_data
64
+ find_or_initialize_by_omniauth_uid(omniauth_data['uid']).tap do |user|
65
+ omniauth_data['user_info'].each do |k, v|
66
+ user.send "#{k}=", v if user.respond_to? "#{k}="
67
+ end
68
+ user.save!
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ # make AR extend the module ActsAsGoogleUser
77
+ ActiveRecord::Base.send :extend, GoogleAuthentication::ActsAsGoogleUser
@@ -0,0 +1,19 @@
1
+ require 'google_authentication'
2
+
3
+ require 'rails'
4
+ require 'action_controller'
5
+
6
+ require 'omniauth'
7
+ require 'devise'
8
+
9
+ module GoogleAuthentication
10
+ # rails engine to add controllers, routes and configuration needed to the gem
11
+ class Engine < Rails::Engine
12
+
13
+ # Initialize devise configuration for omniauth
14
+ initializer "google_authentication.domain", :before => "devise.omniauth" do
15
+ require 'openid/store/filesystem'
16
+ Devise.omniauth :google_apps, OpenID::Store::Filesystem.new('/tmp'), :domain => GoogleAuthentication.domain
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,83 @@
1
+ class PostsController < ApplicationController
2
+ # GET /posts
3
+ # GET /posts.xml
4
+ def index
5
+ @posts = Post.all
6
+
7
+ respond_to do |format|
8
+ format.html # index.html.erb
9
+ format.xml { render :xml => @posts }
10
+ end
11
+ end
12
+
13
+ # GET /posts/1
14
+ # GET /posts/1.xml
15
+ def show
16
+ @post = Post.find(params[:id])
17
+
18
+ respond_to do |format|
19
+ format.html # show.html.erb
20
+ format.xml { render :xml => @post }
21
+ end
22
+ end
23
+
24
+ # GET /posts/new
25
+ # GET /posts/new.xml
26
+ def new
27
+ @post = Post.new
28
+
29
+ respond_to do |format|
30
+ format.html # new.html.erb
31
+ format.xml { render :xml => @post }
32
+ end
33
+ end
34
+
35
+ # GET /posts/1/edit
36
+ def edit
37
+ @post = Post.find(params[:id])
38
+ end
39
+
40
+ # POST /posts
41
+ # POST /posts.xml
42
+ def create
43
+ @post = Post.new(params[:post])
44
+
45
+ respond_to do |format|
46
+ if @post.save
47
+ format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
48
+ format.xml { render :xml => @post, :status => :created, :location => @post }
49
+ else
50
+ format.html { render :action => "new" }
51
+ format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
52
+ end
53
+ end
54
+ end
55
+
56
+ # PUT /posts/1
57
+ # PUT /posts/1.xml
58
+ def update
59
+ @post = Post.find(params[:id])
60
+
61
+ respond_to do |format|
62
+ if @post.update_attributes(params[:post])
63
+ format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
64
+ format.xml { head :ok }
65
+ else
66
+ format.html { render :action => "edit" }
67
+ format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
68
+ end
69
+ end
70
+ end
71
+
72
+ # DELETE /posts/1
73
+ # DELETE /posts/1.xml
74
+ def destroy
75
+ @post = Post.find(params[:id])
76
+ @post.destroy
77
+
78
+ respond_to do |format|
79
+ format.html { redirect_to(posts_url) }
80
+ format.xml { head :ok }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module PostsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class Post < ActiveRecord::Base
2
+ end
@@ -0,0 +1,5 @@
1
+ class User < ActiveRecord::Base
2
+ # provides finder methods for retrieving users by the given omniauth
3
+ acts_as_google_user
4
+
5
+ end
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <% if current_user %>
12
+ <p>Currently logged in as <strong><%= current_user.email %></strong></p>
13
+ <% else %>
14
+ <p>You're not logged in, <%= link_to 'Login', user_omniauth_authorize_path(:google_apps) %></p>
15
+ <% end %>
16
+
17
+ <p class="notice"><%= notice %></p>
18
+ <p class="alert"><%= alert %></p>
19
+
20
+ <%= yield %>
21
+
22
+ </body>
23
+ </html>
@@ -0,0 +1,25 @@
1
+ <%= form_for(@post) do |f| %>
2
+ <% if @post.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @post.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :title %><br />
16
+ <%= f.text_field :title %>
17
+ </div>
18
+ <div class="field">
19
+ <%= f.label :content %><br />
20
+ <%= f.text_area :content %>
21
+ </div>
22
+ <div class="actions">
23
+ <%= f.submit %>
24
+ </div>
25
+ <% end %>