Skip to content

Commit c2e2c84

Browse files
committed
Deprecate Config module
1 parent 16edb8f commit c2e2c84

32 files changed

Lines changed: 326 additions & 304 deletions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
* `Assent.Strategy.decode_response/2` deprecated accepting result tuples and now accepts `Assent.HTTPAdapter.HTTPResponse` structs
1717
* `Assent.Strategy.request/5` deprecated in favor of `Assent.Strategy.http_request/5`
1818
* `Assent.Strategy.decode_response/2` deprecated in favor of `Assent.HTTPAdapter.decode_response/2`
19+
* `Assent.Config.get/3` deprecated in favor of `Keyword.get/3`
20+
* `Assent.Config.put/3` deprecated in favor of `Keyword.put/3`
21+
* `Assent.Config.merge/2` deprecated in favor of `Keyword.merge/2`
22+
* `Assent.Config.t()` type deprecated in favor of `Keyword.t()` type
23+
* `Assent.Config.fetch/2` deprecated in favor of `Assent.fetch_config/2`
1924

2025
## v0.2.10 (2024-04-11)
2126

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ defmodule ProviderAuth do
137137

138138
@config
139139
# Session params should be added to the config so the strategy can use them
140-
|> Config.put(:session_params, session_params)
140+
|> Keyword.put(:session_params, session_params)
141141
|> Github.callback(params)
142142
|> case do
143143
{:ok, %{user: user, token: token}} ->
@@ -166,8 +166,6 @@ config :my_app, :strategies,
166166

167167
```elixir
168168
defmodule MultiProviderAuth do
169-
alias Assent.Config
170-
171169
@spec request(atom()) :: {:ok, map()} | {:error, term()}
172170
def request(provider) do
173171
config = config!(provider)
@@ -180,7 +178,7 @@ defmodule MultiProviderAuth do
180178
config = config!(provider)
181179

182180
config
183-
|> Assent.Config.put(:session_params, session_params)
181+
|> Keyword.put(:session_params, session_params)
184182
|> config[:strategy].callback(params)
185183
end
186184

@@ -189,7 +187,7 @@ defmodule MultiProviderAuth do
189187
Application.get_env(:my_app, :strategies)[provider] ||
190188
raise "No provider configuration for #{provider}"
191189

192-
Config.put(config, :redirect_uri, "http://localhost:4000/oauth/#{provider}/callback")
190+
Keyword.put(config, :redirect_uri, "http://localhost:4000/oauth/#{provider}/callback")
193191
end
194192
end
195193
```

integration/lib/router.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ defmodule IntegrationServer.Router do
7575

7676
unquote(path)
7777
|> config!()
78-
|> Assent.Config.put(:session_params, get_session(conn, :session_params))
78+
|> Keyword.put(:session_params, get_session(conn, :session_params))
7979
|> unquote(module).callback(conn.params)
8080
|> case do
8181
{:ok, %{user: user, token: token}} ->

lib/assent.ex

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
defmodule Assent do
22
@moduledoc false
33

4+
defmodule MissingConfigError do
5+
defexception [:key, :config]
6+
7+
@type t :: %__MODULE__{
8+
key: atom(),
9+
config: Keyword.t()
10+
}
11+
12+
def message(exception) do
13+
key = inspect(exception.key)
14+
config_keys = inspect(Keyword.keys(exception.config))
15+
16+
"Expected #{key} in config, got: #{config_keys}"
17+
end
18+
end
19+
420
defmodule CallbackError do
521
defexception [:message, :error, :error_uri]
622
end
@@ -27,9 +43,9 @@ defmodule Assent do
2743

2844
def message(exception) do
2945
expected_key = inspect(exception.expected_key)
30-
params = inspect(Map.keys(exception.params))
46+
param_keys = exception.params |> Map.keys() |> Enum.sort() |> inspect()
3147

32-
"Expected #{expected_key} in params, got: #{params}"
48+
"Expected #{expected_key} in params, got: #{param_keys}"
3349
end
3450
end
3551

@@ -112,6 +128,49 @@ defmodule Assent do
112128
end
113129
end
114130

