@@ -83,39 +83,38 @@ defmodule Supavisor.ClientHandler.Auth do
8383 else: { :error , :wrong_password }
8484 end
8585
86- def validate_credentials ( :auth_query_jit , secrets , password , ip ) do
86+ def validate_credentials ( :auth_query_jit , tenant , secrets , password , ip ) do
8787 # check if incomming password looks like PAT or a JWT
8888 # otherwise handle as password,
8989 secret = secrets . ( )
9090
9191 if Helpers . token_matches? ( password ) do
92- Logger . debug ( "Looks like a PAT/JWT - #{ inspect ( secret . jit_api_url ) } " )
9392 rhost = ip |> :inet . ntoa ( ) |> to_string ( )
9493
95- case Helpers . check_user_has_jit_role ( secret . jit_api_url , password , secret . user , rhost ) do
94+ case Helpers . check_user_has_jit_role ( tenant . jit_api_url , password , secret . user , rhost ) do
9695 { :ok , true } ->
9796 # set a fake client_key incase upstream switches away from pam mid auth
98- { :ok , :crypto . hash ( :sha256 , password ) }
97+ { :ok , :crypto . hash ( :sha256 , password ) , :auth_query_jit }
9998
10099 { :ok , false } ->
101100 Logger . debug ( "User token is valid but can't assume this role" )
102- { :error , :wrong_password }
101+ { :error , :wrong_password , :auth_query_jit }
103102
104103 { :error , :unauthorized_or_forbidden } ->
105- { :error , :wrong_password }
104+ { :error , :wrong_password , :auth_query_jit }
106105
107106 { :error , _ } ->
108107 Logger . debug ( "Unexpected error while calling API" )
109- { :error , :wrong_password }
108+ { :error , :wrong_password , :auth_query_jit }
110109 end
111110 else
112111 # match against the scram-sha-256 / md5 we have from auth_query
113112 case secret . digest do
114113 :md5 ->
115114 if Helpers . md5 ( [ password , secret . user ] ) == secret . secret do
116- { :ok , nil }
115+ { :ok , nil , :auth_query_md5 }
117116 else
118- { :error , :wrong_password }
117+ { :error , :wrong_password , :auth_query_md5 }
119118 end
120119
121120 _ ->
@@ -128,8 +127,8 @@ defmodule Supavisor.ClientHandler.Auth do
128127 stored_key = :crypto . hash ( :sha256 , client_key )
129128
130129 if :crypto . hash_equals ( stored_key , secret . stored_key ) ,
131- do: { :ok , client_key } ,
132- else: { :error , :wrong_password }
130+ do: { :ok , client_key , :auth_query } ,
131+ else: { :error , :wrong_password , :auth_query }
133132 end
134133 end
135134 end
@@ -248,6 +247,9 @@ defmodule Supavisor.ClientHandler.Auth do
248247
249248 def parse_auth_message ( bin , _scram_method ) do
250249 case Server . decode_pkt ( bin ) do
250+ { :ok , % { tag: :password_message , payload: { :cleartext_password , cls_password } } , _ } ->
251+ { :ok , cls_password }
252+
251253 { :ok ,
252254 % {
253255 tag: :password_message ,
@@ -284,8 +286,19 @@ defmodule Supavisor.ClientHandler.Auth do
284286 }
285287 end
286288
289+ @ spec create_auth_context ( auth_method ( ) , function ( ) , map ( ) ) :: map ( )
290+ def create_auth_context ( :auth_query_jit , secrets , info ) do
291+ % {
292+ method: :auth_query_jit ,
293+ secrets: secrets ,
294+ info: info ,
295+ cls_password: nil ,
296+ signatures: nil
297+ }
298+ end
299+
287300 def create_auth_context ( method , secrets , info )
288- when method in [ :password , :auth_query , :auth_query_jit ] do
301+ when method in [ :password , :auth_query ] do
289302 % {
290303 method: method ,
291304 secrets: secrets ,
@@ -302,6 +315,14 @@ defmodule Supavisor.ClientHandler.Auth do
302315 % { auth_context | signatures: signatures }
303316 end
304317
318+ @ doc """
319+ Updates authentication context with new jit information after first exchange.
320+ """
321+ @ spec update_auth_context_with_jit ( map ( ) , map ( ) ) :: map ( )
322+ def update_auth_context_with_jit ( auth_context , cls_password ) do
323+ % { auth_context | cls_password: cls_password }
324+ end
325+
305326 ## Success Response Preparation
306327
307328 @ doc """
@@ -327,6 +348,15 @@ defmodule Supavisor.ClientHandler.Auth do
327348 fn -> Map . put ( secrets_fn . ( ) , :client_key , client_key ) end
328349 end
329350
351+ def prepare_final_secrets ( secrets_fn , client_key , password ) do
352+ fn ->
353+ Map . merge ( secrets_fn . ( ) , % {
354+ client_key: client_key ,
355+ cls_password: password
356+ } )
357+ end
358+ end
359+
330360 ## Private Helpers
331361
332362 @ spec fetch_secrets_from_database ( Supavisor . id ( ) , map ( ) , String . t ( ) ) ::
@@ -365,7 +395,7 @@ defmodule Supavisor.ClientHandler.Auth do
365395 with { :ok , secret } <- Helpers . get_user_secret ( conn , tenant . auth_query , db_user ) do
366396 auth_type =
367397 case { tenant . use_jit , secret } do
368- { true , % PasswordSecrets { } } -> :auth_query_jit
398+ { true , _ } -> :auth_query_jit
369399 { _ , % MD5Secrets { } } -> :auth_query_md5
370400 { _ , % SASLSecrets { } } -> :auth_query
371401 end
0 commit comments