Skip to content

Commit 88c6452

Browse files
chore(request_2fa_token): return push status (#58)
In request_2fa_token endpoint, return status of push notification for each connected device.
1 parent 1063529 commit 88c6452

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ import (
1010
"github.com/avast/retry-go/v4"
1111
"github.com/gin-gonic/gin"
1212
"github.com/google/uuid"
13+
1314
"github.com/twofas/2fas-server/internal/api/browser_extension/domain"
1415
"github.com/twofas/2fas-server/internal/common/logging"
1516
"github.com/twofas/2fas-server/internal/common/push"
1617
)
1718

1819
var tokenPushNotificationTtl = time.Minute * 3
1920

21+
type PushNotificationStatus string
22+
23+
const (
24+
PushNotificationStatusOK = "ok"
25+
PushNotificationStatusNoFCM = "no_fcm"
26+
PushNotificationStatusError = "error"
27+
PushNotificationStatusUnregistered = "unregistered"
28+
)
29+
2030
type Request2FaTokenPushNotification struct {
2131
ExtensionId string `json:"extension_id"`
2232
IssuerDomain string `json:"issuer_domain"`
@@ -58,23 +68,21 @@ type Request2FaTokenHandler struct {
5868
Pusher push.Pusher
5969
}
6070

61-
func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToken) error {
71+
func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToken) (map[string]PushNotificationStatus, error) {
6272
log := logging.FromContext(ctx)
6373
extId, _ := uuid.Parse(cmd.ExtensionId)
6474

6575
browserExtension, err := h.BrowserExtensionsRepository.FindById(extId)
66-
6776
if err != nil {
68-
return err
77+
return nil, err
6978
}
7079

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

7483
err = h.BrowserExtension2FaRequestRepository.Save(browserExtension2FaRequest)
75-
7684
if err != nil {
77-
return err
85+
return nil, err
7886
}
7987

8088
pairedDevices := h.PairedDevicesRepository.FindAll(browserExtension.Id)
@@ -86,6 +94,8 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
8694
"type": "browser_extension_request",
8795
}
8896

97+
result := map[string]PushNotificationStatus{}
98+
8999
for _, device := range pairedDevices {
90100
if device.FcmToken == "" {
91101
log.WithFields(logging.Fields{
@@ -96,6 +106,7 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
96106
"platform": device.Platform,
97107
"type": "browser_extension_request",
98108
}).Info("Cannot send push notification, missing FCM token")
109+
result[device.Id.String()] = PushNotificationStatusNoFCM
99110
continue
100111
}
101112

@@ -116,8 +127,12 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
116127
retry.Attempts(5),
117128
retry.LastErrorOnly(true),
118129
)
119-
120-
if err != nil && !messaging.IsUnregistered(err) {
130+
if err == nil {
131+
result[device.Id.String()] = PushNotificationStatusOK
132+
} else if messaging.IsUnregistered(err) {
133+
result[device.Id.String()] = PushNotificationStatusUnregistered
134+
} else {
135+
result[device.Id.String()] = PushNotificationStatusError
121136
log.WithFields(logging.Fields{
122137
"extension_id": extId.String(),
123138
"device_id": device.Id.String(),
@@ -130,7 +145,7 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
130145
}
131146
}
132147

133-
return nil
148+
return result, nil
134149
}
135150

136151
func createPushNotificationForIos(token string, data map[string]interface{}) *messaging.Message {

internal/api/browser_extension/ports/http.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"github.com/go-playground/validator/v10"
66
"github.com/google/uuid"
77
"github.com/pkg/errors"
8+
"gorm.io/gorm"
9+
810
"github.com/twofas/2fas-server/internal/api/browser_extension/adapters"
911
"github.com/twofas/2fas-server/internal/api/browser_extension/app"
1012
"github.com/twofas/2fas-server/internal/api/browser_extension/app/command"
1113
"github.com/twofas/2fas-server/internal/api/browser_extension/app/query"
1214
"github.com/twofas/2fas-server/internal/api/browser_extension/domain"
1315
"github.com/twofas/2fas-server/internal/common/api"
1416
"github.com/twofas/2fas-server/internal/common/logging"
15-
"gorm.io/gorm"
1617
)
1718

1819
type RoutesHandler struct {
@@ -311,8 +312,7 @@ func (r *RoutesHandler) Request2FaToken(c *gin.Context) {
311312
return
312313
}
313314

314-
err = r.cqrs.Commands.Request2FaToken.Handle(c.Request.Context(), cmd)
315-
315+
pushResult, err := r.cqrs.Commands.Request2FaToken.Handle(c.Request.Context(), cmd)
316316
if err != nil {
317317
c.JSON(500, api.NewInternalServerError(err))
318318

@@ -325,13 +325,21 @@ func (r *RoutesHandler) Request2FaToken(c *gin.Context) {
325325
}
326326

327327
result, err := r.cqrs.Queries.BrowserExtension2FaRequestQuery.Handle(q)
328-
329328
if err != nil {
330329
c.JSON(500, api.NewInternalServerError(err))
331330
return
332331
}
333332

334-
c.JSON(200, result[0])
333+
var jsonResult struct {
334+
query.BrowserExtension2FaRequestPresenter
335+
PushStatus map[string]command.PushNotificationStatus `json:"push_status"`
336+
}
337+
if len(result) >= 0 && result[0] != nil {
338+
jsonResult.BrowserExtension2FaRequestPresenter = *result[0]
339+
}
340+
jsonResult.PushStatus = pushResult
341+
342+
c.JSON(200, jsonResult)
335343
}
336344

337345
func (r *RoutesHandler) Close2FaRequest(c *gin.Context) {

0 commit comments

Comments
 (0)