obscura-server 0.3.9

A server for relaying secure messages using the Signal Protocol
Documentation
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
openapi: 3.0.3
info:
  title: Obscura Server API
  description: |
    A privacy-first messaging relay.
    - **Control Plane (REST):** Registration, Key Management, Sending.
    - **Data Plane (WebSocket):** Real-time message delivery and acknowledgement.
    - **Protocol:** Protocol Buffers (application/x-protobuf) are used for message bodies to minimize metadata leakage and binary bloat.
  version: 0.0.0
paths:
  # --- Account & Identity ---
  /v1/users:
    post:
      summary: Register a new user device.
      tags: [Account]
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RegistrationRequest'
      responses:
        '201':
          description: Account created.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthResponse'
        '400':
          description: Invalid input.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '409':
          description: Username already exists.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  /v1/sessions:
    post:
      summary: Login and retrieve a session token.
      tags: [Account]
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                username:
                  type: string
                password:
                  type: string
              required:
                - username
                - password
      responses:
        '200':
          description: Authenticated.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthResponse'
        '401':
          description: Unauthorized (Invalid credentials).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

    delete:
      summary: Logout (Revoke Refresh Token).
      description: |
        Revokes the provided Refresh Token, effectively logging the user out of that session.
      tags: [Account]
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              type: object
              required: [refreshToken]
              properties:
                refreshToken:
                  type: string
      responses:
        '200':
          description: Session revoked.
        '401':
          $ref: '#/components/responses/UnauthorizedError'

  /v1/sessions/refresh:
    post:
      summary: Refresh Access Token.
      description: |
        Exchanges a valid Refresh Token for a new pair (Access Token + Refresh Token).
        Implements Rotation: The old Refresh Token is invalidated immediately.
      tags: [Account]
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RefreshRequest'
      responses:
        '200':
          description: Tokens refreshed.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthResponse'
        '401':
          description: Invalid or expired refresh token.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  # --- Keys (X3DH) ---
  /v1/keys:
    post:
      summary: Upload PreKeys or Perform Device Takeover.
      description: |
        Uploads new Signed PreKeys and One-Time PreKeys.

        **Device Takeover:**
        If an `identityKey` is provided and it *differs* from the stored key for this user:
        - Replaces the Identity Key.
        - Deletes ALL old keys (Signed and One-Time).
        - Deletes ALL pending messages.
        - Disconnects active WebSockets.

        If `identityKey` matches the stored key or is omitted, it acts as a standard key refill (appending new keys).
      tags: [Keys]
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PreKeyUpload'
      responses:
        '200':
          description: Keys updated or takeover successful.
        '400':
          description: Invalid key formats, invalid signature, or pre-key limit exceeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          $ref: '#/components/responses/UnauthorizedError'

  /v1/keys/{userId}:
    get:
      summary: Fetch PreKey Bundle.
      description: |
        Returns active PreKey bundle for the target user.
        Server atomically consumes one One-Time PreKey (if available).
      tags: [Keys]
      parameters:
        - name: userId
          in: path
          required: true
          schema:
            type: string
            format: uuid
      responses:
        '200':
          description: PreKey Bundle.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PreKeyBundle'
        '404':
          description: User not found or no keys available.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  # --- Messaging (REST Upstream) ---
  /v1/messages/{recipientId}:
    post:
      summary: Send an encrypted message (Push).
      description: |
        Pushes an encrypted envelope to the target device's queue.
        **Payload:** `EncryptedMessage` (Protobuf).
        The recipient will receive this via WebSocket.
      tags: [Messaging]
      security:
        - bearerAuth: []
      parameters:
        - name: recipientId
          in: path
          required: true
          schema:
            type: string
            format: uuid
      requestBody:
        content:
          application/x-protobuf:
            schema:
              type: string
              format: binary
              description: Serialized `EncryptedMessage` protobuf.
          application/octet-stream:
             schema:
              type: string
              format: binary
              description: Serialized `EncryptedMessage` protobuf.
      responses:
        '201':
          description: Message queued.
        '400':
          description: Invalid Protobuf.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          $ref: '#/components/responses/UnauthorizedError'
        '404':
          description: Recipient not found.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  # --- WebSocket Gateway (Documentation Only) ---
  /v1/gateway:
    get:
      summary: Connect to the Message Stream.
      description: |
        **WebSocket Endpoint.**
        Clients must connect here to receive messages.
        - **Protocol:** `WebSocketFrame` (Protobuf).
        - **Auth:** Pass the JWT in the query string: `ws://.../v1/gateway?token=<jwt>`.
        - **Flow:** Server pushes `Envelope`. Client responds with `AckMessage`. Server deletes message.
      tags: [Messaging]
      parameters:
        - name: token
          in: query
          required: true
          schema:
            type: string
          description: JWT Access Token.
      responses:
        '101':
          description: Switching Protocols.
        '401':
          description: Unauthorized (Invalid or missing token).

  # --- Attachments (Binary Storage) ---
  /v1/attachments:
    post:
      summary: Upload an attachment.
      description: |
        Uploads an encrypted binary blob to long-term storage (S3).
        Blob will be auto-deleted after the configured TTL.
      tags: [Attachments]
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary
      responses:
        '201':
          description: Upload successful.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                    format: uuid
                  expiresAt:
                    type: integer
                    format: int64
                    description: UNIX timestamp of when the file will be deleted.
        '400':
          description: File too large.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '401':
          $ref: '#/components/responses/UnauthorizedError'

  /v1/attachments/{id}:
    get:
      summary: Download an attachment.
      tags: [Attachments]
      security:
        - bearerAuth: []
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
            format: uuid
      responses:
        '200':
          description: Binary file stream.
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
        '401':
          $ref: '#/components/responses/UnauthorizedError'
        '404':
          description: Not found or expired.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

  responses:
    UnauthorizedError:
      description: Unauthorized (Invalid or missing token).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'

  schemas:
    ErrorResponse:
      type: object
      required: [error]
      properties:
        error:
          type: string
          description: Semantic error message.

    AuthResponse:
      type: object
      required:
        - token
        - refreshToken
        - expiresAt
      properties:
        token:
          type: string
          description: Short-lived JWT Access Token.
        refreshToken:
          type: string
          description: Long-lived opaque Refresh Token.
        expiresAt:
          type: integer
          format: int64
          description: UNIX timestamp of access token expiration.

    RefreshRequest:
      type: object
      required: [refreshToken]
      properties:
        refreshToken:
          type: string

    RegistrationRequest:
      type: object
      required:
        - username
        - password
        - identityKey
        - registrationId
        - signedPreKey
        - oneTimePreKeys
      properties:
        username:
          type: string
        password:
          type: string
        identityKey:
          type: string
          format: byte
          description: Base64 encoded public identity key.
        registrationId:
          type: integer
        signedPreKey:
          $ref: '#/components/schemas/SignedPreKeyDto'
        oneTimePreKeys:
          type: array
          items:
            $ref: '#/components/schemas/OneTimePreKeyDto'

    PreKeyUpload:
      type: object
      required:
        - signedPreKey
        - oneTimePreKeys
      properties:
        identityKey:
          type: string
          format: byte
          description: Optional Base64 Identity Key. Triggers takeover if changed.
        registrationId:
          type: integer
          description: Required if identityKey is provided.
        signedPreKey:
          $ref: '#/components/schemas/SignedPreKeyDto'
          description: Signed Pre-Key. ID must be strictly greater than the current stored ID.
        oneTimePreKeys:
          type: array
          items:
            $ref: '#/components/schemas/OneTimePreKeyDto'

    PreKeyBundle:
      type: object
      properties:
        registrationId:
          type: integer
        identityKey:
          type: string
          format: byte
        signedPreKey:
          $ref: '#/components/schemas/SignedPreKeyDto'
        oneTimePreKey:
          $ref: '#/components/schemas/OneTimePreKeyDto'
          nullable: true

    SignedPreKeyDto:
      type: object
      required:
        - keyId
        - publicKey
        - signature
      properties:
        keyId:
          type: integer
        publicKey:
          type: string
          format: byte
          description: Base64 encoded public key
        signature:
          type: string
          format: byte
          description: Base64 encoded signature

    OneTimePreKeyDto:
      type: object
      required:
        - keyId
        - publicKey
      properties:
        keyId:
          type: integer
        publicKey:
          type: string
          format: byte
          description: Base64 encoded public key