2021-03-08 10:31:34 +00:00
# Cadmium Protocol core specifications
2019-11-12 16:39:24 +00:00
2021-03-08 10:31:34 +00:00
## 0. Table of content
2019-11-16 12:14:55 +00:00
2021-03-08 10:31:34 +00:00
- [Cadmium Protocol core specifications ](#cadmium-protocol-core-specifications )
- [0. Table of content ](#0-table-of-content )
- [1. Transport ](#1-transport )
- [2. Federated Entity ID ](#2-federated-entity-id )
- [2.1. Server-part ](#21-server-part )
- [2.2. Username/Room alias/RoomID ](#22-usernameroom-aliasroomid )
- [2.3. Special business rules ](#23-special-business-rules )
- [3. BaseMessage model ](#3-basemessage-model )
- [4. Protocol Errors ](#4-protocol-errors )
- [4.1. Use cases ](#41-use-cases )
- [4.2. JSON Schema ](#42-json-schema )
- [4.2.1. Payload ](#421-payload )
- [5. Basic request ratelimit system ](#5-basic-request-ratelimit-system )
- [5.1. Use cases ](#51-use-cases )
- [5.2. Error types ](#52-error-types )
- [5.2.1. Global error types ](#521-global-error-types )
- [5.3. Business Rules ](#53-business-rules )
- [5.4. JSON Schema ](#54-json-schema )
- [5.4.1. Error payload ](#541-error-payload )
- [6. User authorization ](#6-user-authorization )
- [6.1. Message Type Identifier ](#61-message-type-identifier )
- [6.2. Authorization types ](#62-authorization-types )
- [6.3. Common Flow ](#63-common-flow )
- [6.4. JSON Schema ](#64-json-schema )
2019-12-22 14:39:30 +00:00
2021-03-08 10:31:34 +00:00
## 1. Transport
2019-11-16 12:14:55 +00:00
2021-03-08 10:31:34 +00:00
We are using Websockets for underlying connections and JSON for message serialization format.
## 2. Federated Entity ID
2020-12-26 18:45:08 +00:00
Some reserved formats:
2019-12-22 14:39:30 +00:00
- Room alias: `#<roomAlias>@<serverpart>`
- Username: `@<username>@<serverpart>`
- User ID with any 3PID: `%<type>:<data>@<serverpart>`
- Currently supported only following types: `email` and `msisdn` .
- Raw User ID: `@<UUID>@<serverpart>`
2020-12-26 18:45:08 +00:00
- Message ID: `&msg:<uuid>@<serverpart of source server>`
- Media ID: `&media:<uuid>@<content service serverpart>`
- Group Chat ID: `!<roomID>@<serverpart>`
2019-12-22 14:39:30 +00:00
- Single server-part: `<serverpart>`
2019-11-12 16:39:24 +00:00
2021-03-08 10:31:34 +00:00
### 2.1. Server-part
2019-12-22 14:39:30 +00:00
2019-11-20 13:35:38 +00:00
- hostname: `IPv4 / [IPv6] / dns-domain:<port (1-65535)>` (for end-users use)
2020-07-16 08:28:21 +00:00
- server ID: static SHA256 hash string from 4096 characters (for internal protocol use).
- Generation example (using Linux): `echo -n $(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 4096 | head -n 1) | sha256sum`
2019-11-20 13:35:38 +00:00
2021-03-08 10:31:34 +00:00
### 2.2. Username/Room alias/RoomID
2019-11-12 16:39:24 +00:00
2019-12-29 13:01:35 +00:00
MUST NOT be empty, and MUST contain only the characters `a-z` , `0-9` , `.` , `_` , `=` , `-` , and `/` .
2021-03-08 10:31:34 +00:00
### 2.3. Special business rules
2019-12-22 14:39:30 +00:00
2021-03-08 10:31:34 +00:00
- Room ID MUST be UUID identifier.
2019-11-20 13:35:38 +00:00
- Servers MUST use server ID in internal purposes instead of normal server-part with hostname. Only end-users MUST use normal server-part with hostname. This is done for easy multi-domain serving.
2019-11-12 17:51:29 +00:00
2021-03-08 10:31:34 +00:00
## 3. BaseMessage model
2019-12-22 14:39:30 +00:00
2019-11-12 17:51:29 +00:00
BaseMessage is a basic message model, basis of the whole protocol. It is used for a very easy protocol extension process.
BaseMessage scheme:
2019-12-22 14:39:30 +00:00
2019-12-15 15:04:56 +00:00
```typescript
interface BaseMessage {
/**
* Message identifier (used to track the request-response chain)
*/
2019-12-15 15:08:48 +00:00
id: string,
2019-12-15 15:04:56 +00:00
/**
* Type of message (used to determine which extension this message belongs to)
*/
2019-12-15 15:08:48 +00:00
type: string,
2020-07-16 08:23:42 +00:00
2019-12-15 15:04:56 +00:00
/**
* From which entity this message is send
*/
2019-12-15 15:08:48 +00:00
from: EntityID,
2019-12-15 15:04:56 +00:00
/**
2020-07-16 08:23:42 +00:00
* Message recipients
2019-12-15 15:04:56 +00:00
*/
2020-09-05 17:17:34 +00:00
to: EntityID[],
2019-12-15 15:04:56 +00:00
/**
2019-12-29 13:01:35 +00:00
* Operation success indicator (used to determine if the error happened while processing request) - MUST be only in response from server
2019-12-15 15:04:56 +00:00
*/
2019-12-15 15:08:48 +00:00
ok: boolean,
2019-12-15 15:04:56 +00:00
2019-12-29 13:01:35 +00:00
/**
2021-03-08 10:31:34 +00:00
* Message payload (used to store extra information in message, list of permissible fields in the payload depends on "type" field)
2019-12-29 13:01:35 +00:00
*/
2021-03-08 10:31:34 +00:00
payload: Map< K , V >
}
```
## 4. Protocol Errors
Mechanism of error processing is included into protocol.
Adds into any response message `ok` variable. If `ok` is true - we have no errors, if `ok` is false - we have an error.
### 4.1. Use cases
Client:
```json
{
"id": "abcd",
"type": "incorrectMessageType",
"from": "@juliet@cadmium.im",
"to": "cadmium.im",
"payload": {
"test": "test"
}
}
```
Server:
```json
{
"id": "abcd",
"type": "incorrectMessageType",
"from": "cadmium.im",
"to": "@juliet@cadmium.im",
"ok": false,
"payload": {
"errID": "unhandled",
"errText": "Incorrect type of message (type isn't implemented in the server)",
"errPayload": {}
}
}
```
### 4.2. JSON Schema
2019-12-29 13:01:35 +00:00
2021-03-08 10:31:34 +00:00
#### 4.2.1. Payload
```typescript
interface ErrorPayload {
2019-12-15 15:04:56 +00:00
/**
2021-03-08 10:31:34 +00:00
* Error identifier (defined in extensions, maybe same per extensions)
2019-12-15 15:04:56 +00:00
*/
2021-03-08 10:31:34 +00:00
errID: string,
/**
* Explanation of error in human-readable view
*/
errText: string,
/**
* Advanced error information (fields defined in extensions)
*/
errPayload: Map< K , V >
}
```
## 5. Basic request ratelimit system
This system is intended to limit the number of requests from clients per unit of time.
### 5.1. Use cases
- Client:
```json
{
"id": "abcd",
"type": "profile:register",
"to": "cadmium.org",
"payload": {
"username": "spam_spam_spam",
"thirdPIDs": [],
"password": "spam"
}
}
```
- Server:
```json
{
"id": "abcd",
"type": "profile:register",
"from": "cadmium.org",
"ok": false,
"payload": {
"errID": "ratelimit_exceed",
"errText": "Request ratelimit exceed! Please, try again later!",
"errPayload": {
"retryAfter": 1000
}
}
}
```
### 5.2. Error types
#### 5.2.1. Global error types
- `ratelimit_exceed`
### 5.3. Business Rules
- Server MUST count number of requests per unit of time and drop new requests after specified number of made requests with Protocol Error message.
- Number of requests and used unit of time SHOULD be configurable on server
### 5.4. JSON Schema
#### 5.4.1. Error payload
```typescript
interface RatelimitExceedErrorPayload {
/**
* How long after the client can retry the request (in millis)
*/
retryAfter: number
}
```
## 6. User authorization
Authorization has multiple types which described in this document. Auth flow can vary depending on selected type of authentication. Common flow of auth is described in this document. When authorization was passed successfully, server must mark current session as authorized.
### 6.1. Message Type Identifier
`urn:cadmium:auth`
### 6.2. Authorization types
2021-03-08 13:07:58 +00:00
- `urn:cadmium:auth:login_password` - simple login/pass authorization
- `urn:cadmium:auth:token` - authorization by token
2021-03-08 10:31:34 +00:00
### 6.3. Common Flow
Flow for user authorization (in this example type of auth is `login_password` and `token` ):
1. User obtains correct user/pass pair.
2. User sends this pair to server:
C->S:
```json
{
"id": "abcd",
"type": "urn:cadmium:auth",
"to": ["cadmium.org"],
"payload": {
2021-03-08 13:07:58 +00:00
"type": "urn:cadmium:auth:login_password",
2021-03-08 10:31:34 +00:00
"fields": {
"username": "juliet",
"password": "romeo1"
}
}
}
```
S->C:
```json
{
"id": "abcd",
"type": "urn:cadmium:auth",
"from": "cadmium.org",
"ok": true,
"payload": {
"token": "3b5135a5-aff5-4396-a629-a254f383e82f",
"deviceID": "ABCDEFG"
}
}
```
3. Afterwards, user uses obtained auth token for every new session to authorize it:
C->S:
```json
{
"id": "abcd",
"type": "urn:cadmium:auth",
"to": ["cadmium.org"],
"payload": {
2021-03-08 13:07:58 +00:00
"type": "urn:cadmium:auth:token",
2021-03-08 10:31:34 +00:00
"fields": {
"token": "3b5135a5-aff5-4396-a629-a254f383e82f",
}
}
}
```
S->C:
```json
{
"id": "abcd",
"type": "urn:cadmium:auth",
"from": "cadmium.org",
"ok": true
}
```
### 6.4. JSON Schema
Request:
```typescript
interface AuthReq {
type: string; // type of authorization
fields: AuthFields; // auth fields (depends on type of auth)
}
interface AuthFields {}
2021-03-08 13:07:58 +00:00
// urn:cadmium:auth:login_password
2021-03-08 10:31:34 +00:00
interface LoginPassFields : AuthFields {
username: string;
password: string;
}
2021-03-08 13:07:58 +00:00
// urn:cadmium:auth:token
2021-03-08 10:31:34 +00:00
interface TokenFields : AuthFields {
token: string;
2019-12-15 15:04:56 +00:00
}
2019-12-22 14:39:30 +00:00
```
2021-03-08 10:31:34 +00:00
Response:
```typescript
interface AuthResp {
token: string; // newly created authorization token
deviceID: string; // ID of newly created device (session id in other words)
}
```