module HTTPX::Plugins::OAuth::InstanceMethods

  1. lib/httpx/plugins/oauth.rb

Methods

Public Instance

  1. build_request
  2. oauth_auth
  3. with_access_token

Public Instance methods

build_request(*)
[show source]
    # File lib/httpx/plugins/oauth.rb
158 def build_request(*)
159   request = super
160 
161   return request if request.headers.key?("authorization")
162 
163   oauth_session = @options.oauth_session
164 
165   return request unless oauth_session && oauth_session.access_token
166 
167   request.headers["authorization"] = "Bearer #{oauth_session.access_token}"
168 
169   request
170 end
oauth_auth(**args)
[show source]
    # File lib/httpx/plugins/oauth.rb
114 def oauth_auth(**args)
115   with(oauth_session: OAuthSession.new(**args))
116 end
with_access_token()
[show source]
    # File lib/httpx/plugins/oauth.rb
118 def with_access_token
119   oauth_session = @options.oauth_session
120 
121   oauth_session.load(self)
122 
123   grant_type = oauth_session.grant_type
124 
125   headers = {}
126   form_post = { "grant_type" => grant_type, "scope" => Array(oauth_session.scope).join(" ") }.compact
127 
128   # auth
129   case oauth_session.token_endpoint_auth_method
130   when "client_secret_post"
131     form_post["client_id"] = oauth_session.client_id
132     form_post["client_secret"] = oauth_session.client_secret
133   when "client_secret_basic"
134     headers["authorization"] = Authentication::Basic.new(oauth_session.client_id, oauth_session.client_secret).authenticate
135   end
136 
137   case grant_type
138   when "client_credentials"
139     # do nothing
140   when "refresh_token"
141     form_post["refresh_token"] = oauth_session.refresh_token
142   end
143 
144   token_request = build_request("POST", oauth_session.token_endpoint, headers: headers, form: form_post)
145   token_request.headers.delete("authorization") unless oauth_session.token_endpoint_auth_method == "client_secret_basic"
146 
147   token_response = request(token_request)
148   token_response.raise_for_status
149 
150   payload = token_response.json
151 
152   access_token = payload["access_token"]
153   refresh_token = payload["refresh_token"]
154 
155   with(oauth_session: oauth_session.merge(access_token: access_token, refresh_token: refresh_token))
156 end