Skip to content

Commit 5b50820

Browse files
authored
WPB-18562 Propagate a rate limit error with status 429 on internal call to reauthenticate (#4673)
1 parent d4ec1c6 commit 5b50820

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

changelog.d/5-internal/WPB-18562

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A rate limit error from an internal call to `i/users/:uid/reauthenticate` will now be propagated to the external caller

integration/test/Test/Teams.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Test.Teams where
2121
import API.Brig
2222
import qualified API.BrigInternal as I
2323
import API.Common
24-
import API.Galley (getTeam, getTeamMembers, getTeamMembersCsv, getTeamNotifications)
24+
import API.Galley (deleteTeamMember, getTeam, getTeamMembers, getTeamMembersCsv, getTeamNotifications)
2525
import qualified API.GalleyInternal as I
2626
import API.Gundeck
2727
import qualified API.Nginz as Nginz
@@ -30,6 +30,7 @@ import Control.Monad.Extra (findM)
3030
import Control.Monad.Reader (asks)
3131
import qualified Data.ByteString.Char8 as B8
3232
import qualified Data.Map as Map
33+
import qualified Data.Set as Set
3334
import Data.Time.Clock
3435
import Data.Time.Format
3536
import Notifications
@@ -464,3 +465,12 @@ testUpgradeGuestToTeamShouldFail = do
464465

465466
upgradePersonalToTeam guest "wonderland" `bindResponse` \resp -> do
466467
resp.status `shouldMatchInt` 404
468+
469+
testDeleteTeamUserRatelimitingIsPropagated :: (HasCallStack) => App ()
470+
testDeleteTeamUserRatelimitingIsPropagated = do
471+
(owner, tid, mems) <- createTeam OwnDomain 10
472+
-- this is eventually going to run into rate limiting of internal request `/i/users/:uid/reauthenticate`
473+
statusCodes <- for mems $ \m -> do
474+
bindResponse (deleteTeamMember tid owner m) $ \resp -> do
475+
pure resp.status
476+
Set.fromList statusCodes `shouldMatchSet` ([202, 429] :: [Int])

libs/wire-api/src/Wire/API/Error/Galley.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,25 +362,31 @@ data AuthenticationError
362362
= ReAuthFailed
363363
| VerificationCodeAuthFailed
364364
| VerificationCodeRequired
365+
| RateLimitExceeded
366+
deriving (Show)
365367

366368
type instance MapError 'ReAuthFailed = 'StaticError 403 "access-denied" "This operation requires reauthentication"
367369

368370
type instance MapError 'VerificationCodeAuthFailed = 'StaticError 403 "code-authentication-failed" "Code authentication failed"
369371

370372
type instance MapError 'VerificationCodeRequired = 'StaticError 403 "code-authentication-required" "Verification code required"
371373

374+
type instance MapError 'RateLimitExceeded = 'StaticError 429 "too-many-requests" "Please try again later."
375+
372376
instance IsSwaggerError AuthenticationError where
373377
addToOpenApi =
374378
addStaticErrorToSwagger @(MapError 'ReAuthFailed)
375379
. addStaticErrorToSwagger @(MapError 'VerificationCodeAuthFailed)
376380
. addStaticErrorToSwagger @(MapError 'VerificationCodeRequired)
381+
. addStaticErrorToSwagger @(MapError 'RateLimitExceeded)
377382

378383
type instance ErrorEffect AuthenticationError = Error AuthenticationError
379384

380385
authenticationErrorToDyn :: AuthenticationError -> DynError
381386
authenticationErrorToDyn ReAuthFailed = dynError @(MapError 'ReAuthFailed)
382387
authenticationErrorToDyn VerificationCodeAuthFailed = dynError @(MapError 'VerificationCodeAuthFailed)
383388
authenticationErrorToDyn VerificationCodeRequired = dynError @(MapError 'VerificationCodeRequired)
389+
authenticationErrorToDyn RateLimitExceeded = dynError @(MapError 'RateLimitExceeded)
384390

385391
instance (Member (Error DynError) r) => ServerEffect (Error AuthenticationError) r where
386392
interpretServerEffect = mapError authenticationErrorToDyn

services/galley/src/Galley/Intra/User.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ reAuthUser uid auth = do
148148
method GET
149149
. paths ["/i/users", toByteString' uid, "reauthenticate"]
150150
. json auth
151-
resp <- call Brig (check [status200, status403] . req)
151+
resp <- call Brig (check [status200, status403, status429] . req)
152152
pure $ case (statusCode . responseStatus $ resp, errorLabel resp) of
153153
(200, _) -> Right ()
154154
(403, Just "code-authentication-required") -> Left VerificationCodeRequired
155155
(403, Just "code-authentication-failed") -> Left VerificationCodeAuthFailed
156156
(403, _) -> Left ReAuthFailed
157+
(429, _) -> Left RateLimitExceeded
157158
(_, _) -> Left ReAuthFailed
158159
where
159160
errorLabel :: ResponseLBS -> Maybe Lazy.Text

0 commit comments

Comments
 (0)