authentifyd 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +15 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +26 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/images/authentifyd/en.png +0 -0
  6. data/app/assets/images/authentifyd/facebook-logo.png +0 -0
  7. data/app/assets/images/authentifyd/fr.png +0 -0
  8. data/app/assets/images/authentifyd/google-logo.png +0 -0
  9. data/app/assets/images/authentifyd/signin_with_facebook.png +0 -0
  10. data/app/assets/images/authentifyd/signin_with_fb.png +0 -0
  11. data/app/assets/images/authentifyd/signin_with_google.png +0 -0
  12. data/app/assets/images/authentifyd/signin_with_twitter.png +0 -0
  13. data/app/assets/images/authentifyd/twitter-logo.png +0 -0
  14. data/app/assets/javascripts/authentifyd/application.js +15 -0
  15. data/app/assets/stylesheets/authentifyd/application.css +13 -0
  16. data/app/controllers/authentifyd/application_controller.rb +11 -0
  17. data/app/controllers/authentifyd/authentications_controller.rb +94 -0
  18. data/app/controllers/authentifyd/confirmations_controller.rb +5 -0
  19. data/app/controllers/authentifyd/passwords_controller.rb +4 -0
  20. data/app/controllers/authentifyd/registrations_controller.rb +42 -0
  21. data/app/controllers/authentifyd/sessions_controller.rb +13 -0
  22. data/app/controllers/authentifyd/unlocks_controller.rb +4 -0
  23. data/app/helpers/authentifyd/application_helper.rb +4 -0
  24. data/app/models/authentifyd.rb +5 -0
  25. data/app/models/authentifyd/authentication.rb +16 -0
  26. data/app/models/authentifyd/user.rb +66 -0
  27. data/app/views/authentifyd/authentications/_authentication.html.erb +15 -0
  28. data/app/views/authentifyd/authentications/index.html.erb +14 -0
  29. data/app/views/authentifyd/authentications/link.html.erb +14 -0
  30. data/app/views/authentifyd/confirmations/new.html.erb +25 -0
  31. data/app/views/authentifyd/devise/mailer/confirmation_instructions.html.erb +5 -0
  32. data/app/views/authentifyd/devise/mailer/reset_password_instructions.html.erb +8 -0
  33. data/app/views/authentifyd/devise/mailer/unlock_instructions.html.erb +7 -0
  34. data/app/views/authentifyd/devise/shared/_links.erb +30 -0
  35. data/app/views/authentifyd/layouts/_account_menu.html.haml +24 -0
  36. data/app/views/authentifyd/passwords/edit.html.erb +30 -0
  37. data/app/views/authentifyd/passwords/new.html.erb +27 -0
  38. data/app/views/authentifyd/registrations/_authentifyd_form.html.erb +52 -0
  39. data/app/views/authentifyd/registrations/edit.html.erb +1 -0
  40. data/app/views/authentifyd/registrations/new.html.erb +44 -0
  41. data/app/views/authentifyd/sessions/_social_networks.html.erb +23 -0
  42. data/app/views/authentifyd/sessions/new.html.erb +49 -0
  43. data/app/views/authentifyd/unlocks/new.html.erb +28 -0
  44. data/app/views/layouts/authentifyd/_navbar.html.haml +41 -0
  45. data/app/views/layouts/authentifyd/application.html.erb +19 -0
  46. data/config/initializers/devise.rb +3 -0
  47. data/config/initializers/omniauth.rb +119 -0
  48. data/config/locales/devise.en.yml +100 -0
  49. data/config/locales/devise.fr.yml +113 -0
  50. data/config/locales/en.yml +7 -0
  51. data/config/locales/fr.yml +7 -0
  52. data/config/routes.rb +30 -0
  53. data/db/migrate/20121120091659_create_authentifyd_users.rb +52 -0
  54. data/db/migrate/20121120091700_create_authentifyd_authentications.rb +11 -0
  55. data/db/migrate/20130827085250_add_language_to_user.rb +5 -0
  56. data/lib/authentifyd.rb +39 -0
  57. data/lib/authentifyd/engine.rb +16 -0
  58. data/lib/authentifyd/version.rb +3 -0
  59. data/lib/tasks/authentifyd_tasks.rake +4 -0
  60. metadata +256 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjQ0NzE2OGRkMDhhMmFmMmY3OTY2Yjc1YWQ3MzhlOWZmN2YwMTFiZA==
