login 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2012 SAMER BUNA
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,56 @@
1
+ # LOGIN
2
+
3
+ LOGIN is a Rails engine to quickly enable OAuth logins in a Rails website.
4
+
5
+ 1) Create an OAuth application, 2) Configure LOGIN to work with it, 3) run the migrations,
6
+ and you'll be ready to login users to your site.
7
+
8
+ LOGIN uses Devise and OmniAuth, and curruntly supports :github,
9
+ :twitter, :facebook, and :google_apps providers. You can only do OAuth.
10
+ Login with passwords is not supported, and will never be.
11
+ The world has enough passwords.
12
+
13
+ # INSTALL
14
+
15
+ gem "login"
16
+
17
+ rake login:install:migrations
18
+ rake db:migrate
19
+
20
+ # Configuration Example
21
+
22
+ Enable login via github
23
+
24
+ 1) Create a [github application](https://github.com/settings/applications), note the Client ID and Client Secret.
25
+ The callback URL has to be #{YOUR-SITE}/users/auth/github/callback
26
+
27
+ 2) Initialize this in your app:
28
+
29
+ Login.config = {
30
+ :github => ["#{client_id}", "#{client_secret}", :scope => "user"]
31
+ }
32
+
33
+ 3) Somewhere in your views, add a link to login: ``` link_to "Login in via Github", omniauth_authorize_path('user', :github) ```
34
+ There is also a logout route: ``` link_to "Logout", logout_path ```
35
+
36
+ And that would be it. Devise/OmniAuth/LOGIN take care of the rest.
37
+
38
+ You can use Devise helpers as normal, current_user, user_signed_in?, etc..
39
+
40
+ # What LOGIN does
41
+
42
+ LOGIN creates 2 tables:
43
+ * users: will have a row for each unique user, it also has name and
44
+ accounts columns, and will put information there if available (from OAuth).
45
+ * providers: for each provider a user uses, LOGIN will store a record here.
46
+
47
+ A user might have many providers, the same user (identified
48
+ uniquiely by email when available), can login with github, and
49
+ google_apps, for example, as long as OAuth gives that unique email,
50
+ LOGIN logs the user's record in.
51
+
52
+ LOGIN configures OAuth callbacks to work in an autamatic way, if the
53
+ user is logging to your website for the first time, LOGIN creates a new
54
+ users record for them, after that, it's a normal login for an existing
55
+ record. If you need to capture more information about the user, you can
56
+ always later force them to update the profile, for example.
@@ -0,0 +1,27 @@
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 = 'Login'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,31 @@
1
+ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
2
+
3
+ def github
4
+ provider :github
5
+ end
6
+
7
+ def twitter
8
+ provider :twitter
9
+ end
10
+
11
+ def google_apps
12
+ provider :google_apps
13
+ end
14
+
15
+ def facebook
16
+ provider :facebook
17
+ end
18
+
19
+ private
20
+ def after_sign_in_path_for(resource)
21
+ session[:redirect] || root_path
22
+ end
23
+
24
+ def provider provider_id
25
+ @user = Login::User.provider(request.env["omniauth.auth"], provider_id).user
26
+ session["devise.#{provider_id}_data"] = request.env["omniauth.auth"]
27
+ flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => (provider_id == :google_apps ? :gmail : provider_id).to_s.titleize
28
+
29
+ sign_in_and_redirect @user.reload, :event => :authentication
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ module Login
2
+ class Provider < ActiveRecord::Base
3
+ attr_accessible :provider, :uid
4
+ belongs_to :user
5
+
6
+ module Association
7
+ def find_or_create auth
8
+ provider = where(provider:auth.provider, uid:auth.uid).first
9
+ provider ||= create!(provider:auth.provider, uid:auth.uid)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,46 @@
1
+ module Login
2
+ class User < ActiveRecord::Base
3
+ devise :database_authenticatable, :trackable, :token_authenticatable, :omniauthable
4
+
5
+ attr_accessible :email, :twitter, :github, :screen_name, :first_name, :last_name, :password
6
+
7
+ has_many :providers, :extend => Provider::Association
8
+
9
+ validates :screen_name, :uniqueness => true, :allow_blank => true
10
+ validates :email, :uniqueness => true, :allow_blank => true
11
+ validates :twitter, :uniqueness => true, :allow_blank => true
12
+ validates :github, :uniqueness => true, :allow_blank => true
13
+
14
+ def self.provider auth, provider_id
15
+ raise "Not enough information provided" unless auth.info.nickname.present? || auth.info.email.present?
16
+ find_hash = { email:auth.info.email }
17
+ find_hash = { github:auth.info.nickname } if provider_id == :github
18
+ find_hash = { twitter:auth.info.nickname } if provider_id == :twitter
19
+ user = where(find_hash).first
20
+ twitter, github = "", ""
21
+ twitter = auth.info.nickname if provider_id == :twitter
22
+ github = auth.info.nickname if provider_id == :github
23
+ first_name = auth.info.first_name.presence || auth.info.name.split.first || ""
24
+ last_name = auth.info.last_name.presence || auth.info.name.split.last || ""
25
+ transaction do
26
+ if user
27
+ user.update_attribute :twitter, twitter unless user.twitter.present?
28
+ user.update_attribute :first_name, first_name unless user.first_name.present?
29
+ user.update_attribute :last_name, last_name unless user.last_name.present?
30
+ else
31
+ create_hash = { github: github, twitter: twitter, first_name: first_name, last_name: last_name, password: Devise.friendly_token[0,20] }
32
+ create_hash[:email] = auth.info.email.presence
33
+ user = create! create_hash
34
+ end
35
+ user.providers.find_or_create auth
36
+ end
37
+ end
38
+
39
+ def display_name
40
+ return screen_name if screen_name.present?
41
+ return "@#{github}" if github.present?
42
+ return "@#{twitter}" if twitter.present?
43
+ return email.gsub(/@(.).*$/,'@\1')
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ Rails.application.routes.draw do
2
+ devise_for :users, :class_name => "Login::User", :skip => [:sessions],
3
+ :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
4
+ devise_scope :user do
5
+ get "/logout", :to => "devise/sessions#destroy"
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ class DeviseCreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table(:login_users) do |t|
4
+ t.string :first_name, :last_name, :email, :default => ""
5
+ t.string :encrypted_password, :default => ""
6
+
7
+ # Trackable
8
+ t.integer :sign_in_count, :default => 0
9
+ t.datetime :current_sign_in_at
10
+ t.datetime :last_sign_in_at
11
+ t.string :current_sign_in_ip
12
+ t.string :last_sign_in_ip
13
+
14
+ # Token authenticatable
15
+ t.string :authentication_token
16
+
17
+ # Non-devise fields
18
+ t.string :twitter, :github, :screen_name
19
+
20
+ t.timestamps
21
+ end
22
+
23
+ add_index :login_users, :authentication_token, :unique => true
24
+ end
25
+ end
@@ -0,0 +1,8 @@
1
+ class OmniAuthProviders < ActiveRecord::Migration
2
+ def change
3
+ create_table :login_providers do |t|
4
+ t.integer :user_id, :null => false
5
+ t.string :provider, :uid, :null => false
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ require "login/engine"
2
+
3
+ require "devise"
4
+ require 'omniauth-github'
5
+ require 'omniauth-twitter'
6
+ require 'omniauth-facebook'
7
+ require 'omniauth-google-apps'
8
+
9
+ module Login
10
+ mattr_accessor :config
11
+
12
+ def self.layout
13
+ config[:layout] || "application"
14
+ end
15
+ end
16
+
17
+ Login.config = {}
@@ -0,0 +1,15 @@
1
+ module Login
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Login
4
+ initializer "devise.config" do
5
+ Devise.setup do |config|
6
+ require 'devise/orm/active_record'
7
+ require 'openid/store/filesystem'
8
+ config.sign_out_via = :get
9
+ [:github, :twitter, :facebook, :google_apps].each do |provider|
10
+ config.omniauth provider, *Login.config[provider] if Login.config[provider]
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Login
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :login do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: login
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Samer Buna
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>'
28
+ - !ruby/object:Gem::Version
29
+ version: 3.1.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: devise
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: omniauth-github
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: omniauth-twitter
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: omniauth-facebook
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: omniauth-google-apps
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: sqlite3
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: jquery-rails
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: rspec-rails
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: Devise and Omniauth defaults
159
+ email:
160
+ - samer.buna@gmail.com
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files: []
164
+ files:
165
+ - app/controllers/users/omniauth_callbacks_controller.rb
166
+ - app/models/login/provider.rb
167
+ - app/models/login/user.rb
168
+ - config/routes.rb
169
+ - db/migrate/20120729190657_devise_create_users.rb
170
+ - db/migrate/20120731003104_omni_auth_providers.rb
171
+ - lib/login.rb
172
+ - lib/tasks/login_tasks.rake
173
+ - lib/login/version.rb
174
+ - lib/login/engine.rb
175
+ - MIT-LICENSE
176
+ - Rakefile
177
+ - README.md
178
+ homepage: https://github.com/samerbuna/login
179
+ licenses: []
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ required_rubygems_version: !ruby/object:Gem::Requirement
191
+ none: false
192
+ requirements:
193
+ - - ! '>='
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ requirements: []
197
+ rubyforge_project:
198
+ rubygems_version: 1.8.24
199
+ signing_key:
200
+ specification_version: 3
201
+ summary: Devise and Omniauth
202
+ test_files: []