Skip to content

Commit b90e25a

Browse files
authored
ntf server: fix repeat token registration when it became invalid (regression) (#1539)
1 parent cf4b9f6 commit b90e25a

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/Simplex/Messaging/Notifications/Protocol.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,15 @@ data NtfTknStatus
536536
NTExpired
537537
deriving (Eq, Show)
538538

539+
allowTokenVerification :: NtfTknStatus -> Bool
540+
allowTokenVerification = \case
541+
NTNew -> False
542+
NTRegistered -> True
543+
NTInvalid _ -> False
544+
NTConfirmed -> True
545+
NTActive -> True
546+
NTExpired -> False
547+
539548
allowNtfSubCommands :: NtfTknStatus -> Bool
540549
allowNtfSubCommands = \case
541550
NTNew -> False

src/Simplex/Messaging/Notifications/Server.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{-# LANGUAGE FlexibleContexts #-}
66
{-# LANGUAGE GADTs #-}
77
{-# LANGUAGE LambdaCase #-}
8+
{-# LANGUAGE MultiWayIf #-}
89
{-# LANGUAGE NamedFieldPuns #-}
910
{-# LANGUAGE NumericUnderscores #-}
1011
{-# LANGUAGE OverloadedLists #-}
@@ -729,14 +730,17 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu
729730
logDebug "TNEW - registered token"
730731
let dhSecret = C.dh' dhPubKey tknDhPrivKey
731732
-- it is required that DH secret is the same, to avoid failed verifications if notification is delaying
732-
if tknDhSecret == dhSecret
733-
then do
733+
if
734+
| tknDhSecret /= dhSecret -> pure $ NRErr AUTH
735+
| allowTokenVerification tknStatus -> sendVerification
736+
| otherwise -> withNtfStore (\st -> updateTknStatus st tkn NTRegistered) $ \_ -> sendVerification
737+
where
738+
sendVerification = do
734739
atomically $ writeTBQueue pushQ (tkn, PNVerification tknRegCode)
735740
incNtfStatT token ntfVrfQueued
736741
pure $ NRTknId ntfTknId $ C.publicKey tknDhPrivKey
737-
else pure $ NRErr AUTH
738742
TVFY code -- this allows repeated verification for cases when client connection dropped before server response
739-
| (tknStatus == NTRegistered || tknStatus == NTConfirmed || tknStatus == NTActive) && tknRegCode == code -> do
743+
| allowTokenVerification tknStatus && tknRegCode == code -> do
740744
logDebug "TVFY - token verified"
741745
withNtfStore (`setTokenActive` tkn) $ \_ -> NROk <$ incNtfStatT token tknVerified
742746
| otherwise -> do

tests/AgentTests/NotificationTests.hs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import qualified Data.Text as T
5858
import Data.Text.Encoding (encodeUtf8)
5959
import qualified Data.Text.IO as TIO
6060
import Data.Time.Clock.System (systemToUTCTime)
61+
import qualified Database.PostgreSQL.Simple as PSQL
6162
import NtfClient
6263
import SMPAgentClient (agentCfg, initAgentServers, initAgentServers2, testDB, testDB2, testNtfServer, testNtfServer2)
6364
import SMPClient (cfgMS, cfgJ2QS, cfgVPrev, ntfTestPort, ntfTestPort2, serverStoreConfig, testPort, testPort2, withSmpServer, withSmpServerConfigOn, withSmpServerStoreLogOn, withSmpServerStoreMsgLogOn, xit'')
@@ -74,6 +75,7 @@ import Simplex.Messaging.Encoding.String
7475
import Simplex.Messaging.Notifications.Protocol
7576
import Simplex.Messaging.Notifications.Server.Env (NtfServerConfig (..))
7677
import Simplex.Messaging.Notifications.Server.Push.APNS
78+
import Simplex.Messaging.Notifications.Server.Store.Postgres (closeNtfDbStore, newNtfDbStore, withDB')
7779
import Simplex.Messaging.Notifications.Types (NtfTknAction (..), NtfToken (..))
7880
import Simplex.Messaging.Parsers (parseAll)
7981
import Simplex.Messaging.Protocol (ErrorType (AUTH), MsgFlags (MsgFlags), NMsgMeta (..), NtfServer, ProtocolServer (..), SMPMsgMeta (..), SubscriptionMode (..))
@@ -122,12 +124,10 @@ notificationTests ps@(t, _) = do
122124
it "should keep working with active token until replaced" $
123125
withAPNSMockServer $ \apns ->
124126
testNtfTokenChangeServers t apns
125-
-- TODO [ntfdb] modify database in the test
126-
xit "should re-register token in NTInvalid status after register attempt" $
127+
it "should re-register token in NTInvalid status after register attempt" $
127128
withAPNSMockServer $ \apns ->
128129
testNtfTokenReRegisterInvalid t apns
129-
-- TODO [ntfdb] modify database in the test
130-
xit "should re-register token in NTInvalid status after checking token" $
130+
it "should re-register token in NTInvalid status after checking token" $
131131
withAPNSMockServer $ \apns ->
132132
testNtfTokenReRegisterInvalidOnCheck t apns
133133
describe "notification server tests" $ do
@@ -489,7 +489,9 @@ testNtfTokenReRegisterInvalid t apns = do
489489
withNtfServer t $ pure ()
490490

491491
threadDelay 250000
492-
replaceSubstringInFile ntfTestStoreLogFile "tokenStatus=ACTIVE" "tokenStatus=INVALID"
492+
st <- newNtfDbStore ntfTestDBCfg
493+
Right 1 <- withDB' "test" st $ \db -> PSQL.execute db "UPDATE tokens SET status = ? WHERE status = ?" (NTInvalid Nothing, NTActive)
494+
closeNtfDbStore st
493495

494496
threadDelay 250000
495497
withNtfServer t $ do
@@ -518,7 +520,9 @@ testNtfTokenReRegisterInvalidOnCheck t apns = do
518520
withNtfServer t $ pure ()
519521

520522
threadDelay 250000
521-
replaceSubstringInFile ntfTestStoreLogFile "tokenStatus=ACTIVE" "tokenStatus=INVALID"
523+
st <- newNtfDbStore ntfTestDBCfg
524+
Right 1 <- withDB' "test" st $ \db -> PSQL.execute db "UPDATE tokens SET status = ? WHERE status = ?" (NTInvalid Nothing, NTActive)
525+
closeNtfDbStore st
522526

523527
threadDelay 250000
524528
withNtfServer t $ do

0 commit comments

Comments
 (0)