Skip to content

Commit

Permalink
Merge pull request #28 from Simon-Initiative/bugfix/mer-2569-public-k…
Browse files Browse the repository at this point in the history
…ey-error

[BUGFIX][MER-2569] Handle bad JWKS responses
  • Loading branch information
darrensiegel authored Oct 9, 2023
2 parents a1d1c71 + 0fa1477 commit 25e3870
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 25e3870

Please sign in to comment.