Class: OmniContacts::Middleware::OAuth1

Inherits:
BaseOAuth
  • Object
show all
Includes:
Authorization::OAuth1
Defined in:
lib/omnicontacts/middleware/oauth1.rb

Direct Known Subclasses

Importer::Yahoo

Constant Summary

Constants included from Authorization::OAuth1

Authorization::OAuth1::OAUTH_VERSION

Constants included from HTTPUtils

HTTPUtils::SSL_PORT

Instance Attribute Summary collapse

Attributes inherited from BaseOAuth

#ssl_ca_file

Instance Method Summary collapse

Methods included from Authorization::OAuth1

#authorization_url, #fetch_access_token, #fetch_authorization_token, #oauth_signature

Methods included from HTTPUtils

encode, host_url_from_rack_env, query_string_to_map, scheme, to_query_string

Methods inherited from BaseOAuth

#call, #class_name

Constructor Details

#initialize(app, consumer_key, consumer_secret, options = {}) ⇒ OAuth1

Returns a new instance of OAuth1.



17
18
19
20
21
22
23
# File 'lib/omnicontacts/middleware/oauth1.rb', line 17

def initialize app, consumer_key, consumer_secret, options = {}
  super app, options
  @consumer_key = consumer_key
  @consumer_secret = consumer_secret
  @callback_path = options[:callback_path] || "/contacts/#{class_name}/callback"
  @token_prop_name = "#{base_prop_name}.oauth_token"
end

Instance Attribute Details

#callback_pathObject (readonly) Also known as: redirect_path

Returns the value of attribute callback_path.



15
16
17
# File 'lib/omnicontacts/middleware/oauth1.rb', line 15

def callback_path
  @callback_path
end

#consumer_keyObject (readonly)

Returns the value of attribute consumer_key.



15
16
17
# File 'lib/omnicontacts/middleware/oauth1.rb', line 15

def consumer_key
  @consumer_key
end

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



15
16
17
# File 'lib/omnicontacts/middleware/oauth1.rb', line 15

def consumer_secret
  @consumer_secret
end

Instance Method Details

#callbackObject



25
26
27
# File 'lib/omnicontacts/middleware/oauth1.rb', line 25

def callback
  host_url_from_rack_env(@env) + callback_path
end

#fetch_contactsObject

Parses the authorization token from the query string and obtain the relative secret from the session. Finally it calls fetch_contacts_from_token_and_verifier. If token is found in the query string an AuhorizationError is raised.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/omnicontacts/middleware/oauth1.rb', line 54

def fetch_contacts
  params = query_string_to_map(@env["QUERY_STRING"])
  oauth_token = params["oauth_token"]
  oauth_verifier = params["oauth_verifier"]
  oauth_token_secret = session[token_secret_prop_name(oauth_token)]
  if oauth_token && oauth_verifier && oauth_token_secret
    fetch_contacts_from_token_and_verifier(oauth_token, oauth_token_secret, oauth_verifier)
  else
    raise AuthorizationError.new("User did not grant access to contacts list")
  end
end

#redirect_to_authorization_site(auth_token) ⇒ Object



45
46
47
# File 'lib/omnicontacts/middleware/oauth1.rb', line 45

def redirect_to_authorization_site auth_token
  [302, {"location" => authorization_url(auth_token)}, []]
end

#request_authorization_from_userObject

Obtains an authorization token from the server, stores it and the session and redirect the user to the authorization website.



34
35
36
37
38
39
# File 'lib/omnicontacts/middleware/oauth1.rb', line 34

def request_authorization_from_user
  (auth_token, auth_token_secret) = fetch_authorization_token
  session[@token_prop_name] = auth_token
  session[token_secret_prop_name(auth_token)] = auth_token_secret
  redirect_to_authorization_site(auth_token)
end

#token_secret_prop_name(oauth_token) ⇒ Object



41
42
43
# File 'lib/omnicontacts/middleware/oauth1.rb', line 41

def token_secret_prop_name oauth_token
  "#{base_prop_name}.#{oauth_token}.oauth_token_secret"
end