Skip to content

Commit 986b423

Browse files
authored
feat: Format it! (#20)
Format it!
1 parent fb0c78f commit 986b423

19 files changed

+697
-572
lines changed

config/sys.config

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
[{id_token,
2-
[ { providers
3-
, [ {google, <<"https://accounts.google.com/.well-known/openid-configuration">>}
4-
, {microsoft, <<"https://login.microsoftonline.com/common/.well-known/openid-configuration">>}
5-
, {heimdall, <<"https://sandbox.kivra.com/.well-known/openid-configuration">>}
6-
]
7-
}
8-
, {async_revalidate, false}
9-
, {sign_keys, [{<<"ES256">>, #{ttu => 10}}]}
10-
, {pubkeys_storage_module, ets_pubkeys_storage}
11-
]
12-
}].
1+
[
2+
{id_token, [
3+
{providers, [
4+
{google, <<"https://accounts.google.com/.well-known/openid-configuration">>},
5+
{microsoft,
6+
<<"https://login.microsoftonline.com/common/.well-known/openid-configuration">>},
7+
{heimdall, <<"https://sandbox.kivra.com/.well-known/openid-configuration">>}
8+
]},
9+
{async_revalidate, false},
10+
{sign_keys, [{<<"ES256">>, #{ttu => 10}}]},
11+
{pubkeys_storage_module, ets_pubkeys_storage}
12+
]}
13+
].

elvis.config

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
%% -*- erlang -*-
2-
[ {elvis,
3-
[ {config,
4-
[ #{dirs => [ "src"
5-
],
6-
filter => "*.erl",
7-
ruleset => erl_files,
8-
rules => [ {elvis_style, no_block_expressions, disable}
9-
]
10-
},
11-
#{dirs => [ "test"
12-
],
13-
filter => "*.erl",
14-
ruleset => erl_files,
15-
rules => [ {elvis_style, no_block_expressions, disable}
16-
, {elvis_style, no_debug_call, disable}
17-
]
18-
}
19-
]
20-
}
21-
]
22-
}
2+
[
3+
{elvis, [
4+
{config, [
5+
#{
6+
dirs => ["src"],
7+
filter => "*.erl",
8+
ruleset => erl_files,
9+
rules => [{elvis_style, no_block_expressions, disable}]
10+
},
11+
#{
12+
dirs => ["test"],
13+
filter => "*.erl",
14+
ruleset => erl_files,
15+
rules => [
16+
{elvis_style, no_block_expressions, disable},
17+
{elvis_style, no_debug_call, disable}
18+
]
19+
}
20+
]}
21+
]}
2322
].
2423

2524
%%%_* Emacs ====================================================================

rebar.config

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,32 @@
1616
{override, jose, [{erl_opts, [debug_info, no_warnings_as_errors]}]}
1717
]}.
1818

19-
{deps, [ {jsx, {git, "https://github.com/talentdeficit/jsx.git" , {tag, "v3.1.0"}}}
20-
, {jose, {git, "https://github.com/potatosalad/erlang-jose.git" , {tag, "1.11.5"}}}
21-
, {hackney, {git, "https://github.com/benoitc/hackney.git" , {tag, "1.18.0"}}}
22-
]}.
19+
{deps, [
20+
{jsx, {git, "https://github.com/talentdeficit/jsx.git", {tag, "v3.1.0"}}},
21+
{jose, {git, "https://github.com/potatosalad/erlang-jose.git", {tag, "1.11.5"}}},
22+
{hackney, {git, "https://github.com/benoitc/hackney.git", {tag, "1.18.0"}}}
23+
]}.
2324

24-
{shell, [{config, "config/sys.config"},
25-
{apps, [id_token]}
26-
]}.
25+
{shell, [
26+
{config, "config/sys.config"},
27+
{apps, [id_token]}
28+
]}.
2729

2830
{project_plugins, [
31+
{erlfmt, "1.5.0"},
2932
{rebar3_proper, "0.12.1"},
3033
{rebar3_lint, "3.2.6"}
3134
]}.
3235

