Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ linters:
- errcheck
- errchkjson
- errname
# - errorlint
# - exhaustive
# - fatcontext
# - forbidigo
# - forcetypeassert
# - funlen
# - ginkgolinter
# - gocheckcompilerdirectives
# - gochecknoinits
# - gochecksumtype
- errorlint
- exhaustive
- fatcontext
- forbidigo
- forcetypeassert
- funlen
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoinits
- gochecksumtype
# - gocognit
# - goconst
# - gocritic
Expand Down Expand Up @@ -103,6 +103,10 @@ linters:
- third_party$
- builtin$
- examples$
rules:
- path: cmd/migrate/main.go
linters:
- forbidigo
formatters:
enable:
- gofmt
Expand Down
10 changes: 5 additions & 5 deletions e2e-tests/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -123,7 +122,7 @@ func doRequest(t *testing.T, base, uri, method string, payload []byte, resp inte
response, err := http.DefaultClient.Do(request)
require.NoError(t, err)

logRequest(request, response)
logRequest(t, request, response)

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

func logRequest(req *http.Request, resp *http.Response) {
func logRequest(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
if DebugHttpRequests {
rawBody, _ := io.ReadAll(resp.Body)

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

fmt.Printf("Request: %s: %s %v \n", req.Method, req.URL.RequestURI(), req.Body)
fmt.Println("Response: ", req.URL.RequestURI(), resp.StatusCode, string(rawBody))
t.Logf("Request: %s: %s %v", req.Method, req.URL.RequestURI(), req.Body)
t.Log("Response: ", req.URL.RequestURI(), resp.StatusCode, string(rawBody))
}
}
70 changes: 42 additions & 28 deletions internal/api/browser_extension/app/command/request_2fa_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,11 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
log := logging.FromContext(ctx)
extId, _ := uuid.Parse(cmd.ExtensionId)

browserExtension, err := h.BrowserExtensionsRepository.FindById(extId)
pairedDevices, err := h.findPairedDevices(extId, cmd)
if err != nil {
return nil, err
}

tokenRequestId, _ := uuid.Parse(cmd.Id)
browserExtension2FaRequest := domain.NewBrowserExtension2FaRequest(tokenRequestId, browserExtension.Id, cmd.Domain)

err = h.BrowserExtension2FaRequestRepository.Save(browserExtension2FaRequest)
if err != nil {
return nil, err
}

pairedDevices := h.PairedDevicesRepository.FindAll(browserExtension.Id)

data := map[string]interface{}{
"extension_id": extId.String(),
"request_id": cmd.Id,
Expand All @@ -115,23 +105,7 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
continue
}

var err error
var notification *messaging.Message

switch device.Platform {
case domain.Android:
notification = createPushNotificationForAndroid(device.FcmToken, data)
case domain.IOS:
notification = createPushNotificationForIos(device.FcmToken, data)
}

err = retry.Do(
func() error {
return h.Pusher.Send(ctx, notification)
},
retry.Attempts(5),
retry.LastErrorOnly(true),
)
err := h.sendNotification(ctx, device, data)
if err == nil {
result[device.Id.String()] = PushNotificationStatusOK
} else if messaging.IsUnregistered(err) {
Expand All @@ -153,6 +127,46 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
return result, nil
}

func (h *Request2FaTokenHandler) findPairedDevices(extId uuid.UUID, cmd *Request2FaToken) ([]*domain.ExtensionDevice, error) {
browserExtension, err := h.BrowserExtensionsRepository.FindById(extId)
if err != nil {
return nil, err
}

tokenRequestId, err := uuid.Parse(cmd.Id)
if err != nil {
return nil, fmt.Errorf("failed to parse token request id: %w", err)
}

browserExtension2FaRequest := domain.NewBrowserExtension2FaRequest(tokenRequestId, browserExtension.Id, cmd.Domain)

err = h.BrowserExtension2FaRequestRepository.Save(browserExtension2FaRequest)
if err != nil {
return nil, err
}

return h.PairedDevicesRepository.FindAll(browserExtension.Id), nil
}

func (h *Request2FaTokenHandler) sendNotification(ctx context.Context, device *domain.ExtensionDevice, data map[string]interface{}) error {
var notification *messaging.Message

switch device.Platform {
case domain.Android:
notification = createPushNotificationForAndroid(device.FcmToken, data)
case domain.IOS:
notification = createPushNotificationForIos(device.FcmToken, data)
}

return retry.Do(
func() error {
return h.Pusher.Send(ctx, notification)
},
retry.Attempts(5),
retry.LastErrorOnly(true),
)
}

func createPushNotificationForIos(token string, data map[string]interface{}) *messaging.Message {
ttl := time.Now().Add(tokenPushNotificationTtl)

Expand Down
2 changes: 1 addition & 1 deletion internal/api/browser_extension/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type BrowserExtensionModule struct {
Config config.Configuration
}

func NewBrowserExtensionModule(
func NewBrowserExtensionModule( //nolint:funlen // This is an initialization function.
config config.Configuration,
gorm *gorm.DB,
database *sql.DB,
Expand Down
Loading
Loading