@@ -5375,32 +5375,32 @@ func FindWorkflowAppByName(ctx context.Context, appName string) ([]WorkflowApp,
53755375// Also validates that the clientID matches the org's configured SSO
53765376func FindUserBySSOIdentity (ctx context.Context , sub , clientID , orgID , email string ) (User , error ) {
53775377 var emptyUser User
5378-
5378+
53795379 // Check if Sub is empty - user hasn't connected SSO yet
53805380 if sub == "" {
53815381 return emptyUser , errors .New ("connect user account with SSO first" )
53825382 }
5383-
5383+
53845384 if clientID == "" || orgID == "" || email == "" {
53855385 return emptyUser , errors .New ("clientID, orgID, and email are all required" )
53865386 }
5387-
5387+
53885388 // Verify the clientID actually matches the org's SSO configuration
53895389 org , err := GetOrg (ctx , orgID )
53905390 if err != nil {
53915391 return emptyUser , fmt .Errorf ("failed to get org %s: %w" , orgID , err )
53925392 }
5393-
5393+
53945394 if org .SSOConfig .OpenIdClientId != clientID {
53955395 return emptyUser , fmt .Errorf ("clientID %s does not match org's configured SSO client ID %s" , clientID , org .SSOConfig .OpenIdClientId )
53965396 }
5397-
5397+
53985398 // Normalize email for comparison
53995399 normalizedEmail := strings .ToLower (strings .TrimSpace (email ))
5400-
5400+
54015401 nameKey := "Users"
54025402 var users []User
5403-
5403+
54045404 if project .DbType == "opensearch" {
54055405 // OpenSearch query to find users with matching SSO info
54065406 var buf bytes.Buffer
@@ -5444,11 +5444,11 @@ func FindUserBySSOIdentity(ctx context.Context, sub, clientID, orgID, email stri
54445444 },
54455445 },
54465446 }
5447-
5447+
54485448 if err := json .NewEncoder (& buf ).Encode (query ); err != nil {
54495449 return emptyUser , fmt .Errorf ("failed to encode opensearch query: %w" , err )
54505450 }
5451-
5451+
54525452 resp , err := project .Es .Search (ctx , & opensearchapi.SearchReq {
54535453 Indices : []string {strings .ToLower (GetESIndexPrefix (nameKey ))},
54545454 Body : & buf ,
@@ -5459,23 +5459,23 @@ func FindUserBySSOIdentity(ctx context.Context, sub, clientID, orgID, email stri
54595459 if err != nil {
54605460 return emptyUser , fmt .Errorf ("opensearch query failed: %w" , err )
54615461 }
5462-
5462+
54635463 res := resp .Inspect ().Response
54645464 defer res .Body .Close ()
54655465 if res .StatusCode != 200 && res .StatusCode != 201 {
54665466 return emptyUser , fmt .Errorf ("opensearch error response: %d" , res .StatusCode )
54675467 }
5468-
5468+
54695469 var r map [string ]interface {}
54705470 if err := json .NewDecoder (res .Body ).Decode (& r ); err != nil {
54715471 return emptyUser , fmt .Errorf ("failed to parse opensearch response: %w" , err )
54725472 }
5473-
5473+
54745474 hits , ok := r ["hits" ].(map [string ]interface {})["hits" ].([]interface {})
54755475 if ! ok {
54765476 return emptyUser , errors .New ("no matching user found" )
54775477 }
5478-
5478+
54795479 for _ , hit := range hits {
54805480 if source , ok := hit .(map [string ]interface {})["_source" ]; ok {
54815481 data , _ := json .Marshal (source )
@@ -5493,32 +5493,32 @@ func FindUserBySSOIdentity(ctx context.Context, sub, clientID, orgID, email stri
54935493 if err != nil {
54945494 return emptyUser , fmt .Errorf ("datastore query failed: %w" , err )
54955495 }
5496-
5496+
54975497 // Filter users to find exact SSO match
54985498 var matchingUsers []User
54995499 for _ , user := range users {
55005500 for _ , ssoInfo := range user .SSOInfos {
5501- if ssoInfo .Sub == sub &&
5502- ssoInfo .ClientID == clientID &&
5503- ssoInfo .OrgID == orgID {
5501+ if ssoInfo .Sub == sub &&
5502+ ssoInfo .ClientID == clientID &&
5503+ ssoInfo .OrgID == orgID {
55045504 matchingUsers = append (matchingUsers , user )
55055505 break
55065506 }
55075507 }
55085508 }
55095509 users = matchingUsers
55105510 }
5511-
5511+
55125512 if len (users ) == 0 {
55135513 return emptyUser , fmt .Errorf ("no user found with Sub=%s, ClientID=%s, OrgID=%s, Email=%s" , sub , clientID , orgID , normalizedEmail )
55145514 }
5515-
5515+
55165516 if len (users ) > 1 {
5517- log .Printf ("[CRITICAL] Multiple users found with same SSO identity: Sub=%s, ClientID=%s, OrgID=%s, Email=%s" ,
5517+ log .Printf ("[CRITICAL] Multiple users found with same SSO identity: Sub=%s, ClientID=%s, OrgID=%s, Email=%s" ,
55185518 sub , clientID , orgID , normalizedEmail )
55195519 return emptyUser , errors .New ("multiple users found with same SSO identity - data integrity issue" )
55205520 }
5521-
5521+
55225522 return users [0 ], nil
55235523}
55245524
@@ -5816,6 +5816,7 @@ func GetUser(ctx context.Context, username string) (*User, error) {
58165816}
58175817
58185818func (u * User ) GetSSOInfo (orgID string ) (SSOInfo , bool ) {
5819+ log .Printf ("[DEBUG] Getting SSOInfo for user %s and org %s" , u .Id , orgID )
58195820 for _ , sso := range u .SSOInfos {
58205821 if sso .OrgID == orgID {
58215822 return sso , true
0 commit comments