33-
{profiles,
34-
[{test, [
36+
{profiles, [
37+
{test, [
3538
{cover_enabled, true},
3639
{cover_opts, [verbose]},
3740
{erl_opts, [nowarn_export_all]},
38-
{deps, [ {proper, "1.4.0"}
39-
, {meck, "0.8.13"}
40-
]}
41+
{deps, [
42+
{proper, "1.4.0"},
43+
{meck, "0.8.13"}
44+
]}
4145
]}
4246
]}.
4347

@@ -48,3 +52,9 @@
4852
deprecated_function_calls,
4953
deprecated_functions
5054
]}.
55+
56+
{erlfmt, [
57+
write,
58+
{files, ["**/*.{hrl,erl,app.src,config}"]},
59+
{exclude_files, ["_build/**", ".direnv/**", ".github/**", "doc/**"]}
60+
]}.

src/ets_pubkeys_storage.erl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ start() -> gen_server:start({local, ?MODULE}, ?MODULE, [], []).
1515
stop() -> gen_server:stop(?MODULE).
1616

1717
delete(Key) ->
18-
ets:delete(?MODULE, Key),
19-
ok.
18+
ets:delete(?MODULE, Key),
19+
ok.
2020

2121
get(Kid) ->
22-
case ets:lookup(?MODULE, Kid) of
23-
[{Kid, Key}] -> {ok, Key};
24-
[] -> {error, not_found}
25-
end.
22+
case ets:lookup(?MODULE, Kid) of
23+
[{Kid, Key}] -> {ok, Key};
24+
[] -> {error, not_found}
25+
end.
2626

2727
get_all() ->
28-
Objects = ets:tab2list(?MODULE),
29-
{ok, [K || {_, K} <- Objects]}.
28+
Objects = ets:tab2list(?MODULE),
29+
{ok, [K || {_, K} <- Objects]}.
3030

3131
put(#{<<"kid">> := Kid} = Key) ->
32-
ets:insert(?MODULE, {Kid, Key}),
33-
ok.
32+
ets:insert(?MODULE, {Kid, Key}),
33+
ok.
3434

3535
%% gen_server callbacks
3636
init(A) ->
37-
?MODULE = ets:new(?MODULE, ?ETS_OPTIONS),
38-
{ok, A}.
37+
?MODULE = ets:new(?MODULE, ?ETS_OPTIONS),
38+
{ok, A}.
3939
handle_call(_, _, S) -> {noreply, S}.
4040
handle_cast(_, S) -> {noreply, S}.
4141

src/id_token.app.src

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
{application, id_token,
2-
[{description, "An OTP application to validate Id tokens"},
3-
{vsn, git},
4-
{registered, []},
5-
{mod, {id_token_app, []}},
6-
{applications,
7-
[kernel,
8-
stdlib,
9-
hackney,
10-
jose,
11-
jsx
12-
]},
13-
{env,[]},
14-
{modules, []},
1+
{application, id_token, [
2+
{description, "An OTP application to validate Id tokens"},
3+
{vsn, git},
4+
{registered, []},
5+
{mod, {id_token_app, []}},
6+
{applications, [
7+
kernel,
8+
stdlib,
9+
hackney,
10+
jose,
11+
jsx
12+
]},
13+
{env, []},
14+
{modules, []},
1515

16-
{licenses, ["Apache 2.0"]},
17-
{links, []}
18-
]}.
16+
{licenses, ["Apache 2.0"]},
17+
{links, []}
18+
]}.

src/id_token.erl

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,41 @@
44
-ignore_xref([validate/2, sign/2]).
55
-export([validate/2, sign/2]).
66

7-
-spec validate(atom(), binary()) -> {ok, map()} |
8-
{error, invalid_signature |
9-
expired |
10-
no_public_key_matches
11-
}.
7+
-spec validate(atom(), binary()) ->
8+
{ok, map()}
9+
| {error,
10+
invalid_signature
11+
| expired
12+
| no_public_key_matches}.
1213
validate(Provider, IdToken) ->
13-
#{exp_at := ExpAt, keys := Keys} =
14-
id_token_provider:get_cached_keys(Provider),
15-
case ExpAt > id_token_util:now_gregorian_seconds() of
16-
true ->
17-
case id_token_jws:validate(IdToken, Keys) of
18-
{error, no_public_key_matches} ->
19-
Kid = id_token_jws:extract_kid(IdToken),
20-
refresh_and_validate(Provider, IdToken, #{kid => Kid});
21-
Result ->
22-
Result
23-
end;
24-
false ->
25-
refresh_and_validate(Provider, IdToken)
26-
end.
14+
#{exp_at := ExpAt, keys := Keys} =
15+
id_token_provider:get_cached_keys(Provider),
16+
case ExpAt > id_token_util:now_gregorian_seconds() of
17+
true ->
18+
case id_token_jws:validate(IdToken, Keys) of
19+
{error, no_public_key_matches} ->
20+
Kid = id_token_jws:extract_kid(IdToken),
21+
refresh_and_validate(Provider, IdToken, #{kid => Kid});
22+
Result ->
23+
Result
24+
end;
25+
false ->
26+
refresh_and_validate(Provider, IdToken)
27+
end.
2728

2829
sign(Alg, Claims) ->
29-
case id_token_sign:get_sign_key_fun(Alg) of
30-
{error, not_found} -> {error, no_key_for_alg};
31-
{ok, SignKeyFun} -> id_token_jws:sign(Claims, SignKeyFun())
32-
end.
30+
case id_token_sign:get_sign_key_fun(Alg) of
31+
{error, not_found} -> {error, no_key_for_alg};
32+
{ok, SignKeyFun} -> id_token_jws:sign(Claims, SignKeyFun())
33+
end.
3334

3435
refresh_and_validate(Provider, IdToken) ->
35-
refresh_and_validate(Provider, IdToken, #{}).
36+
refresh_and_validate(Provider, IdToken, #{}).
3637

3738
refresh_and_validate(Provider, IdToken, Opts) ->
38-
#{keys := FreshKeys} =
39-
id_token_provider:refresh_keys(Provider, Opts),
40-
id_token_jws:validate(IdToken, FreshKeys).
39+
#{keys := FreshKeys} =
40+
id_token_provider:refresh_keys(Provider, Opts),
41+
id_token_jws:validate(IdToken, FreshKeys).
4142

4243
%%%_* Emacs ============================================================
4344
%%% Local Variables:

src/id_token_app.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
-export([start/2, stop/1]).
1111

1212
start(_StartType, _StartArgs) ->
13-
id_token_sup:start_link().
13+
id_token_sup:start_link().
1414

1515
stop(_State) ->
16-
ok.
16+
ok.
1717

1818
%%%_* Emacs ============================================================
1919
%%% Local Variables:

src/id_token_jwks.erl

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
-module(id_token_jwks).
22

3-
-export([get_pub_keys/1,
4-
get_jwks_uri/1,
5-
get_providers/0
6-
]).
3+
-export([
4+
get_pub_keys/1,
5+
get_jwks_uri/1,
6+
get_providers/0
7+
]).
78

89
-type keys() :: #{exp_at := integer(), keys := list()}.
910
-type provider() :: {atom(), binary()}.
@@ -12,38 +13,42 @@
1213

1314
-spec get_pub_keys(binary()) -> {ok, keys()} | {error, any()}.
1415
get_pub_keys(Uri) ->
15-
case hackney:request(get, Uri, [], <<>>, [with_body]) of
16-
{ok, 200, Headers, Body} ->
17-
#{<<"keys">> := Keys} = jsx:decode(Body, [return_maps]),
18-
CacheControl = hackney_headers:parse(<<"Cache-Control">>, Headers),
19-
{match, [MaxAgeBin]} = re:run(CacheControl,
20-
<<"max-age=(\\d+)">>,
21-
[{capture, all_but_first, binary}]),
22-
MaxAge = binary_to_integer(MaxAgeBin),
23-
{ok, #{exp_at => id_token_util:now_gregorian_seconds() + MaxAge,
24-
keys => Keys}};
25-
{ok, _, _, _} ->
26-
{error, service_unavailable};
27-
{error, Reason} ->
28-
{error, Reason}
29-
end.
16+
case hackney:request(get, Uri, [], <<>>, [with_body]) of
17+
{ok, 200, Headers, Body} ->
18+
#{<<"keys">> := Keys} = jsx:decode(Body, [return_maps]),
19+
CacheControl = hackney_headers:parse(<<"Cache-Control">>, Headers),
20+
{match, [MaxAgeBin]} = re:run(
21+
CacheControl,
22+
<<"max-age=(\\d+)">>,
23+
[{capture, all_but_first, binary}]
24+
),
25+
MaxAge = binary_to_integer(MaxAgeBin),
26+
{ok, #{
27+
exp_at => id_token_util:now_gregorian_seconds() + MaxAge,
28+
keys => Keys
29+
}};
30+
{ok, _, _, _} ->
31+
{error, service_unavailable};
32+
{error, Reason} ->
33+
{error, Reason}
34+
end.
3035

3136
%% Returns the jwks_uri given the well-known open id connect configuration URI
3237
-spec get_jwks_uri(binary()) -> {ok, binary()} | {error, any()}.
3338
get_jwks_uri(Uri) ->
34-
case hackney:request(get, Uri, [], <<>>, [with_body]) of
35-
{ok, 200, _Headers, Body} ->
36-
#{<<"jwks_uri">> := JWKSUri} = jsx:decode(Body, [return_maps]),
37-
{ok, JWKSUri};
38-
{ok, _, _, _} ->
39-
{error, service_unavailable};
40-
{error, Reason} ->
41-
{error, Reason}
42-
end.
39+
case hackney:request(get, Uri, [], <<>>, [with_body]) of
40+
{ok, 200, _Headers, Body} ->
41+
#{<<"jwks_uri">> := JWKSUri} = jsx:decode(Body, [return_maps]),
42+
{ok, JWKSUri};
43+
{ok, _, _, _} ->
44+
{error, service_unavailable};
45+
{error, Reason} ->
46+
{error, Reason}
47+
end.
4348

4449
-spec get_providers() -> [provider()].
4550
get_providers() ->
46-
application:get_env(id_token, providers, []).
51+
application:get_env(id_token, providers, []).
4752

4853
%%%_* Emacs ============================================================
4954
%%% Local Variables:

0 commit comments

Comments
 (0)