|
| 1 | +-- Tests for the custom_access_token_hook that adds sso_required claim. |
| 2 | +create function tests.test_sso_access_token_hook() |
| 3 | +returns setof text as $$ |
| 4 | +declare |
| 5 | + provider_acme uuid = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'; |
| 6 | + provider_bigcorp uuid = 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb'; |
| 7 | + alice_id uuid = '11111111-1111-1111-1111-111111111111'; |
| 8 | + bob_id uuid = '22222222-2222-2222-2222-222222222222'; |
| 9 | + result jsonb; |
| 10 | +begin |
| 11 | + -- Setup: test users. |
| 12 | + insert into auth.users (id, email) values |
| 13 | + (alice_id, 'alice@example.com'), |
| 14 | + (bob_id, 'bob@example.com') |
| 15 | + on conflict do nothing; |
| 16 | + |
| 17 | + -- Setup: two SSO providers. |
| 18 | + insert into auth.sso_providers (id) values (provider_acme), (provider_bigcorp) |
| 19 | + on conflict do nothing; |
| 20 | + |
| 21 | + -- Tenants: acmeCo and bigcorpCo have SSO configured, openCo does not. |
| 22 | + delete from tenants; |
| 23 | + insert into tenants (tenant, sso_provider_id) values |
| 24 | + ('acmeCo/', provider_acme), |
| 25 | + ('bigcorpCo/', provider_bigcorp), |
| 26 | + ('openCo/', null); |
| 27 | + |
| 28 | + -- Alice has grants on all three, SSO identity for acmeCo only. |
| 29 | + delete from user_grants; |
| 30 | + insert into user_grants (user_id, object_role, capability) values |
| 31 | + (alice_id, 'acmeCo/', 'admin'), |
| 32 | + (alice_id, 'bigcorpCo/', 'read'), |
| 33 | + (alice_id, 'openCo/', 'admin'); |
| 34 | + |
| 35 | + delete from auth.identities where user_id = alice_id; |
| 36 | + insert into auth.identities (user_id, provider, provider_id, identity_data) values |
| 37 | + (alice_id, 'sso', provider_acme::text, '{}'::jsonb); |
| 38 | + |
| 39 | + -- Alice should get sso_required with bigcorpCo's provider (she lacks that identity). |
| 40 | + select public.custom_access_token_hook(jsonb_build_object( |
| 41 | + 'user_id', alice_id, |
| 42 | + 'claims', jsonb_build_object('sub', alice_id) |
| 43 | + )) into result; |
| 44 | + |
| 45 | + return next is( |
| 46 | + result->'claims'->'sso_required', |
| 47 | + jsonb_build_array(provider_bigcorp), |
| 48 | + 'Alice gets sso_required with bigcorpCo provider ID' |
| 49 | + ); |
| 50 | + |
| 51 | + -- Bob has grants on acmeCo and bigcorpCo, no SSO identities at all. |
| 52 | + insert into user_grants (user_id, object_role, capability) values |
| 53 | + (bob_id, 'acmeCo/', 'read'), |
| 54 | + (bob_id, 'bigcorpCo/', 'read'), |
| 55 | + (bob_id, 'openCo/', 'read'); |
| 56 | + |
| 57 | + delete from auth.identities where user_id = bob_id; |
| 58 | + |
| 59 | + select public.custom_access_token_hook(jsonb_build_object( |
| 60 | + 'user_id', bob_id, |
| 61 | + 'claims', jsonb_build_object('sub', bob_id) |
| 62 | + )) into result; |
| 63 | + |
| 64 | + -- Bob should get both provider IDs (order not guaranteed, so check containment). |
| 65 | + return next ok( |
| 66 | + result->'claims'->'sso_required' @> jsonb_build_array(provider_acme) |
| 67 | + and result->'claims'->'sso_required' @> jsonb_build_array(provider_bigcorp), |
| 68 | + 'Bob gets sso_required with both provider IDs' |
| 69 | + ); |
| 70 | + return next is( |
| 71 | + jsonb_array_length(result->'claims'->'sso_required'), |
| 72 | + 2, |
| 73 | + 'Bob has exactly 2 provider IDs in sso_required' |
| 74 | + ); |
| 75 | + |
| 76 | + -- Give Alice an SSO identity for bigcorpCo too — sso_required should disappear. |
| 77 | + insert into auth.identities (user_id, provider, provider_id, identity_data) values |
| 78 | + (alice_id, 'sso', provider_bigcorp::text, '{}'::jsonb); |
| 79 | + |
| 80 | + select public.custom_access_token_hook(jsonb_build_object( |
| 81 | + 'user_id', alice_id, |
| 82 | + 'claims', jsonb_build_object('sub', alice_id) |
| 83 | + )) into result; |
| 84 | + |
| 85 | + return next ok( |
| 86 | + result->'claims'->'sso_required' is null, |
| 87 | + 'Alice with both SSO identities has no sso_required claim' |
| 88 | + ); |
| 89 | + |
| 90 | + -- Clean up the extra identity. |
| 91 | + delete from auth.identities |
| 92 | + where user_id = alice_id |
| 93 | + and provider_id = provider_bigcorp::text; |
| 94 | + |
| 95 | + -- User with only open-tenant grants: no sso_required. |
| 96 | + delete from user_grants where user_id = bob_id; |
| 97 | + insert into user_grants (user_id, object_role, capability) values |
| 98 | + (bob_id, 'openCo/', 'read'); |
| 99 | + |
| 100 | + select public.custom_access_token_hook(jsonb_build_object( |
| 101 | + 'user_id', bob_id, |
| 102 | + 'claims', jsonb_build_object('sub', bob_id) |
| 103 | + )) into result; |
| 104 | + |
| 105 | + return next ok( |
| 106 | + result->'claims'->'sso_required' is null, |
| 107 | + 'User with only open-tenant grants has no sso_required claim' |
| 108 | + ); |
| 109 | + |
| 110 | + -- SSO user with matching identity on their own tenant: no nudge. |
| 111 | + delete from user_grants where user_id = alice_id; |
| 112 | + insert into user_grants (user_id, object_role, capability) values |
| 113 | + (alice_id, 'acmeCo/', 'admin'); |
| 114 | + |
| 115 | + select public.custom_access_token_hook(jsonb_build_object( |
| 116 | + 'user_id', alice_id, |
| 117 | + 'claims', jsonb_build_object('sub', alice_id) |
| 118 | + )) into result; |
| 119 | + |
| 120 | + return next ok( |
| 121 | + result->'claims'->'sso_required' is null, |
| 122 | + 'SSO user with matching identity on own tenant is not nudged' |
| 123 | + ); |
| 124 | + |
| 125 | + return; |
| 126 | +end |
| 127 | +$$ language plpgsql; |
0 commit comments