Skip to content

Commit

Permalink
Make Lti_1p3.Utils.fetch_public_key/2 more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
augusto1024 committed Sep 21, 2023
1 parent a1d1c71 commit 0fa1477
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions lib/lti_1p3/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,36 @@ defmodule Lti_1p3.Utils do
error
end

case Enum.find(public_key_set["keys"], fn key -> key["kid"] == kid end) do
nil ->
{:error,
%{
reason: :key_not_found,
msg: "Key with kid #{kid} not found in the fetched list of public keys"
}}

public_key_json ->
public_key =
public_key_json
|> convert_map_to_base64url()
|> JOSE.JWK.from()

{:ok, public_key}
if is_container(public_key_set) do
case Enum.find(public_key_set["keys"], fn key -> is_container(key) && key["kid"] == kid end) do
nil ->
return_key_not_found(kid)

public_key_json ->
public_key =
public_key_json
|> convert_map_to_base64url()
|> JOSE.JWK.from()

{:ok, public_key}
end
else
return_key_not_found(kid)
end
end

defp is_container(container) do
Keyword.keyword?(container) || is_map(container) || is_struct(container)
end

defp return_key_not_found(kid) do
{:error,
%{
reason: :key_not_found,
msg: "Key with kid #{kid} not found in the fetched list of public keys"
}}
end

@doc """
Given a map representing a JWK, encodes all its values to Base64URL.
"""
Expand Down

0 comments on commit 0fa1477

Please sign in to comment.