|
| 1 | +%% This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +%% License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +%% file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +%% |
| 5 | +%% Copyright (c) 2007-2024 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | +-module(oauth2_client_test_util). |
| 8 | + |
| 9 | +-compile(export_all). |
| 10 | + |
| 11 | +-define(DEFAULT_EXPIRATION_IN_SECONDS, 2). |
| 12 | + |
| 13 | +%% |
| 14 | +%% API |
| 15 | +%% |
| 16 | + |
| 17 | +sign_token_hs(Token, #{<<"kid">> := TokenKey} = Jwk) -> |
| 18 | + sign_token_hs(Token, Jwk, TokenKey). |
| 19 | + |
| 20 | +sign_token_hs(Token, Jwk, TokenKey) -> |
| 21 | + Jws = #{ |
| 22 | + <<"alg">> => <<"HS256">>, |
| 23 | + <<"kid">> => TokenKey |
| 24 | + }, |
| 25 | + sign_token(Token, Jwk, Jws). |
| 26 | + |
| 27 | +sign_token_rsa(Token, Jwk, TokenKey) -> |
| 28 | + Jws = #{ |
| 29 | + <<"alg">> => <<"RS256">>, |
| 30 | + <<"kid">> => TokenKey |
| 31 | + }, |
| 32 | + sign_token(Token, Jwk, Jws). |
| 33 | + |
| 34 | +sign_token_no_kid(Token, Jwk) -> |
| 35 | + Signed = jose_jwt:sign(Jwk, Token), |
| 36 | + jose_jws:compact(Signed). |
| 37 | + |
| 38 | +sign_token(Token, Jwk, Jws) -> |
| 39 | + Signed = jose_jwt:sign(Jwk, Jws, Token), |
| 40 | + jose_jws:compact(Signed). |
| 41 | + |
| 42 | +fixture_jwk() -> |
| 43 | + fixture_jwk(<<"token-key">>). |
| 44 | + |
| 45 | +fixture_jwk(TokenKey) -> |
| 46 | + fixture_jwk(TokenKey, <<"dG9rZW5rZXk">>). |
| 47 | + |
| 48 | +fixture_jwk(TokenKey, K) -> |
| 49 | + #{<<"alg">> => <<"HS256">>, |
| 50 | + <<"k">> => K, |
| 51 | + <<"kid">> => TokenKey, |
| 52 | + <<"kty">> => <<"oct">>, |
| 53 | + <<"use">> => <<"sig">>, |
| 54 | + <<"value">> => TokenKey}. |
| 55 | + |
| 56 | +full_permission_scopes() -> |
| 57 | + [<<"rabbitmq.configure:*/*">>, |
| 58 | + <<"rabbitmq.write:*/*">>, |
| 59 | + <<"rabbitmq.read:*/*">>]. |
| 60 | + |
| 61 | +expirable_token() -> |
| 62 | + expirable_token(?DEFAULT_EXPIRATION_IN_SECONDS). |
| 63 | + |
| 64 | +expirable_token(Seconds) -> |
| 65 | + TokenPayload = fixture_token(), |
| 66 | + %% expiration is a timestamp with precision in seconds |
| 67 | + TokenPayload#{<<"exp">> := os:system_time(seconds) + Seconds}. |
| 68 | + |
| 69 | +expirable_token_with_expiration_time(ExpiresIn) -> |
| 70 | + TokenPayload = fixture_token(), |
| 71 | + %% expiration is a timestamp with precision in seconds |
| 72 | + TokenPayload#{<<"exp">> := ExpiresIn}. |
| 73 | + |
| 74 | +expired_token() -> |
| 75 | + expired_token_with_scopes(full_permission_scopes()). |
| 76 | + |
| 77 | +expired_token_with_scopes(Scopes) -> |
| 78 | + token_with_scopes_and_expiration(Scopes, seconds_in_the_past(10)). |
| 79 | + |
| 80 | +fixture_token_with_scopes(Scopes) -> |
| 81 | + token_with_scopes_and_expiration(Scopes, default_expiration_moment()). |
| 82 | + |
| 83 | +token_with_scopes_and_expiration(Scopes, Expiration) -> |
| 84 | + %% expiration is a timestamp with precision in seconds |
| 85 | + #{<<"exp">> => Expiration, |
| 86 | + <<"iss">> => <<"unit_test">>, |
| 87 | + <<"foo">> => <<"bar">>, |
| 88 | + <<"aud">> => [<<"rabbitmq">>], |
| 89 | + <<"scope">> => Scopes}. |
| 90 | + |
| 91 | +token_without_scopes() -> |
| 92 | + %% expiration is a timestamp with precision in seconds |
| 93 | + #{ |
| 94 | + <<"iss">> => <<"unit_test">>, |
| 95 | + <<"foo">> => <<"bar">>, |
| 96 | + <<"aud">> => [<<"rabbitmq">>] |
| 97 | + }. |
| 98 | + |
| 99 | +fixture_token() -> |
| 100 | + fixture_token([]). |
| 101 | + |
| 102 | +token_with_sub(TokenFixture, Sub) -> |
| 103 | + maps:put(<<"sub">>, Sub, TokenFixture). |
| 104 | +token_with_scopes(TokenFixture, Scopes) -> |
| 105 | + maps:put(<<"scope">>, Scopes, TokenFixture). |
| 106 | + |
| 107 | +fixture_token(ExtraScopes) -> |
| 108 | + Scopes = [<<"rabbitmq.configure:vhost/foo">>, |
| 109 | + <<"rabbitmq.write:vhost/foo">>, |
| 110 | + <<"rabbitmq.read:vhost/foo">>, |
| 111 | + <<"rabbitmq.read:vhost/bar">>, |
| 112 | + <<"rabbitmq.read:vhost/bar/%23%2Ffoo">>] ++ ExtraScopes, |
| 113 | + fixture_token_with_scopes(Scopes). |
| 114 | + |
| 115 | +fixture_token_with_full_permissions() -> |
| 116 | + fixture_token_with_scopes(full_permission_scopes()). |
| 117 | + |
| 118 | +plain_token_without_scopes_and_aud() -> |
| 119 | + %% expiration is a timestamp with precision in seconds |
| 120 | + #{<<"exp">> => default_expiration_moment(), |
| 121 | + <<"iss">> => <<"unit_test">>, |
| 122 | + <<"foo">> => <<"bar">>}. |
| 123 | + |
| 124 | +token_with_scope_alias_in_scope_field(Value) -> |
| 125 | + %% expiration is a timestamp with precision in seconds |
| 126 | + #{<<"exp">> => default_expiration_moment(), |
| 127 | + <<"iss">> => <<"unit_test">>, |
| 128 | + <<"foo">> => <<"bar">>, |
| 129 | + <<"aud">> => [<<"rabbitmq">>], |
| 130 | + <<"scope">> => Value}. |
| 131 | + |
| 132 | +token_with_scope_alias_in_claim_field(Claims, Scopes) -> |
| 133 | + %% expiration is a timestamp with precision in seconds |
| 134 | + #{<<"exp">> => default_expiration_moment(), |
| 135 | + <<"iss">> => <<"unit_test">>, |
| 136 | + <<"foo">> => <<"bar">>, |
| 137 | + <<"aud">> => [<<"rabbitmq">>], |
| 138 | + <<"scope">> => Scopes, |
| 139 | + <<"claims">> => Claims}. |
| 140 | + |
| 141 | +seconds_in_the_future() -> |
| 142 | + seconds_in_the_future(30). |
| 143 | + |
| 144 | +seconds_in_the_future(N) -> |
| 145 | + os:system_time(seconds) + N. |
| 146 | + |
| 147 | +seconds_in_the_past() -> |
| 148 | + seconds_in_the_past(10). |
| 149 | + |
| 150 | +seconds_in_the_past(N) -> |
| 151 | + os:system_time(seconds) - N. |
| 152 | + |
| 153 | +default_expiration_moment() -> |
| 154 | + seconds_in_the_future(30). |
0 commit comments