Токен подписи Google - недействительная подпись запроса (Postgresql) - PullRequest
0 голосов
/ 16 июня 2020

При попытке создать действительную подпись для облачного хранилища Google я всегда получаю:

Подпись запроса, которую мы рассчитали, не соответствует предоставленной вами подписи. Проверьте свой секретный ключ Google и метод подписи.

Я могу проверить, что канонический запрос ha sh соответствует каноническому запросу StringToSign ha sh.

Итак, я считаю, что при генерации подписи есть ошибка. Но все кажется правильным (по крайней мере, из того, что мне говорят документы Google)

create or replace function qx.create_google_storage_signature(
  string_to_sign text,
  request_date text,
  request_location text,
  request_service text,
  request_type text
)
  returns text
  volatile
  language plpgsql
as $$
declare
  hmac_secret text;
  key_date text;
  key_region text;
  key_service text;
  signing_key text;
  message_digest text;
begin
  -- todo : we need to not store this in plain text
  hmac_secret = qx.current_setting('qx.google_storage_hmac_secret');

  -- https://cloud.google.com/storage/docs/authentication/signatures#derive-key
  key_date = hmac(request_date, concat('GOOG4', hmac_secret), 'sha256');
  key_region = hmac(request_location, key_date, 'sha256');
  key_service = hmac(request_service, key_region, 'sha256');
  signing_key = hmac(request_type, key_service, 'sha256');

  message_digest = hmac(string_to_sign, signing_key, 'sha256');

  -- https://cloud.google.com/storage/docs/authentication/signatures#after_signing
  return encode(message_digest::text::bytea, 'hex');
end
$$;

Вот полный sql код: https://gist.github.com/lukepolo/1bc4ee9e8133ab33484a8d8ec8ef9e17

1 Ответ

1 голос
/ 17 июня 2020

Я перепутал типы должны были быть bytea

create or replace function qx.create_google_storage_signature(
  string_to_sign text,
  request_date text,
  request_location text,
  request_service text,
  request_type text
)
  returns text
  volatile
  language plpgsql
as $$
declare
  hmac_secret text;
  message_digest text;
begin
  -- todo : we need to not store this in plain text
  hmac_secret = qx.current_setting('qx.google_storage_hmac_secret');

  -- https://cloud.google.com/storage/docs/authentication/signatures#derive-key
  message_digest = hmac(string_to_sign::text::bytea, hmac(request_type::text::bytea, hmac(request_service::text::bytea, hmac(request_location::text::bytea, hmac(request_date, concat('GOOG4', hmac_secret), 'sha256'), 'sha256'), 'sha256'), 'sha256'), 'sha256');

  -- https://cloud.google.com/storage/docs/authentication/signatures#after_signing
  return encode(message_digest::text::bytea, 'hex');
end
$$;
...