131+
@doc """
132+
Fetches the key value from the configuration.
133+
134+
Returns a `Assent.MissingConfigError` if the key is not found.
135+
"""
136+
@spec fetch_config(Keyword.t(), atom()) :: {:ok, any()} | {:error, MissingConfigError.t()}
137+
def fetch_config(config, key) when is_list(config) and is_atom(key) do
138+
case Keyword.fetch(config, key) do
139+
{:ok, value} -> {:ok, value}
140+
:error -> {:error, MissingConfigError.exception(key: key, config: config)}
141+
end
142+
end
143+
144+
@doc """
145+
Fetches the key value from the params.
146+
147+
Returns a `Assent.MissingParamError` if the key is not found.
148+
"""
149+
@spec fetch_param(map(), binary()) :: {:ok, any()} | {:error, MissingParamError.t()}
150+
def fetch_param(params, key) when is_map(params) and is_binary(key) do
151+
case Map.fetch(params, key) do
152+
{:ok, value} -> {:ok, value}
153+
:error -> {:error, MissingParamError.exception(expected_key: key, params: params)}
154+
end
155+
end
156+
157+
@default_json_library (Code.ensure_loaded?(JSON) && JSON) || Jason
158+
159+
@doc """
160+
Fetches the JSON library in config.
161+
162+
If not found in provided config, this will attempt to load the JSON library
163+
from global application environment for `:assent`. Defaults to
164+
`#{inspect(@default_json_library)}`.
165+
"""
166+
@spec json_library(Keyword.t()) :: module()
167+
def json_library(config) do
168+
case Keyword.fetch(config, :json_library) do
169+
:error -> Application.get_env(:assent, :json_library, @default_json_library)
170+
{:ok, json_library} -> json_library
171+
end
172+
end
173+
115174
import Bitwise
116175

117176
@doc false

lib/assent/config.ex

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
# TODO: Deprecated, remove in 0.3
12
defmodule Assent.Config do
2-
@moduledoc """
3-
Methods to handle configurations.
4-
"""
3+
@moduledoc false
54

6-
defmodule MissingKeyError do
5+
defmodule MissingConfigError do
76
@type t :: %__MODULE__{}
87

98
defexception [:key]
@@ -15,51 +14,29 @@ defmodule Assent.Config do
1514

1615
@type t :: Keyword.t()
1716

18-
@doc """
19-
Fetches the key value from the configuration.
20-
"""
21-
@spec fetch(t(), atom()) :: {:ok, any()} | {:error, MissingKeyError.t()}
22-
def fetch(config, key) do
23-
case Keyword.fetch(config, key) do
24-
{:ok, value} -> {:ok, value}
25-
:error -> {:error, MissingKeyError.exception(key: key)}
26-
end
27-
end
17+
@doc false
18+
@deprecated "Use Assent.fetch_config/2 instead"
19+
def fetch(config, key), do: Assent.fetch_config(config, key)
2820

21+
@deprecated "Use Keyword.get/3 instead"
2922
defdelegate get(config, key, default), to: Keyword
3023

24+
@deprecated "Use Keyword.put/3 instead"
3125
defdelegate put(config, key, value), to: Keyword
3226

27+
@deprecated "Use Keyword.merge/2 instead"
3328
defdelegate merge(config_a, config_b), to: Keyword
3429

35-
@default_json_library (Code.ensure_loaded?(JSON) && JSON) || Jason
36-
37-
@doc """
38-
Fetches the JSON library in config.
39-
40-
If not found in provided config, this will attempt to load the JSON library
41-
from global application environment for `:assent`. Defaults to
42-
`#{inspect(@default_json_library)}`.
43-
"""
44-
@spec json_library(t()) :: module()
45-
def json_library(config) do
46-
case get(config, :json_library, nil) do
47-
nil ->
48-
Application.get_env(:assent, :json_library, @default_json_library)
49-
50-
json_library ->
51-
json_library
52-
end
53-
end
30+
@deprecated "Use Assent.json_library/1 instead"
31+
def json_library(config), do: Assent.json_library(config)
5432

55-
# TODO: Remove in next major version
5633
def __base_url__(config) do
57-
case fetch(config, :base_url) do
34+
case Assent.fetch_config(config, :base_url) do
5835
{:ok, base_url} ->
5936
{:ok, base_url}
6037

