From 89a17c17c4be919a6e1193769e7c228370d8bbb9 Mon Sep 17 00:00:00 2001 From: frederikhors <41120635+frederikhors@users.noreply.github.com> Date: Wed, 29 Jan 2020 13:14:14 +0100 Subject: [PATCH 1/5] Repetition range replaceable by '+' --- defaults/values.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/defaults/values.go b/defaults/values.go index 1f0fc1d..4f0c278 100644 --- a/defaults/values.go +++ b/defaults/values.go @@ -174,7 +174,7 @@ func NewHTTPBodyReader(readJSON, useUsernameNotEmail bool) *HTTPBodyReader { pidRules = Rules{ FieldName: pid, Required: true, MatchError: "Must be a valid e-mail address", - MustMatch: regexp.MustCompile(`.*@.*\.[a-z]{1,}`), + MustMatch: regexp.MustCompile(`.*@.*\.[a-z]+`), } } From 9c764f69fe089c033b43bd1caf481ca0a1193222 Mon Sep 17 00:00:00 2001 From: frederikhors <41120635+frederikhors@users.noreply.github.com> Date: Wed, 29 Jan 2020 14:04:44 +0100 Subject: [PATCH 2/5] Redundant type conversion --- defaults/validation_test.go | 2 +- otp/twofactor/sms2fa/sms_test.go | 2 +- remember/remember.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/defaults/validation_test.go b/defaults/validation_test.go index 8a4fbda..ba9a693 100644 --- a/defaults/validation_test.go +++ b/defaults/validation_test.go @@ -76,7 +76,7 @@ func TestValidate_Confirm(t *testing.T) { } }() - errs = authboss.ErrorList(validator.Validate()) + errs = validator.Validate() if len(errs) != 0 { t.Error("Expected no errors:", errs) } diff --git a/otp/twofactor/sms2fa/sms_test.go b/otp/twofactor/sms2fa/sms_test.go index 4e8d0c1..0f6c8fd 100644 --- a/otp/twofactor/sms2fa/sms_test.go +++ b/otp/twofactor/sms2fa/sms_test.go @@ -355,7 +355,7 @@ func TestValidatorPostSend(t *testing.T) { t.Error("should have sent a code") } - *h.sender = smsHolderSender("") + *h.sender = "" // When action is confirm, it retrieves the phone number from // the session, not the user. diff --git a/remember/remember.go b/remember/remember.go index 5af8259..5bfae16 100644 --- a/remember/remember.go +++ b/remember/remember.go @@ -166,7 +166,7 @@ func (r *Remember) AfterPasswordReset(w http.ResponseWriter, req *http.Request, // GenerateToken creates a remember me token func GenerateToken(pid string) (hash string, token string, err error) { rawToken := make([]byte, nNonceSize+len(pid)+1) - copy(rawToken, []byte(pid)) + copy(rawToken, pid) rawToken[len(pid)] = ';' if _, err := io.ReadFull(rand.Reader, rawToken[len(pid)+1:]); err != nil { From 827256e4d7a86312db889d84924eb1694bc8c1e5 Mon Sep 17 00:00:00 2001 From: frederikhors <41120635+frederikhors@users.noreply.github.com> Date: Wed, 29 Jan 2020 14:08:11 +0100 Subject: [PATCH 3/5] Redundant types in composite literals --- authboss_test.go | 2 +- context_test.go | 2 +- defaults/validation_test.go | 4 ++-- defaults/values.go | 2 +- module_test.go | 2 +- oauth2/oauth2_test.go | 4 ++-- otp/twofactor/sms2fa/sms.go | 4 ++-- otp/twofactor/totp2fa/totp.go | 6 +++--- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/authboss_test.go b/authboss_test.go index a9982a0..73c0419 100644 --- a/authboss_test.go +++ b/authboss_test.go @@ -55,7 +55,7 @@ func TestAuthbossMiddleware(t *testing.T) { ab.Core.Logger = mockLogger{} ab.Storage.Server = &mockServerStorer{ Users: map[string]*mockUser{ - "test@test.com": &mockUser{}, + "test@test.com": {}, }, } diff --git a/context_test.go b/context_test.go index d269d92..16c54a5 100644 --- a/context_test.go +++ b/context_test.go @@ -21,7 +21,7 @@ func testSetupContext() (*Authboss, *http.Request) { ab.Storage.SessionState = newMockClientStateRW(SessionKey, "george-pid") ab.Storage.Server = &mockServerStorer{ Users: map[string]*mockUser{ - "george-pid": &mockUser{Email: "george-pid", Password: "unreadable"}, + "george-pid": {Email: "george-pid", Password: "unreadable"}, }, } r := httptest.NewRequest("GET", "/", nil) diff --git a/defaults/validation_test.go b/defaults/validation_test.go index 8a4fbda..2203b6d 100644 --- a/defaults/validation_test.go +++ b/defaults/validation_test.go @@ -15,11 +15,11 @@ func TestValidate(t *testing.T) { "email": "john@john.com", }, Ruleset: []Rules{ - Rules{ + { FieldName: "username", MinLength: 5, }, - Rules{ + { FieldName: "missing_field", Required: true, }, diff --git a/defaults/values.go b/defaults/values.go index 1f0fc1d..536d338 100644 --- a/defaults/values.go +++ b/defaults/values.go @@ -204,7 +204,7 @@ func NewHTTPBodyReader(readJSON, useUsernameNotEmail bool) *HTTPBodyReader { "recover_end": {FormValuePassword, authboss.ConfirmPrefix + FormValuePassword}, }, Whitelist: map[string][]string{ - "register": []string{FormValueEmail, FormValuePassword}, + "register": {FormValueEmail, FormValuePassword}, }, } } diff --git a/module_test.go b/module_test.go index 994a4f0..3ef7e6b 100644 --- a/module_test.go +++ b/module_test.go @@ -76,7 +76,7 @@ func TestModuleLoadedMiddleware(t *testing.T) { "oauth2": nil, } ab.Config.Modules.OAuth2Providers = map[string]OAuth2Provider{ - "google": OAuth2Provider{}, + "google": {}, } var mods map[string]bool diff --git a/oauth2/oauth2_test.go b/oauth2/oauth2_test.go index 08cca02..8d9f8da 100644 --- a/oauth2/oauth2_test.go +++ b/oauth2/oauth2_test.go @@ -23,7 +23,7 @@ func init() { } var testProviders = map[string]authboss.OAuth2Provider{ - "google": authboss.OAuth2Provider{ + "google": { OAuth2Config: &oauth2.Config{ ClientID: `jazz`, ClientSecret: `hands`, @@ -35,7 +35,7 @@ var testProviders = map[string]authboss.OAuth2Provider{ FindUserDetails: GoogleUserDetails, AdditionalParams: url.Values{"include_requested_scopes": []string{"true"}}, }, - "facebook": authboss.OAuth2Provider{ + "facebook": { OAuth2Config: &oauth2.Config{ ClientID: `jazz`, ClientSecret: `hands`, diff --git a/otp/twofactor/sms2fa/sms.go b/otp/twofactor/sms2fa/sms.go index 94383b3..a603aa1 100644 --- a/otp/twofactor/sms2fa/sms.go +++ b/otp/twofactor/sms2fa/sms.go @@ -263,7 +263,7 @@ func (s *SMS) PostSetup(w http.ResponseWriter, r *http.Request) error { number := smsVals.GetPhoneNumber() if len(number) == 0 { data := authboss.HTMLData{ - authboss.DataValidation: map[string][]string{FormValuePhoneNumber: []string{"must provide a phone number"}}, + authboss.DataValidation: map[string][]string{FormValuePhoneNumber: {"must provide a phone number"}}, } return s.Core.Responder.Respond(w, r, http.StatusOK, PageSMSSetup, data) } @@ -401,7 +401,7 @@ func (s *SMSValidator) validateCode(w http.ResponseWriter, r *http.Request, user logger.Infof("user %s sms 2fa failure (wrong code)", user.GetPID()) data := authboss.HTMLData{ - authboss.DataValidation: map[string][]string{FormValueCode: []string{"2fa code was invalid"}}, + authboss.DataValidation: map[string][]string{FormValueCode: {"2fa code was invalid"}}, } return s.Authboss.Core.Responder.Respond(w, r, http.StatusOK, s.Page, data) } diff --git a/otp/twofactor/totp2fa/totp.go b/otp/twofactor/totp2fa/totp.go index 6f27cec..f11a0f9 100644 --- a/otp/twofactor/totp2fa/totp.go +++ b/otp/twofactor/totp2fa/totp.go @@ -262,7 +262,7 @@ func (t *TOTP) PostConfirm(w http.ResponseWriter, r *http.Request) error { ok = totp.Validate(inputCode, totpSecret) if !ok { data := authboss.HTMLData{ - authboss.DataValidation: map[string][]string{FormValueCode: []string{"2fa code was invalid"}}, + authboss.DataValidation: map[string][]string{FormValueCode: {"2fa code was invalid"}}, DataTOTPSecret: totpSecret, } return t.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageTOTPConfirm, data) @@ -310,7 +310,7 @@ func (t *TOTP) PostRemove(w http.ResponseWriter, r *http.Request) error { return err case !ok: data := authboss.HTMLData{ - authboss.DataValidation: map[string][]string{FormValueCode: []string{"2fa code was invalid"}}, + authboss.DataValidation: map[string][]string{FormValueCode: {"2fa code was invalid"}}, } return t.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageTOTPRemove, data) } @@ -355,7 +355,7 @@ func (t *TOTP) PostValidate(w http.ResponseWriter, r *http.Request) error { logger.Infof("user %s totp 2fa failure (wrong code)", user.GetPID()) data := authboss.HTMLData{ - authboss.DataValidation: map[string][]string{FormValueCode: []string{"2fa code was invalid"}}, + authboss.DataValidation: map[string][]string{FormValueCode: {"2fa code was invalid"}}, } return t.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageTOTPValidate, data) } From 6f6f2e6fd7ba761b761e3dac6743ced9c69e987c Mon Sep 17 00:00:00 2001 From: frederikhors <41120635+frederikhors@users.noreply.github.com> Date: Wed, 29 Jan 2020 14:52:00 +0100 Subject: [PATCH 4/5] Imported pkg name as name identifier --- oauth2/oauth2.go | 6 +++--- oauth2/oauth2_test.go | 8 ++++---- otp/twofactor/totp2fa/totp_test.go | 4 ++-- recover/recover.go | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/oauth2/oauth2.go b/oauth2/oauth2.go index 01f9cfb..84b65a9 100644 --- a/oauth2/oauth2.go +++ b/oauth2/oauth2.go @@ -143,16 +143,16 @@ func (o *OAuth2) Start(w http.ResponseWriter, r *http.Request) error { authboss.DelSession(w, authboss.SessionOAuth2Params) } - url := cfg.OAuth2Config.AuthCodeURL(state) + authCodeUrl := cfg.OAuth2Config.AuthCodeURL(state) extraParams := cfg.AdditionalParams.Encode() if len(extraParams) > 0 { - url = fmt.Sprintf("%s&%s", url, extraParams) + authCodeUrl = fmt.Sprintf("%s&%s", authCodeUrl, extraParams) } ro := authboss.RedirectOptions{ Code: http.StatusTemporaryRedirect, - RedirectPath: url, + RedirectPath: authCodeUrl, } return o.Authboss.Core.Redirector.Redirect(w, r, ro) } diff --git a/oauth2/oauth2_test.go b/oauth2/oauth2_test.go index 08cca02..657d131 100644 --- a/oauth2/oauth2_test.go +++ b/oauth2/oauth2_test.go @@ -131,11 +131,11 @@ func TestStart(t *testing.T) { t.Error("code was wrong:", h.redirector.Options.Code) } - url, err := url.Parse(h.redirector.Options.RedirectPath) + redirectPathUrl, err := url.Parse(h.redirector.Options.RedirectPath) if err != nil { t.Fatal(err) } - query := url.Query() + query := redirectPathUrl.Query() if state := query.Get("state"); len(state) == 0 { t.Error("our nonce should have been here") } @@ -145,8 +145,8 @@ func TestStart(t *testing.T) { if clientID := query.Get("client_id"); clientID != "jazz" { t.Error("clientID was wrong:", clientID) } - if url.Host != "accounts.google.com" { - t.Error("host was wrong:", url.Host) + if redirectPathUrl.Host != "accounts.google.com" { + t.Error("host was wrong:", redirectPathUrl.Host) } if h.session.ClientValues[authboss.SessionOAuth2State] != query.Get("state") { diff --git a/otp/twofactor/totp2fa/totp_test.go b/otp/twofactor/totp2fa/totp_test.go index aa16b2b..f468901 100644 --- a/otp/twofactor/totp2fa/totp_test.go +++ b/otp/twofactor/totp2fa/totp_test.go @@ -28,8 +28,8 @@ func TestTOTPSetup(t *testing.T) { ab.Config.Core.ViewRenderer = renderer ab.Config.Core.ErrorHandler = errHandler - totp := &TOTP{Authboss: ab} - if err := totp.Setup(); err != nil { + totpNew := &TOTP{Authboss: ab} + if err := totpNew.Setup(); err != nil { t.Fatal(err) } diff --git a/recover/recover.go b/recover/recover.go index 71efaa7..bede4ad 100644 --- a/recover/recover.go +++ b/recover/recover.go @@ -271,8 +271,8 @@ func (r *Recover) EndPost(w http.ResponseWriter, req *http.Request) error { } func (r *Recover) invalidToken(page string, w http.ResponseWriter, req *http.Request) error { - errors := []error{errors.New("recovery token is invalid")} - data := authboss.HTMLData{authboss.DataValidation: authboss.ErrorMap(errors)} + errorsAll := []error{errors.New("recovery token is invalid")} + data := authboss.HTMLData{authboss.DataValidation: authboss.ErrorMap(errorsAll)} return r.Authboss.Core.Responder.Respond(w, req, http.StatusOK, PageRecoverEnd, data) } From e4d1e169350e15d55d973e51c703fa0643a0344f Mon Sep 17 00:00:00 2001 From: frederikhors <41120635+frederikhors@users.noreply.github.com> Date: Wed, 29 Jan 2020 14:59:47 +0100 Subject: [PATCH 5/5] Variable 'delete' collides with builtin function --- defaults/router_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/defaults/router_test.go b/defaults/router_test.go index aa09bde..c1fba5e 100644 --- a/defaults/router_test.go +++ b/defaults/router_test.go @@ -12,7 +12,7 @@ func TestRouter(t *testing.T) { t.Parallel() r := NewRouter() - var get, post, delete string + var get, post, del string wantGet, wantPost, wantDelete := "testget", "testpost", "testdelete" r.Get("/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -37,7 +37,7 @@ func TestRouter(t *testing.T) { panic(err) } - delete = string(b) + del = string(b) })) wr := httptest.NewRecorder() @@ -46,8 +46,8 @@ func TestRouter(t *testing.T) { if get != wantGet { t.Error("want:", wantGet, "got:", get) } - if len(post) != 0 || len(delete) != 0 { - t.Error("should be empty:", post, delete) + if len(post) != 0 || len(del) != 0 { + t.Error("should be empty:", post, del) } wr = httptest.NewRecorder() @@ -56,15 +56,15 @@ func TestRouter(t *testing.T) { if post != wantPost { t.Error("want:", wantPost, "got:", post) } - if len(delete) != 0 { - t.Error("should be empty:", delete) + if len(del) != 0 { + t.Error("should be empty:", del) } wr = httptest.NewRecorder() req = httptest.NewRequest("DELETE", "/test", strings.NewReader("testdelete")) r.ServeHTTP(wr, req) - if delete != wantDelete { - t.Error("want:", wantDelete, "got:", delete) + if del != wantDelete { + t.Error("want:", wantDelete, "got:", del) } }