class X1SatSupport::Client

Public Class Methods

new(config) click to toggle source

Description

This method initialize the client and client's variables

Parameters

  • config: - hash from config file that can be generated by 'rake x1_sat_support:generate_config'

# File lib/x1_sat_support/client.rb, line 12
def initialize(config)
  @headers = {'X-Codebig-Principal' => config["credentials"]["principal"],
             'X-Client-Id' => config["credentials"]["client_id"],
             'X-Client-Secret' => config["credentials"]["client_secret"]}
  @key_url = config["key_url"]
  @sat_token_url = config["sat_token_url"]
  @path_to_store_keys = config["path_to_store_keys"]
  @keys_expiration_time = config["keys_expiration_time"]
  @numbers_of_keys_to_keep = config["numbers_of_keys_to_keep"]
end

Public Instance Methods

update_keys() click to toggle source

Description

This method updates public keys It creates folder and write a file with public key there

# File lib/x1_sat_support/client.rb, line 27
def update_keys
  unless @key_url && @sat_token_url && @path_to_store_keys && @keys_expiration_time
    raise "Config is incorrect."
  end
  kid = get_kid
  unless kid
    raise "Key ID is not present"
  end
  key_name = kid + '.pub'
  key = get_current_key
  Dir.mkdir(@path_to_store_keys) unless File.exists?(@path_to_store_keys)
  File.open(File.join(@path_to_store_keys,key_name), 'w') {|f| f.write(key)}
  check_keys
end

Private Instance Methods

check_keys() click to toggle source

Description

This method checks existed public keys. It sorts all keys *.pub in defined path by created time (path_to_store_keys in config). If file's lifetime is bigger then keys_expiration_time from the config, method will delete it. If numbers_of_keys_to_keep parameter is present in config, method removes files until files.count will be equal numbers_of_keys_to_keep.

Return

This method returns public key

# File lib/x1_sat_support/client.rb, line 82
def check_keys
  files = Dir[File.join(@path_to_store_keys, "/*.pub")].sort_by {|file| File.ctime(file)}
  files.each do |file|
    File.delete(files.delete(file)) if (Time.now - File.ctime(file)) > @keys_expiration_time
  end
  if @numbers_of_keys_to_keep
    until @numbers_of_keys_to_keep >= files.count do
      File.delete(files.shift)
    end
  end
end
get_current_key() click to toggle source

Description

This method makes get request that return public key

Return

This method returns public key

# File lib/x1_sat_support/client.rb, line 71
def get_current_key
  RestClient.get(@key_url, @headers).body
end
get_kid() click to toggle source
==== Description
This method makes get request that return encoded service access token and decode it
==== Return
This method returns array of hashes with key id

[{β€œjti”=>β€œ<id>”,

 "iss"=>"...",
 "sub"=>"...",
 "iat"=>...,
 "nbf"=>...,
 "exp"=>...,
 "version"=>"1.0",
 "allowedResources"=>{"allowedDeviceIds"=>["*"], "allowedPartners"=>["*"], "allowedServiceAccountIds"=>["*"], "allowedUserIds"=>["*"]},
 "capabilities"=>[],
 "aud"=>[]},
{"kid"=>"...", "alg"=>"RS256"}]
# File lib/x1_sat_support/client.rb, line 60
def get_kid
  encoded_token = JSON.parse(RestClient.get(@sat_token_url, @headers).body)["serviceAccessToken"]
  token = JWT.decode(encoded_token, nil, false, { verify_not_before: false })
  token.last["kid"]
end