6138
{:error, error} ->
62-
case fetch(config, :site) do
39+
case Assent.fetch_config(config, :site) do
6340
{:ok, base_url} ->
6441
IO.warn("The `:site` configuration key is deprecated, use `:base_url` instead")
6542
{:ok, base_url}

lib/assent/http_adapter.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule Assent.HTTPAdapter do
2626
end
2727
end
2828
"""
29-
alias Assent.{Config, InvalidResponseError, ServerUnreachableError}
29+
alias Assent.{InvalidResponseError, ServerUnreachableError}
3030

3131
defmodule HTTPResponse do
3232
@moduledoc """
@@ -100,12 +100,12 @@ defmodule Assent.HTTPAdapter do
100100
- `:http_adapter` - The HTTP adapter to use, defaults to
101101
`#{inspect(elem(@default_http_client, 0))}`.
102102
- `:json_library` - The JSON library to use, see
103-
`Assent.Config.json_library/1`.
103+
`Assent.json_library/1`.
104104
"""
105105
@spec request(atom(), binary(), binary() | nil, list(), Keyword.t()) ::
106106
{:ok, HTTPResponse.t()} | {:error, HTTPResponse.t()} | {:error, term()}
107107
def request(method, url, body, headers, opts) do
108-
{http_adapter, http_adapter_opts} = get_http_adapter(opts)
108+
{http_adapter, http_adapter_opts} = get_adapter(opts)
109109

110110
method
111111
|> http_adapter.request(url, body, headers, http_adapter_opts)
@@ -133,7 +133,7 @@ defmodule Assent.HTTPAdapter do
133133
end
134134
end
135135

136-
defp get_http_adapter(opts) do
136+
defp get_adapter(opts) do
137137
default_http_adapter = Application.get_env(:assent, :http_adapter, @default_http_client)
138138

139139
case Keyword.get(opts, :http_adapter, default_http_adapter) do
@@ -148,7 +148,7 @@ defmodule Assent.HTTPAdapter do
148148
## Options
149149
150150
- `:json_library` - The JSON library to use, see
151-
`Assent.Config.json_library/1`
151+
`Assent.json_library/1`
152152
"""
153153
@spec decode_response(HTTPResponse.t(), Keyword.t()) ::
154154
{:ok, HTTPResponse.t()} | {:error, InvalidResponseError.t()}
@@ -162,10 +162,10 @@ defmodule Assent.HTTPAdapter do
162162
defp decode(headers, body, opts) when is_binary(body) do
163163
case List.keyfind(headers, "content-type", 0) do
164164
{"content-type", "application/json" <> _rest} ->
165-
Config.json_library(opts).decode(body)
165+
Assent.json_library(opts).decode(body)
166166

167167
{"content-type", "text/javascript" <> _rest} ->
168-
Config.json_library(opts).decode(body)
168+
Assent.json_library(opts).decode(body)
169169

170170
{"content-type", "application/x-www-form-urlencoded" <> _reset} ->
171171
{:ok, URI.decode_query(body)}

lib/assent/jwt_adapter.ex

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ defmodule Assent.JWTAdapter do
3030
end
3131
end
3232
"""
33-
34-
alias Assent.Config
35-
3633
@callback sign(map(), binary(), binary(), Keyword.t()) :: {:ok, binary()} | {:error, term()}
3734
@callback verify(binary(), binary() | map() | nil, Keyword.t()) ::
3835
{:ok, map()} | {:error, term()}
@@ -45,7 +42,7 @@ defmodule Assent.JWTAdapter do
4542
## Options
4643
4744
- `:json_library` - The JSON library to use, optional, see
48-
`Assent.Config.json_library/1`.
45+
`Assent.json_library/1`.
4946
- `:jwt_adapter` - The JWT adapter module to use, optional, defaults to
5047
`#{inspect(@default_jwt_adapter)}`
5148
"""
@@ -61,7 +58,7 @@ defmodule Assent.JWTAdapter do
6158
## Options
6259
6360
- `:json_library` - The JSON library to use, optional, see
64-
`Assent.Config.json_library/1`.
61+
`Assent.json_library/1`.
6562
- `:jwt_adapter` - The JWT adapter module to use, optional, defaults to
6663
`#{inspect(@default_jwt_adapter)}`
6764
"""
@@ -72,7 +69,7 @@ defmodule Assent.JWTAdapter do
7269
end
7370

7471
defp get_adapter(opts) do
75-
default_opts = Keyword.put(opts, :json_library, Config.json_library(opts))
72+
default_opts = Keyword.put(opts, :json_library, Assent.json_library(opts))
7673
default_jwt_adapter = Application.get_env(:assent, :jwt_adapter, @default_jwt_adapter)
7774

7875
case Keyword.get(opts, :jwt_adapter, default_jwt_adapter) do
@@ -91,9 +88,9 @@ defmodule Assent.JWTAdapter do
9188
"""
9289
@spec load_private_key(Keyword.t()) :: {:ok, binary()} | {:error, term()}
9390
def load_private_key(config) do
94-
case Config.fetch(config, :private_key_path) do
91+
case Assent.fetch_config(config, :private_key_path) do
9592
{:ok, path} -> read(path)
96-
{:error, _any} -> Config.fetch(config, :private_key)
93+
{:error, _any} -> Assent.fetch_config(config, :private_key)
9794
end
9895
end
9996

