Class: OAuthController

Inherits:
Object show all
Defined in:
lib/simple_mapper/default_plugins/oauth.rb

Overview

We’ll have an instance of these for each controller-model pair.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  # Signature method used by server. Defaults to HMAC-SHA1
  :signature_method=>'HMAC-SHA1',

  # default paths on site. These are the same as the defaults set up by the generators
  :request_token_path=>'/oauth/request_token',
  :authorize_path=>'/oauth/authorize',
  :access_token_path=>'/oauth/access_token',

  # How do we send the oauth values to the server see 
  # http://oauth.googlecode.com/svn/spec/branches/1.0/drafts/6/spec.html#consumer_req_param for more info
  #
  # Possible values:
  #
  #   :authorize - via the Authorize header (Default) ( option 1. in spec)
  #   :post - url form encoded in body of POST request ( option 2. in spec)
  #   :query - via the query part of the url ( option 3. in spec)
  :auth_method=>:authorize, 

  # Default http method used for OAuth Token Requests (defaults to :post)
  :http_method=>:post, 

  :version=>"1.0",

  # Default authorization method: have the controller redirect to the authorize_url.
  :authorization_method => lambda {|model| redirect(model.oauth.consumer.authorize_url)},

  # Default session: grab session from the controller's session method -- session['Person_oauth'] for the Person ActiveResource model.
  :session => lambda {|model| session[model.name.to_s + '_oauth'] ||= {} }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, model, consumer_key, consumer_secret, options = {}) ⇒ OAuthController

Returns a new instance of OAuthController.



107
108
109
110
111
112
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 107

def initialize(controller, model, consumer_key, consumer_secret, options={})
  @controller = controller
  @options = DEFAULT_OPTIONS.merge(options)
  @model = @options.delete(:model) || model
  @consumer = OAuth::Consumer.new(consumer_key, consumer_secret, options)
end

Instance Attribute Details

#consumerObject

Returns the value of attribute consumer.



105
106
107
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 105

def consumer
  @consumer
end

#optionsObject

Returns the value of attribute options.



105
106
107
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 105

def options
  @options
end

Instance Method Details

#authenticate!Object

The session is what holds which models are authenticated with what tokens. We just need the controller to retreive the session and to send back redirects when necessary.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 124

def authenticate!
  # 1) If we have no tokens, get a request_token and run the authorization method.
  # 2) If we have a request_token, assume the user has already answered the question, go ahead and try to get an access_token.
  if access_token
    return true
  elsif request_token
    return @controller.begin_pathway(@options[:authorization_method].in_context(controller).call(@model)) if @options[:authorization_method].is_a?(Proc)
    return true if access_token # For scriptables
  else
    raise RuntimeError, "It seems there is a problem between your OAuth client and the OAuth provider you are contacting. Inspect the naming of the token and token secret parameters being sent by the website."
  end
end

#authorized?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 114

def authorized?
  !!session[:access_token]
end

#request_signed!(request) ⇒ Object



137
138
139
140
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 137

def request_signed!(request)
  @consumer.sign!(request, current_token)
  request
end

#scriptable?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/simple_mapper/default_plugins/oauth.rb', line 118

def scriptable?
  @options[:authorization_method] == :scriptable
end