@@ -10,13 +10,23 @@ import (
10
10
"github.com/avast/retry-go/v4"
11
11
"github.com/gin-gonic/gin"
12
12
"github.com/google/uuid"
13
+
13
14
"github.com/twofas/2fas-server/internal/api/browser_extension/domain"
14
15
"github.com/twofas/2fas-server/internal/common/logging"
15
16
"github.com/twofas/2fas-server/internal/common/push"
16
17
)
17
18
18
19
var tokenPushNotificationTtl = time .Minute * 3
19
20
21
+ type PushNotificationStatus string
22
+
23
+ const (
24
+ PushNotificationStatusOK = "ok"
25
+ PushNotificationStatusNoFCM = "no_fcm"
26
+ PushNotificationStatusError = "error"
27
+ PushNotificationStatusUnregistered = "unregistered"
28
+ )
29
+
20
30
type Request2FaTokenPushNotification struct {
21
31
ExtensionId string `json:"extension_id"`
22
32
IssuerDomain string `json:"issuer_domain"`
@@ -58,23 +68,21 @@ type Request2FaTokenHandler struct {
58
68
Pusher push.Pusher
59
69
}
60
70
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 ) {
62
72
log := logging .FromContext (ctx )
63
73
extId , _ := uuid .Parse (cmd .ExtensionId )
64
74
65
75
browserExtension , err := h .BrowserExtensionsRepository .FindById (extId )
66
-
67
76
if err != nil {
68
- return err
77
+ return nil , err
69
78
}
70
79
71
80
tokenRequestId , _ := uuid .Parse (cmd .Id )
72
81
browserExtension2FaRequest := domain .NewBrowserExtension2FaRequest (tokenRequestId , browserExtension .Id , cmd .Domain )
73
82
74
83
err = h .BrowserExtension2FaRequestRepository .Save (browserExtension2FaRequest )
75
-
76
84
if err != nil {
77
- return err
85
+ return nil , err
78
86
}
79
87
80
88
pairedDevices := h .PairedDevicesRepository .FindAll (browserExtension .Id )
@@ -86,6 +94,8 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
86
94
"type" : "browser_extension_request" ,
87
95
}
88
96
97
+ result := map [string ]PushNotificationStatus {}
98
+
89
99
for _ , device := range pairedDevices {
90
100
if device .FcmToken == "" {
91
101
log .WithFields (logging.Fields {
@@ -96,6 +106,7 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
96
106
"platform" : device .Platform ,
97
107
"type" : "browser_extension_request" ,
98
108
}).Info ("Cannot send push notification, missing FCM token" )
109
+ result [device .Id .String ()] = PushNotificationStatusNoFCM
99
110
continue
100
111
}
101
112
@@ -116,8 +127,12 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
116
127
retry .Attempts (5 ),
117
128
retry .LastErrorOnly (true ),
118
129
)
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
121
136
log .WithFields (logging.Fields {
122
137
"extension_id" : extId .String (),
123
138
"device_id" : device .Id .String (),
@@ -130,7 +145,7 @@ func (h *Request2FaTokenHandler) Handle(ctx context.Context, cmd *Request2FaToke
130
145
}
131
146
}
132
147
133
- return nil
148
+ return result , nil
134
149
}
135
150
136
151
func createPushNotificationForIos (token string , data map [string ]interface {}) * messaging.Message {
0 commit comments