test: create jwt authenticator with bad public key

This commit is contained in:
JimMoen 2024-07-08 16:34:24 +08:00
parent f76444fbf8
commit ae3b8fe146
No known key found for this signature in database
2 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,3 @@
-----BEGIN PUBLIC KEY-----
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-----END PUBLIC KEY-----

View File

@ -178,6 +178,7 @@ t_public_key(_) ->
from => password,
acl_claim_name => <<"acl">>,
use_jwks => false,
enable => true,
algorithm => 'public-key',
public_key => PublicKey,
verify_claims => [],
@ -199,6 +200,51 @@ t_public_key(_) ->
?assertEqual(ok, emqx_authn_jwt:destroy(State)),
ok.
t_bad_public_keys(_) ->
BaseConfig = #{
mechanism => jwt,
from => password,
acl_claim_name => <<"acl">>,
use_jwks => false,
algorithm => 'public-key',
verify_claims => [],
disconnect_after_expire => false
},
%% try create with invalid public key
?assertMatch(
{error, invalid_public_key},
emqx_authn_jwt:create(?AUTHN_ID, BaseConfig#{
enable => true,
public_key => <<"bad_public_key">>
})
),
%% no such file
?assertMatch(
{error, invalid_public_key},
emqx_authn_jwt:create(?AUTHN_ID, BaseConfig#{
enable => true,
public_key => data_file("bad_flie_path.pem")
})
),
%% bad public key file content
?assertMatch(
{error, invalid_public_key},
emqx_authn_jwt:create(?AUTHN_ID, BaseConfig#{
enable => true,
public_key => data_file("bad_public_key_file.pem")
})
),
%% assume jwk authenticator is disabled
{ok, State} =
emqx_authn_jwt:create(?AUTHN_ID, BaseConfig#{public_key => <<"bad_public_key">>}),
?assertEqual(ok, emqx_authn_jwt:destroy(State)),
ok.
t_jwt_in_username(_) ->
Secret = <<"abcdef">>,
Config = #{