@@ -64,8 +64,8 @@ defmodule RealtimeWeb.RealtimeChannel do
64
64
:ok <- limit_channels ( socket ) ,
65
65
:ok <- limit_max_users ( socket . assigns ) ,
66
66
{ :ok , claims , confirm_token_ref , access_token , _ } <- confirm_token ( socket ) ,
67
- { :ok , db_conn } <- Connect . lookup_or_start_connection ( tenant_id ) ,
68
67
socket = assign_authorization_context ( socket , sub_topic , access_token , claims ) ,
68
+ { :ok , db_conn } <- Connect . lookup_or_start_connection ( tenant_id ) ,
69
69
{ :ok , socket } <- maybe_assign_policies ( sub_topic , db_conn , socket ) do
70
70
tenant_topic = Tenants . tenant_topic ( tenant_id , sub_topic , ! socket . assigns . private? )
71
71
@@ -424,19 +424,19 @@ defmodule RealtimeWeb.RealtimeChannel do
424
424
def handle_in ( "access_token" , % { "access_token" => refresh_token } , socket ) when is_binary ( refresh_token ) do
425
425
% {
426
426
assigns: % {
427
- access_token: access_token ,
427
+ tenant: tenant_id ,
428
428
pg_sub_ref: pg_sub_ref ,
429
429
channel_name: channel_name ,
430
- pg_change_params: pg_change_params ,
431
- tenant: tenant
430
+ pg_change_params: pg_change_params
432
431
}
433
432
} = socket
434
433
435
- socket = assign ( socket , :access_token , refresh_token )
434
+ # Update token and reset policies
435
+ socket = assign ( socket , % { access_token: refresh_token , policies: nil } )
436
436
437
437
with { :ok , claims , confirm_token_ref , _ , socket } <- confirm_token ( socket ) ,
438
- socket = assign_authorization_context ( socket , channel_name , access_token , claims ) ,
439
- { :ok , db_conn } <- Connect . lookup_or_start_connection ( tenant ) ,
438
+ socket = assign_authorization_context ( socket , channel_name , refresh_token , claims ) ,
439
+ { :ok , db_conn } <- Connect . lookup_or_start_connection ( tenant_id ) ,
440
440
{ :ok , socket } <- maybe_assign_policies ( channel_name , db_conn , socket ) do
441
441
Helpers . cancel_timer ( pg_sub_ref )
442
442
pg_change_params = Enum . map ( pg_change_params , & Map . put ( & 1 , :claims , claims ) )
@@ -455,23 +455,23 @@ defmodule RealtimeWeb.RealtimeChannel do
455
455
456
456
{ :noreply , assign ( socket , assigns ) }
457
457
else
458
- { :error , :unauthorized , msg } ->
459
- shutdown_response ( socket , msg )
460
-
461
- { :error , :expired_token , msg } ->
458
+ { :error , reason , msg } when reason in ~w( unauthorized expired_token token_malformed) a ->
462
459
shutdown_response ( socket , msg )
463
460
464
461
{ :error , :missing_claims } ->
465
462
shutdown_response ( socket , "Fields `role` and `exp` are required in JWT" )
466
463
467
- { :error , :token_malformed , msg } ->
468
- shutdown_response ( socket , msg )
469
-
470
464
{ :error , :unable_to_set_policies , _msg } ->
471
465
shutdown_response ( socket , "Realtime was unable to connect to the project database" )
472
466
473
467
{ :error , error } ->
474
468
shutdown_response ( socket , inspect ( error ) )
469
+
470
+ { :error , :rpc_error , :timeout } ->
471
+ shutdown_response ( socket , "Node request timeout" )
472
+
473
+ { :error , :rpc_error , reason } ->
474
+ shutdown_response ( socket , "RPC call error: " <> inspect ( reason ) )
475
475
end
476
476
end
477
477
@@ -595,22 +595,14 @@ defmodule RealtimeWeb.RealtimeChannel do
595
595
end
596
596
597
597
defp confirm_token ( % { assigns: assigns } = socket ) do
598
- % {
599
- jwt_secret: jwt_secret ,
600
- access_token: access_token ,
601
- tenant: tenant_id
602
- } = assigns
598
+ % { jwt_secret: jwt_secret , access_token: access_token } = assigns
603
599
604
- topic = Map . get ( assigns , :topic )
605
- socket = Map . put ( socket , :policies , nil )
606
600
jwt_jwks = Map . get ( assigns , :jwt_jwks )
607
601
608
602
with jwt_secret_dec <- Crypto . decrypt! ( jwt_secret ) ,
609
603
{ :ok , % { "exp" => exp } = claims } when is_integer ( exp ) <-
610
604
ChannelsAuthorization . authorize_conn ( access_token , jwt_secret_dec , jwt_jwks ) ,
611
- exp_diff when exp_diff > 0 <- exp - Joken . current_time ( ) ,
612
- { :ok , db_conn } <- Connect . lookup_or_start_connection ( tenant_id ) ,
613
- { :ok , socket } <- maybe_assign_policies ( topic , db_conn , socket ) do
605
+ exp_diff when exp_diff > 0 <- exp - Joken . current_time ( ) do
614
606
if ref = assigns [ :confirm_token_ref ] , do: Helpers . cancel_timer ( ref )
615
607
616
608
interval = min ( @ confirm_token_ms_interval , exp_diff * 1000 )
@@ -761,7 +753,7 @@ defmodule RealtimeWeb.RealtimeChannel do
761
753
end
762
754
763
755
defp maybe_assign_policies ( topic , db_conn , % { assigns: % { private?: true } } = socket )
764
- when not is_nil ( topic ) and not is_nil ( db_conn ) do
756
+ when not is_nil ( topic ) do
765
757
authorization_context = socket . assigns . authorization_context
766
758
policies = socket . assigns . policies || % Policies { }
767
759
@@ -785,9 +777,7 @@ defmodule RealtimeWeb.RealtimeChannel do
785
777
end
786
778
end
787
779
788
- defp maybe_assign_policies ( _ , _ , socket ) do
789
- { :ok , assign ( socket , policies: nil ) }
790
- end
780
+ defp maybe_assign_policies ( _ , _ , socket ) , do: { :ok , assign ( socket , policies: nil ) }
791
781
792
782
defp only_private? ( tenant_id , % { assigns: % { private?: private? } } ) do
793
783
tenant = Tenants.Cache . get_tenant_by_external_id ( tenant_id )
0 commit comments