From 9b24954f9443dd5dc166cf8fec0047bea3542902 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 24 Feb 2021 09:30:44 +0000 Subject: [PATCH] Fix check for empty string --- cmd/mytoken-server/mytoken-setup/setup.go | 2 +- internal/config/config.go | 2 +- internal/db/db.go | 2 +- internal/db/dbrepo/authcodeinforepo/state/state.go | 4 ++-- .../supertokenrepo/transfercoderepo/pollingCodeTmpST.go | 2 +- .../db/dbrepo/supertokenrepo/transfercoderepo/proxytoken.go | 6 +++--- internal/endpoints/revocation/revocationEndpoint.go | 2 +- internal/endpoints/token/access/accessTokenEndpoint.go | 4 ++-- internal/endpoints/token/super/transferEndpoint.go | 4 ++-- internal/oidc/revoke/revoke.go | 2 +- internal/utils/ctxUtils/token.go | 2 +- shared/supertoken/pkg/stid/stid.go | 2 +- shared/supertoken/pkg/supertoken.go | 4 ++-- shared/supertoken/restrictions/restriction.go | 2 +- 14 files changed, 20 insertions(+), 20 deletions(-) diff --git a/cmd/mytoken-server/mytoken-setup/setup.go b/cmd/mytoken-server/mytoken-setup/setup.go index 3a324425..dfbe26b0 100644 --- a/cmd/mytoken-server/mytoken-setup/setup.go +++ b/cmd/mytoken-server/mytoken-setup/setup.go @@ -101,7 +101,7 @@ func (c *commandGenSigningKey) Execute(args []string) error { // Execute implements the flags.Commander interface func (c *commandCreateDB) Execute(args []string) error { password := "" - if c.Password != nil && len(*c.Password) == 0 { // -p specified without argument + if c.Password != nil && *c.Password == "" { // -p specified without argument password = prompter.Password("Database Password") } db := cluster.NewFromConfig(config.DBConf{ diff --git a/internal/config/config.go b/internal/config/config.go index de28cc9a..4d13ae01 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -215,7 +215,7 @@ func validate() error { iss0, iss1 := issuerUtils.GetIssuerWithAndWithoutSlash(p.Issuer) conf.ProviderByIssuer[iss0] = p conf.ProviderByIssuer[iss1] = p - if len(p.AudienceRequestParameter) == 0 { + if p.AudienceRequestParameter == "" { p.AudienceRequestParameter = "resource" } } diff --git a/internal/db/db.go b/internal/db/db.go index a7b0355f..7ad3a26d 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -31,7 +31,7 @@ func Connect() { // NewNullString creates a new sql.NullString from the given string func NewNullString(s string) sql.NullString { - if len(s) == 0 { + if s == "" { return sql.NullString{} } return sql.NullString{ diff --git a/internal/db/dbrepo/authcodeinforepo/state/state.go b/internal/db/dbrepo/authcodeinforepo/state/state.go index ee61d96b..32829525 100644 --- a/internal/db/dbrepo/authcodeinforepo/state/state.go +++ b/internal/db/dbrepo/authcodeinforepo/state/state.go @@ -24,14 +24,14 @@ func NewState(state string) *State { } func (s *State) Hash() string { - if len(s.hash) == 0 { + if s.hash == "" { s.hash = hashUtils.SHA512Str([]byte(s.state)) } return s.hash } func (s *State) PollingCode() string { - if len(s.pollingCode) == 0 { + if s.pollingCode == "" { s.pollingCode = hashUtils.HMACSHA512Str([]byte(s.state), []byte("polling_code"))[:config.Get().Features.Polling.Len] log.WithField("state", s.state).WithField("polling_code", s.pollingCode).Debug("Created polling_code for state") } diff --git a/internal/db/dbrepo/supertokenrepo/transfercoderepo/pollingCodeTmpST.go b/internal/db/dbrepo/supertokenrepo/transfercoderepo/pollingCodeTmpST.go index a418ac00..37ff55c2 100644 --- a/internal/db/dbrepo/supertokenrepo/transfercoderepo/pollingCodeTmpST.go +++ b/internal/db/dbrepo/supertokenrepo/transfercoderepo/pollingCodeTmpST.go @@ -45,7 +45,7 @@ func PopTokenForTransferCode(tx *sqlx.Tx, pollingCode string) (jwt string, err e if err != nil { return err } - if !valid || len(jwt) == 0 { + if !valid || jwt == "" { return nil } return pt.Delete(tx) diff --git a/internal/db/dbrepo/supertokenrepo/transfercoderepo/proxytoken.go b/internal/db/dbrepo/supertokenrepo/transfercoderepo/proxytoken.go index c00765c4..c7ebf69c 100644 --- a/internal/db/dbrepo/supertokenrepo/transfercoderepo/proxytoken.go +++ b/internal/db/dbrepo/supertokenrepo/transfercoderepo/proxytoken.go @@ -58,7 +58,7 @@ func (pt proxyToken) Token() string { // ID returns the id of this token func (pt *proxyToken) ID() string { - if len(pt.id) == 0 { + if pt.id == "" { pt.id = hashUtils.SHA512Str([]byte(pt.token)) } return pt.id @@ -78,7 +78,7 @@ func (pt *proxyToken) JWT(tx *sqlx.Tx) (jwt string, valid bool, err error) { valid = true return } - if len(pt.encryptedJWT) == 0 { + if pt.encryptedJWT == "" { if err = db.RunWithinTransaction(tx, func(tx *sqlx.Tx) error { return tx.Get(&pt.encryptedJWT, `SELECT jwt FROM ProxyTokens WHERE id=?`, pt.id) }); err != nil { @@ -90,7 +90,7 @@ func (pt *proxyToken) JWT(tx *sqlx.Tx) (jwt string, valid bool, err error) { } } valid = true - if len(pt.encryptedJWT) == 0 { + if pt.encryptedJWT == "" { return } jwt, err = cryptUtils.AES256Decrypt(pt.encryptedJWT, pt.token) diff --git a/internal/endpoints/revocation/revocationEndpoint.go b/internal/endpoints/revocation/revocationEndpoint.go index 8dfa0dfb..66bc25ae 100644 --- a/internal/endpoints/revocation/revocationEndpoint.go +++ b/internal/endpoints/revocation/revocationEndpoint.go @@ -30,7 +30,7 @@ func HandleRevoke(ctx *fiber.Ctx) error { } log.Trace("Parsed super token request") clearCookie := false - if len(req.Token) == 0 { + if req.Token == "" { req.Token = ctx.Cookies("mytoken-supertoken") clearCookie = true } diff --git a/internal/endpoints/token/access/accessTokenEndpoint.go b/internal/endpoints/token/access/accessTokenEndpoint.go index 8badfdf9..b6d8a589 100644 --- a/internal/endpoints/token/access/accessTokenEndpoint.go +++ b/internal/endpoints/token/access/accessTokenEndpoint.go @@ -91,7 +91,7 @@ func HandleAccessTokenEndpoint(ctx *fiber.Ctx) error { return res.Send(ctx) } log.Trace("Checked super token capabilities") - if len(req.Issuer) == 0 { + if req.Issuer == "" { req.Issuer = st.OIDCIssuer } else if req.Issuer != st.OIDCIssuer { res := serverModel.Response{ @@ -131,7 +131,7 @@ func handleAccessTokenRefresh(st *supertoken.SuperToken, req request.AccessToken } else if len(usedRestriction.Scope) > 0 { scopes = usedRestriction.Scope } - if len(req.Audience) != 0 { + if req.Audience != "" { auds = req.Audience } else if len(usedRestriction.Audiences) > 0 { auds = strings.Join(usedRestriction.Audiences, " ") diff --git a/internal/endpoints/token/super/transferEndpoint.go b/internal/endpoints/token/super/transferEndpoint.go index 5d44b62c..13f3aaf6 100644 --- a/internal/endpoints/token/super/transferEndpoint.go +++ b/internal/endpoints/token/super/transferEndpoint.go @@ -18,7 +18,7 @@ import ( // HandleCreateTransferCodeForExistingSuperToken handles request to create a transfer code for an existing supertoken func HandleCreateTransferCodeForExistingSuperToken(ctx *fiber.Ctx) error { token := ctxUtils.GetAuthHeaderToken(ctx) - if len(token) == 0 { + if token == "" { var req pkg.CreateTransferCodeRequest if err := json.Unmarshal(ctx.Body(), &req); err != nil { return model.Response{ @@ -27,7 +27,7 @@ func HandleCreateTransferCodeForExistingSuperToken(ctx *fiber.Ctx) error { }.Send(ctx) } token = req.SuperToken - if len(token) == 0 { + if token == "" { return model.Response{ Status: fiber.StatusUnauthorized, Response: pkgModel.BadRequestError("required parameter 'super_token' missing"), diff --git a/internal/oidc/revoke/revoke.go b/internal/oidc/revoke/revoke.go index 898ecc26..21566ad5 100644 --- a/internal/oidc/revoke/revoke.go +++ b/internal/oidc/revoke/revoke.go @@ -10,7 +10,7 @@ import ( // RefreshToken revokes a refresh token func RefreshToken(provider *config.ProviderConf, rt string) *model.Response { - if len(provider.Endpoints.Revocation) == 0 { + if provider.Endpoints.Revocation == "" { return nil } req := oidcReqRes.NewRTRevokeRequest(rt) diff --git a/internal/utils/ctxUtils/token.go b/internal/utils/ctxUtils/token.go index 0667d466..6f27cd40 100644 --- a/internal/utils/ctxUtils/token.go +++ b/internal/utils/ctxUtils/token.go @@ -29,7 +29,7 @@ func GetSuperTokenStr(ctx *fiber.Ctx) string { // GetSuperToken checks a fiber.Ctx for a super token and returns a token object func GetSuperToken(ctx *fiber.Ctx) *token.Token { tok := GetSuperTokenStr(ctx) - if len(tok) == 0 { + if tok == "" { return nil } t, err := token.GetLongSuperToken(tok) diff --git a/shared/supertoken/pkg/stid/stid.go b/shared/supertoken/pkg/stid/stid.go index af639c36..2e094ea0 100644 --- a/shared/supertoken/pkg/stid/stid.go +++ b/shared/supertoken/pkg/stid/stid.go @@ -38,7 +38,7 @@ func (i *STID) HashValid() bool { } func (i *STID) Hash() string { - if len(i.hash) == 0 && i.Valid() { + if i.hash == "" && i.Valid() { i.hash = hashUtils.SHA512Str(i.Bytes()) } return i.hash diff --git a/shared/supertoken/pkg/supertoken.go b/shared/supertoken/pkg/supertoken.go index 2f9cb691..5ed3d722 100644 --- a/shared/supertoken/pkg/supertoken.go +++ b/shared/supertoken/pkg/supertoken.go @@ -44,7 +44,7 @@ func (st *SuperToken) verifyID() bool { } func (st *SuperToken) verifySubject() bool { - if len(st.Subject) == 0 { + if st.Subject == "" { return false } if st.Subject != issuerUtils.CombineSubIss(st.OIDCSubject, st.OIDCIssuer) { @@ -186,7 +186,7 @@ func CreateTransferCode(stid stid.STID, jwt string, newST bool, responseType mod // ToTokenResponse creates a SuperTokenResponse for this SuperToken according to the passed model.ResponseType func (st *SuperToken) ToTokenResponse(responseType model.ResponseType, networkData serverModel.ClientMetaData, jwt string) (response.SuperTokenResponse, error) { - if len(jwt) == 0 { + if jwt == "" { jwt = st.jwt } switch responseType { diff --git a/shared/supertoken/restrictions/restriction.go b/shared/supertoken/restrictions/restriction.go index 1180ed1f..84da56de 100644 --- a/shared/supertoken/restrictions/restriction.go +++ b/shared/supertoken/restrictions/restriction.go @@ -213,7 +213,7 @@ func (r Restrictions) WithScopes(scopes []string) (ret Restrictions) { return r } for _, rr := range r { - if len(rr.Scope) == 0 || utils.IsSubSet(scopes, utils.SplitIgnoreEmpty(rr.Scope, " ")) { + if rr.Scope == "" || utils.IsSubSet(scopes, utils.SplitIgnoreEmpty(rr.Scope, " ")) { ret = append(ret, rr) } }