Skip to content

Commit b95466d

Browse files
committed
Servantify: misc brig internal api
- `get "/i/users/:uid/contacts" (continue getContactListH)` - `get "/i/users/activation-code" (continue getActivationCodeH)` - `get "/i/users/password-reset-code" (continue getPasswordResetCodeH)`
1 parent d7b5f83 commit b95466d

File tree

8 files changed

+101
-71
lines changed

8 files changed

+101
-71
lines changed

libs/brig-types/default.nix

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
, imports
1515
, lib
1616
, QuickCheck
17-
, schema-profunctor
1817
, swagger2
1918
, tasty
2019
, tasty-hunit
@@ -39,8 +38,6 @@ mkDerivation {
3938
containers
4039
imports
4140
QuickCheck
42-
schema-profunctor
43-
swagger2
4441
text
4542
time
4643
tinylog

libs/brig-types/src/Brig/Types/Connection.hs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{-# LANGUAGE OverloadedStrings #-}
2-
31
-- This file is part of the Wire Server implementation.
42
--
53
-- Copyright (C) 2022 Wire Swiss GmbH <[email protected]>
@@ -32,15 +30,9 @@ import Data.Aeson
3230
import Data.Id (UserId)
3331
import Data.Qualified
3432
import Imports
33+
import Wire.API.User (UserIds (..))
3534
import Wire.Arbitrary
3635

37-
-- | Response type for endpoints returning lists of users with a specific connection state.
38-
-- E.g. 'getContactList' returns a 'UserIds' containing the list of connections in an
39-
-- 'Accepted' state.
40-
data UserIds = UserIds
41-
{cUsers :: [UserId]}
42-
deriving (Eq, Show, Generic)
43-
4436
-- FUTUREWORK: This needs to get Qualified IDs when implementing
4537
-- Legalhold + Federation, as it's used in the internal
4638
-- putConnectionInternal / galley->Brig "/i/users/connections-status"
@@ -59,15 +51,3 @@ instance FromJSON UpdateConnectionsInternal
5951

6052
-- | `{"tag":"BlockForMissingLHConsent","contents":["3ae7f23a-bd47-11eb-932d-5fccbbcde454",["3ae7f23a-bd47-11eb-932d-5fccbbcde454"]]}`
6153
instance ToJSON UpdateConnectionsInternal
62-
63-
----------------------------------------------------------------------------
64-
-- JSON instances
65-
66-
instance FromJSON UserIds where
67-
parseJSON = withObject "userids" $ \o ->
68-
UserIds <$> o .: "ids"
69-
70-
instance ToJSON UserIds where
71-
toJSON (UserIds us) =
72-
object
73-
["ids" .= us]

libs/wire-api/src/Wire/API/Routes/Internal/Brig.hs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ where
3939

4040
import Control.Lens ((.~))
4141
import Data.Aeson (FromJSON, ToJSON)
42-
import Data.ByteString.Conversion (List)
4342
import qualified Data.Code as Code
4443
import Data.CommaSeparatedList
4544
import Data.Handle (Handle)
@@ -251,6 +250,29 @@ type AccountAPI =
251250
Bool
252251
:> Get '[Servant.JSON] [UserAccount]
253252
)
253+
:<|> Named
254+
"iGetUserContacts"
255+
( "users"
256+
:> Capture "uid" UserId
257+
:> "contacts"
258+
:> Get '[Servant.JSON] UserIds
259+
)
260+
:<|> Named
261+
"iGetUserActivationCode"
262+
( "users"
263+
:> "activation-code"
264+
:> QueryParam' [Optional, Strict] "email" Email
265+
:> QueryParam' [Optional, Strict] "phone" Phone
266+
:> Get '[Servant.JSON] GetActivationCodeResp
267+
)
268+
:<|> Named
269+
"iGetUserPasswordResetCode"
270+
( "users"
271+
:> "password-reset-code"
272+
:> QueryParam' [Optional, Strict] "email" Email
273+
:> QueryParam' [Optional, Strict] "phone" Phone
274+
:> Get '[Servant.JSON] GetPasswordResetCodeResp
275+
)
254276

255277
-- | The missing ref is implicit by the capture
256278
data NewKeyPackageRef = NewKeyPackageRef

libs/wire-api/src/Wire/API/User.hs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
module Wire.API.User
2323
( ListUsersById (..),
2424
UserIdList (..),
25+
UserIds (..),
2526
QualifiedUserIdList (..),
2627
LimitedQualifiedUserIdList (..),
2728
ScimUserInfo (..),
@@ -115,6 +116,10 @@ module Wire.API.User
115116
-- * List Users
116117
ListUsersQuery (..),
117118

119+
-- * misc internal
120+
GetActivationCodeResp (..),
121+
GetPasswordResetCodeResp (..),
122+
118123
-- * re-exports
119124
module Wire.API.User.Identity,
120125
module Wire.API.User.Profile,
@@ -182,9 +187,10 @@ import Wire.API.Provider.Service (ServiceRef)
182187
import Wire.API.Routes.MultiVerb
183188
import Wire.API.Team (BindingNewTeam, bindingNewTeamObjectSchema)
184189
import Wire.API.Team.Role
185-
import Wire.API.User.Activation (ActivationCode)
190+
import Wire.API.User.Activation (ActivationCode, ActivationKey)
186191
import Wire.API.User.Auth (CookieLabel)
187192
import Wire.API.User.Identity
193+
import Wire.API.User.Password
188194
import Wire.API.User.Profile
189195
import Wire.API.User.RichInfo
190196
import Wire.Arbitrary (Arbitrary (arbitrary), GenericUniform (..))
@@ -224,6 +230,52 @@ instance ToSchema UserIdList where
224230
<$> mUsers
225231
.= field "user_ids" (array schema)
226232

233+
-- | Response type for endpoints returning lists of users with a specific connection state.
234+
-- E.g. 'getContactList' returns a 'UserIds' containing the list of connections in an
235+
-- 'Accepted' state.
236+
--
237+
-- There really shouldn't be both types `UserIds` and `UserIdList`, but refactoring them
238+
-- away requires changing the api.
239+
newtype UserIds = UserIds
240+
{cUsers :: [UserId]}
241+
deriving (Eq, Show, Generic)
242+
deriving newtype (Arbitrary)
243+
deriving (FromJSON, ToJSON, S.ToSchema) via Schema UserIds
244+
245+
instance ToSchema UserIds where
246+
schema =
247+
object "UserIds" $
248+
UserIds
249+
<$> cUsers
250+
.= field "ids" (array schema)
251+
252+
--------------------------------------------------------------------------------
253+
-- misc internal
254+
255+
newtype GetActivationCodeResp = GetActivationCodeResp {fromGetActivationCodeResp :: (ActivationKey, ActivationCode)}
256+
deriving (Eq, Show, Generic)
257+
deriving newtype (Arbitrary)
258+
deriving (FromJSON, ToJSON, S.ToSchema) via Schema GetActivationCodeResp
259+
260+
instance ToSchema GetActivationCodeResp where
261+
schema =
262+
object "GetActivationCodeResp" $
263+
curry GetActivationCodeResp
264+
<$> (fst . fromGetActivationCodeResp) .= field "key" schema
265+
<*> (snd . fromGetActivationCodeResp) .= field "code" schema
266+
267+
newtype GetPasswordResetCodeResp = GetPasswordResetCodeResp {fromGetPasswordResetCodeResp :: (PasswordResetKey, PasswordResetCode)}
268+
deriving (Eq, Show, Generic)
269+
deriving newtype (Arbitrary)
270+
deriving (FromJSON, ToJSON, S.ToSchema) via Schema GetPasswordResetCodeResp
271+
272+
instance ToSchema GetPasswordResetCodeResp where
273+
schema =
274+
object "GetPasswordResetCodeResp" $
275+
curry GetPasswordResetCodeResp
276+
<$> (fst . fromGetPasswordResetCodeResp) .= field "key" schema
277+
<*> (snd . fromGetPasswordResetCodeResp) .= field "code" schema
278+
227279
--------------------------------------------------------------------------------
228280
-- QualifiedUserIdList
229281

services/brig/src/Brig/API.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ sitemap ::
3838
( Member BlacklistPhonePrefixStore r,
3939
Member BlacklistStore r,
4040
Member GalleyProvider r,
41-
Member CodeStore r,
4241
Member (Concurrency 'Unsafe) r,
43-
Member PasswordResetStore r,
4442
Member (UserPendingActivationStore p) r
4543
) =>
4644
Routes () (Handler r) ()

services/brig/src/Brig/API/Internal.hs

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import qualified Brig.User.EJPD
6262
import qualified Brig.User.Search.Index as Index
6363
import Control.Error hiding (bool)
6464
import Control.Lens (view)
65-
import Data.Aeson hiding (json)
6665
import Data.ByteString.Conversion
6766
import qualified Data.ByteString.Conversion as List
6867
import Data.CommaSeparatedList
@@ -72,6 +71,7 @@ import qualified Data.Map.Strict as Map
7271
import Data.Qualified
7372
import qualified Data.Set as Set
7473
import Imports hiding (cs, head)
74+
import qualified Imports
7575
import Network.HTTP.Types.Status
7676
import Network.Wai (Response)
7777
import Network.Wai.Predicate hiding (result, setStatus)
@@ -98,7 +98,6 @@ import qualified Wire.API.Team.Feature as ApiFt
9898
import Wire.API.User
9999
import Wire.API.User.Activation
100100
import Wire.API.User.Client
101-
import Wire.API.User.Password
102101
import Wire.API.User.RichInfo
103102

104103
---------------------------------------------------------------------------
@@ -107,6 +106,8 @@ import Wire.API.User.RichInfo
107106
servantSitemap ::
108107
forall r p.
109108
( Member BlacklistStore r,
109+
Member CodeStore r,
110+
Member PasswordResetStore r,
110111
Member GalleyProvider r,
111112
Member (UserPendingActivationStore p) r
112113
) =>
@@ -153,6 +154,8 @@ mlsAPI =
153154

154155
accountAPI ::
155156
( Member BlacklistStore r,
157+
Member CodeStore r,
158+
Member PasswordResetStore r,
156159
Member GalleyProvider r,
157160
Member (UserPendingActivationStore p) r
158161
) =>
@@ -166,6 +169,9 @@ accountAPI =
166169
:<|> Named @"iGetUserStatus" getAccountStatusH
167170
:<|> Named @"iGetUsersByEmailOrPhone" listAccountsByIdentityH
168171
:<|> Named @"iGetUsersByIdsOrHandles" listActivatedAccountsH
172+
:<|> Named @"iGetUserContacts" getContactListH
173+
:<|> Named @"iGetUserActivationCode" getActivationCodeH
174+
:<|> Named @"iGetUserPasswordResetCode" getPasswordResetCodeH
169175

170176
teamsAPI :: ServerT BrigIRoutes.TeamsAPI (Handler r)
171177
teamsAPI = Named @"updateSearchVisibilityInbound" Index.updateSearchVisibilityInbound
@@ -293,9 +299,7 @@ internalSearchIndexAPI =
293299
-- Sitemap (wai-route)
294300

295301
sitemap ::
296-
( Member CodeStore r,
297-
Member PasswordResetStore r,
298-
Member BlacklistStore r,
302+
( Member BlacklistStore r,
299303
Member BlacklistPhonePrefixStore r,
300304
Member GalleyProvider r,
301305
Member (UserPendingActivationStore p) r
@@ -306,18 +310,6 @@ sitemap = unsafeCallsFed @'Brig @"on-user-deleted-connections" $ do
306310
accept "application" "json"
307311
.&. jsonRequest @UpdateConnectionsInternal
308312

309-
get "/i/users/:uid/contacts" (continue getContactListH) $
310-
accept "application" "json"
311-
.&. capture "uid"
312-
313-
get "/i/users/activation-code" (continue getActivationCodeH) $
314-
accept "application" "json"
315-
.&. (param "email" ||| param "phone")
316-
317-
get "/i/users/password-reset-code" (continue getPasswordResetCodeH) $
318-
accept "application" "json"
319-
.&. (param "email" ||| param "phone")
320-
321313
-- This endpoint can lead to the following events being sent:
322314
-- - UserIdentityRemoved event to target user
323315
post "/i/users/revoke-identity" (continue revokeIdentityH) $
@@ -579,42 +571,35 @@ listAccountsByIdentityH mbEmail mbPhone (fromMaybe False -> includePendingInvita
579571
u2 <- maybe (pure []) (\phone -> API.lookupAccountsByIdentity (Right phone) includePendingInvitations) mbPhone
580572
pure $ u1 <> u2
581573

582-
getActivationCodeH :: JSON ::: Either Email Phone -> (Handler r) Response
583-
getActivationCodeH (_ ::: emailOrPhone) = do
584-
json <$> getActivationCode emailOrPhone
574+
getActivationCodeH :: Maybe Email -> Maybe Phone -> (Handler r) GetActivationCodeResp
575+
getActivationCodeH (Just email) Nothing = getActivationCode (Left email)
576+
getActivationCodeH Nothing (Just phone) = getActivationCode (Right phone)
577+
getActivationCodeH bade badp = throwStd (badRequest ("need exactly one of email, phone: " <> Imports.cs (show (bade, badp))))
585578

586579
getActivationCode :: Either Email Phone -> (Handler r) GetActivationCodeResp
587580
getActivationCode emailOrPhone = do
588581
apair <- lift . wrapClient $ API.lookupActivationCode emailOrPhone
589582
maybe (throwStd activationKeyNotFound) (pure . GetActivationCodeResp) apair
590583

591-
newtype GetActivationCodeResp = GetActivationCodeResp (ActivationKey, ActivationCode)
592-
593-
instance ToJSON GetActivationCodeResp where
594-
toJSON (GetActivationCodeResp (k, c)) = object ["key" .= k, "code" .= c]
595-
596584
getPasswordResetCodeH ::
597585
( Member CodeStore r,
598586
Member PasswordResetStore r
599587
) =>
600-
JSON ::: Either Email Phone ->
601-
(Handler r) Response
602-
getPasswordResetCodeH (_ ::: emailOrPhone) = do
603-
maybe (throwStd (errorToWai @'E.InvalidPasswordResetKey)) (pure . json) =<< lift (getPasswordResetCode emailOrPhone)
588+
Maybe Email ->
589+
Maybe Phone ->
590+
(Handler r) GetPasswordResetCodeResp
591+
getPasswordResetCodeH (Just email) Nothing = getPasswordResetCode (Left email)
592+
getPasswordResetCodeH Nothing (Just phone) = getPasswordResetCode (Right phone)
593+
getPasswordResetCodeH bade badp = throwStd (badRequest ("need exactly one of email, phone: " <> Imports.cs (show (bade, badp))))
604594

605595
getPasswordResetCode ::
606596
( Member CodeStore r,
607597
Member PasswordResetStore r
608598
) =>
609599
Either Email Phone ->
610-
(AppT r) (Maybe GetPasswordResetCodeResp)
600+
(Handler r) GetPasswordResetCodeResp
611601
getPasswordResetCode emailOrPhone =
612-
GetPasswordResetCodeResp <$$> API.lookupPasswordResetCode emailOrPhone
613-
614-
newtype GetPasswordResetCodeResp = GetPasswordResetCodeResp (PasswordResetKey, PasswordResetCode)
615-
616-
instance ToJSON GetPasswordResetCodeResp where
617-
toJSON (GetPasswordResetCodeResp (k, c)) = object ["key" .= k, "code" .= c]
602+
(GetPasswordResetCodeResp <$$> lift (API.lookupPasswordResetCode emailOrPhone)) >>= maybe (throwStd (errorToWai @'E.InvalidPasswordResetKey)) pure
618603

619604
changeAccountStatusH :: UserId -> AccountStatusUpdate -> (Handler r) NoContent
620605
changeAccountStatusH usr (suStatus -> status) = do
@@ -798,7 +783,5 @@ checkHandleInternalH =
798783
API.CheckHandleFound -> pure $ setStatus status200 empty
799784
API.CheckHandleNotFound -> pure $ setStatus status404 empty
800785

801-
getContactListH :: JSON ::: UserId -> (Handler r) Response
802-
getContactListH (_ ::: uid) = do
803-
contacts <- lift . wrapClient $ API.lookupContactList uid
804-
pure $ json $ UserIds contacts
786+
getContactListH :: UserId -> (Handler r) UserIds
787+
getContactListH uid = lift . wrapClient $ UserIds <$> API.lookupContactList uid

services/brig/src/Brig/Data/Activation.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
-- | Activation of 'Email' addresses and 'Phone' numbers.
1919
module Brig.Data.Activation
2020
( Activation (..),
21-
ActivationKey (..),
22-
ActivationCode (..),
2321
ActivationEvent (..),
2422
ActivationError (..),
2523
activationErrorToRegisterError,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ where
3737

3838
import Bilge hiding (getHeader, options, statusCode)
3939
import Bilge.RPC
40-
import Brig.Types.Connection (UpdateConnectionsInternal, cUsers)
40+
import Brig.Types.Connection (UpdateConnectionsInternal)
4141
import qualified Brig.Types.Intra as Brig
4242
import Control.Error hiding (bool, isRight)
4343
import Control.Lens (view, (^.))

0 commit comments

Comments
 (0)