Skip to content

Commit a5edd53

Browse files
refactor: add more linters (#64)
1 parent d72ab86 commit a5edd53

11 files changed

Lines changed: 175 additions & 226 deletions

File tree

.golangci.yml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ linters:
1919
- errcheck
2020
- errchkjson
2121
- errname
22-
# - errorlint
23-
# - exhaustive
24-
# - fatcontext
25-
# - forbidigo
26-
# - forcetypeassert
27-
# - funlen
28-
# - ginkgolinter
29-
# - gocheckcompilerdirectives
30-
# - gochecknoinits
31-
# - gochecksumtype
22+
- errorlint
23+
- exhaustive
24+
- fatcontext
25+
- forbidigo
26+
- forcetypeassert
27+
- funlen
28+
- ginkgolinter
29+
- gocheckcompilerdirectives
30+
- gochecknoinits
31+
- gochecksumtype
3232
# - gocognit
3333
# - goconst
3434
# - gocritic
@@ -103,6 +103,10 @@ linters:
103103
- third_party$
104104
- builtin$
105105
- examples$
106+
rules:
107+
- path: cmd/migrate/main.go
108+
linters:
109+
- forbidigo
106110
formatters:
107111
enable:
108112
- gofmt

e2e-tests/http.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/base64"
66
"encoding/json"
7-
"fmt"
87
"io"
98
"net/http"
109
"net/url"
@@ -123,7 +122,7 @@ func doRequest(t *testing.T, base, uri, method string, payload []byte, resp inte
123122
response, err := http.DefaultClient.Do(request)
124123
require.NoError(t, err)
125124

126-
logRequest(request, response)
125+
logRequest(t, request, response)
127126

128127
rawBody, err := io.ReadAll(response.Body)
129128
require.NoError(t, err)
@@ -152,14 +151,15 @@ func createRequest(method, uri string, payload []byte) *http.Request {
152151
return request
153152
}
154153

155-
func logRequest(req *http.Request, resp *http.Response) {
154+
func logRequest(t *testing.T, req *http.Request, resp *http.Response) {
155+
t.Helper()
156156
if DebugHttpRequests {
157157
rawBody, _ := io.ReadAll(resp.Body)
158158

159159
resp.Body.Close()
160160
resp.Body = io.NopCloser(bytes.NewBuffer(rawBody))
161161

162-
fmt.Printf("Request: %s: %s %v \n", req.Method, req.URL.RequestURI(), req.Body)
163-
fmt.Println("Response: ", req.URL.RequestURI(), resp.StatusCode, string(rawBody))
162+
t.Logf("Request: %s: %s %v", req.Method, req.URL.RequestURI(), req.Body)
163+
t.Log("Response: ", req.URL.RequestURI(), resp.StatusCode, string(rawBody))
164164
}
165165
}

internal/api/browser_extension/app/command/request_2fa_token.go

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,11 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
7777
log := logging.FromContext(ctx)
7878
extId, _ := uuid.Parse(cmd.ExtensionId)
7979

80-
browserExtension, err := h.BrowserExtensionsRepository.FindById(extId)
80+
pairedDevices, err := h.findPairedDevices(extId, cmd)
8181
if err != nil {
8282
return nil, err
8383
}
8484

85-
tokenRequestId, _ := uuid.Parse(cmd.Id)
86-
browserExtension2FaRequest := domain.NewBrowserExtension2FaRequest(tokenRequestId, browserExtension.Id, cmd.Domain)
87-
88-
err = h.BrowserExtension2FaRequestRepository.Save(browserExtension2FaRequest)
89-
if err != nil {
90-
return nil, err
91-
}
92-
93-
pairedDevices := h.PairedDevicesRepository.FindAll(browserExtension.Id)
94-
9585
data := map[string]interface{}{
9686
"extension_id": extId.String(),
9787
"request_id": cmd.Id,
@@ -115,23 +105,7 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
115105
continue
116106
}
117107

118-
var err error
119-
var notification *messaging.Message
120-
121-
switch device.Platform {
122-
case domain.Android:
123-
notification = createPushNotificationForAndroid(device.FcmToken, data)
124-
case domain.IOS:
125-
notification = createPushNotificationForIos(device.FcmToken, data)
126-
}
127-
128-
err = retry.Do(
129-
func() error {
130-
return h.Pusher.Send(ctx, notification)
131-
},
132-
retry.Attempts(5),
133-
retry.LastErrorOnly(true),
134-
)
108+
err := h.sendNotification(ctx, device, data)
135109
if err == nil {
136110
result[device.Id.String()] = PushNotificationStatusOK
137111
} else if messaging.IsUnregistered(err) {
@@ -153,6 +127,46 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
153127
return result, nil
154128
}
155129

130+
func (h *Request2FaTokenHandler) findPairedDevices(extId uuid.UUID, cmd *Request2FaToken) ([]*domain.ExtensionDevice, error) {
131+
browserExtension, err := h.BrowserExtensionsRepository.FindById(extId)
132+
if err != nil {
133+
return nil, err
134+
}
135+
136+
tokenRequestId, err := uuid.Parse(cmd.Id)
137+
if err != nil {
138+
return nil, fmt.Errorf("failed to parse token request id: %w", err)
139+
}
140+
141+
browserExtension2FaRequest := domain.NewBrowserExtension2FaRequest(tokenRequestId, browserExtension.Id, cmd.Domain)
142+
143+
err = h.BrowserExtension2FaRequestRepository.Save(browserExtension2FaRequest)
144+
if err != nil {
145+
return nil, err
146+
}
147+
148+
return h.PairedDevicesRepository.FindAll(browserExtension.Id), nil
149+
}
150+
151+
func (h *Request2FaTokenHandler) sendNotification(ctx context.Context, device *domain.ExtensionDevice, data map[string]interface{}) error {
152+
var notification *messaging.Message
153+
154+
switch device.Platform {
155+
case domain.Android:
156+
notification = createPushNotificationForAndroid(device.FcmToken, data)
157+
case domain.IOS:
158+
notification = createPushNotificationForIos(device.FcmToken, data)
159+
}
160+
161+
return retry.Do(
162+
func() error {
163+
return h.Pusher.Send(ctx, notification)
164+
},
165+
retry.Attempts(5),
166+
retry.LastErrorOnly(true),
167+
)
168+
}
169+
156170
func createPushNotificationForIos(token string, data map[string]interface{}) *messaging.Message {
157171
ttl := time.Now().Add(tokenPushNotificationTtl)
158172

internal/api/browser_extension/service/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type BrowserExtensionModule struct {
2828
Config config.Configuration
2929
}
3030

31-
func NewBrowserExtensionModule(
31+
func NewBrowserExtensionModule( //nolint:funlen // This is an initialization function.
3232
config config.Configuration,
3333
gorm *gorm.DB,
3434
database *sql.DB,

0 commit comments

Comments
 (0)