Skip to content

Commit

Permalink
WPB-6258 Connection request from deleted user (#3861)
Browse files Browse the repository at this point in the history
  • Loading branch information
battermann authored and elland committed Feb 21, 2024
1 parent e793394 commit d3d495a
Show file tree
Hide file tree
Showing 22 changed files with 541 additions and 109 deletions.
1 change: 1 addition & 0 deletions changelog.d/3-bug-fixes/WPB-6258
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Send connection cancelled event to local pending connection when user gets deleted
6 changes: 6 additions & 0 deletions integration/test/Notifications.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ isConvDeleteNotif n = fieldEquals n "payload.0.type" "conversation.delete"
isTeamMemberLeaveNotif :: MakesValue a => a -> App Bool
isTeamMemberLeaveNotif n = nPayload n %. "type" `isEqual` "team.member-leave"

isConnectionNotif :: MakesValue a => String -> a -> App Bool
isConnectionNotif status n =
(&&)
<$> nPayload n %. "type" `isEqual` "user.connection"
<*> nPayload n %. "connection.status" `isEqual` status

assertLeaveNotification ::
( HasCallStack,
MakesValue fromUser,
Expand Down
12 changes: 12 additions & 0 deletions integration/test/Test/Connection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Test.Connection where
import API.Brig (getConnection, postConnection, putConnection)
import API.BrigInternal
import API.Galley
import Notifications
import SetupHelpers
import Testlib.Prelude
import UnliftIO.Async (forConcurrently_)
Expand Down Expand Up @@ -401,3 +402,14 @@ testFederationAllowMixedConnectWithRemote =
connectTwoUsers alice bob
where
defSearchPolicy = "full_search"

testPendingConnectionUserDeleted :: HasCallStack => Domain -> App ()
testPendingConnectionUserDeleted bobsDomain = do
alice <- randomUser OwnDomain def
bob <- randomUser bobsDomain def

withWebSockets [bob] $ \[bobWs] -> do
void $ postConnection alice bob >>= getBody 201
void $ awaitMatch (isConnectionNotif "pending") bobWs
void $ deleteUser alice
void $ awaitMatch (isConnectionNotif "cancelled") bobWs
3 changes: 3 additions & 0 deletions libs/polysemy-wire-zoo/src/Wire/Sem/Paging/Cassandra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Data.Id
import Data.Qualified
import Data.Range
import Imports
import Wire.API.Connection (UserConnection)
import Wire.API.Team.Member (HardTruncationLimit, TeamMember)
import qualified Wire.Sem.Paging as E

Expand Down Expand Up @@ -97,6 +98,8 @@ type instance E.PagingBounds CassandraPaging TeamMember = Range 1 HardTruncation

type instance E.PagingBounds InternalPaging TeamId = Range 1 100 Int32

type instance E.PagingBounds InternalPaging (Remote UserConnection) = Range 1 1000 Int32

instance E.Paging InternalPaging where
pageItems (InternalPage (_, _, items)) = items
pageHasMore (InternalPage (p, _, _)) = hasMore p
Expand Down
2 changes: 1 addition & 1 deletion libs/types-common/src/Data/Range.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ import Test.QuickCheck qualified as QC
newtype Range (n :: Nat) (m :: Nat) a = Range
{ fromRange :: a
}
deriving (Eq, Ord, Show)
deriving (Eq, Ord, Show, Functor)

toRange :: (n <= x, x <= m, KnownNat x, Num a) => Proxy x -> Range n m a
toRange = Range . fromIntegral . natVal
Expand Down
4 changes: 4 additions & 0 deletions libs/wire-api/src/Wire/API/Routes/Internal/Brig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ type AccountAPI =
"createUserNoVerify"
( "users"
:> MakesFederatedCall 'Brig "on-user-deleted-connections"
:> MakesFederatedCall 'Brig "send-connection-action"
:> ReqBody '[Servant.JSON] NewUser
:> MultiVerb 'POST '[Servant.JSON] RegisterInternalResponses (Either RegisterError SelfProfile)
)
Expand All @@ -181,6 +182,7 @@ type AccountAPI =
( "users"
:> "spar"
:> MakesFederatedCall 'Brig "on-user-deleted-connections"
:> MakesFederatedCall 'Brig "send-connection-action"
:> ReqBody '[Servant.JSON] NewUserSpar
:> MultiVerb 'POST '[Servant.JSON] CreateUserSparInternalResponses (Either CreateUserSparError SelfProfile)
)
Expand Down Expand Up @@ -679,13 +681,15 @@ type AuthAPI =
"legalhold-login"
( "legalhold-login"
:> MakesFederatedCall 'Brig "on-user-deleted-connections"
:> MakesFederatedCall 'Brig "send-connection-action"
:> ReqBody '[JSON] LegalHoldLogin
:> MultiVerb1 'POST '[JSON] TokenResponse
)
:<|> Named
"sso-login"
( "sso-login"
:> MakesFederatedCall 'Brig "on-user-deleted-connections"
:> MakesFederatedCall 'Brig "send-connection-action"
:> ReqBody '[JSON] SsoLogin
:> QueryParam' [Optional, Strict] "persist" Bool
:> MultiVerb1 'POST '[JSON] TokenResponse
Expand Down
14 changes: 14 additions & 0 deletions libs/wire-api/src/Wire/API/Routes/Public/Brig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ type SelfAPI =
\password, it must be provided. if password is correct, or if neither \
\a verified identity nor a password exists, account deletion \
\is scheduled immediately."
:> MakesFederatedCall 'Brig "send-connection-action"
:> CanThrow 'InvalidUser
:> CanThrow 'InvalidCode
:> CanThrow 'BadCredentials
Expand All @@ -333,6 +334,7 @@ type SelfAPI =
Named
"put-self"
( Summary "Update your profile."
:> MakesFederatedCall 'Brig "send-connection-action"
:> ZUser
:> ZConn
:> "self"
Expand All @@ -358,6 +360,7 @@ type SelfAPI =
:> Description
"Your phone number can only be removed if you also have an \
\email address and a password."
:> MakesFederatedCall 'Brig "send-connection-action"
:> ZUser
:> ZConn
:> "self"
Expand All @@ -373,6 +376,7 @@ type SelfAPI =
:> Description
"Your email address can only be removed if you also have a \
\phone number."
:> MakesFederatedCall 'Brig "send-connection-action"
:> ZUser
:> ZConn
:> "self"
Expand Down Expand Up @@ -405,6 +409,7 @@ type SelfAPI =
:<|> Named
"change-locale"
( Summary "Change your locale."
:> MakesFederatedCall 'Brig "send-connection-action"
:> ZUser
:> ZConn
:> "self"
Expand All @@ -415,6 +420,8 @@ type SelfAPI =
:<|> Named
"change-handle"
( Summary "Change your handle."
:> MakesFederatedCall 'Brig "send-connection-action"
:> MakesFederatedCall 'Brig "send-connection-action"
:> ZUser
:> ZConn
:> "self"
Expand Down Expand Up @@ -477,6 +484,7 @@ type AccountAPI =
"If the environment where the registration takes \
\place is private and a registered email address or phone \
\number is not whitelisted, a 403 error is returned."
:> MakesFederatedCall 'Brig "send-connection-action"
:> "register"
:> ReqBody '[JSON] NewUserPublic
:> MultiVerb 'POST '[JSON] RegisterResponses (Either RegisterError RegisterSuccess)
Expand All @@ -487,6 +495,7 @@ type AccountAPI =
:<|> Named
"verify-delete"
( Summary "Verify account deletion with a code."
:> MakesFederatedCall 'Brig "send-connection-action"
:> CanThrow 'InvalidCode
:> "delete"
:> ReqBody '[JSON] VerifyDeleteUser
Expand All @@ -498,6 +507,7 @@ type AccountAPI =
:<|> Named
"get-activate"
( Summary "Activate (i.e. confirm) an email address or phone number."
:> MakesFederatedCall 'Brig "send-connection-action"
:> Description "See also 'POST /activate' which has a larger feature set."
:> CanThrow 'UserKeyExists
:> CanThrow 'InvalidActivationCodeWrongUser
Expand All @@ -524,6 +534,7 @@ type AccountAPI =
:> Description
"Activation only succeeds once and the number of \
\failed attempts for a valid key is limited."
:> MakesFederatedCall 'Brig "send-connection-action"
:> CanThrow 'UserKeyExists
:> CanThrow 'InvalidActivationCodeWrongUser
:> CanThrow 'InvalidActivationCodeWrongCode
Expand Down Expand Up @@ -728,6 +739,7 @@ type UserClientAPI =
Named
"add-client"
( Summary "Register a new client"
:> MakesFederatedCall 'Brig "send-connection-action"
:> CanThrow 'TooManyClients
:> CanThrow 'MissingAuth
:> CanThrow 'MalformedPrekeys
Expand Down Expand Up @@ -1334,6 +1346,7 @@ type AuthAPI =
\ Every other combination is invalid.\
\ Access tokens can be given as query parameter or authorisation\
\ header, with the latter being preferred."
:> MakesFederatedCall 'Brig "send-connection-action"
:> QueryParam "client_id" ClientId
:> Cookies '["zuid" ::: SomeUserToken]
:> Bearer SomeAccessToken
Expand Down Expand Up @@ -1364,6 +1377,7 @@ type AuthAPI =
( "login"
:> Summary "Authenticate a user to obtain a cookie and first access token"
:> Description "Logins are throttled at the server's discretion"
:> MakesFederatedCall 'Brig "send-connection-action"
:> ReqBody '[JSON] Login
:> QueryParam'
[ Optional,
Expand Down
2 changes: 2 additions & 0 deletions services/brig/brig.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ library
Brig.Effects.BlacklistStore.Cassandra
Brig.Effects.CodeStore
Brig.Effects.CodeStore.Cassandra
Brig.Effects.ConnectionStore
Brig.Effects.ConnectionStore.Cassandra
Brig.Effects.FederationConfigStore
Brig.Effects.FederationConfigStore.Cassandra
Brig.Effects.GalleyProvider
Expand Down
29 changes: 24 additions & 5 deletions services/brig/src/Brig/API/Auth.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Brig.API.User
import Brig.App
import Brig.Data.User qualified as User
import Brig.Effects.BlacklistStore
import Brig.Effects.ConnectionStore (ConnectionStore)
import Brig.Effects.GalleyProvider
import Brig.Options
import Brig.User.Auth qualified as Auth
Expand All @@ -37,24 +38,30 @@ import Data.List1 (List1 (..))
import Data.Qualified
import Data.Text qualified as T
import Data.Text.Lazy qualified as LT
import Data.Time.Clock (UTCTime)
import Data.ZAuth.Token qualified as ZAuth
import Imports
import Network.HTTP.Types
import Network.Wai.Utilities ((!>>))
import Network.Wai.Utilities.Error qualified as Wai
import Polysemy
import Polysemy.Input (Input)
import Polysemy.TinyLog (TinyLog)
import Wire.API.User
import Wire.API.User.Auth hiding (access)
import Wire.API.User.Auth.LegalHold
import Wire.API.User.Auth.ReAuth
import Wire.API.User.Auth.Sso
import Wire.NotificationSubsystem
import Wire.Sem.Paging.Cassandra (InternalPaging)

accessH ::
( Member TinyLog r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r
Member NotificationSubsystem r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r
) =>
Maybe ClientId ->
[Either Text SomeUserToken] ->
Expand All @@ -70,7 +77,10 @@ access ::
( TokenPair u a,
Member TinyLog r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r
Member NotificationSubsystem r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r
) =>
Maybe ClientId ->
NonEmpty (Token u) ->
Expand All @@ -90,7 +100,10 @@ login ::
( Member GalleyProvider r,
Member TinyLog r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r
Member NotificationSubsystem r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r
) =>
Login ->
Maybe Bool ->
Expand Down Expand Up @@ -150,7 +163,10 @@ legalHoldLogin ::
( Member GalleyProvider r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member TinyLog r
Member TinyLog r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r
) =>
LegalHoldLogin ->
Handler r SomeAccess
Expand All @@ -162,7 +178,10 @@ legalHoldLogin lhl = do
ssoLogin ::
( Member TinyLog r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r
Member NotificationSubsystem r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r
) =>
SsoLogin ->
Maybe Bool ->
Expand Down
24 changes: 20 additions & 4 deletions services/brig/src/Brig/API/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import Brig.App
import Brig.Data.Client qualified as Data
import Brig.Data.Nonce as Nonce
import Brig.Data.User qualified as Data
import Brig.Effects.ConnectionStore (ConnectionStore)
import Brig.Effects.GalleyProvider (GalleyProvider)
import Brig.Effects.GalleyProvider qualified as GalleyProvider
import Brig.Effects.JwtTools (JwtTools)
Expand Down Expand Up @@ -86,10 +87,12 @@ import Data.Map.Strict qualified as Map
import Data.Misc (PlainTextPassword6)
import Data.Qualified
import Data.Set qualified as Set
import Data.Time.Clock (UTCTime)
import Imports
import Network.HTTP.Types.Method (StdMethod)
import Network.Wai.Utilities
import Polysemy
import Polysemy.Input (Input)
import Polysemy.TinyLog
import Servant (Link, ToHttpApiData (toUrlPiece))
import System.Logger.Class (field, msg, val, (~~))
Expand All @@ -110,6 +113,7 @@ import Wire.NotificationSubsystem
import Wire.Sem.Concurrency
import Wire.Sem.FromUTC (FromUTC (fromUTCTime))
import Wire.Sem.Now as Now
import Wire.Sem.Paging.Cassandra (InternalPaging)

lookupLocalClient :: UserId -> ClientId -> (AppT r) (Maybe Client)
lookupLocalClient uid = wrapClient . Data.lookupClient uid
Expand Down Expand Up @@ -158,7 +162,10 @@ addClient ::
( Member GalleyProvider r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member TinyLog r
Member TinyLog r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r
) =>
UserId ->
Maybe ConnId ->
Expand All @@ -173,7 +180,10 @@ addClientWithReAuthPolicy ::
( Member GalleyProvider r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member TinyLog r
Member TinyLog r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r
) =>
Data.ReAuthPolicy ->
UserId ->
Expand Down Expand Up @@ -475,7 +485,10 @@ pubClient c =
legalHoldClientRequested ::
( Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member TinyLog r
Member TinyLog r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r
) =>
UserId ->
LegalHoldClientRequest ->
Expand All @@ -493,7 +506,10 @@ legalHoldClientRequested targetUser (LegalHoldClientRequest _requester lastPreke
removeLegalHoldClient ::
( Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member TinyLog r
Member TinyLog r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r
) =>
UserId ->
AppT r ()
Expand Down
Loading

0 comments on commit d3d495a

Please sign in to comment.