-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from amezcua/firebase-certs
Firebase certs to support FB JavaScript API
- Loading branch information
Showing
16 changed files
with
150 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule Jwt.FirebaseCerts.PublicKey do | ||
|
||
@httpclient Jwt.HttpCacheClient | ||
@certs_url "https://www.googleapis.com/robot/v1/metadata/x509/[email protected]" | ||
|
||
def getfor(id), do: fetch id | ||
|
||
defp fetch(id) do | ||
@httpclient.get!(@certs_url) | ||
|> get_response_body | ||
|> extract_public_key_for_id(id) | ||
end | ||
|
||
defp get_response_body(%{body: body, headers: _headers, status_code: 200}), do: {:ok, body} | ||
defp get_response_body(%{body: body, headers: _headers, status_code: _status_code}), do: {:error, body} | ||
|
||
defp extract_public_key_for_id({:error, _}, _id), do: nil | ||
defp extract_public_key_for_id({:ok, body}, id) do | ||
{:ok, parsed} = Poison.Parser.parse body | ||
|
||
cert = parsed[id] | ||
|
||
case cert do | ||
nil -> {:error, "Public key id not found"} | ||
cert_string -> Jwt.PemParser.extract_exponent_and_modulus_from_pem_cert cert_string | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Jwt.PemParser do | ||
def extract_exponent_and_modulus_from_pem_cert(pem_cert) do | ||
[{_, dert, _}] = :public_key.pem_decode(pem_cert) | ||
otp = :public_key.pkix_decode_cert(dert, :otp) | ||
|
||
[_, mod, exp] = otp | ||
|> Tuple.to_list | ||
|> Enum.slice(1..1) | ||
|> hd | ||
|> Tuple.to_list | ||
|> Enum.slice(7..7) | ||
|> hd | ||
|> Tuple.to_list | ||
|> Enum.slice(2..2) | ||
|> hd | ||
|> Tuple.to_list | ||
|
||
{:ok, %{exp: exp, mod: mod}} | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule FirebaseCertsTest do | ||
use ExUnit.Case, async: true | ||
|
||
require Logger | ||
|
||
@certificate "-----BEGIN CERTIFICATE-----\nMIIDHDCCAgSgAwIBAgIIH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dw1Ey3mBCSbdOkIMuTxCzfjkvXrbmGirIAfn8INJyIEg8Vnav-NaBT5ShIcie_ovVZy97iReNcubRZn34ipMQrai_k_JaRjlVMn0m0u9dee44S4XHlvdC4hkC-DpBZubDDj3BVQq-Ztk9H9CZREuPUjdfy3anflBE_BwQ" | ||
|
||
test "Verify signature Firebase certificate" do | ||
[{_, dert, _}] = :public_key.pem_decode(@certificate) | ||
otp = :public_key.pkix_decode_cert(dert, :otp) | ||
|
||
[_, mod, exp] = otp | ||
|> Tuple.to_list | ||
|> Enum.slice(1..1) | ||
|> hd | ||
|> Tuple.to_list | ||
|> Enum.slice(7..7) | ||
|> hd | ||
|> Tuple.to_list | ||
|> Enum.slice(2..2) | ||
|> hd | ||
|> Tuple.to_list | ||
|
||
pk = [exp, mod] | ||
signature = Base.url_decode64! @signature, padding: false | ||
msg = @header <> "." <> @claims | ||
|
||
assert :crypto.verify :rsa, :sha256, msg, signature, pk | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule Jwt.FirebaseCerts.PublicKey.Mock do | ||
require Logger | ||
|
||
@certificate "-----BEGIN CERTIFICATE-----\nMIIDHDCCAgSgAwIBAgIIHCf0ZvzSh6gwDQYJKoZIhvcNAQEFBQAwMTEvMC0GA1UE\nAxMmc2VjdXJldG9rZW4uc3lzdGVtLmdzZXJ2aWNlYWNjb3VudC5jb20wHhcNMTcw\nMTAzMDA0NTI2WhcNMTcwMTA2MDExNTI2WjAxMS8wLQYDVQQDEyZzZWN1cmV0b2tl\nbi5zeXN0ZW0uZ3NlcnZpY2VhY2NvdW50LmNvbTCCASIwDQYJKoZIhvcNAQEBBQAD\nggEPADCCAQoCggEBAMew823wkK4hu02ROzImYBkryY8V6hH5aKbIcbXoaktTn/en\n5PB29MFqgy5GXrPsg6knIxsx2RR+yX5qWvMJJlxYz/NytMNkgFZPh+wtgEG0XmA5\n34J1nS6p9Lg/5jgxTyDmN9/WOj9Ml4DgQSFzz4f4AGwStw+dOERXBz+wASrs+8qL\ntxLt/Z2ENAqMDnxaY8VdOqNlFeQuBca3KQsZEvv3jObFGwrtFsCa+gnQ2JNYCACz\nuaJou68I4P0SOc17NqX8NZtyW/UmBam2WEHfE9C8qGUk96sfIktSL5MwRYQyl3UP\nU+SiorVkGBJgTGMHSymsbR/Ia4tB+nQ4KIuEbWUCAwEAAaM4MDYwDAYDVR0TAQH/\nBAIwADAOBgNVHQ8BAf8EBAMCB4AwFgYDVR0lAQH/BAwwCgYIKwYBBQUHAwIwDQYJ\nKoZIhvcNAQEFBQADggEBAJ3M+1HJTt/IQZ/09eW8B9gwgBBVhawZ1n0+aY7SuKSB\njHz474nUlQVz6SxBmeVScb9IXh2lL1K+YYrP/O7sP/OUYXHfjxKGNFMDl/lo/Pgq\nMmSOXnvbvEOgXrsgF8/ytj2BEyMe7wuZC7impUCkbarV4FKcvqjff1iLzAKWn4et\nRJJEyEngemOfAl2l9JBuQG/mouOyapv1g7B9mfRTTYKtbFl1o3N1GJtWC78pD+K9\noawYyz9tiGmm8IKW/KYiJhj616eq06ugEb3VHDjFL+hjGPmORPUND/al+XibYU/q\nNN45J+dag1ZeKxu1Ugc9p2IQB1RMtLMfGmaBldHQ5dY=\n-----END CERTIFICATE-----\n" | ||
|
||
def getfor(id) do | ||
Jwt.PemParser.extract_exponent_and_modulus_from_pem_cert @certificate | ||
end | ||
end |
Oops, something went wrong.