5
+ data.tar.gz: !binary |-
6
+ ZDA2ZDc3MTc0YmVkNTkxMGM5ZWQ3YzdmOWYxODJmZGQ3YmM0ODNjNg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NWFlZDAxODNiZjNiMzYwM2UyYTU5MGZiMGI0NDRkN2ZlZTU5ZjRiOWQ5Mzg1
10
+ NGI5NWE0OTg5Yjg3ZDM2NzFlODA3YmQxMWIxMDE0YzRiYjllNDYzNGEwNGU0
11
+ ZjljOThlOTdiODVjNzQxZjYzZGI2YTRkMmZiNTAzYzBiMmQwZWY=
12
+ data.tar.gz: !binary |-
13
+ NGQzYTFlNjU1NjM0YzQ2ZTNjMWIyOTk5NjJlNmRiN2M5YTBhZDE5YzhjZDY4
14
+ NjdiZTU3OWI2OWYzZDgxMDYyYmRkZjQzOGQ5MjhmZDBkYmMyNWJjNWJhMDRj
15
+ YjRmZTNjMDE5OWNlNDkxZjU3YzdmMmNhZmE2YmZhYWI4MTEzZjg=
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ = Authentifyd
2
+
3
+ Devise + Omniauth Authentication encapsulated in an Engine. Uses Devise. Parts taken from perens-instant-user and sso-devise-omniauth-provider.
4
+
5
+ == Installation
6
+
7
+ * gem 'authentifyd'
8
+ * rake authentifyd:install:migrations
9
+ * rake db:migrate
10
+ * in config/routes.rb add:
11
+ mount Authentifyd::Engine => "/#{your root for authentifyd}"
12
+ * NB: don't mount Engine on root "/". This could be a feature added in the future but for the moment this possibility is not implemented.
13
+
14
+ * Configure Devise: copy paste http://raw.github.com/plataformatec/devise/master/lib/generators/templates/devise.rb in config/initializers/devise.rb. Edit it.
15
+ * NB: set "config.pepper".
16
+ ex: require 'securerandom' -> SecureRandom.hex(64)
17
+ * Add Facebook and Twitter config files
18
+ * Example
19
+ staging:
20
+ app_id: APP ID FOR STAGING ENV
21
+ app_secret: APP SECRET FOR STAGING ENV
22
+ development: &development
23
+ app_id: APP ID FOR DEV ENV
24
+ app_secret: APP SECRET FOR DEV ENV
25
+ test:
26
+ <<: *development
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Authentifyd'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
25
+ load 'rails/tasks/engine.rake'
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec)
32
+
33
+ task :cucumber => 'app:cucumber'
34
+ task :default => [:spec, :cucumber]
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,11 @@
1
+ module Authentifyd
2
+ class ApplicationController < ActionController::Base
3
+ include Localyzed::LocalyzedController
4
+ end
5
+ end
6
+
7
+ class ActionController::Base
8
+ def after_sign_out_path_for(resource_or_scope)
9
+ params[:callback_url] || session[:referrer] || root_path
10
+ end
11
+ end
@@ -0,0 +1,94 @@
1
+ class Authentifyd::AuthenticationsController < Authentifyd::ApplicationController
2
+ include Localyzed::LocalyzedController
3
+ skip_before_filter :redirect_to_localized_path_if, :only => [:create, :failure]
4
+
5
+ before_filter :authenticate_user!, :except => [:create, :link, :add]
6
+ before_filter :check_id_correct, :only => [:link, :add]
7
+
8
+ def index
9
+ @authentications = current_user.authentications.all
10
+
11
+ respond_to do |format|
12
+ format.html # index.html.erb
13
+ format.xml { render :xml => @authentications }
14
+ end
15
+ end
16
+
17
+ def new
18
+ @authentication = Authentifyd::Authentication.new
19
+ end
20
+
21
+ def add
22
+ @user ||= Authentifyd::User.find(params[:user_id])
23
+ if @user.valid_password?(params[:user][:password])
24
+ omniauth = session[:omniauth]
25
+ @user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
26
+ session[:omniauth] = nil
27
+ sign_in_and_redirect(:user, @user)
28
+ else
29
+ flash[:notice] = "Incorrect Password"
30
+ return redirect_to link_accounts_url(@user.id)
31
+ end
32
+ end
33
+
34
+ def link
35
+ @user ||= Authentifyd::User.find(params[:user_id])
36
+ end
37
+
38
+ def create
39
+ omniauth = request.env['omniauth.auth']
40
+ authentication = Authentifyd::Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
41
+ if authentication
42
+ flash[:notice] = "Signed in successfully"
43
+ sign_in_and_redirect(:user, authentication.user)
44
+ else
45
+ user = user_signed_in? ? current_user : Authentifyd::User.new
46
+ user.apply_omniauth(omniauth)
47
+ if user_signed_in?
48
+ user.save
49
+ redirect_to accounts_url
50
+ else
51
+ omniauth_email = (omniauth["info"] && omniauth["info"]["email"]).try(:to_s)
52
+ user.email = omniauth_email unless user_signed_in?
53
+ if user.save
54
+ flash[:notice] = "Successfully registered"
55
+ sign_in_and_redirect(:user, user)
56
+ else
57
+ session[:omniauth] = omniauth.except('extra')
58
+ session[:omniauth_email] = omniauth_email
59
+
60
+ # Check if email already taken. If so, ask user to link_accounts
61
+ if user.errors[:email][0] =~ /has already been taken/ # omniauth? TBD
62
+ # fetch the user with this email id!
63
+ user = Authentifyd::User.find_by_email(user.email)
64
+ return redirect_to link_accounts_url(user.id)
65
+ end
66
+ redirect_to new_user_registration_url
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ def failure
73
+ flash[:notice] = params[:message]
74
+ redirect_to root_path
75
+ end
76
+
77
+ def destroy
78
+ @authentication = Authentifyd::Authentication.find(params[:id])
79
+ @authentication.destroy
80
+
81
+ respond_to do |format|
82
+ format.html { redirect_to(accounts_url) }
83
+ format.xml { head :ok }
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def check_id_correct
90
+ unless @user = Authentifyd::User.find_by_id(params[:user_id])
91
+ redirect_to new_user_session_url
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,5 @@
1
+ class Authentifyd::ConfirmationsController < Devise::ConfirmationsController
2
+ include Localyzed::LocalyzedController
3
+
4
+ layout "authentifyd/application"
5
+ end
@@ -0,0 +1,4 @@
1
+ class Authentifyd::PasswordsController < Devise::PasswordsController
2
+ include Localyzed::LocalyzedController
3
+ layout "authentifyd/application"
4
+ end
@@ -0,0 +1,42 @@
1
+ class Authentifyd::RegistrationsController < Devise::RegistrationsController
2
+ include Localyzed::LocalyzedController
3
+
4
+ layout "authentifyd/application"
5
+ before_filter :define_body_class
6
+
7
+ def new
8
+ # Building the resource with information that MAY BE available from omniauth!
9
+ build_resource(:email => session[:omniauth_email])
10
+ render :new
11
+ end
12
+
13
+ def create
14
+ build_resource
15
+
16
+ if session[:omniauth] && @user.errors[:email][0] =~ /has already been taken/
17
+ user = Authentifyd::User.find_by_email(@user.email)
18
+ # Link Accounts - if via social connect
19
+ return redirect_to link_accounts_url(user.id)
20
+ end
21
+
22
+ # normal processing
23
+ super
24
+ session[:omniauth] = nil unless @user.new_record?
25
+ end
26
+
27
+ def build_resource(*args)
28
+ super
29
+
30
+ if session[:omniauth]
31
+ @user.apply_omniauth(session[:omniauth])
32
+ @user.valid?
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def define_body_class
39
+ @body_class = "authentifyd_registrations_#{action_name}"
40
+ end
41
+
42
+ end
@@ -0,0 +1,13 @@
1
+ class Authentifyd::SessionsController < Devise::SessionsController
2
+ include Localyzed::LocalyzedController
3
+ layout "authentifyd/application"
4
+ # before_filter :define_body_class
5
+
6
+ before_filter :define_body_class
7
+ private
8
+
9
+ def define_body_class
10
+ @body_class = "authentifyd_sessions_#{action_name}"
11
+ end
12
+
13
+ end
@@ -0,0 +1,4 @@
1
+ class Authentifyd::UnlocksController < Devise::UnlocksController
2
+ include Localyzed::LocalyzedController
3
+ layout "authentifyd/application"
4
+ end
@@ -0,0 +1,4 @@
1
+ module Authentifyd
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module Authentifyd
2
+ def self.table_name_prefix
3
+ 'authentifyd_'
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ class Authentifyd::Authentication < ActiveRecord::Base
2
+ attr_accessible :provider, :uid, :user_id
3
+ belongs_to :user
4
+
5
+ def displayable_name
6
+ displayable_names(provider).try(:capitalize)
7
+ end
8
+
9
+ def displayable_names(provder)
10
+ case provder
11
+ when 'google_oauth2' then 'google'
12
+ else
13
+ provder
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,66 @@
1
+ class Authentifyd::User < ActiveRecord::Base
2
+ has_many :authentications, :dependent => :delete_all
3
+
4
+ # Include default devise modules. Others available are:
5
+ # :encryptable, :timeoutable and :omniauthable
6
+ devise :database_authenticatable
7
+ devise :registerable,
8
+ :recoverable, :rememberable, :trackable, :validatable,
9
+ :confirmable, :lockable, :token_authenticatable, :timeoutable
10
+
11
+ def apply_omniauth(omniauth)
12
+ authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
13
+ end
14
+
15
+ # Setup accessible (or protected) attributes for your model
16
+ attr_accessible :email, :password, :password_confirmation, :remember_me, :language
17
+
18
+ ##
19
+ # :method: before_create__:ensure_password_exists
20
+ #
21
+ # creates a password, allowing user to signup
22
+ # also creates a reset_password_token so that the user can be redirected to a reste_password url automatically
23
+ #
24
+ before_validation :generate_password_token, :confirm_if_already_confirmed
25
+
26
+ before_save :store_language
27
+
28
+ private
29
+
30
+ # creates a password for the user if the password is not set
31
+ # allowing registration via omniauth and with email only
32
+ #
33
+ # called before_validation
34
+ #
35
+ def generate_password_token
36
+ unless self.encrypted_password && !self.encrypted_password.blank?
37
+ pwd = Devise.friendly_token[0,20]
38
+ self.password = pwd
39
+ set_reset_password_token
40
+ end
41
+ end
42
+
43
+ def set_reset_password_token
44
+ # if is not created from omniauth, generate reset_password_token
45
+ # we have also customized the confirmation logic to directly send a reset password
46
+ self.reset_password_token = Devise.friendly_token[0,20] if self.reset_password_token.nil?
47
+ self.reset_password_sent_at = Time.new
48
+ end
49
+
50
+ # sets confirmed_at to the user if the user is_already_confirmed
51
+ #
52
+ # called before_validation
53
+ #
54
+ def confirm_if_already_confirmed
55
+ if self.email && self.authentications.any?
56
+ new_record? ? skip_confirmation! : skip_reconfirmation!
57
+ end
58
+ end
59
+
60
+ # sets the language with with the user has registered
61
+ #
62
+ def store_language
63
+ self.language ||= I18n.locale.to_s
64
+ end
65
+
66
+ end
@@ -0,0 +1,15 @@
1
+ <div class='span12 <%= "provider-li #{authentication.provider}" %>'>
2
+ <div class='pull-left'>
3
+ <%= image_tag "authentifyd/#{authentication.displayable_name.downcase}-logo.png", :class=>'pull-left', :width => '30'%>
4
+ &nbsp;
5
+ <%= authentication.displayable_name %>
6
+ </div>
7
+ <div class='pull-right'>
8
+ <%= link_to destroy_account_path(authentication), method: :delete do %>
9
+ <i class='icon-unlink'></i>&nbsp;
10
+ <%= t("Unlink_account") %>
11
+ <% end %>
12
+ </div>
13
+ <div class='clearfix'></div>
14
+ </br>
15
+ </div>