lib/assent/jwt_adapter/assent_jwt.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Assent.JWTAdapter.AssentJWT do
88
99
See `Assent.JWTAdapter` for more.
1010
"""
11-
alias Assent.{Config, JWTAdapter}
11+
alias Assent.JWTAdapter
1212

1313
@behaviour Assent.JWTAdapter
1414

@@ -26,9 +26,9 @@ defmodule Assent.JWTAdapter.AssentJWT do
2626

2727
defp encode_header(alg, opts) do
2828
header =
29-
case Keyword.has_key?(opts, :private_key_id) do
30-
false -> %{"typ" => "JWT", "alg" => alg}
31-
true -> %{"typ" => "JWT", "alg" => alg, "kid" => Keyword.get(opts, :private_key_id)}
29+
case Keyword.fetch(opts, :private_key_id) do
30+
:error -> %{"typ" => "JWT", "alg" => alg}
31+
{:ok, private_key_id} -> %{"typ" => "JWT", "alg" => alg, "kid" => private_key_id}
3232
end
3333

3434
case encode_json_base64(header, opts) do
@@ -41,7 +41,7 @@ defmodule Assent.JWTAdapter.AssentJWT do
4141
end
4242

4343
defp encode_json_base64(map, opts) do
44-
with {:ok, json_library} <- Config.fetch(opts, :json_library),
44+
with {:ok, json_library} <- Assent.fetch_config(opts, :json_library),
4545
{:ok, json} <- json_encode(json_library, map) do
4646
{:ok, Base.url_encode64(json, padding: false)}
4747
end
@@ -173,7 +173,7 @@ defmodule Assent.JWTAdapter.AssentJWT do
173173
end
174174

175175
defp decode_header(header, opts) do
176-
with {:ok, json_library} <- Config.fetch(opts, :json_library),
176+
with {:ok, json_library} <- Assent.fetch_config(opts, :json_library),
177177
{:ok, header} <- decode_base64_url(header),
178178
{:ok, header} <- decode_json(header, json_library),
179179
{:ok, alg} <- fetch_alg(header) do
@@ -202,7 +202,7 @@ defmodule Assent.JWTAdapter.AssentJWT do
202202
defp fetch_alg(_header), do: {:error, "No \"alg\" found in header"}
203203

204204
defp decode_claims(claims, opts) do
205-
with {:ok, json_library} <- Config.fetch(opts, :json_library),
205+
with {:ok, json_library} <- Assent.fetch_config(opts, :json_library),
206206
{:ok, claims} <- decode_base64_url(claims),
207207
{:ok, claims} <- decode_json(claims, json_library) do
208208
{:ok, claims}

lib/assent/jwt_adapter/jose.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ defmodule Assent.JWTAdapter.JOSE do
3232
defp jws(alg, opts) do
3333
jws = %{"alg" => alg}
3434

35-
case Keyword.get(opts, :private_key_id) do
36-
nil -> jws
37-
kid -> Map.put(jws, "kid", kid)
35+
case Keyword.fetch(opts, :private_key_id) do
36+
:error -> jws
37+
{:ok, kid} -> Map.put(jws, "kid", kid)
3838
end
3939
end
4040

0 commit comments

Comments
 (0)