korero-chat 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -2
- package/dist/LICENSE +661 -0
- package/dist/auth.d.ts +23 -0
- package/dist/auth.js +22 -0
- package/dist/channel.d.ts +44 -0
- package/dist/channel.js +56 -0
- package/dist/direct-crypto.d.ts +26 -0
- package/dist/direct-crypto.js +79 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +20 -0
- package/dist/http.d.ts +286 -0
- package/dist/http.js +95 -0
- package/dist/identity.d.ts +12 -0
- package/dist/identity.js +72 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +8 -0
- package/dist/session.d.ts +143 -0
- package/dist/session.js +648 -0
- package/dist/transport.d.ts +16 -0
- package/dist/transport.js +28 -0
- package/dist/types.d.ts +218 -0
- package/dist/types.js +12 -0
- package/package.json +36 -4
- package/src/auth.ts +42 -0
- package/src/channel.ts +71 -0
- package/src/direct-crypto.ts +90 -0
- package/src/errors.ts +20 -0
- package/src/http.ts +216 -0
- package/src/identity.ts +84 -0
- package/src/index.ts +18 -0
- package/src/session.ts +648 -0
- package/src/transport.ts +38 -0
- package/src/types.ts +198 -0
package/src/http.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { serverOrigin, webTransport } from "./transport.js";
|
|
2
|
+
import type { Account, ServerInfo } from "./types.js";
|
|
3
|
+
|
|
4
|
+
type Query = Record<string, string | number | boolean | undefined>;
|
|
5
|
+
type QueryOptions<T extends Query> = { query: T };
|
|
6
|
+
type Requester = (request: Request) => Promise<Response>;
|
|
7
|
+
type Empty = Record<string, never>;
|
|
8
|
+
|
|
9
|
+
export type ApiResult<T> =
|
|
10
|
+
| { data: T; error: null; status: number }
|
|
11
|
+
| { data: null; error: { status: number; value: unknown }; status: number };
|
|
12
|
+
|
|
13
|
+
export type ChannelPermission = "join" | "speak" | "text" | "deleteChannels" | "mute" |
|
|
14
|
+
"attachFiles" | "shareScreen" | "useCamera" | "whisper";
|
|
15
|
+
export type Permission = ChannelPermission | "createChannels" | "editChannels" | "move" | "kick" |
|
|
16
|
+
"ban" | "manageRoles" | "manageInvitations" | "manageSettings" | "viewAdmin" | "sendDirectMessages" | "removeChannels";
|
|
17
|
+
|
|
18
|
+
export interface ChannelPermissionTable {
|
|
19
|
+
editable: ChannelPermission[];
|
|
20
|
+
roles: { name: string; permissions: Record<ChannelPermission, { allow: boolean | null; inherited: boolean }> }[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface AccountPage {
|
|
24
|
+
accounts: (Account & { editable: boolean; bannable: boolean; banned: boolean })[];
|
|
25
|
+
next: string | null;
|
|
26
|
+
roles: string[];
|
|
27
|
+
builtInRoles: (RoleDefinition & { editable: boolean; defaultPermissions: Permission[] })[];
|
|
28
|
+
customRoles: RoleDefinition[];
|
|
29
|
+
assignablePermissions: Permission[];
|
|
30
|
+
canDefineRoles: boolean;
|
|
31
|
+
defaultRole: string;
|
|
32
|
+
registrationPermissions: Permission[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface RoleDefinition { name: string; permissions: Permission[] }
|
|
36
|
+
|
|
37
|
+
export interface AuditPage {
|
|
38
|
+
events: {
|
|
39
|
+
id: number;
|
|
40
|
+
action: string;
|
|
41
|
+
actorId: string | null;
|
|
42
|
+
actorName: string | null;
|
|
43
|
+
targetId: string | null;
|
|
44
|
+
targetName: string | null;
|
|
45
|
+
createdAt: number;
|
|
46
|
+
details: Record<string, string | number | boolean | string[] | null> | null;
|
|
47
|
+
}[];
|
|
48
|
+
next: number | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface ServerPreferences {
|
|
52
|
+
motd: string;
|
|
53
|
+
defaultRole: string;
|
|
54
|
+
canSetDefaultRole: boolean;
|
|
55
|
+
defaultRoles: string[];
|
|
56
|
+
defaultAudioBitrate: number | null;
|
|
57
|
+
configuredAudioBitrate: number;
|
|
58
|
+
audioBitrateLocked: boolean;
|
|
59
|
+
maxAttachmentMiB: number;
|
|
60
|
+
maxSupportedAttachmentMiB: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface Invitation {
|
|
64
|
+
id: string;
|
|
65
|
+
createdBy: string;
|
|
66
|
+
expiresAt: number | null;
|
|
67
|
+
usesLeft: number | null;
|
|
68
|
+
revokedAt: number | null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface ConnectionDetails {
|
|
72
|
+
displayName: string;
|
|
73
|
+
channelName: string | null;
|
|
74
|
+
connectedSeconds: number;
|
|
75
|
+
latencyMs: number | null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ServerCapabilities {
|
|
79
|
+
protocolVersions: number[];
|
|
80
|
+
websocketPath: string;
|
|
81
|
+
audio: string;
|
|
82
|
+
targetAudioBitrate: number;
|
|
83
|
+
heartbeatSeconds: number;
|
|
84
|
+
maxWebSocketBytes: number;
|
|
85
|
+
udp: boolean;
|
|
86
|
+
tcp: boolean;
|
|
87
|
+
textPersistence: boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface AttachmentUpload {
|
|
91
|
+
connectionId: string;
|
|
92
|
+
channelId: string;
|
|
93
|
+
text: string;
|
|
94
|
+
html?: string;
|
|
95
|
+
name: string;
|
|
96
|
+
data: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface ChannelOrder {
|
|
100
|
+
channelId: string;
|
|
101
|
+
targetId: string;
|
|
102
|
+
placement: "before" | "after" | "inside";
|
|
103
|
+
parentId?: string | null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Typed v1 HTTP endpoints. The requester supplies credentials and any platform transport. */
|
|
107
|
+
export function createApi(origin: string, send: Requester = (request) => webTransport.fetch(request, { timeout: 8000 })) {
|
|
108
|
+
async function request<T>(method: string, path: string, body?: unknown, query?: Query): Promise<ApiResult<T>> {
|
|
109
|
+
const url = new URL("/api/v1/" + path, origin);
|
|
110
|
+
for (const [key, value] of Object.entries(query ?? {})) {
|
|
111
|
+
if (value !== undefined) url.searchParams.set(key, String(value));
|
|
112
|
+
}
|
|
113
|
+
const response = await send(new Request(url, {
|
|
114
|
+
method,
|
|
115
|
+
headers: body === undefined ? undefined : { "content-type": "application/json" },
|
|
116
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
117
|
+
}));
|
|
118
|
+
const text = await response.text();
|
|
119
|
+
let value: unknown = text;
|
|
120
|
+
if (text) {
|
|
121
|
+
try { value = JSON.parse(text); } catch {}
|
|
122
|
+
} else value = null;
|
|
123
|
+
const { status } = response;
|
|
124
|
+
return response.ok ? { data: value as T, error: null, status } : { data: null, error: { status, value }, status };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const accountPicture = Object.assign(
|
|
128
|
+
({ accountId }: { accountId: string }) => ({
|
|
129
|
+
get: () => request<{ avatarId: string | null; data: string | null }>("GET", "account/picture/" + encodeURIComponent(accountId)),
|
|
130
|
+
}),
|
|
131
|
+
{ patch: (body: { data: string | null }) => request<{ avatarId: string | null }>("PATCH", "account/picture", body) },
|
|
132
|
+
);
|
|
133
|
+
const attachments = Object.assign(
|
|
134
|
+
({ messageId }: { messageId: string }) => ({
|
|
135
|
+
get: ({ query }: QueryOptions<{ connectionId: string }>) =>
|
|
136
|
+
request<{ data: string }>("GET", "chat/attachments/" + encodeURIComponent(messageId), undefined, query),
|
|
137
|
+
}),
|
|
138
|
+
{ post: (body: AttachmentUpload) => request<{ messageId: string }>("POST", "chat/attachments", body) },
|
|
139
|
+
);
|
|
140
|
+
const channels = Object.assign(
|
|
141
|
+
({ id }: { id: string }) => {
|
|
142
|
+
const path = "channels/" + encodeURIComponent(id) + "/permissions";
|
|
143
|
+
return { permissions: {
|
|
144
|
+
get: () => request<ChannelPermissionTable>("GET", path),
|
|
145
|
+
patch: (body: { changes: { role: string; permission: ChannelPermission; allow: boolean | null }[] }) =>
|
|
146
|
+
request<ChannelPermissionTable>("PATCH", path, body),
|
|
147
|
+
} };
|
|
148
|
+
},
|
|
149
|
+
{ order: { patch: (body: ChannelOrder) => request<Empty>("PATCH", "channels/order", body) } },
|
|
150
|
+
);
|
|
151
|
+
const invitations = Object.assign(
|
|
152
|
+
({ id }: { id: string }) => {
|
|
153
|
+
const path = "admin/invitations/" + encodeURIComponent(id);
|
|
154
|
+
return {
|
|
155
|
+
patch: (body: { expiresAt?: number | null; usesLeft?: number | null }) => request<Invitation>("PATCH", path, body),
|
|
156
|
+
delete: () => request<Empty>("DELETE", path),
|
|
157
|
+
};
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
get: () => request<{ invitations: Invitation[] }>("GET", "admin/invitations"),
|
|
161
|
+
post: (body: { expiresAt?: number; expiresInSeconds?: number; uses?: number }) =>
|
|
162
|
+
request<Pick<Invitation, "id" | "expiresAt" | "usesLeft"> & { token: string }>("POST", "admin/invitations", body),
|
|
163
|
+
},
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
server: {
|
|
168
|
+
info: { get: () => request<ServerInfo>("GET", "server/info") },
|
|
169
|
+
capabilities: { get: () => request<ServerCapabilities>("GET", "server/capabilities") },
|
|
170
|
+
},
|
|
171
|
+
account: {
|
|
172
|
+
get: () => request<Account>("GET", "account"),
|
|
173
|
+
patch: (body: { displayName: string }) => request<Account>("PATCH", "account", body),
|
|
174
|
+
picture: accountPicture,
|
|
175
|
+
sessions: Object.assign(
|
|
176
|
+
({ id }: { id: string }) => ({ delete: () => request<Empty>("DELETE", "account/sessions/" + encodeURIComponent(id)) }),
|
|
177
|
+
{ get: () => request<{ id: string; deviceName: string; createdAt: number; lastActive: number; expiresAt: number }[]>("GET", "account/sessions") },
|
|
178
|
+
),
|
|
179
|
+
},
|
|
180
|
+
connections: ({ id }: { id: string }) => ({
|
|
181
|
+
get: () => request<ConnectionDetails>("GET", "connections/" + encodeURIComponent(id)),
|
|
182
|
+
}),
|
|
183
|
+
channels,
|
|
184
|
+
chat: { attachments },
|
|
185
|
+
admin: {
|
|
186
|
+
accounts: { get: (options?: QueryOptions<{ after?: string }>) => request<AccountPage>("GET", "admin/accounts", undefined, options?.query) },
|
|
187
|
+
badges: { patch: (body: { accountId: string; badge: string | null }) => request<{ badge: string | null }>("PATCH", "admin/badges", body) },
|
|
188
|
+
roles: {
|
|
189
|
+
patch: (body: { accountId: string; role: string }) => request<{ role: string }>("PATCH", "admin/roles", body),
|
|
190
|
+
post: (body: RoleDefinition) => request<RoleDefinition>("POST", "admin/roles", body),
|
|
191
|
+
definition: {
|
|
192
|
+
patch: (body: RoleDefinition) => request<RoleDefinition>("PATCH", "admin/roles/definition", body),
|
|
193
|
+
delete: (body: { name: string }) => request<Empty>("DELETE", "admin/roles/definition", body),
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
audit: { get: (options?: QueryOptions<{ before?: number; prefix?: string; actorId?: string }>) =>
|
|
197
|
+
request<AuditPage>("GET", "admin/audit", undefined, options?.query) },
|
|
198
|
+
preferences: {
|
|
199
|
+
get: () => request<ServerPreferences>("GET", "admin/preferences"),
|
|
200
|
+
patch: (body: Partial<Pick<ServerPreferences, "motd" | "defaultRole" | "defaultAudioBitrate" | "maxAttachmentMiB">>) =>
|
|
201
|
+
request<ServerPreferences>("PATCH", "admin/preferences", body),
|
|
202
|
+
},
|
|
203
|
+
banner: { patch: (body: { bannerImageUrl: string | null }) => request<typeof body>("PATCH", "admin/banner", body) },
|
|
204
|
+
invitations,
|
|
205
|
+
password: {
|
|
206
|
+
get: () => request<{ passwordRequired: boolean; passwordLocked: boolean }>("GET", "admin/password"),
|
|
207
|
+
patch: (body: { password: string | null }) => request<{ passwordRequired: boolean }>("PATCH", "admin/password", body),
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Public server information is available before signing in. */
|
|
214
|
+
export function serverApi(address: string) {
|
|
215
|
+
return createApi(serverOrigin(address)).server;
|
|
216
|
+
}
|
package/src/identity.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
2
|
+
export interface DeviceIdentity { publicKey: string; privateKey: string }
|
|
3
|
+
|
|
4
|
+
const encoder = new TextEncoder();
|
|
5
|
+
const header = encoder.encode("KORERO-IDENTITY-1");
|
|
6
|
+
const pkcs8Header = [0x30, 0x51, 2, 1, 1, 0x30, 5, 6, 3, 0x2b, 0x65, 0x70, 4, 0x22, 4, 0x20];
|
|
7
|
+
|
|
8
|
+
export function encodeBase64Url(bytes: ArrayBuffer | Uint8Array) {
|
|
9
|
+
const data = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
10
|
+
let binary = "";
|
|
11
|
+
for (let offset = 0; offset < data.length; offset += 8192) {
|
|
12
|
+
binary += String.fromCharCode(...data.subarray(offset, offset + 8192));
|
|
13
|
+
}
|
|
14
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function decodeBase64Url(value: string) {
|
|
18
|
+
return Uint8Array.from(atob(value.replace(/-/g, "+").replace(/_/g, "/")), (character) => character.charCodeAt(0));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function checkEnvelope(pkcs8: Uint8Array) {
|
|
22
|
+
if (pkcs8.length !== 83 || !pkcs8Header.every((byte, index) => byte === pkcs8[index]) ||
|
|
23
|
+
pkcs8[48] !== 0x81 || pkcs8[49] !== 0x21 || pkcs8[50] !== 0) throw new Error("Invalid Korero.chat identity key.");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function identitySeed(privateKey: string) {
|
|
27
|
+
const pkcs8 = decodeBase64Url(privateKey);
|
|
28
|
+
checkEnvelope(pkcs8);
|
|
29
|
+
return pkcs8.slice(16, 48);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function generateIdentity(): Promise<DeviceIdentity> {
|
|
33
|
+
const seed = crypto.getRandomValues(new Uint8Array(32));
|
|
34
|
+
const publicKey = ed25519.getPublicKey(seed);
|
|
35
|
+
// Use the same PKCS#8 envelope as the native client.
|
|
36
|
+
const pkcs8 = Uint8Array.of(...pkcs8Header, ...seed, 0x81, 0x21, 0, ...publicKey);
|
|
37
|
+
return { privateKey: encodeBase64Url(pkcs8), publicKey: encodeBase64Url(publicKey) };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function unpack(pkcs8: Uint8Array) {
|
|
41
|
+
checkEnvelope(pkcs8);
|
|
42
|
+
const seed = pkcs8.slice(16, 48);
|
|
43
|
+
const publicKey = encodeBase64Url(ed25519.getPublicKey(seed));
|
|
44
|
+
if (publicKey !== encodeBase64Url(pkcs8.slice(51))) throw new Error("Invalid Korero.chat identity key.");
|
|
45
|
+
return { seed, publicKey };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function identityFromPrivateKey(privateKey: string): DeviceIdentity {
|
|
49
|
+
const { publicKey } = unpack(decodeBase64Url(privateKey));
|
|
50
|
+
return { privateKey, publicKey };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function wrappingKey(passphrase: string, salt: Uint8Array<ArrayBuffer>) {
|
|
54
|
+
if (Array.from(passphrase).length < 12) throw new Error("Use a passphrase of at least 12 characters.");
|
|
55
|
+
const base = await crypto.subtle.importKey("raw", encoder.encode(passphrase), "PBKDF2", false, ["deriveKey"]);
|
|
56
|
+
return crypto.subtle.deriveKey({ name: "PBKDF2", salt, iterations: 600000, hash: "SHA-256" },
|
|
57
|
+
base, { name: "AES-GCM", length: 256 }, false, ["encrypt", "decrypt"]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function importIdentity(blob: string, passphrase: string): Promise<DeviceIdentity> {
|
|
61
|
+
const bytes = decodeBase64Url(blob.trim());
|
|
62
|
+
const offset = header.length;
|
|
63
|
+
if (bytes.length !== offset + 16 + 12 + 83 + 16 || !header.every((byte, index) => bytes[index] === byte)) {
|
|
64
|
+
throw new Error("Invalid Korero.chat identity export.");
|
|
65
|
+
}
|
|
66
|
+
const key = await wrappingKey(passphrase, bytes.slice(offset, offset + 16));
|
|
67
|
+
const pkcs8 = await crypto.subtle.decrypt({ name: "AES-GCM", iv: bytes.slice(offset + 16, offset + 28), additionalData: header },
|
|
68
|
+
key, bytes.slice(offset + 28)).catch(() => { throw new Error("Incorrect passphrase or damaged identity export."); });
|
|
69
|
+
const { publicKey } = unpack(new Uint8Array(pkcs8));
|
|
70
|
+
return { privateKey: encodeBase64Url(pkcs8), publicKey };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function exportIdentity(privateKey: string, passphrase: string) {
|
|
74
|
+
const salt = crypto.getRandomValues(new Uint8Array(16));
|
|
75
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
76
|
+
const key = await wrappingKey(passphrase, salt);
|
|
77
|
+
const sealed = await crypto.subtle.encrypt({ name: "AES-GCM", iv, additionalData: header }, key, decodeBase64Url(privateKey));
|
|
78
|
+
return encodeBase64Url(Uint8Array.of(...header, ...salt, ...iv, ...new Uint8Array(sealed)));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function signIdentity(identity: DeviceIdentity, transcript: unknown[]) {
|
|
82
|
+
const { seed } = unpack(decodeBase64Url(identity.privateKey));
|
|
83
|
+
return encodeBase64Url(ed25519.sign(encoder.encode(JSON.stringify(transcript)), seed));
|
|
84
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { Channel, ChannelMessage } from "./channel.js";
|
|
2
|
+
export type { ChannelOptions, MessageContent } from "./channel.js";
|
|
3
|
+
export { KoreroClient } from "./session.js";
|
|
4
|
+
export type { ClientEvents, ConnectOptions, SessionOptions, SessionMedia, SessionMediaAdapter } from "./session.js";
|
|
5
|
+
export { createApi, serverApi } from "./http.js";
|
|
6
|
+
export type {
|
|
7
|
+
AccountPage, ApiResult, AttachmentUpload, AuditPage, ChannelOrder, ChannelPermission, ChannelPermissionTable,
|
|
8
|
+
ConnectionDetails, Invitation, Permission, RoleDefinition, ServerCapabilities, ServerPreferences,
|
|
9
|
+
} from "./http.js";
|
|
10
|
+
export { serverOrigin, webTransport } from "./transport.js";
|
|
11
|
+
export type { ServerSocket, Transport } from "./transport.js";
|
|
12
|
+
export { messageFrom, requestError } from "./errors.js";
|
|
13
|
+
export { generateIdentity, importIdentity, exportIdentity, identityFromPrivateKey, encodeBase64Url, decodeBase64Url } from "./identity.js";
|
|
14
|
+
export type { DeviceIdentity } from "./identity.js";
|
|
15
|
+
export type { IdentityAction, IdentityAuthentication } from "./auth.js";
|
|
16
|
+
export { directConversation, encodeDirectContent, decodeDirectContent } from "./direct-crypto.js";
|
|
17
|
+
export type { DirectCursor, DirectPacket, DirectPage, DirectPeer } from "./direct-crypto.js";
|
|
18
|
+
export * from "./types.js";
|