-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathCargohold.hs
More file actions
353 lines (328 loc) · 11 KB
/
Copy pathCargohold.hs
File metadata and controls
353 lines (328 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.
module Wire.API.Routes.Public.Cargohold where
import Data.Id
import Data.Kind
import Data.Metrics.Servant
import Data.Qualified
import Data.SOP
import Imports
import Servant
import Servant.OpenApi.Internal.Orphans ()
import URI.ByteString
import Wire.API.Asset
import Wire.API.Error
import Wire.API.Error.Cargohold
import Wire.API.Routes.API
import Wire.API.Routes.AssetBody
import Wire.API.Routes.MultiVerb
import Wire.API.Routes.Named
import Wire.API.Routes.Public
import Wire.API.Routes.QualifiedCapture
import Wire.API.Routes.Version
data PrincipalTag = UserPrincipalTag | BotPrincipalTag | ProviderPrincipalTag
deriving (Eq, Show)
instance RenderableSymbol UserPrincipalTag where
renderSymbol = "user"
instance RenderableSymbol BotPrincipalTag where
renderSymbol = "bot"
instance RenderableSymbol ProviderPrincipalTag where
renderSymbol = "provider"
type family PrincipalId (tag :: PrincipalTag) = (id :: Type) | id -> tag where
PrincipalId 'UserPrincipalTag = Local UserId
PrincipalId 'BotPrincipalTag = BotId
PrincipalId 'ProviderPrincipalTag = ProviderId
type family ApplyPrincipalPath (tag :: PrincipalTag) api
type instance
ApplyPrincipalPath 'UserPrincipalTag api =
ZLocalUser :> Until 'V2 :> "assets" :> "v3" :> api
type instance ApplyPrincipalPath 'BotPrincipalTag api = ZBot :> "bot" :> "assets" :> api
type instance ApplyPrincipalPath 'ProviderPrincipalTag api = ZProvider :> "provider" :> "assets" :> api
type instance
SpecialiseToVersion v ((tag :: PrincipalTag) :> api) =
SpecialiseToVersion v (ApplyPrincipalPath tag api)
instance (HasServer (ApplyPrincipalPath tag api) ctx) => HasServer (tag :> api) ctx where
type ServerT (tag :> api) m = ServerT (ApplyPrincipalPath tag api) m
route _ = route (Proxy @(ApplyPrincipalPath tag api))
hoistServerWithContext _ = hoistServerWithContext (Proxy @(ApplyPrincipalPath tag api))
instance (RoutesToPaths (ApplyPrincipalPath tag api)) => RoutesToPaths (tag :> api) where
getRoutes = getRoutes @(ApplyPrincipalPath tag api)
type AssetLocationHeader r =
'[DescHeader "Location" "Asset location" (AssetLocation r)]
type AssetRedirect =
WithHeaders
(AssetLocationHeader Absolute)
(AssetLocation Absolute)
(RespondEmpty 302 "Asset found")
type AssetStreaming =
RespondStreaming
200
"Asset returned directly with content type `application/octet-stream`"
OctetStream
type GetAsset =
MultiVerb
'GET
'[JSON]
'[ErrorResponse 'AssetNotFound, AssetRedirect]
(Maybe (AssetLocation Absolute))
type CargoholdAPI =
( Summary "Renew an asset token"
:> Until 'V2
:> CanThrow 'AssetNotFound
:> CanThrow 'Unauthorised
:> ZLocalUser
:> "assets"
:> "v3"
:> Capture "key" AssetKey
:> "token"
:> Post '[JSON] NewAssetToken
)
:<|> ( Summary "Delete an asset token"
:> Until 'V2
:> Description "**Note**: deleting the token makes the asset public."
:> ZLocalUser
:> "assets"
:> "v3"
:> Capture "key" AssetKey
:> "token"
:> MultiVerb
'DELETE
'[JSON]
'[RespondEmpty 200 "Asset token deleted"]
()
)
:<|> BaseAPIv3 'UserPrincipalTag
:<|> BaseAPIv3 'BotPrincipalTag
:<|> BaseAPIv3 'ProviderPrincipalTag
:<|> QualifiedAPI
:<|> LegacyAPI
:<|> MainAPI
-- | Asset API for a given principal (user/bot/provider).
--
-- This was introduced before API versioning, and the user endpoints contain a
-- v3 suffix, which is removed starting from API V2.
type BaseAPIv3 (tag :: PrincipalTag) =
Named
'("assets-upload-v3", tag)
( Summary "Upload an asset"
:> CanThrow 'AssetTooLarge
:> CanThrow 'InvalidLength
:> CanThrow 'IncompleteBody
:> tag
:> AssetBody
:> MultiVerb
'POST
'[JSON]
'[ WithHeaders
(AssetLocationHeader Relative)
(Asset, AssetLocation Relative)
(Respond 201 "Asset posted" Asset)
]
(Asset, AssetLocation Relative)
)
:<|> Named
'("assets-download-v3", tag)
( Summary "Download an asset"
:> tag
:> Capture "key" AssetKey
:> Header "Asset-Token" AssetToken
:> QueryParam "asset_token" AssetToken
:> ZHostOpt
:> GetAsset
)
:<|> Named
'("assets-delete-v3", tag)
( Summary "Delete an asset"
:> CanThrow 'AssetNotFound
:> CanThrow 'Unauthorised
:> tag
:> Capture "key" AssetKey
:> MultiVerb
'DELETE
'[JSON]
'[RespondEmpty 200 "Asset deleted"]
()
)
-- | Qualified asset API. Only download and delete endpoints are supported, as
-- upload has stayed unqualified. These endpoints also predate API versioning,
-- and contain a v4 suffix.
type QualifiedAPI =
Named
"assets-download-v4"
( Summary "Download an asset"
:> Until 'V2
:> Description
"**Note**: local assets result in a redirect, \
\while remote assets are streamed directly."
:> ZLocalUser
:> "assets"
:> "v4"
:> QualifiedCapture "key" AssetKey
:> Header "Asset-Token" AssetToken
:> QueryParam "asset_token" AssetToken
:> ZHostOpt
:> MultiVerb
'GET
'()
'[ ErrorResponse 'AssetNotFound,
AssetRedirect,
AssetStreaming
]
(Maybe LocalOrRemoteAsset)
)
:<|> Named
"assets-delete-v4"
( Summary "Delete an asset"
:> Until 'V2
:> Description "**Note**: only local assets can be deleted."
:> CanThrow 'AssetNotFound
:> CanThrow 'Unauthorised
:> ZLocalUser
:> "assets"
:> "v4"
:> QualifiedCapture "key" AssetKey
:> MultiVerb
'DELETE
'[JSON]
'[RespondEmpty 200 "Asset deleted"]
()
)
-- Old endpoints, predating BaseAPIv3, and therefore API versioning.
type LegacyAPI =
Named
"assets-download-legacy"
( ZLocalUser
:> Until 'V2
:> "assets"
:> QueryParam' [Required, Strict] "conv_id" ConvId
:> Capture "id" AssetId
:> GetAsset
)
:<|> Named
"assets-conv-download-legacy"
( ZLocalUser
:> Until 'V2
:> "conversations"
:> Capture "cnv" ConvId
:> "assets"
:> Capture "id" AssetId
:> GetAsset
)
:<|> Named
"assets-conv-otr-download-legacy"
( ZLocalUser
:> Until 'V2
:> "conversations"
:> Capture "cnv" ConvId
:> "otr"
:> "assets"
:> Capture "id" AssetId
:> GetAsset
)
-- | With API versioning, the previous ad-hoc v3/v4 versioning is abandoned, and
-- asset endpoints are versioned normally as part of the public API, without any
-- explicit prefix.
type MainAPI =
Named
"tokens-renew"
( Summary "Renew an asset token"
:> From 'V2
:> CanThrow 'AssetNotFound
:> CanThrow 'Unauthorised
:> ZLocalUser
:> "assets"
:> Capture "key" AssetKey
:> "token"
:> Post '[JSON] NewAssetToken
)
:<|> Named
"tokens-delete"
( Summary "Delete an asset token"
:> From 'V2
:> Description "**Note**: deleting the token makes the asset public."
:> ZLocalUser
:> "assets"
:> Capture "key" AssetKey
:> "token"
:> MultiVerb
'DELETE
'[JSON]
'[RespondEmpty 200 "Asset token deleted"]
()
)
:<|> Named
"assets-upload"
( Summary "Upload an asset"
:> From 'V2
:> CanThrow 'AssetTooLarge
:> CanThrow 'InvalidLength
:> CanThrow 'IncompleteBody
:> ZLocalUser
:> "assets"
:> AssetBody
:> MultiVerb
'POST
'[JSON]
'[ WithHeaders
(AssetLocationHeader Relative)
(Asset, AssetLocation Relative)
(Respond 201 "Asset posted" Asset)
]
(Asset, AssetLocation Relative)
)
:<|> Named
"assets-download"
( Summary "Download an asset"
:> From 'V2
:> Description
"**Note**: local assets result in a redirect, \
\while remote assets are streamed directly."
:> CanThrow 'NoMatchingAssetEndpoint
:> ZLocalUser
:> "assets"
:> QualifiedCapture "key" AssetKey
:> Header "Asset-Token" AssetToken
:> QueryParam "asset_token" AssetToken
:> ZHostOpt
:> MultiVerb
'GET
'()
'[ ErrorResponse 'AssetNotFound,
AssetRedirect,
AssetStreaming
]
(Maybe LocalOrRemoteAsset)
)
:<|> Named
"assets-delete"
( Summary "Delete an asset"
:> From 'V2
:> Description "**Note**: only local assets can be deleted."
:> CanThrow 'AssetNotFound
:> CanThrow 'Unauthorised
:> ZLocalUser
:> "assets"
:> QualifiedCapture "key" AssetKey
:> MultiVerb
'DELETE
'[JSON]
'[RespondEmpty 200 "Asset deleted"]
()
)
data CargoholdAPITag
instance ServiceAPI CargoholdAPITag v where
type ServiceAPIRoutes CargoholdAPITag = CargoholdAPI