Class: OmniContacts::Middleware::OAuth2

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

Constant Summary

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::OAuth2

#authorization_url, #fetch_access_token, #refresh_access_token

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, client_id, client_secret, options = {}) ⇒ OAuth2

Returns a new instance of OAuth2.



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

def initialize app, client_id, client_secret, options ={}
  super app, options
  @client_id = client_id
  @client_secret = client_secret
  @redirect_path = options[:redirect_path] || "#{ MOUNT_PATH }#{class_name}/callback"
  @ssl_ca_file = options[:ssl_ca_file]
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



16
17
18
# File 'lib/omnicontacts/middleware/oauth2.rb', line 16

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



16
17
18
# File 'lib/omnicontacts/middleware/oauth2.rb', line 16

def client_secret
  @client_secret
end

#redirect_pathObject (readonly)

Returns the value of attribute redirect_path.



16
17
18
# File 'lib/omnicontacts/middleware/oauth2.rb', line 16

def redirect_path
  @redirect_path
end

Instance Method Details

#fetch_contactsObject

It extract the authorization code from the query string. It uses it to obtain an access token. If the authorization code has a refresh token associated with it in the session, it uses the obtain an access token. It fetches the list of contacts and stores the refresh token associated with the access token in the session. Finally it returns the list of contacts. If no authorization code is found in the query string an AuthoriazationError is raised.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/omnicontacts/middleware/oauth2.rb', line 44

def fetch_contacts
  code = query_string_to_map(@env["QUERY_STRING"])["code"]
  if code
    refresh_token = session[refresh_token_prop_name(code)]
    (access_token, token_type, refresh_token) = if refresh_token
                                                  refresh_access_token(refresh_token)
                                                else
                                                  fetch_access_token(code)
                                                end
    contacts = fetch_contacts_using_access_token(access_token, token_type)
    session[refresh_token_prop_name(code)] = refresh_token if refresh_token
    contacts
  else
    raise AuthorizationError.new("User did not grant access to contacts list")
  end
end

#redirect_uriObject



31
32
33
# File 'lib/omnicontacts/middleware/oauth2.rb', line 31

def redirect_uri
  host_url_from_rack_env(@env) + redirect_path
end

#refresh_token_prop_name(code) ⇒ Object



61
62
63
# File 'lib/omnicontacts/middleware/oauth2.rb', line 61

def refresh_token_prop_name code
  "#{base_prop_name}.#{code}.refresh_token"
end

#request_authorization_from_userObject



26
27
28
29
# File 'lib/omnicontacts/middleware/oauth2.rb', line 26

def request_authorization_from_user
  target_url = append_state_query(authorization_url)
  [302, {"Content-Type" => "application/x-www-form-urlencoded", "location" => target_url}, []]
end