Class: OmniGroupContacts::Middleware::OAuth2
- Includes:
- Authorization::OAuth2
- Defined in:
- lib/omnigroupcontacts/middleware/oauth2.rb
Direct Known Subclasses
Constant Summary
Constants included from HTTPUtils
Instance Attribute Summary collapse
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#redirect_path ⇒ Object
readonly
Returns the value of attribute redirect_path.
Attributes inherited from BaseOAuth
Instance Method Summary collapse
- #fetch_group_contacts(groups, access_token, token_type) ⇒ Object
-
#fetch_groups ⇒ Object
It extract the authorization code from the query string.
-
#initialize(app, client_id, client_secret, options = {}) ⇒ OAuth2
constructor
A new instance of OAuth2.
- #redirect_uri ⇒ Object
- #refresh_token_prop_name(code) ⇒ Object
- #request_authorization_from_user ⇒ Object
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
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/omnigroupcontacts/middleware/oauth2.rb', line 18 def initialize app, client_id, client_secret, ={} super app, @client_id = client_id @client_secret = client_secret @redirect_path = [:redirect_path] || "#{ MOUNT_PATH }#{class_name}/callback" @ssl_ca_file = [:ssl_ca_file] end |
Instance Attribute Details
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
16 17 18 |
# File 'lib/omnigroupcontacts/middleware/oauth2.rb', line 16 def client_id @client_id end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
16 17 18 |
# File 'lib/omnigroupcontacts/middleware/oauth2.rb', line 16 def client_secret @client_secret end |
#redirect_path ⇒ Object (readonly)
Returns the value of attribute redirect_path.
16 17 18 |
# File 'lib/omnigroupcontacts/middleware/oauth2.rb', line 16 def redirect_path @redirect_path end |
Instance Method Details
#fetch_group_contacts(groups, access_token, token_type) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/omnigroupcontacts/middleware/oauth2.rb', line 62 def fetch_group_contacts(groups, access_token, token_type) group_contacts_all = {} groups.each do |group| Rails.logger.info "---------#{group.inspect}---------" group_contacts = fetch_contacts_using_access_token(access_token, token_type, group[:id]) group_contacts_all[group[:title]] = group_contacts end group_contacts_all end |
#fetch_groups ⇒ Object
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 60 |
# File 'lib/omnigroupcontacts/middleware/oauth2.rb', line 44 def fetch_groups 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 groups = fetch_groups_using_access_token(access_token, token_type) group_contacts = fetch_group_contacts(groups, access_token, token_type) session[refresh_token_prop_name(code)] = refresh_token if refresh_token group_contacts else raise AuthorizationError.new("User did not grant access to contacts list") end end |
#redirect_uri ⇒ Object
31 32 33 |
# File 'lib/omnigroupcontacts/middleware/oauth2.rb', line 31 def redirect_uri host_url_from_rack_env(@env) + redirect_path end |
#refresh_token_prop_name(code) ⇒ Object
74 75 76 |
# File 'lib/omnigroupcontacts/middleware/oauth2.rb', line 74 def refresh_token_prop_name code "#{base_prop_name}.#{code}.refresh_token" end |
#request_authorization_from_user ⇒ Object
26 27 28 29 |
# File 'lib/omnigroupcontacts/middleware/oauth2.rb', line 26 def target_url = append_state_query() [302, {"Content-Type" => "application/x-www-form-urlencoded", "location" => target_url}, []] end |