Generate User API Keys for testing

Continuing the discussion from User API keys specification:

I created a small utility script in order to test User API keys locally.

First install dependencies:

gem install addressable openssl base64 json

Then the script is here:

require 'addressable'
require 'openssl'
require 'base64'
require 'json'

PRIVATE_KEY = OpenSSL::PKey::RSA.new(2048)
PUBLIC_KEY = PRIVATE_KEY.public_key

puts 'What is the target site?'
site = STDIN.gets.chomp

template = Addressable::Template.new("#{site}/user-api-key/new{?query*}")

url = template.expand({
  query: {
    application_name: 'ruby',
    client_id: `hostname`,
    scopes: 'read',
    public_key: PUBLIC_KEY,
    nonce: 1
  }
})

puts "navigate to #{url}."
puts
puts "copy the generated key in here"
puts
puts "press ENTER type end and press ENTER again"
puts

$/ = "end"
encoded_key = STDIN.gets.chomp


private_key = OpenSSL::PKey::RSA.new(PRIVATE_KEY)

user_api_key = JSON.parse(private_key.private_decrypt(Base64.decode64(encoded_key)))

puts "Your User API Key is #{user_api_key['key']}"
14 Likes

Hi @Falco
I need this solution and i could understand the code partially since i am not familiar with Ruby. Is there any way can you please help me with a Python sample if you have? That would be a great help since we are stuck here

Regards
Chitta

Hi @Chittaranjan_Jena,

I migrated the script to Python. It was almost straightforward. The tricky part was to figure out that PKCS1_PADDING was used, which corresponds to PyCryptodomes PKCS1_v1_5.

1 Like

Thanks @marph. We could do it from the admin settings section.