Skip to content

Commit

Permalink
fetch -> fetch!
Browse files Browse the repository at this point in the history
  • Loading branch information
MaeIsBad committed Nov 20, 2023
1 parent 714b61a commit 34f3c2f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/prima_auth0_ex/jwks_strategy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule PrimaAuth0Ex.JwksStrategy do
use JokenJwks.DefaultStrategyTemplate

def init_opts(opts) do
jwks_url = Config.server(:auth0_base_url) |> OpenIDConfiguration.fetch() |> Map.fetch!(:jwks_uri)
jwks_url = Config.server(:auth0_base_url) |> OpenIDConfiguration.fetch!() |> Map.fetch!(:jwks_uri)
Keyword.merge(opts, jwks_url: jwks_url)
end
end
13 changes: 10 additions & 3 deletions lib/prima_auth0_ex/openid_configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ defmodule PrimaAuth0Ex.OpenIDConfiguration do
Doesn't implement caching and always makes a http request, avoid calling this in hot code paths
"""
@spec fetch(String.t()) :: __MODULE__.t()
def fetch(base_url) do
@spec fetch!(String.t()) :: __MODULE__.t()
def fetch!(base_url) do
url = metadata_url(base_url)
%HTTPoison.Response{status_code: status_code, body: meta_body} = Telepoison.get!(url, accept: "application/json")

true = status_code in 200..299
unless status_code in 200..299 do
raise """
Failed to retrieve the openid-configuration from #{url}, server sent ${status_code}:
#{meta_body}
This is most likely caused by an incorrect base_url.
"""
end

metadata = Jason.decode!(meta_body)
metadata = Map.new(@struct_keys, fn key -> {key, metadata[Atom.to_string(key)]} end)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule PrimaAuth0Ex.TokenProvider.Auth0AuthorizationService do

@impl PrimaAuth0Ex.TokenProvider.AuthorizationService
def retrieve_token(credentials, audience) do
url = OpenIDConfiguration.fetch(credentials.base_url).token_endpoint
url = OpenIDConfiguration.fetch!(credentials.base_url).token_endpoint
request_body = body(credentials, audience)

Logger.info("Requesting token to Auth0",
Expand Down
2 changes: 1 addition & 1 deletion test/prima_auth0_ex/openid_configuration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule PrimaAuth0Ex.OpenIDConfigurationTest do

base_url = "http://localhost:#{bypass.port}"

fetched = OpenIDConfiguration.fetch(base_url)
fetched = OpenIDConfiguration.fetch!(base_url)

assert fetched.issuer == config.issuer
assert fetched.token_endpoint == config.token_endpoint
Expand Down

0 comments on commit 34f3c2f

Please sign in to comment.