Skip to content

Commit

Permalink
Changed OAuth2 client configuration to a more Authlogic-ish approach
Browse files Browse the repository at this point in the history
  • Loading branch information
andyhite committed Jun 14, 2010
1 parent e6c0bd4 commit 0a2a15b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
10 changes: 7 additions & 3 deletions lib/authlogic_oauth2/acts_as_authentic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def save(perform_validation = true, &block)
result
end

# accessors for oauth2 fields
# Accessors for oauth2 fields
def oauth2_token
read_attribute(oauth2_token_field)
end
Expand All @@ -67,7 +67,8 @@ def oauth2_token=(value)
write_attribute(oauth2_token_field, value.blank? ? nil : value)
end

def oauth2_client
# Provides access to an API exposed on the access_token object
def oauth2_access
access_token
end

Expand All @@ -87,6 +88,8 @@ def authenticate_with_oauth2
# Restore any attributes which were saved before redirecting to the oauth2 server
self.attributes = session_class.controller.session.delete(:authlogic_oauth2_attributes)
self.oauth2_token = generate_access_token.token

# Execute callback if it's defined in the user model
self.after_oauth2_authentication if self.respond_to?(:after_oauth2_authentication)
end

Expand All @@ -101,7 +104,8 @@ def using_oauth2?
def validate_password_with_oauth2?
!using_oauth2? && require_password?
end


# Convenience methods for accessing configuration values
def oauth2_token_field
self.class.oauth2_token_field
end
Expand Down
27 changes: 19 additions & 8 deletions lib/authlogic_oauth2/oauth2_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def redirecting_to_oauth2_server?
end

def redirect_to_oauth2
authorize_url = oauth2.web_server.authorize_url(:redirect_uri => build_callback_url, :scope => oauth2_scope)
authorize_url = oauth2_client.web_server.authorize_url(:redirect_uri => build_callback_url, :scope => oauth2_scope)

# Store the class which is redirecting, so we can ensure other classes
# don't get confused and attempt to use the response
Expand All @@ -35,30 +35,41 @@ def build_callback_url
end

def generate_access_token
oauth2.web_server.get_access_token(oauth2_controller.params[:code], :redirect_uri => build_callback_url)
oauth2_client.web_server.get_access_token(oauth2_controller.params[:code], :redirect_uri => build_callback_url)
end

def oauth2_response
oauth2_controller.params && oauth2_controller.params[:code]
end


def oauth2_client
OAuth2::Client.new(oauth2_client_id, oauth2_client_secret, :site => oauth2_site)
end

# Convenience method for accessing the session controller
def oauth2_controller
is_auth_session? ? controller : session_class.controller
end

def oauth2
is_auth_session? ? self.class.oauth2_client : session_class.oauth2_client
# Convenience methods for accessing session configuration values
def oauth2_client_id
is_auth_session? ? self.class.oauth2_client_id : session_class.oauth2_client_id
end

def oauth2_client_secret
is_auth_session? ? self.class.oauth2_client_secret : session_class.oauth2_client_secret
end

def oauth2_site
is_auth_session? ? self.class.oauth2_site : session_class.oauth2_site
end

def oauth2_scope
is_auth_session? ? self.class.oauth2_scope : session_class.oauth2_scope
rescue NoMethodError
nil
end

def is_auth_session?
self.is_a?(Authlogic::Session::Base)
end

end
end
45 changes: 45 additions & 0 deletions lib/authlogic_oauth2/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@ def find_by_oauth2_method(value = nil)
rw_config(:find_by_oauth2_method, value, :find_by_oauth2_token)
end
alias_method :find_by_oauth2_method=, :find_by_oauth2_method

# * <tt>Default:</tt> ''
# * <tt>Accepts:</tt> String
def oauth2_client_id(value = nil)
rw_config(:oauth2_client_id, value, '')
end
alias_method :oauth2_client_id=, :oauth2_client_id

# * <tt>Default:</tt> ''
# * <tt>Accepts:</tt> String
def oauth2_client_secret(value = nil)
rw_config(:oauth2_client_secret, value, '')
end
alias_method :oauth2_client_secret=, :oauth2_client_secret

# * <tt>Default:</tt> ''
# * <tt>Accepts:</tt> String
def oauth2_site(value = nil)
rw_config(:oauth2_site, value, '')
end
alias_method :oauth2_site=, :oauth2_site

# * <tt>Default:</tt> ''
# * <tt>Accepts:</tt> String
def oauth2_scope(value = nil)
rw_config(:oauth2_scope, value, '')
end
alias_method :oauth2_scope=, :oauth2_scope
end

module Methods
Expand Down Expand Up @@ -67,9 +95,26 @@ def authenticate_with_oauth2
end
end

# Convenience methods for accessing configuration values
def find_by_oauth2_method
self.class.find_by_oauth2_method
end

def oauth2_client_id
self.class.oauth2_client_id
end

def oauth2_client_secret
self.class.oauth2_client_secret
end

def oauth2_site
self.class.oauth2_site
end

def oauth2_scope
self.class.oauth2_scope
end
end
end
end

0 comments on commit 0a2a15b

Please sign in to comment.