Skip to content

Commit

Permalink
Merge pull request #37 from Questspace-v2/wp/google-username/0
Browse files Browse the repository at this point in the history
chore: Use random user-number name for google OAuth
  • Loading branch information
BasedDepartment1 authored Oct 13, 2024
2 parents f53fdfe + 7eb43c9 commit 48e2ab5
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/internal/questspace/authservice/googleservice/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package googleservice

import (
"context"
"math/rand"
"net/http"
"strconv"
"time"

"github.com/cenkalti/backoff/v4"
"go.uber.org/zap"
"golang.org/x/xerrors"
"google.golang.org/api/idtoken"

Expand All @@ -12,6 +17,7 @@ import (
"questspace/internal/questspace/userservice/usertypes"
"questspace/pkg/auth/jwt"
"questspace/pkg/httperrors"
"questspace/pkg/logging"
"questspace/pkg/storage"
)

Expand Down Expand Up @@ -70,11 +76,12 @@ func (a *Auth) parseToken(ctx context.Context, idToken string) (storage.CreateOr
if err != nil {
return storage.CreateOrUpdateRequest{}, httperrors.Errorf(http.StatusBadRequest, "bad token: %w", err)
}
randNum := rand.Int() % (1 << 32) //nolint:gosec

return storage.CreateOrUpdateRequest{
ExternalID: payload.Claims["sub"].(string),
CreateUserRequest: storage.CreateUserRequest{
Username: payload.Claims["email"].(string),
Username: "user-" + strconv.Itoa(randNum),
AvatarURL: payload.Claims["picture"].(string),
},
}, nil
Expand All @@ -86,7 +93,15 @@ func (a *Auth) doGoogleOAuth(
req *storage.CreateOrUpdateRequest,
resp *authtypes.Response,
) error {
user, err := s.CreateOrUpdateByExternalID(ctx, req)
policy := backoff.WithContext(
backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 5), ctx,
)

user, err := backoff.RetryNotifyWithData(func() (*storage.User, error) {
return s.CreateOrUpdateByExternalID(ctx, req)
}, policy, func(err error, d time.Duration) {
logging.Warn(ctx, "Google OAuth: new retry", zap.Error(err), zap.Duration("passed", d))
})
if err != nil {
return xerrors.Errorf("create or update google user: %w", err)
}
Expand Down

0 comments on commit 48e2ab5

Please sign in to comment.