Skip to content

Instantly share code, notes, and snippets.

@AndrewDryga
Last active August 1, 2017 19:44
Show Gist options
  • Save AndrewDryga/f6c017201493db7e59ccbfb579085146 to your computer and use it in GitHub Desktop.
Save AndrewDryga/f6c017201493db7e59ccbfb579085146 to your computer and use it in GitHub Desktop.
AEL: Simplified code for context that is responsible for Signing URL's
defmodule Ael.Secrets.API do
def get_secret_url(secret) do
%Secret{
action: action,
expires_at: expires_at,
bucket: bucket,
resource_id: resource_id,
resource_name: resource_name
} = secret
canonicalized_resource = "/#{bucket}/#{resource_id}/#{resource_name}"
signature =
action
|> string_to_sign(expires_at, canonicalized_resource)
|> sign()
|> Base.encode64()
|> URI.encode_www_form()
"https://storage.googleapis.com#{canonicalized_resource}" <>
"?GoogleAccessId=#{get_from_registry(:gcs_service_account_id)}" <>
"&Expires=#{expires_at}" <>
"&Signature=#{signature}"
end
defp string_to_sign(action, expires_at, canonicalized_resource),
do: Enum.join([action, "", "", expires_at, canonicalized_resource], "\n")
defp sign(plaintext),
do: :public_key.sign(plaintext, :sha256, get_from_registry(:gcs_service_account_key))
defp get_from_registry(key) do
[{_pid, val}] = Registry.lookup(Ael.Registry, key)
val
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment