Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add phone to user create in SSO (#7404)

boojack931963ae7ffc
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index a99155fe8d..6b2c45cf64 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -10,7 +10,6 @@ import (
"strings"
"time"
- "github.com/nyaruka/phonenumbers"
"github.com/pquerna/otp/totp"
"golang.org/x/crypto/bcrypt"
"golang.org/x/exp/slices"
@@ -134,7 +133,7 @@ func (s *AuthService) CreateUser(ctx context.Context, request *v1pb.CreateUserRe
firstEndUser := count == 0
if request.User.Phone != "" {
- if err := validatePhone(request.User.Phone); err != nil {
+ if err := common.ValidatePhone(request.User.Phone); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid phone %q, error: %v", request.User.Phone, err)
}
}
@@ -331,7 +330,7 @@ func (s *AuthService) UpdateUser(ctx context.Context, request *v1pb.UpdateUserRe
}
case "phone":
if request.User.Phone != "" {
- if err := validatePhone(request.User.Phone); err != nil {
+ if err := common.ValidatePhone(request.User.Phone); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid phone number %q, error: %v", request.User.Phone, err)
}
}
@@ -685,7 +684,6 @@ func (s *AuthService) getOrCreateUserWithIDP(ctx context.Context, request *v1pb.
}
var userInfo *storepb.IdentityProviderUserInfo
- var fieldMapping *storepb.FieldMapping
if idp.Type == storepb.IdentityProviderType_OAUTH2 {
oauth2Context := request.IdpContext.GetOauth2Context()
if oauth2Context == nil {
@@ -704,7 +702,6 @@ func (s *AuthService) getOrCreateUserWithIDP(ctx context.Context, request *v1pb.
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get user info: %v", err)
}
- fieldMapping = idp.Config.GetOauth2Config().FieldMapping
} else if idp.Type == storepb.IdentityProviderType_OIDC {
oauth2Context := request.IdpContext.GetOauth2Context()
if oauth2Context == nil {
@@ -736,7 +733,6 @@ func (s *AuthService) getOrCreateUserWithIDP(ctx context.Context, request *v1pb.
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get user info: %v", err)
}
- fieldMapping = idp.Config.GetOidcConfig().FieldMapping
} else {
return nil, status.Errorf(codes.InvalidArgument, "identity provider type %s not supported", idp.Type.String())
}
@@ -745,14 +741,16 @@ func (s *AuthService) getOrCreateUserWithIDP(ctx context.Context, request *v1pb.
}
// The userinfo's email comes from identity provider, it has to be converted to lower-case.
- var email string
- if fieldMapping.Identifier == fieldMapping.Email {
- email = strings.ToLower(userInfo.Email)
- } else if idp.Domain != "" {
- domain := extractDomain(idp.Domain)
- email = strings.ToLower(fmt.Sprintf("%s@%s", userInfo.Identifier, domain))
- }
- if email == "" {
+ email := strings.ToLower(userInfo.Identifier)
+ if err := validateEmail(email); err != nil {
+ // If the email is invalid, we will try to use the domain and identifier to construct the email.
+ if idp.Domain != "" {
+ domain := extractDomain(idp.Domain)
+ email = strings.ToLower(fmt.Sprintf("%s@%s", userInfo.Identifier, domain))
+ }
+ }
+ // If the email is still invalid, we will return an error.
+ if err := validateEmail(email); err != nil {
return nil, status.Errorf(codes.NotFound, "unable to identify the user by provider user info")
}
users, err := s.store.ListUsers(ctx, &store.FindUserMessage{
@@ -780,6 +778,7 @@ func (s *AuthService) getOrCreateUserWithIDP(ctx context.Context, request *v1pb.
newUser, err := s.store.CreateUser(ctx, &store.UserMessage{
Name: userInfo.DisplayName,
Email: email,
+ Phone: userInfo.Phone,
Type: api.EndUser,
PasswordHash: string(passwordHash),
}, api.SystemBotID)
@@ -832,17 +831,6 @@ func validateEmail(email string) error {
return nil
}
-func validatePhone(phone string) error {
- phoneNumber, err := phonenumbers.Parse(phone, "")
- if err != nil {
- return err
- }
- if !phonenumbers.IsValidNumber(phoneNumber) {
- return errors.New("invalid phone number")
- }
- return nil
-}
-
func extractDomain(input string) string {
pattern := `[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+`
regExp, err := regexp.Compile(pattern)
diff --git a/backend/api/v1/auth_service_test.go b/backend/api/v1/auth_service_test.go
index a9a0ae6af6..fc391083f3 100644
--- a/backend/api/v1/auth_service_test.go
+++ b/backend/api/v1/auth_service_test.go
@@ -2,30 +2,6 @@ package v1
import "testing"
-func TestValidatePhone(t *testing.T) {
- tests := []struct {
- phone string
- want bool
- }{
- {
- phone: "1234567890",
- want: false,
- },
- {
- phone: "+8615655556666",
- want: true,
- },
- }
-
- for _, test := range tests {
- got := validatePhone(test.phone)
- isValid := got == nil
- if isValid != test.want {
- t.Errorf("validatePhone %s, err %v", test.phone, got)
- }
- }
-}
-
func TestExtractDomain(t *testing.T) {
tests := []struct {
domain string
diff --git a/backend/common/util.go b/backend/common/util.go
index 9eade0385f..1accdbdedc 100644
--- a/backend/common/util.go
+++ b/backend/common/util.go
@@ -14,6 +14,7 @@ import (
"strings"
"time"
+ "github.com/nyaruka/phonenumbers"
"github.com/pkg/errors"
)
@@ -220,3 +221,15 @@ func NormalizeExternalURL(url string) (string, error) {
}
return r, nil
}
+
+// ValidatePhone validates the phone number.
+func ValidatePhone(phone string) error {
+ phoneNumber, err := phonenumbers.Parse(phone, "")
+ if err != nil {
+ return err
+ }
+ if !phonenumbers.IsValidNumber(phoneNumber) {
+ return errors.New("invalid phone number")
+ }
+ return nil
+}
diff --git a/backend/common/util_test.go b/backend/common/util_test.go
index 45b72fab9b..b85fe75752 100644
--- a/backend/common/util_test.go
+++ b/backend/common/util_test.go
@@ -226,3 +226,27 @@ func TestNormalizeExternalURL(t *testing.T) {
})
}
}
+
+func TestValidatePhone(t *testing.T) {
+ tests := []struct {
+ phone string
+ want bool
+ }{
+ {
+ phone: "1234567890",
+ want: false,
+ },
+ {
+ phone: "+8615655556666",
+ want: true,
+ },
+ }
+
+ for _, test := range tests {
+ got := ValidatePhone(test.phone)
+ isValid := got == nil
+ if isValid != test.want {
+ t.Errorf("validatePhone %s, err %v", test.phone, got)
+ }
+ }
+}
diff --git a/backend/plugin/idp/oauth2/oauth2.go b/backend/plugin/idp/oauth2/oauth2.go
index 32b3c46fcf..633c07216c 100644
--- a/backend/plugin/idp/oauth2/oauth2.go
+++ b/backend/plugin/idp/oauth2/oauth2.go
@@ -13,6 +13,7 @@ import (
"go.uber.org/zap"
"golang.org/x/oauth2"
+ "github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/plugin/idp"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -134,5 +135,13 @@ func (p *IdentityProvider) UserInfo(token string) (*storepb.IdentityProviderUser
userInfo.Email = v
}
}
+ if p.config.FieldMapping.Phone != "" {
+ if v, ok := idp.GetValueWithKey(claims, p.config.FieldMapping.Phone).(string); ok {
+ // Only set phone if it's valid.
+ if err := common.ValidatePhone(v); err == nil {
+ userInfo.Phone = v
+ }
+ }
+ }
return userInfo, nil
}
diff --git a/backend/plugin/idp/oidc/oidc.go b/backend/plugin/idp/oidc/oidc.go
index 2a00319243..b495835a73 100644
--- a/backend/plugin/idp/oidc/oidc.go
+++ b/backend/plugin/idp/oidc/oidc.go
@@ -11,6 +11,7 @@ import (
"go.uber.org/zap"
"golang.org/x/oauth2"
+ "github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/plugin/idp"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -158,5 +159,13 @@ func (p *IdentityProvider) UserInfo(ctx context.Context, token *oauth2.Token, no
userInfo.Email = v
}
}
+ if p.config.FieldMapping.Phone != "" {
+ if v, ok := idp.GetValueWithKey(claims, p.config.FieldMapping.Phone).(string); ok {
+ // Only set phone if it's valid.
+ if err := common.ValidatePhone(v); err == nil {
+ userInfo.Phone = v
+ }
+ }
+ }
return userInfo, nil
}