Class: ZendeskAPI::Middleware::Request::ApiTokenImpersonate

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/zendesk_api/middleware/request/api_token_impersonate.rb

Overview

ApiTokenImpersonate If Thread.current is set, it will modify the Authorization header to impersonate that user using the API token from the current Authorization header.

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zendesk_api/middleware/request/api_token_impersonate.rb', line 12

def call(env)
  if Thread.current[:zendesk_thread_local_username] && env[:request_headers][:authorization] =~ /^Basic /
    current_u_p_encoded = env[:request_headers][:authorization].split(/\s+/)[1]
    current_u_p = Base64.urlsafe_decode64(current_u_p_encoded)
    unless current_u_p.include?("/token:") && (parts = current_u_p.split(":")) && parts.length == 2 && parts[0].include?("/token")
      warn "WARNING: ApiTokenImpersonate passed in invalid format. It should be in the format username/token:APITOKEN"
      return @app.call(env)
    end

    next_u_p = "#{Thread.current[:zendesk_thread_local_username]}/token:#{parts[1]}"
    env[:request_headers][:authorization] = "Basic #{Base64.urlsafe_encode64(next_u_p)}"
  end
  @app.call(env)
end