langalex-authlogic_oauth2 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +12 -0
- data/README.rdoc +126 -0
- data/Rakefile +14 -0
- data/init.rb +1 -0
- data/langalex-authlogic_oauth2.gemspec +36 -0
- data/lib/authlogic_oauth2.rb +25 -0
- data/lib/authlogic_oauth2/acts_as_authentic.rb +119 -0
- data/lib/authlogic_oauth2/helper.rb +16 -0
- data/lib/authlogic_oauth2/oauth2_process.rb +75 -0
- data/lib/authlogic_oauth2/session.rb +120 -0
- data/lib/authlogic_oauth2/version.rb +51 -0
- data/lib/oauth2_callback_filter.rb +12 -0
- data/rails/init.rb +1 -0
- metadata +119 -0
data/Manifest
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Manifest
|
2
|
+
README.rdoc
|
3
|
+
Rakefile
|
4
|
+
init.rb
|
5
|
+
lib/authlogic_oauth2.rb
|
6
|
+
lib/authlogic_oauth2/acts_as_authentic.rb
|
7
|
+
lib/authlogic_oauth2/helper.rb
|
8
|
+
lib/authlogic_oauth2/oauth2_process.rb
|
9
|
+
lib/authlogic_oauth2/session.rb
|
10
|
+
lib/authlogic_oauth2/version.rb
|
11
|
+
lib/oauth2_callback_filter.rb
|
12
|
+
rails/init.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
= Authlogic OAuth2
|
2
|
+
|
3
|
+
This plugin works very much like jrallison's authlogic_oauth gem (http://github.com/jrallison/authlogic_oauth), and much of the code is repurposed from his work. If you have used authlogic_oauth before, then you should have no problem using authlogic_oauth2.
|
4
|
+
|
5
|
+
== Install and use
|
6
|
+
|
7
|
+
=== 1. Install the authlogic and oauth2 gems
|
8
|
+
|
9
|
+
config.gem "authlogic"
|
10
|
+
config.gem "oauth2"
|
11
|
+
config.gem "authlogic_oauth2"
|
12
|
+
|
13
|
+
$ sudo rake gems:install
|
14
|
+
|
15
|
+
For older version of Rails, you can install authlogic_oauth2 as a plugin:
|
16
|
+
|
17
|
+
$ script/plugin install git://github.com/andyhite/authlogic_oauth2.git
|
18
|
+
|
19
|
+
=== 2. Set up and configure authlogic
|
20
|
+
|
21
|
+
For information about how to set up and configure authlogic, please consult the authlogic README (http://github.com/binarylogic/authlogic)
|
22
|
+
|
23
|
+
=== 3. Add the necessary fields to your authlogic users table
|
24
|
+
|
25
|
+
class AddOauth2FieldsToUser < ActiveRecord::Migration
|
26
|
+
def self.up
|
27
|
+
add_column :users, :oauth2_token, :string
|
28
|
+
add_index :users, :oauth2_token
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.down
|
32
|
+
remove_column :users, :oauth2_token
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
IMPORTANT: make sure that you allow null values for crypted_password and password_salt if they aren't required for OAuth2 users.
|
37
|
+
|
38
|
+
=== 4. Configure your OAuth2 client in the UserSession model
|
39
|
+
|
40
|
+
The oauth2_client_id, oauth2_client_secret and oauth2_site configuration values must be specified so we can initialize the connection with your OAuth2 provider. The oauth2_scope value is optional, and is used to request extended permissions from your provider.
|
41
|
+
|
42
|
+
Here's an example for Facebook:
|
43
|
+
|
44
|
+
class UserSession < Authlogic::Session::Base
|
45
|
+
oauth2_client_id "APPLICATION_ID"
|
46
|
+
oauth2_client_secret "APPLICATION_SECRET"
|
47
|
+
oauth2_site "https://graph.facebook.com"
|
48
|
+
oauth2_scope "email,user_birthday"
|
49
|
+
end
|
50
|
+
|
51
|
+
Optional: Customize error messages
|
52
|
+
|
53
|
+
Class User < ActiveRecord::Base
|
54
|
+
acts_as_authentic do |config| # this should already be there
|
55
|
+
c.duplicate_oauth2_token_message = "You are already signed up. Please log in."
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
=== 5. Make sure you save your objects properly
|
60
|
+
|
61
|
+
We need to redirect the user to their oauth2 provider so they can authenticate and then pick things back up when they're returned, so any calls to User#save or UserSession#save need to be updated to the following format:
|
62
|
+
|
63
|
+
@user.save do |result|
|
64
|
+
if result
|
65
|
+
# Do something
|
66
|
+
else
|
67
|
+
# Do something else
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
and
|
72
|
+
|
73
|
+
@user_session.save do |result|
|
74
|
+
if result
|
75
|
+
# Do something
|
76
|
+
else
|
77
|
+
# Do something else
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
=== 6. Add the login and register buttons to their respective forms
|
82
|
+
|
83
|
+
In file app/views/user_sessions/new.html.erb:
|
84
|
+
|
85
|
+
<% form_for @user_session, :url => user_session_path do |f| %>
|
86
|
+
# All your other form stuff goes here, if you need it.
|
87
|
+
<%= oauth2_login_button :value => "Login using Facebook" %>
|
88
|
+
<% end %>
|
89
|
+
|
90
|
+
In file app/views/users/new.html.erb:
|
91
|
+
|
92
|
+
<% form_for @user, :url => account_path do |f| %>
|
93
|
+
# All your other form stuff goes here, if you need it.
|
94
|
+
<%= oauth2_register_button :value => "Register using Facebook" %>
|
95
|
+
<% end %>
|
96
|
+
|
97
|
+
=== 7. There is no step 7
|
98
|
+
|
99
|
+
If you followed these steps correctly, then you should be able to register and login using OAuth2.
|
100
|
+
|
101
|
+
== Accessing API endpoints
|
102
|
+
|
103
|
+
You can easily access any API endpoints that are exposed to an OAuth2 user by utilizing the oauth2 gem's "get" method on current_user#oauth2_access. For instance, you can access information about the currently logged in user's Facebook profile by doing the following:
|
104
|
+
|
105
|
+
current_user.oauth2_access.get('/me')
|
106
|
+
|
107
|
+
This will return a JSON string representing the user's profile information.
|
108
|
+
|
109
|
+
You can pre-populate user information by using the after_oauth2_authentication hook in your user model:
|
110
|
+
|
111
|
+
require 'json'
|
112
|
+
|
113
|
+
class User < ActiveRecord::Base
|
114
|
+
...
|
115
|
+
|
116
|
+
def after_oauth2_authentication
|
117
|
+
json = oauth2_access.get('/me')
|
118
|
+
|
119
|
+
if user_data = JSON.parse(json)
|
120
|
+
self.name = user_data['name']
|
121
|
+
self.facebook_uid = user_data['id']
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
You can get more information about the Facebook Graph API on the following website: http://developers.facebook.com/docs/api
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
require File.dirname(__FILE__) + "/lib/authlogic_oauth2/version"
|
5
|
+
|
6
|
+
Echoe.new('langalex-authlogic_oauth2', AuthlogicOauth2::Version::STRING) do |p|
|
7
|
+
p.description = "Authlogic OAuth2 is an extension of the Authlogic library to add OAuth2 support. OAuth2 can be used to allow users to login with their Facebook credentials."
|
8
|
+
p.url = "http://github.com/langalex/authlogic_oauth2"
|
9
|
+
p.author = "Andrew Hite"
|
10
|
+
p.email = "andrew@andrew-hite.com"
|
11
|
+
p.runtime_dependencies = ['authlogic', 'oauth2']
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
File.dirname(__FILE__) + "/rails/init.rb"
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{langalex-authlogic_oauth2}
|
5
|
+
s.version = "1.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Andrew Hite"]
|
9
|
+
s.date = %q{2011-01-25}
|
10
|
+
s.description = %q{Authlogic OAuth2 is an extension of the Authlogic library to add OAuth2 support. OAuth2 can be used to allow users to login with their Facebook credentials.}
|
11
|
+
s.email = %q{andrew@andrew-hite.com}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/authlogic_oauth2.rb", "lib/authlogic_oauth2/acts_as_authentic.rb", "lib/authlogic_oauth2/helper.rb", "lib/authlogic_oauth2/oauth2_process.rb", "lib/authlogic_oauth2/session.rb", "lib/authlogic_oauth2/version.rb", "lib/oauth2_callback_filter.rb"]
|
13
|
+
s.files = ["Manifest", "README.rdoc", "Rakefile", "init.rb", "lib/authlogic_oauth2.rb", "lib/authlogic_oauth2/acts_as_authentic.rb", "lib/authlogic_oauth2/helper.rb", "lib/authlogic_oauth2/oauth2_process.rb", "lib/authlogic_oauth2/session.rb", "lib/authlogic_oauth2/version.rb", "lib/oauth2_callback_filter.rb", "rails/init.rb", "langalex-authlogic_oauth2.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/langalex/authlogic_oauth2}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Langalex-authlogic_oauth2", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{langalex-authlogic_oauth2}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{Authlogic OAuth2 is an extension of the Authlogic library to add OAuth2 support. OAuth2 can be used to allow users to login with their Facebook credentials.}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
s.add_runtime_dependency(%q<authlogic>, [">= 0"])
|
27
|
+
s.add_runtime_dependency(%q<oauth2>, [">= 0"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<authlogic>, [">= 0"])
|
30
|
+
s.add_dependency(%q<oauth2>, [">= 0"])
|
31
|
+
end
|
32
|
+
else
|
33
|
+
s.add_dependency(%q<authlogic>, [">= 0"])
|
34
|
+
s.add_dependency(%q<oauth2>, [">= 0"])
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/authlogic_oauth2/version"
|
2
|
+
require File.dirname(__FILE__) + "/authlogic_oauth2/oauth2_process"
|
3
|
+
require File.dirname(__FILE__) + "/authlogic_oauth2/acts_as_authentic"
|
4
|
+
require File.dirname(__FILE__) + "/authlogic_oauth2/session"
|
5
|
+
require File.dirname(__FILE__) + "/authlogic_oauth2/helper"
|
6
|
+
require File.dirname(__FILE__) + "/oauth2_callback_filter"
|
7
|
+
|
8
|
+
ActiveRecord::Base.send(:include, AuthlogicOauth2::ActsAsAuthentic)
|
9
|
+
Authlogic::Session::Base.send(:include, AuthlogicOauth2::Session)
|
10
|
+
ActionController::Base.helper AuthlogicOauth2::Helper
|
11
|
+
|
12
|
+
# Throw callback rack app into the middleware stack
|
13
|
+
if defined?(ActionController::Metal)
|
14
|
+
# Rails >= 3.0
|
15
|
+
require 'oauth2_callback_filter'
|
16
|
+
if Rails.application.instance_variable_get('@app')
|
17
|
+
Rails.application.instance_variable_set('@app', Oauth2CallbackFilter.new(Rails.application.app))
|
18
|
+
else
|
19
|
+
Rails.configuration.middleware.use(Oauth2CallbackFilter)
|
20
|
+
end
|
21
|
+
elsif defined?(ActionController::Dispatcher) && defined?(ActionController::Dispatcher.middleware)
|
22
|
+
# Rails >= 2.3
|
23
|
+
require 'oauth2_callback_filter'
|
24
|
+
ActionController::Dispatcher.middleware.use(Oauth2CallbackFilter)
|
25
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
module AuthlogicOauth2
|
2
|
+
module ActsAsAuthentic
|
3
|
+
def self.included(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
extend Config
|
6
|
+
add_acts_as_authentic_module(Methods, :prepend)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Config
|
11
|
+
# The name of the oauth2 token field in the database.
|
12
|
+
#
|
13
|
+
# * <tt>Default:</tt> :oauth2_token
|
14
|
+
# * <tt>Accepts:</tt> Symbol
|
15
|
+
def oauth2_token_field(value = nil)
|
16
|
+
rw_config(:oauth2_token_field, value, :oauth2_token)
|
17
|
+
end
|
18
|
+
alias_method :oauth2_token_field=, :oauth2_token_field
|
19
|
+
|
20
|
+
def duplicate_oauth2_token_message(value = nil)
|
21
|
+
rw_config(:duplicate_oauth2_token_message, value, "You are already signed up. Please log in.")
|
22
|
+
end
|
23
|
+
alias_method :duplicate_oauth2_token_message=, :duplicate_oauth2_token_message
|
24
|
+
end
|
25
|
+
|
26
|
+
module Methods
|
27
|
+
include Oauth2Process
|
28
|
+
|
29
|
+
# Set up some simple validations
|
30
|
+
def self.included(klass)
|
31
|
+
klass.class_eval do
|
32
|
+
alias_method "#{oauth2_token_field.to_s}=".to_sym, :oauth2_token=
|
33
|
+
end
|
34
|
+
|
35
|
+
return if !klass.column_names.include?(klass.oauth2_token_field.to_s)
|
36
|
+
|
37
|
+
klass.class_eval do
|
38
|
+
validate :validate_by_oauth2, :if => :authenticating_with_oauth2?
|
39
|
+
|
40
|
+
validates_uniqueness_of klass.oauth2_token_field, :message => rw_config(:duplicate_oauth2_token_message, nil), :scope => validations_scope, :if => :using_oauth2?
|
41
|
+
|
42
|
+
validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_with_oauth2?)
|
43
|
+
validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_with_oauth2?)
|
44
|
+
validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_with_oauth2?)
|
45
|
+
validates_length_of_login_field_options validates_length_of_login_field_options.merge(:if => :validate_password_with_oauth2?)
|
46
|
+
validates_format_of_login_field_options validates_format_of_login_field_options.merge(:if => :validate_password_with_oauth2?)
|
47
|
+
end
|
48
|
+
|
49
|
+
# email needs to be optional for oauth2
|
50
|
+
klass.validate_email_field = false
|
51
|
+
end
|
52
|
+
|
53
|
+
def save(perform_validation = true, &block)
|
54
|
+
if perform_validation && block_given? && redirecting_to_oauth2_server?
|
55
|
+
# Save attributes so they aren't lost during the authentication with the oauth2 server
|
56
|
+
session_class.controller.session[:authlogic_oauth2_attributes] = attributes.reject!{|k, v| v.blank?}
|
57
|
+
redirect_to_oauth2
|
58
|
+
return false
|
59
|
+
end
|
60
|
+
|
61
|
+
result = super
|
62
|
+
yield(result) if block_given?
|
63
|
+
result
|
64
|
+
end
|
65
|
+
|
66
|
+
# Accessors for oauth2 fields
|
67
|
+
def oauth2_token
|
68
|
+
read_attribute(oauth2_token_field)
|
69
|
+
end
|
70
|
+
|
71
|
+
def oauth2_token=(value)
|
72
|
+
write_attribute(oauth2_token_field, value.blank? ? nil : value)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Provides access to an API exposed on the access_token object
|
76
|
+
def oauth2_access
|
77
|
+
access_token
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def authenticating_with_oauth2?
|
83
|
+
# Controller isn't available in all contexts (e.g. irb)
|
84
|
+
return false unless session_class.controller
|
85
|
+
|
86
|
+
# Initial request when user presses one of the button helpers
|
87
|
+
(session_class.controller.params && !session_class.controller.params[:register_with_oauth2].blank?) ||
|
88
|
+
# When the oauth2 provider responds and we made the initial request
|
89
|
+
(oauth2_response && session_class.controller.session && session_class.controller.session[:oauth2_request_class] == self.class.name)
|
90
|
+
end
|
91
|
+
|
92
|
+
def authenticate_with_oauth2
|
93
|
+
# Restore any attributes which were saved before redirecting to the oauth2 server
|
94
|
+
self.attributes = session_class.controller.session.delete(:authlogic_oauth2_attributes)
|
95
|
+
self.oauth2_token = generate_access_token.token
|
96
|
+
|
97
|
+
# Execute callback if it's defined in the user model
|
98
|
+
self.after_oauth2_authentication if self.respond_to?(:after_oauth2_authentication)
|
99
|
+
end
|
100
|
+
|
101
|
+
def access_token
|
102
|
+
OAuth2::AccessToken.new(oauth2_client, read_attribute(oauth2_token_field))
|
103
|
+
end
|
104
|
+
|
105
|
+
def using_oauth2?
|
106
|
+
respond_to?(oauth2_token_field) && !oauth2_token.blank?
|
107
|
+
end
|
108
|
+
|
109
|
+
def validate_password_with_oauth2?
|
110
|
+
!using_oauth2? && require_password?
|
111
|
+
end
|
112
|
+
|
113
|
+
# Convenience methods for accessing configuration values
|
114
|
+
def oauth2_token_field
|
115
|
+
self.class.oauth2_token_field
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module AuthlogicOauth2
|
2
|
+
module Helper
|
3
|
+
def oauth2_register_button(options = {})
|
4
|
+
oauth2_button('register_with_oauth2', options)
|
5
|
+
end
|
6
|
+
|
7
|
+
def oauth2_login_button(options = {})
|
8
|
+
oauth2_button('login_with_oauth2', options)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
def oauth2_button(name, options = {})
|
13
|
+
"<input type='submit' value='#{options[:value]}' name='#{name}' id='user_submit' class='#{options[:class]}'/>"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module AuthlogicOauth2
|
2
|
+
module Oauth2Process
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def validate_by_oauth2
|
7
|
+
validate_email_field = false
|
8
|
+
|
9
|
+
if oauth2_response.blank?
|
10
|
+
redirect_to_oauth2
|
11
|
+
else
|
12
|
+
authenticate_with_oauth2
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def redirecting_to_oauth2_server?
|
17
|
+
authenticating_with_oauth2? && oauth2_response.blank?
|
18
|
+
end
|
19
|
+
|
20
|
+
def redirect_to_oauth2
|
21
|
+
authorize_url = oauth2_client.web_server.authorize_url(:redirect_uri => build_callback_url, :scope => oauth2_scope)
|
22
|
+
|
23
|
+
# Store the class which is redirecting, so we can ensure other classes
|
24
|
+
# don't get confused and attempt to use the response
|
25
|
+
oauth2_controller.session[:oauth2_request_class] = self.class.name
|
26
|
+
|
27
|
+
# Tell our rack callback filter what method the current request is using
|
28
|
+
oauth2_controller.session[:oauth2_callback_method] = oauth2_controller.request.method
|
29
|
+
|
30
|
+
oauth2_controller.redirect_to authorize_url
|
31
|
+
end
|
32
|
+
|
33
|
+
def build_callback_url
|
34
|
+
oauth2_controller.url_for :controller => oauth2_controller.controller_name, :action => oauth2_controller.action_name
|
35
|
+
end
|
36
|
+
|
37
|
+
def generate_access_token
|
38
|
+
oauth2_client.web_server.get_access_token(oauth2_controller.params[:code], :redirect_uri => build_callback_url)
|
39
|
+
end
|
40
|
+
|
41
|
+
def oauth2_response
|
42
|
+
oauth2_controller.params && oauth2_controller.params[:code]
|
43
|
+
end
|
44
|
+
|
45
|
+
def oauth2_client
|
46
|
+
OAuth2::Client.new(oauth2_client_id, oauth2_client_secret, :site => oauth2_site)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Convenience method for accessing the session controller
|
50
|
+
def oauth2_controller
|
51
|
+
is_auth_session? ? controller : session_class.controller
|
52
|
+
end
|
53
|
+
|
54
|
+
# Convenience methods for accessing session configuration values
|
55
|
+
def oauth2_client_id
|
56
|
+
is_auth_session? ? self.class.oauth2_client_id : session_class.oauth2_client_id
|
57
|
+
end
|
58
|
+
|
59
|
+
def oauth2_client_secret
|
60
|
+
is_auth_session? ? self.class.oauth2_client_secret : session_class.oauth2_client_secret
|
61
|
+
end
|
62
|
+
|
63
|
+
def oauth2_site
|
64
|
+
is_auth_session? ? self.class.oauth2_site : session_class.oauth2_site
|
65
|
+
end
|
66
|
+
|
67
|
+
def oauth2_scope
|
68
|
+
is_auth_session? ? self.class.oauth2_scope : session_class.oauth2_scope
|
69
|
+
end
|
70
|
+
|
71
|
+
def is_auth_session?
|
72
|
+
self.is_a?(Authlogic::Session::Base)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module AuthlogicOauth2
|
2
|
+
# This module is responsible for adding oauth2
|
3
|
+
# to the Authlogic::Session::Base class.
|
4
|
+
module Session
|
5
|
+
def self.included(klass)
|
6
|
+
klass.class_eval do
|
7
|
+
extend Config
|
8
|
+
include Methods
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module Config
|
13
|
+
# * <tt>Default:</tt> :find_by_oauth2_token
|
14
|
+
# * <tt>Accepts:</tt> Symbol
|
15
|
+
def find_by_oauth2_method(value = nil)
|
16
|
+
rw_config(:find_by_oauth2_method, value, :find_by_oauth2_token)
|
17
|
+
end
|
18
|
+
alias_method :find_by_oauth2_method=, :find_by_oauth2_method
|
19
|
+
|
20
|
+
# * <tt>Default:</tt> ''
|
21
|
+
# * <tt>Accepts:</tt> String
|
22
|
+
def oauth2_client_id(value = nil)
|
23
|
+
rw_config(:oauth2_client_id, value, '')
|
24
|
+
end
|
25
|
+
alias_method :oauth2_client_id=, :oauth2_client_id
|
26
|
+
|
27
|
+
# * <tt>Default:</tt> ''
|
28
|
+
# * <tt>Accepts:</tt> String
|
29
|
+
def oauth2_client_secret(value = nil)
|
30
|
+
rw_config(:oauth2_client_secret, value, '')
|
31
|
+
end
|
32
|
+
alias_method :oauth2_client_secret=, :oauth2_client_secret
|
33
|
+
|
34
|
+
# * <tt>Default:</tt> ''
|
35
|
+
# * <tt>Accepts:</tt> String
|
36
|
+
def oauth2_site(value = nil)
|
37
|
+
rw_config(:oauth2_site, value, '')
|
38
|
+
end
|
39
|
+
alias_method :oauth2_site=, :oauth2_site
|
40
|
+
|
41
|
+
# * <tt>Default:</tt> ''
|
42
|
+
# * <tt>Accepts:</tt> String
|
43
|
+
def oauth2_scope(value = nil)
|
44
|
+
rw_config(:oauth2_scope, value, '')
|
45
|
+
end
|
46
|
+
alias_method :oauth2_scope=, :oauth2_scope
|
47
|
+
end
|
48
|
+
|
49
|
+
module Methods
|
50
|
+
include Oauth2Process
|
51
|
+
|
52
|
+
def self.included(klass)
|
53
|
+
klass.class_eval do
|
54
|
+
validate :validate_by_oauth2, :if => :authenticating_with_oauth2?
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Hooks into credentials so that you can pass a user who has already has an oauth2 access token.
|
59
|
+
def credentials=(value)
|
60
|
+
super
|
61
|
+
values = value.is_a?(Array) ? value : [value]
|
62
|
+
hash = values.first.is_a?(Hash) ? values.first.with_indifferent_access : nil
|
63
|
+
self.record = hash[:priority_record] if !hash.nil? && hash.key?(:priority_record)
|
64
|
+
end
|
65
|
+
|
66
|
+
def record=(record)
|
67
|
+
@record = record
|
68
|
+
end
|
69
|
+
|
70
|
+
# Clears out the block if we are authenticating with oauth2,
|
71
|
+
# so that we can redirect without a DoubleRender error.
|
72
|
+
def save(&block)
|
73
|
+
block = nil if redirecting_to_oauth2_server?
|
74
|
+
super(&block)
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def authenticating_with_oauth2?
|
80
|
+
# Initial request when user presses one of the button helpers
|
81
|
+
(controller.params && !controller.params[:login_with_oauth2].blank?) ||
|
82
|
+
# When the oauth2 provider responds and we made the initial request
|
83
|
+
(oauth2_response && controller.session && controller.session[:oauth2_request_class] == self.class.name)
|
84
|
+
end
|
85
|
+
|
86
|
+
def authenticate_with_oauth2
|
87
|
+
if @record
|
88
|
+
self.attempted_record = record
|
89
|
+
else
|
90
|
+
self.attempted_record = search_for_record(find_by_oauth2_method, generate_access_token.token)
|
91
|
+
end
|
92
|
+
|
93
|
+
if !attempted_record
|
94
|
+
errors.add_to_base("Could not find user in our database, have you registered with your Oauth2 account?")
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Convenience methods for accessing configuration values
|
99
|
+
def find_by_oauth2_method
|
100
|
+
self.class.find_by_oauth2_method
|
101
|
+
end
|
102
|
+
|
103
|
+
def oauth2_client_id
|
104
|
+
self.class.oauth2_client_id
|
105
|
+
end
|
106
|
+
|
107
|
+
def oauth2_client_secret
|
108
|
+
self.class.oauth2_client_secret
|
109
|
+
end
|
110
|
+
|
111
|
+
def oauth2_site
|
112
|
+
self.class.oauth2_site
|
113
|
+
end
|
114
|
+
|
115
|
+
def oauth2_scope
|
116
|
+
self.class.oauth2_scope
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module AuthlogicOauth2
|
2
|
+
# A class for describing the current version of a library. The version
|
3
|
+
# consists of three parts: the +major+ number, the +minor+ number, and the
|
4
|
+
# +tiny+ (or +patch+) number.
|
5
|
+
class Version
|
6
|
+
include Comparable
|
7
|
+
|
8
|
+
# A convenience method for instantiating a new Version instance with the
|
9
|
+
# given +major+, +minor+, and +tiny+ components.
|
10
|
+
def self.[](major, minor, tiny)
|
11
|
+
new(major, minor, tiny)
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :major, :minor, :tiny
|
15
|
+
|
16
|
+
# Create a new Version object with the given components.
|
17
|
+
def initialize(major, minor, tiny)
|
18
|
+
@major, @minor, @tiny = major, minor, tiny
|
19
|
+
end
|
20
|
+
|
21
|
+
# Compare this version to the given +version+ object.
|
22
|
+
def <=>(version)
|
23
|
+
to_i <=> version.to_i
|
24
|
+
end
|
25
|
+
|
26
|
+
# Converts this version object to a string, where each of the three
|
27
|
+
# version components are joined by the '.' character. E.g., 2.0.0.
|
28
|
+
def to_s
|
29
|
+
@to_s ||= [@major, @minor, @tiny].join(".")
|
30
|
+
end
|
31
|
+
|
32
|
+
# Converts this version to a canonical integer that may be compared
|
33
|
+
# against other version objects.
|
34
|
+
def to_i
|
35
|
+
@to_i ||= @major * 1_000_000 + @minor * 1_000 + @tiny
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_a
|
39
|
+
[@major, @minor, @tiny]
|
40
|
+
end
|
41
|
+
|
42
|
+
MAJOR = 1
|
43
|
+
MINOR = 1
|
44
|
+
TINY = 0
|
45
|
+
|
46
|
+
# The current version as a Version instance
|
47
|
+
CURRENT = new(MAJOR, MINOR, TINY)
|
48
|
+
# The current version as a String
|
49
|
+
STRING = CURRENT.to_s
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class Oauth2CallbackFilter
|
2
|
+
def initialize(app)
|
3
|
+
@app = app
|
4
|
+
end
|
5
|
+
|
6
|
+
def call(env)
|
7
|
+
unless env["rack.session"][:oauth2_callback_method].blank?
|
8
|
+
env["REQUEST_METHOD"] = env["rack.session"].delete(:oauth2_callback_method).to_s.upcase
|
9
|
+
end
|
10
|
+
@app.call(env)
|
11
|
+
end
|
12
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "authlogic_oauth2"
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: langalex-authlogic_oauth2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Andrew Hite
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-25 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: authlogic
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: oauth2
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Authlogic OAuth2 is an extension of the Authlogic library to add OAuth2 support. OAuth2 can be used to allow users to login with their Facebook credentials.
|
50
|
+
email: andrew@andrew-hite.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files:
|
56
|
+
- README.rdoc
|
57
|
+
- lib/authlogic_oauth2.rb
|
58
|
+
- lib/authlogic_oauth2/acts_as_authentic.rb
|
59
|
+
- lib/authlogic_oauth2/helper.rb
|
60
|
+
- lib/authlogic_oauth2/oauth2_process.rb
|
61
|
+
- lib/authlogic_oauth2/session.rb
|
62
|
+
- lib/authlogic_oauth2/version.rb
|
63
|
+
- lib/oauth2_callback_filter.rb
|
64
|
+
files:
|
65
|
+
- Manifest
|
66
|
+
- README.rdoc
|
67
|
+
- Rakefile
|
68
|
+
- init.rb
|
69
|
+
- lib/authlogic_oauth2.rb
|
70
|
+
- lib/authlogic_oauth2/acts_as_authentic.rb
|
71
|
+
- lib/authlogic_oauth2/helper.rb
|
72
|
+
- lib/authlogic_oauth2/oauth2_process.rb
|
73
|
+
- lib/authlogic_oauth2/session.rb
|
74
|
+
- lib/authlogic_oauth2/version.rb
|
75
|
+
- lib/oauth2_callback_filter.rb
|
76
|
+
- rails/init.rb
|
77
|
+
- langalex-authlogic_oauth2.gemspec
|
78
|
+
has_rdoc: true
|
79
|
+
homepage: http://github.com/langalex/authlogic_oauth2
|
80
|
+
licenses: []
|
81
|
+
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options:
|
84
|
+
- --line-numbers
|
85
|
+
- --inline-source
|
86
|
+
- --title
|
87
|
+
- Langalex-authlogic_oauth2
|
88
|
+
- --main
|
89
|
+
- README.rdoc
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 3
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
hash: 11
|
107
|
+
segments:
|
108
|
+
- 1
|
109
|
+
- 2
|
110
|
+
version: "1.2"
|
111
|
+
requirements: []
|
112
|
+
|
113
|
+
rubyforge_project: langalex-authlogic_oauth2
|
114
|
+
rubygems_version: 1.3.7
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: Authlogic OAuth2 is an extension of the Authlogic library to add OAuth2 support. OAuth2 can be used to allow users to login with their Facebook credentials.
|
118
|
+
test_files: []
|
119
|
+
|