Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass url_extension for token retrieval #303

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/oidcc_token.erl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
preferred_auth_methods => [oidcc_auth_util:auth_method(), ...],
refresh_jwks => oidcc_jwt_util:refresh_jwks_for_unknown_kid_fun(),
redirect_uri := uri_string:uri_string(),
request_opts => oidcc_http_util:request_opts()
request_opts => oidcc_http_util:request_opts(),
url_extension => oidcc_http_util:query_params()
}.
%% Options for retrieving a token
%%
Expand All @@ -124,7 +125,8 @@
#{
scope => oidcc_scope:scopes(),
refresh_jwks => oidcc_jwt_util:refresh_jwks_for_unknown_kid_fun(),
request_opts => oidcc_http_util:request_opts()
request_opts => oidcc_http_util:request_opts(),
url_extension => oidcc_http_util:query_params()
}.
%% See {@link refresh_opts_no_sub()}

Expand All @@ -133,7 +135,8 @@
scope => oidcc_scope:scopes(),
refresh_jwks => oidcc_jwt_util:refresh_jwks_for_unknown_kid_fun(),
expected_subject := binary(),
request_opts => oidcc_http_util:request_opts()
request_opts => oidcc_http_util:request_opts(),
url_extension => oidcc_http_util:query_params()
}.
%% Options for refreshing a token
%%
Expand All @@ -152,13 +155,15 @@
scope => oidcc_scope:scopes(),
refresh_jwks => oidcc_jwt_util:refresh_jwks_for_unknown_kid_fun(),
request_opts => oidcc_http_util:request_opts(),
kid => binary()
kid => binary(),
url_extension => oidcc_http_util:query_params()
}.

-type client_credentials_opts() :: #{
scope => oidcc_scope:scopes(),
refresh_jwks => oidcc_jwt_util:refresh_jwks_for_unknown_kid_fun(),
request_opts => oidcc_http_util:request_opts()
request_opts => oidcc_http_util:request_opts(),
url_extension => oidcc_http_util:query_params()
}.

-type error() ::
Expand Down Expand Up @@ -835,6 +840,14 @@ retrieve_a_token(QsBodyIn, PkceVerifier, ClientContext, Opts, TelemetryOpts, Aut
} =
Configuration,

QueryParams = maps:get(url_extension, Opts, []),

Endpoint =
case QueryParams of
[] -> TokenEndpoint;
_ -> [TokenEndpoint, <<"?">>, uri_string:compose_query(QueryParams)]
end,

Header0 = [{"accept", "application/jwt, application/json"}],

Body0 = add_pkce_verifier(QsBodyIn, PkceVerifier),
Expand All @@ -851,8 +864,7 @@ retrieve_a_token(QsBodyIn, PkceVerifier, ClientContext, Opts, TelemetryOpts, Aut
Body0, Header0, SupportedAuthMethods, SigningAlgs, Opts, ClientContext
),
Request =
{TokenEndpoint, Header, "application/x-www-form-urlencoded",
uri_string:compose_query(Body)},
{Endpoint, Header, "application/x-www-form-urlencoded", uri_string:compose_query(Body)},
RequestOpts = maps:get(request_opts, Opts, #{}),
{ok, {{json, TokenResponse}, _Headers}} ?=
oidcc_http_util:request(post, Request, TelemetryOpts, RequestOpts),
Expand Down
4 changes: 2 additions & 2 deletions test/oidcc_token_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ retrieve_none_test() ->
_HttpOpts,
_Opts
) ->
TokenEndpoint = ReqTokenEndpoint,
?assertEqual(<<TokenEndpoint/binary, "?foo=bar">>, iolist_to_binary(ReqTokenEndpoint)),
?assertMatch({"authorization", _}, proplists:lookup("authorization", Header)),
?assertMatch(
#{
Expand All @@ -103,7 +103,7 @@ retrieve_none_test() ->
oidcc_token:retrieve(
AuthCode,
ClientContext,
#{redirect_uri => LocalEndpoint}
#{redirect_uri => LocalEndpoint, url_extension => [{<<"foo">>, <<"bar">>}]}
)
),

Expand Down