From 823c408b6b140305d62f2df52e238708bfec16fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20M=C3=A4nnchen?= Date: Sat, 24 Aug 2024 17:39:06 +0000 Subject: [PATCH] fixup --- src/oidcc_token_introspection.erl | 40 ++++++++++++------------- test/oidcc_token_introspection_test.erl | 14 ++++++--- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/oidcc_token_introspection.erl b/src/oidcc_token_introspection.erl index ad43d47..510e0e8 100644 --- a/src/oidcc_token_introspection.erl +++ b/src/oidcc_token_introspection.erl @@ -120,8 +120,7 @@ introspect(AccessToken, ClientContext, Opts) -> provider_configuration = Configuration, client_id = ClientId, client_secret = ClientSecret - } = - ClientContext, + } = ClientContext, #oidcc_provider_configuration{ introspection_endpoint = Endpoint0, issuer = Issuer, @@ -167,10 +166,8 @@ introspect(AccessToken, ClientContext, Opts) -> uri_string:compose_query(Body)}, {ok, {{json, Token}, _Headers}} ?= oidcc_http_util:request(post, Request, TelemetryOpts, RequestOpts), - client_match( - extract_response(Token), - ClientContext, - maps:get(client_self_only, Opts, true)) + {ok, TokenMap} ?= extract_response(Token), + client_match(TokenMap, ClientContext, maps:get(client_self_only, Opts, true)) else {error, {use_dpop_nonce, NewDpopNonce, _}} when DpopOpts =:= #{} @@ -187,20 +184,21 @@ introspect(AccessToken, ClientContext, Opts) -> end end. --spec client_match({ok, Token}, ClientContext, ClientSelfOnly) -> -{ok, t()} -| {error, error()} +-spec client_match(Introspection, ClientContext, ClientSelfOnly) -> + {ok, t()} | {error, error()} when - Token :: t(), - ClientContext :: oidcc_client_context:t(), - ClientSelfOnly :: boolean(). -client_match({ok,Token},_,false) -> - {ok, Token}; -client_match({ok, #oidcc_token_introspection{client_id = ClientId} = Token}, - #oidcc_client_context{client_id = ClientId}, - true) -> - {ok, Token}; -client_match(_,_,true) -> + Introspection :: t(), + ClientContext :: oidcc_client_context:t(), + ClientSelfOnly :: boolean(). +client_match(Introspection,_,false) -> + {ok, Introspection}; +client_match( + #oidcc_token_introspection{client_id = ClientId} = Introspection, + #oidcc_client_context{client_id = ClientId}, + true +) -> + {ok, Introspection}; +client_match(_Introspection, _ClientContext, true) -> {error, client_id_mismatch}. -spec extract_response(TokenMap) -> @@ -225,11 +223,11 @@ extract_response(TokenMap) -> Aud = maps:get(<<"aud">>, TokenMap, undefined), Iss = maps:get(<<"iss">>, TokenMap, undefined), Jti = maps:get(<<"jti">>, TokenMap, undefined), - Cid = maps:get(<<"client_id">>, TokenMap, undefined), + ClientId = maps:get(<<"client_id">>, TokenMap, undefined), {ok, #oidcc_token_introspection{ active = Active, scope = oidcc_scope:parse(Scope), - client_id = Cid, + client_id = ClientId, username = Username, exp = Exp, token_type = TokenType, diff --git a/test/oidcc_token_introspection_test.erl b/test/oidcc_token_introspection_test.erl index f5dc726..3e0b13e 100644 --- a/test/oidcc_token_introspection_test.erl +++ b/test/oidcc_token_introspection_test.erl @@ -204,9 +204,9 @@ introspection_issuer_client_id_test() -> {ok, ConfigurationBinary} = file:read_file(PrivDir ++ "/test/fixtures/example-metadata.json"), {ok, #oidcc_provider_configuration{ - introspection_endpoint = IntrospectionEndpoint, - issuer = Issuer - } = + introspection_endpoint = IntrospectionEndpoint, + issuer = Issuer + } = Configuration} = oidcc_provider_configuration:decode_configuration(jose:decode(ConfigurationBinary)), @@ -230,7 +230,13 @@ introspection_issuer_client_id_test() -> _RequestOpts ) -> IntrospectionEndpoint = ReqEndpoint, - {ok, {{json, #{<<"active">> => true, <<"client_id">> => OtherClientId, <<"iss">> => Issuer}}, []}} + {ok, + { + {json, #{ + <<"active">> => true, <<"client_id">> => OtherClientId, <<"iss">> => Issuer + }}, + [] + }} end, ok = meck:expect(oidcc_http_util, request, HttpFun),