Module: VeracodeApiSigning::Utils

Included in:
HMACAuth, Plugins::FaradayMiddleware
Defined in:
lib/veracode_api_signing/utils.rb

Instance Method Summary collapse

Instance Method Details

#generate_nonceString

Returns nonce string.

Returns:

  • (String)

    nonce string



14
15
16
# File 'lib/veracode_api_signing/utils.rb', line 14

def generate_nonce
  SecureRandom.hex(16)
end

#get_current_timestampInteger

Returns current epoch time * 1000 rounded.

Returns:

  • (Integer)

    current epoch time * 1000 rounded



9
10
11
# File 'lib/veracode_api_signing/utils.rb', line 9

def get_current_timestamp
  Time.now.utc.to_i * 1000.round
end

#get_host_from_url(url) ⇒ String

Returns just returns the host.

Examples:

get_host_from_url("https://api.example.com/foo/bar") #=> "api.example.com"

Parameters:

  • url (String)

    the url to parse

Returns:

  • (String)

    just returns the host



22
23
24
# File 'lib/veracode_api_signing/utils.rb', line 22

def get_host_from_url(url)
  parsed_url(url).host
end

#get_path_and_params_from_url(url) ⇒ String

Returns the the path and params formatted, or an empty String

Examples:

get_path_and_params_from_url("https://api.example.com/foo/bar") #=> "/foo/bar"
get_path_and_params_from_url("https://api.example.com") #=> ""
get_path_and_params_from_url("https://api.example.com/apm/v1/assets?page=2") #=> "/apm/v1/assets?page=2"

Parameters:

  • url (String)

    the url to parse

Returns:

  • (String)

    returns the the path and params formatted, or an empty String



34
35
36
37
38
39
40
41
42
# File 'lib/veracode_api_signing/utils.rb', line 34

def get_path_and_params_from_url(url)
  uri = parsed_url(url)
  path = uri.path
  params = uri.query
  return "" if (path.nil? || path.empty?) && params.nil?

  built_url = URI::HTTPS.build(path: path, query: params)
  built_url.request_uri
end

#get_scheme_from_url(url) ⇒ Object

Examples:

get_scheme_from_url("https://api.example.com/foo/bar") #=> "https"
get_scheme_from_url("api.example.com") #=> ""

Parameters:

  • url (String)

    the url to parse



49
50
51
# File 'lib/veracode_api_signing/utils.rb', line 49

def get_scheme_from_url(url)
  parsed_url(url).scheme.to_s
end

#parsed_url(url) ⇒ Object



53
54
55
# File 'lib/veracode_api_signing/utils.rb', line 53

def parsed_url(url)
  URI(url)
end