@@ -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+
156170func createPushNotificationForIos (token string , data map [string ]interface {}) * messaging.Message {
157171 ttl := time .Now ().Add (tokenPushNotificationTtl )
158172
0 commit comments