Created
October 7, 2016 00:55
-
-
Save ins429/6c09a7572751fc47b7f955f345d7d5d5 to your computer and use it in GitHub Desktop.
Google url signer for Elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule GoogleMaps.UrlSigner do | |
@module_doc """ | |
Generate a signed url for google maps API. | |
https://developers.google.com/maps/documentation/streetview/get-api-key#generating_valid_signatures | |
""" | |
def call(private_key, url) do | |
parsed_url = URI.parse(url) | |
path_and_query = "#{parsed_url.path}?#{parsed_url.query}" | |
# Create a signature using the private key and the URL(path and query). | |
# And encode the signature into base64 for url use form. | |
signature = | |
Base.url_decode64!(private_key) | |
|> create_signature(path_and_query) | |
|> Base.url_encode64 | |
"#{parsed_url.scheme}://#{parsed_url.host}#{path_and_query}&signature=#{signature}" | |
end | |
defp create_signature(key, data) do | |
:crypto.hmac(:sha, key, data) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment