4
4
from typing import Tuple
5
5
from unittest import mock
6
6
7
+ import jwcrypto .jwk
8
+ import jwcrypto .jws
7
9
import pytest
8
10
9
11
from keycloak import KeycloakAdmin , KeycloakOpenID
@@ -317,6 +319,39 @@ def test_decode_token(oid_with_credentials: Tuple[KeycloakOpenID, str, str]):
317
319
assert decoded_refresh_token ["typ" ] == "Refresh" , decoded_refresh_token
318
320
319
321
322
+ def test_decode_token_invalid_token (oid_with_credentials : Tuple [KeycloakOpenID , str , str ]):
323
+ """Test decode token with an invalid token.
324
+
325
+ :param oid_with_credentials: Keycloak OpenID client with pre-configured user credentials
326
+ :type oid_with_credentials: Tuple[KeycloakOpenID, str, str]
327
+ """
328
+ oid , username , password = oid_with_credentials
329
+ token = oid .token (username = username , password = password )
330
+ access_token = token ["access_token" ]
331
+ decoded_access_token = oid .decode_token (token = access_token )
332
+
333
+ key = oid .public_key ()
334
+ key = "-----BEGIN PUBLIC KEY-----\n " + key + "\n -----END PUBLIC KEY-----"
335
+ key = jwcrypto .jwk .JWK .from_pem (key .encode ("utf-8" ))
336
+
337
+ invalid_access_token = access_token + "a"
338
+ with pytest .raises (jwcrypto .jws .InvalidJWSSignature ):
339
+ decoded_invalid_access_token = oid .decode_token (token = invalid_access_token , validate = True )
340
+
341
+ with pytest .raises (jwcrypto .jws .InvalidJWSSignature ):
342
+ decoded_invalid_access_token = oid .decode_token (
343
+ token = invalid_access_token , validate = True , key = key
344
+ )
345
+
346
+ decoded_invalid_access_token = oid .decode_token (token = invalid_access_token , validate = False )
347
+ assert decoded_access_token == decoded_invalid_access_token
348
+
349
+ decoded_invalid_access_token = oid .decode_token (
350
+ token = invalid_access_token , validate = False , key = key
351
+ )
352
+ assert decoded_access_token == decoded_invalid_access_token
353
+
354
+
320
355
def test_load_authorization_config (oid_with_credentials_authz : Tuple [KeycloakOpenID , str , str ]):
321
356
"""Test load authorization config.
322
357
@@ -765,7 +800,7 @@ async def test_a_introspect(oid_with_credentials: Tuple[KeycloakOpenID, str, str
765
800
766
801
@pytest .mark .asyncio
767
802
async def test_a_decode_token (oid_with_credentials : Tuple [KeycloakOpenID , str , str ]):
768
- """Test decode token.
803
+ """Test decode token asynchronously .
769
804
770
805
:param oid_with_credentials: Keycloak OpenID client with pre-configured user credentials
771
806
:type oid_with_credentials: Tuple[KeycloakOpenID, str, str]
@@ -781,6 +816,44 @@ async def test_a_decode_token(oid_with_credentials: Tuple[KeycloakOpenID, str, s
781
816
assert decoded_refresh_token ["typ" ] == "Refresh" , decoded_refresh_token
782
817
783
818
819
+ @pytest .mark .asyncio
820
+ async def test_a_decode_token_invalid_token (oid_with_credentials : Tuple [KeycloakOpenID , str , str ]):
821
+ """Test decode token asynchronously an invalid token.
822
+
823
+ :param oid_with_credentials: Keycloak OpenID client with pre-configured user credentials
824
+ :type oid_with_credentials: Tuple[KeycloakOpenID, str, str]
825
+ """
826
+ oid , username , password = oid_with_credentials
827
+ token = await oid .a_token (username = username , password = password )
828
+ access_token = token ["access_token" ]
829
+ decoded_access_token = await oid .a_decode_token (token = access_token )
830
+
831
+ key = await oid .a_public_key ()
832
+ key = "-----BEGIN PUBLIC KEY-----\n " + key + "\n -----END PUBLIC KEY-----"
833
+ key = jwcrypto .jwk .JWK .from_pem (key .encode ("utf-8" ))
834
+
835
+ invalid_access_token = access_token + "a"
836
+ with pytest .raises (jwcrypto .jws .InvalidJWSSignature ):
837
+ decoded_invalid_access_token = await oid .a_decode_token (
838
+ token = invalid_access_token , validate = True
839
+ )
840
+
841
+ with pytest .raises (jwcrypto .jws .InvalidJWSSignature ):
842
+ decoded_invalid_access_token = await oid .a_decode_token (
843
+ token = invalid_access_token , validate = True , key = key
844
+ )
845
+
846
+ decoded_invalid_access_token = await oid .a_decode_token (
847
+ token = invalid_access_token , validate = False
848
+ )
849
+ assert decoded_access_token == decoded_invalid_access_token
850
+
851
+ decoded_invalid_access_token = await oid .a_decode_token (
852
+ token = invalid_access_token , validate = False , key = key
853
+ )
854
+ assert decoded_access_token == decoded_invalid_access_token
855
+
856
+
784
857
@pytest .mark .asyncio
785
858
async def test_a_load_authorization_config (
786
859
oid_with_credentials_authz : Tuple [KeycloakOpenID , str , str ]
0 commit comments