Skip to content

Commit f652d5c

Browse files
Add function that detects if token is JWT
1 parent 9f1c626 commit f652d5c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

deps/rabbitmq_auth_backend_oauth2/src/uaa_jwt_jwt.erl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
%%
77
-module(uaa_jwt_jwt).
88

9-
-export([decode_and_verify/3, get_key_id/1, get_aud/1]).
9+
-export([decode_and_verify/3, get_key_id/1, get_aud/1, is_jwt_token/1]).
1010

1111
-include_lib("jose/include/jose_jwt.hrl").
1212
-include_lib("jose/include/jose_jws.hrl").
@@ -42,3 +42,9 @@ get_aud(Token) ->
4242
catch Type:Err:Stacktrace ->
4343
{error, {invalid_token, Type, Err, Stacktrace}}
4444
end.
45+
46+
is_jwt_token(Token) ->
47+
case binary:split(Token, <<".">>, [global]) of
48+
[_, _, _] -> true;
49+
_ -> false
50+
end.

deps/rabbitmq_auth_backend_oauth2/test/jwks_SUITE.erl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ all() ->
4141

4242
groups() ->
4343
[{happy_path, [], [
44+
test_is_jwt_token,
45+
test_is_not_jwt_token,
4446
test_successful_connection_with_a_full_permission_token_and_all_defaults,
4547
test_successful_connection_with_a_full_permission_token_and_explicitly_configured_vhost,
4648
test_successful_connection_with_simple_strings_for_aud_and_scope,
@@ -685,6 +687,13 @@ test_successful_connection_with_a_full_permission_token_and_all_defaults(Config)
685687
{_Algo, Token} = get_config(Config, fixture_jwt),
686688
verify_queue_declare_with_token(Config, Token).
687689

690+
test_is_jwt_token(Config) ->
691+
{_Algo, Token} = get_config(Config, fixture_jwt),
692+
?assertEqual(true, uaa_jwt_jwt:is_jwt_token(Token)).
693+
694+
test_is_not_jwt_token(_) ->
695+
?assertEqual(false, uaa_jwt_jwt:is_jwt_token(<<"some opaque token">>)).
696+
688697
verify_queue_declare_with_token(Config, Token) ->
689698
Conn = open_unmanaged_connection(Config, 0, <<"username">>, Token),
690699
{ok, Ch} = amqp_connection:open_channel(Conn),

0 commit comments

Comments
 (0)