From 45abbd534c48f57f9278490304466e2aca7c6a78 Mon Sep 17 00:00:00 2001 From: ayaanqui Date: Mon, 29 Apr 2024 11:39:52 -0500 Subject: [PATCH] refactor: use https://github.com/thanhpk/randstr --- go.mod | 9 +++++---- go.sum | 2 ++ services/user_service.go | 6 ++---- utils/strings.go | 15 --------------- 4 files changed, 9 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index 3009944..ffc0ec8 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,12 @@ go 1.21 require ( github.com/99designs/gqlgen v0.17.44 + github.com/cloudinary/cloudinary-go/v2 v2.7.0 github.com/go-chi/chi/v5 v5.0.12 github.com/go-jet/jet/v2 v2.11.0 github.com/ory/dockertest/v3 v3.10.0 + github.com/sendgrid/rest v2.6.9+incompatible + github.com/sendgrid/sendgrid-go v3.14.0+incompatible github.com/vektah/gqlparser/v2 v2.5.11 golang.org/x/crypto v0.21.0 ) @@ -18,7 +21,6 @@ require ( github.com/Microsoft/go-winio v0.6.1 // indirect github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect - github.com/cloudinary/cloudinary-go/v2 v2.7.0 // indirect github.com/containerd/continuity v0.4.3 // indirect github.com/creasty/defaults v1.5.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect @@ -59,10 +61,9 @@ require ( github.com/opencontainers/runc v1.1.12 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/sendgrid/rest v2.6.9+incompatible // indirect - github.com/sendgrid/sendgrid-go v3.14.0+incompatible // indirect github.com/sirupsen/logrus v1.9.3 // indirect github.com/stretchr/testify v1.8.4 // indirect + github.com/thanhpk/randstr v1.0.6 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect github.com/xeipuuv/gojsonschema v1.2.0 // indirect @@ -94,7 +95,7 @@ require ( github.com/go-playground/validator/v10 v10.18.0 github.com/gohugoio/hugo v0.111.3 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible - github.com/google/uuid v1.6.0 // indirect + github.com/google/uuid v1.6.0 github.com/lib/pq v1.10.9 github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index 333f73b..3f895b5 100644 --- a/go.sum +++ b/go.sum @@ -402,6 +402,8 @@ github.com/tdewolff/parse/v2 v2.6.5 h1:lYvWBk55GkqKl0JJenGpmrgu/cPHQQ6/Mm1hBGswo github.com/tdewolff/parse/v2 v2.6.5/go.mod h1:woz0cgbLwFdtbjJu8PIKxhW05KplTFQkOdX78o+Jgrs= github.com/tdewolff/test v1.0.7 h1:8Vs0142DmPFW/bQeHRP3MV19m1gvndjUb1sn8yy74LM= github.com/tdewolff/test v1.0.7/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= +github.com/thanhpk/randstr v1.0.6 h1:psAOktJFD4vV9NEVb3qkhRSMvYh4ORRaj1+w/hn4B+o= +github.com/thanhpk/randstr v1.0.6/go.mod h1:M/H2P1eNLZzlDwAzpkkkUvoyNNMbzRGhESZuEQk3r0U= github.com/urfave/cli v1.22.12 h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8= github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8= github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho= diff --git a/services/user_service.go b/services/user_service.go index b588859..4a02dd7 100644 --- a/services/user_service.go +++ b/services/user_service.go @@ -12,6 +12,7 @@ import ( "github.com/stadio-app/stadio-backend/graph/gmodel" "github.com/stadio-app/stadio-backend/types" "github.com/stadio-app/stadio-backend/utils" + "github.com/thanhpk/randstr" "golang.org/x/crypto/bcrypt" "google.golang.org/api/googleapi" "google.golang.org/api/oauth2/v2" @@ -236,10 +237,7 @@ func (service Service) CreateAuthState(ctx context.Context, user gmodel.User, au } func (service Service) CreateEmailVerification(ctx context.Context, user gmodel.User) (model.EmailVerification, error) { - code, code_err := utils.GenerateRandomUrlEncodedString(EMAIL_VERIFICATION_CODE_LEN) - if code_err != nil { - return model.EmailVerification{}, code_err - } + code := randstr.String(EMAIL_VERIFICATION_CODE_LEN) query := table.EmailVerification.INSERT( table.EmailVerification.UserID, diff --git a/utils/strings.go b/utils/strings.go index 5ecb24d..d4b585b 100644 --- a/utils/strings.go +++ b/utils/strings.go @@ -1,16 +1 @@ package utils - -import ( - "crypto/rand" - "encoding/base64" -) - -// Generate random string URL encoded -func GenerateRandomUrlEncodedString(length int) (string, error) { - buffer := make([]byte, length) - _, err := rand.Read(buffer) - if err != nil { - return "", err - } - return base64.URLEncoding.EncodeToString(buffer)[:length], nil -}