テスト用のユーザーAPIキーを生成する

User API キーの仕様からの議論を継続します。

ユーザー API キーをローカルでテストするために、小さなユーティリティスクリプトを作成しました。

まず、依存関係をインストールします。

gem install addressable openssl base64 json

次に、スクリプトはここにあります。

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

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

puts 'ターゲットサイトは何ですか?'
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 "#{url} に移動してください。"
puts

puts "生成されたキーをここに入力してください"
puts

puts "ENTER を押して end と入力し、もう一度 ENTER を押してください"
puts

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

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

puts "ユーザー API キーは #{user_api_key['key']} です"
「いいね!」 19