Method: DocuSign_Monitor::ApiClient#request_jwt_application_token

Defined in:
lib/docusign_monitor/client/api_client.rb

#request_jwt_application_token(client_id, private_key_or_filename, expires_in = 3600, scopes = OAuth::SCOPE_SIGNATURE) ⇒ OAuth::OAuthToken

Request JWT User Token

Parameters:

  • client_id (String)

    DocuSign OAuth Client Id(AKA Integrator Key)

  • private_key_or_filename (String)

    the RSA private key

  • expires_in (Number) (defaults to: 3600)

    number of seconds remaining before the JWT assertion is considered as invalid

  • scopes (defaults to: OAuth::SCOPE_SIGNATURE)

    The list of requested scopes. Client applications may be scoped to a limited set of system access.

Returns:

Raises:

  • (ArgumentError)


497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/docusign_monitor/client/api_client.rb', line 497

def request_jwt_application_token(client_id, private_key_or_filename, expires_in = 3600,scopes=OAuth::SCOPE_SIGNATURE)
  raise ArgumentError.new('client_id cannot be empty')  if client_id.empty?
  raise ArgumentError.new('private_key_or_filename cannot be empty')  if private_key_or_filename.empty?

  scopes = scopes.join(' ') if scopes.kind_of?(Array)
  scopes = OAuth::SCOPE_SIGNATURE if scopes.empty?
  expires_in = 3600 if expires_in > 3600
  now = Time.now.to_i
  claim = {
      "iss" => client_id,
      "aud" => self.get_oauth_base_path,
      "iat" => now,
      "exp" => now + expires_in,
      "scope"=> scopes
  }
  
  private_key = if private_key_or_filename.include?("-----BEGIN RSA PRIVATE KEY-----")
                  private_key_or_filename
                else
                  File.read(private_key_or_filename)
                end

  private_key_bytes = OpenSSL::PKey::RSA.new private_key
  token = JWT.encode claim, private_key_bytes, 'RS256'
  params = {
      :header_params => {"Content-Type" => "application/x-www-form-urlencoded"},
      :form_params => {
          "assertion" => token,
          "grant_type" => OAuth::GRANT_TYPE_JWT
      },
      :return_type => 'OAuth::OAuthToken',
      :oauth => true
  }
  data, status_code, headers = self.call_api("POST", "/oauth/token", params)

  raise ApiError.new('Some error accrued during process') if data.nil?

  self.set_default_header('Authorization', data.token_type + ' ' + data.access_token)
  data
end