@@ -23,44 +23,57 @@ defmodule Realtime.Tenants.AuthorizationTest do
23
23
]
24
24
test "authenticated user has expected policies" , context do
25
25
{ :ok , policies } =
26
- Authorization . get_read_authorizations (
27
- % Policies { } ,
28
- context . db_conn ,
29
- context . authorization_context
30
- )
26
+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
31
27
32
28
{ :ok , policies } =
33
- Authorization . get_write_authorizations (
34
- policies ,
35
- context . db_conn ,
36
- context . authorization_context
37
- )
29
+ Authorization . get_write_authorizations ( policies , context . db_conn , context . authorization_context )
38
30
39
31
assert % Policies {
40
32
broadcast: % BroadcastPolicies { read: true , write: true } ,
41
33
presence: % PresencePolicies { read: true , write: true }
42
34
} == policies
43
35
end
44
36
37
+ @ tag role: "authenticated" ,
38
+ policies: [ :authenticated_read_matching_user_sub ] ,
39
+ sub: "ccbdfd51-c5aa-4d61-8c17-647664466a26"
40
+ test "authenticated user sub is available" , context do
41
+ assert { :ok , % Policies { broadcast: % BroadcastPolicies { read: true , write: nil } } } =
42
+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
43
+
44
+ authorization_context = % { context . authorization_context | sub: "135f6d25-5840-4266-a8ca-b9a45960e424" }
45
+
46
+ assert { :ok , % Policies { broadcast: % BroadcastPolicies { read: false , write: nil } } } =
47
+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , authorization_context )
48
+ end
49
+
50
+ @ tag role: "authenticated" ,
51
+ policies: [ :read_matching_user_role ]
52
+ test "user role is exposed" , context do
53
+ # policy role is checking for "authenticated"
54
+ # set_config is setting request.jwt.claim.role to authenticated as well
55
+ assert { :ok , % Policies { broadcast: % BroadcastPolicies { read: true , write: nil } } } =
56
+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
57
+
58
+ authorization_context = % { context . authorization_context | role: "anon" }
59
+
60
+ # policy role is checking for "authenticated"
61
+ # set_config is setting request.jwt.claim.role to anon
62
+ assert { :ok , % Policies { broadcast: % BroadcastPolicies { read: false , write: nil } } } =
63
+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , authorization_context )
64
+ end
65
+
45
66
@ tag role: "anon" ,
46
67
policies: [
47
68
:authenticated_read_broadcast_and_presence ,
48
69
:authenticated_write_broadcast_and_presence
49
70
]
50
71
test "anon user has no policies" , context do
51
72
{ :ok , policies } =
52
- Authorization . get_read_authorizations (
53
- % Policies { } ,
54
- context . db_conn ,
55
- context . authorization_context
56
- )
73
+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
57
74
58
75
{ :ok , policies } =
59
- Authorization . get_write_authorizations (
60
- policies ,
61
- context . db_conn ,
62
- context . authorization_context
63
- )
76
+ Authorization . get_write_authorizations ( policies , context . db_conn , context . authorization_context )
64
77
65
78
assert % Policies {
66
79
broadcast: % BroadcastPolicies { read: false , write: false } ,
@@ -119,39 +132,19 @@ defmodule Realtime.Tenants.AuthorizationTest do
119
132
policies: [ :broken_read_presence , :broken_write_presence ]
120
133
test "broken RLS policy sets policies to false and shows error to user" , context do
121
134
assert { :error , :rls_policy_error , % Postgrex.Error { } } =
122
- Authorization . get_read_authorizations (
123
- % Policies { } ,
124
- context . db_conn ,
125
- context . authorization_context
126
- )
135
+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
127
136
128
137
assert { :error , :rls_policy_error , % Postgrex.Error { } } =
129
- Authorization . get_write_authorizations (
130
- % Policies { } ,
131
- context . db_conn ,
132
- context . authorization_context
133
- )
138
+ Authorization . get_write_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
134
139
135
140
assert { :error , :rls_policy_error , % Postgrex.Error { } } =
136
- Authorization . get_read_authorizations (
137
- % Policies { } ,
138
- context . db_conn ,
139
- context . authorization_context
140
- )
141
+ Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
141
142
142
143
assert { :error , :rls_policy_error , % Postgrex.Error { } } =
143
- Authorization . get_write_authorizations (
144
- % Policies { } ,
145
- context . db_conn ,
146
- context . authorization_context
147
- )
144
+ Authorization . get_write_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
148
145
149
146
assert { :error , :rls_policy_error , % Postgrex.Error { } } =
150
- Authorization . get_write_authorizations (
151
- % Policies { } ,
152
- context . db_conn ,
153
- context . authorization_context
154
- )
147
+ Authorization . get_write_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
155
148
end
156
149
end
157
150
@@ -162,19 +155,8 @@ defmodule Realtime.Tenants.AuthorizationTest do
162
155
:authenticated_write_broadcast_and_presence
163
156
]
164
157
test "authenticated user has expected policies" , context do
165
- { :ok , _ } =
166
- Authorization . get_read_authorizations (
167
- % Policies { } ,
168
- context . db_conn ,
169
- context . authorization_context
170
- )
171
-
172
- { :ok , _ } =
173
- Authorization . get_write_authorizations (
174
- % Policies { } ,
175
- context . db_conn ,
176
- context . authorization_context
177
- )
158
+ { :ok , _ } = Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
159
+ { :ok , _ } = Authorization . get_write_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
178
160
179
161
{ :ok , db_conn } = Database . connect ( context . tenant , "realtime_test" )
180
162
assert { :ok , [ ] } = Repo . all ( db_conn , Message , Message )
@@ -205,19 +187,8 @@ defmodule Realtime.Tenants.AuthorizationTest do
205
187
% { }
206
188
)
207
189
208
- { :ok , _ } =
209
- Authorization . get_read_authorizations (
210
- % Policies { } ,
211
- context . db_conn ,
212
- context . authorization_context
213
- )
214
-
215
- { :ok , _ } =
216
- Authorization . get_write_authorizations (
217
- % Policies { } ,
218
- context . db_conn ,
219
- context . authorization_context
220
- )
190
+ { :ok , _ } = Authorization . get_read_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
191
+ { :ok , _ } = Authorization . get_write_authorizations ( % Policies { } , context . db_conn , context . authorization_context )
221
192
222
193
external_id = context . authorization_context . tenant_id
223
194
@@ -232,19 +203,20 @@ defmodule Realtime.Tenants.AuthorizationTest do
232
203
def rls_context ( context ) do
233
204
tenant = Containers . checkout_tenant ( run_migrations: true )
234
205
{ :ok , db_conn } = Database . connect ( tenant , "realtime_test" , :stop )
235
- topic = random_string ( )
206
+ topic = context [ :topic ] || random_string ( )
236
207
237
- create_rls_policies ( db_conn , context . policies , % { topic: topic } )
208
+ create_rls_policies ( db_conn , context . policies , % { topic: topic , sub: context [ :sub ] , role: context . role } )
238
209
239
- claims = % { sub: random_string ( ) , role: context . role , exp: Joken . current_time ( ) + 1_000 }
210
+ claims = % { " sub" => context [ :sub ] || random_string ( ) , " role" => context . role , " exp" => Joken . current_time ( ) + 1_000 }
240
211
241
212
authorization_context =
242
213
Authorization . build_authorization_params ( % {
243
214
tenant_id: tenant . external_id ,
244
215
topic: topic ,
245
216
claims: claims ,
246
217
headers: [ { "header-1" , "value-1" } ] ,
247
- role: claims . role
218
+ role: claims [ "role" ] ,
219
+ sub: claims [ "sub" ]
248
220
} )
249
221
250
222
Realtime.Tenants.Migrations . create_partitions ( db_conn )
0 commit comments