Skip to content

Commit

Permalink
Fix spec errors
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Dec 27, 2024
1 parent b5c8b22 commit 8bd38ea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/assent/strategies/oauth2/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ defmodule Assent.Strategy.OAuth2.Base do

@spec authorize_url(Keyword.t(), module()) ::
{:ok, %{session_params: %{state: binary()}, url: binary()}}
| {:error, term()}
def authorize_url(config, strategy) do
config
|> set_config(strategy)
Expand Down
21 changes: 16 additions & 5 deletions lib/assent/strategies/oidc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,23 @@ defmodule Assent.Strategy.OIDC do
end

defp peek_header(encoded, config) do
with [header, _, _] <- String.split(encoded, "."),
{:ok, json} <- Base.url_decode64(header, padding: false) do
with {:ok, header} <- split_header(encoded),
{:ok, json} <- decode_base64_url(header) do
Config.json_library(config).decode(json)
else
{:error, error} -> {:error, error}
_any -> {:error, "The ID Token is not a valid JWT"}
end
end

defp split_header(encoded) do
case String.split(encoded, ".") do
[header, _, _] -> {:ok, header}
_ -> {:error, "The ID Token is not a valid JWT"}
end
end

defp decode_base64_url(encoded) do
case Base.url_decode64(encoded, padding: false) do
{:ok, decoded} -> {:ok, decoded}
:error -> {:error, "Invalid Base64URL"}
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/assent/strategies/oidc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ defmodule Assent.Strategy.OIDCTest do
{:error, "The ID Token is not a valid JWT"}
end

test "with invalid base64 header in id_token", %{config: config} do
assert OIDC.validate_id_token(config, "@invalid.payload.signature") ==
{:error, "Invalid Base64URL"}
end

test "with no `:client_secret`", %{config: config, id_token: id_token} do
config = Keyword.delete(config, :client_secret)

Expand Down

0 comments on commit 8bd38ea

Please sign in to comment.