Class: Fastlane::Helper::RustoredTokenHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/rustored/helper/rustored_token_helper.rb

Constant Summary collapse

BASE_URL =
'https://public-api.rustore.ru'

Class Method Summary collapse

Class Method Details

.clientObject



16
17
18
19
20
21
# File 'lib/fastlane/plugin/rustored/helper/rustored_token_helper.rb', line 16

def self.client
  @client ||= Faraday.new(url: BASE_URL) do |f|
    f.request(:json)
    f.response(:json)
  end
end

.gen_token_signature(key_id:, private_key:, timestamp:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/fastlane/plugin/rustored/helper/rustored_token_helper.rb', line 23

def self.gen_token_signature(key_id:, private_key:, timestamp:)
  private_key_data = Base64.strict_decode64(private_key)
  rsa_private_key = OpenSSL::PKey::RSA.new(private_key_data)

  message_to_sign = key_id + timestamp

  digest = OpenSSL::Digest.new('SHA512')
  signature_bytes = rsa_private_key.sign(digest, message_to_sign)
  Base64.strict_encode64(signature_bytes)
end

.get_token(key_id:, private_key:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fastlane/plugin/rustored/helper/rustored_token_helper.rb', line 34

def self.get_token(key_id:, private_key:)
  timestamp = Time.now.utc.iso8601(3)
  sig = gen_token_signature(key_id: key_id, private_key: private_key, timestamp: timestamp)

  response = client.post('/public/auth') do |req|
    req.body = {
      keyId: key_id,
      signature: sig,
      timestamp: timestamp
    }
  end

  body = response.body.kind_of?(Hash) ? response.body : {}
  token = body.dig('body', 'jwe')
  return token if response.success? && body['code'] == 'OK' && !token.to_s.empty?

  UI.user_error!("Failed to get token: #{response.status} - #{body['message'] || body['code'] || 'Unknown RuStore response'}")
rescue ArgumentError, OpenSSL::PKey::PKeyError => e
  UI.user_error!("Invalid RuStore private key: #{e.message}")
rescue Faraday::Error => e
  UI.user_error!("Failed to get RuStore token: #{e.message}")
end