@@ -16,6 +16,17 @@ import (
1616 "github.com/supabase/auth/internal/utilities"
1717)
1818
19+ func isPostgresUniqueViolation (err error ) bool {
20+ errToCheck := err
21+ if httpErr , ok := err .(* apierrors.HTTPError ); ok && httpErr .InternalError != nil {
22+ errToCheck = httpErr .InternalError
23+ }
24+ if pgErr := utilities .NewPostgresError (errToCheck ); pgErr != nil && pgErr .IsUniqueConstraintViolated () {
25+ return true
26+ }
27+ return false
28+ }
29+
1930func (a * API ) requireSCIMAuthentication (w http.ResponseWriter , r * http.Request ) (context.Context , error ) {
2031 ctx := r .Context ()
2132 db := a .db .WithContext (ctx )
@@ -418,7 +429,7 @@ func (a *API) scimReplaceUser(w http.ResponseWriter, r *http.Request) error {
418429 user .Identities [i ].IdentityData ["external_id" ] = params .ExternalID
419430 user .Identities [i ].IdentityData ["sub" ] = params .ExternalID
420431 if err := tx .UpdateOnly (& user .Identities [i ], "provider_id" , "identity_data" ); err != nil {
421- if models . IsUniqueConstraintViolatedError (err ) {
432+ if isPostgresUniqueViolation (err ) {
422433 return apierrors .NewSCIMConflictError ("externalId already exists" , "uniqueness" )
423434 }
424435 return apierrors .NewInternalServerError ("Error updating identity" ).WithInternalError (err )
@@ -568,7 +579,7 @@ func (a *API) applySCIMUserPatch(tx *storage.Connection, user *models.User, op S
568579 user .Identities [i ].IdentityData ["external_id" ] = externalID
569580 user .Identities [i ].IdentityData ["sub" ] = externalID
570581 if err := tx .UpdateOnly (& user .Identities [i ], "provider_id" , "identity_data" ); err != nil {
571- if models . IsUniqueConstraintViolatedError (err ) {
582+ if isPostgresUniqueViolation (err ) {
572583 return apierrors .NewSCIMConflictError ("externalId already exists" , "uniqueness" )
573584 }
574585 return apierrors .NewInternalServerError ("Error updating identity" ).WithInternalError (err )
@@ -669,7 +680,7 @@ func (a *API) applySCIMUserPatch(tx *storage.Connection, user *models.User, op S
669680 user .Identities [i ].IdentityData ["external_id" ] = externalID
670681 user .Identities [i ].IdentityData ["sub" ] = externalID
671682 if err := tx .UpdateOnly (& user .Identities [i ], "provider_id" , "identity_data" ); err != nil {
672- if models . IsUniqueConstraintViolatedError (err ) {
683+ if isPostgresUniqueViolation (err ) {
673684 return apierrors .NewSCIMConflictError ("externalId already exists" , "uniqueness" )
674685 }
675686 return apierrors .NewInternalServerError ("Error updating identity" ).WithInternalError (err )
@@ -802,7 +813,7 @@ func (a *API) applySCIMUserPatch(tx *storage.Connection, user *models.User, op S
802813 user .Identities [i ].IdentityData ["external_id" ] = externalID
803814 user .Identities [i ].IdentityData ["sub" ] = externalID
804815 if err := tx .UpdateOnly (& user .Identities [i ], "provider_id" , "identity_data" ); err != nil {
805- if models . IsUniqueConstraintViolatedError (err ) {
816+ if isPostgresUniqueViolation (err ) {
806817 return apierrors .NewSCIMConflictError ("externalId already exists" , "uniqueness" )
807818 }
808819 return apierrors .NewInternalServerError ("Error updating identity" ).WithInternalError (err )
0 commit comments