/
Util.ts
executable file
·298 lines (279 loc) · 11.5 KB
/
Util.ts
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
// Copyright 2020 Vircadia Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
import { Request } from 'express';
import { Accounts } from '@Entities/Accounts';
import { AccountEntity } from '@Entities/AccountEntity';
import { Domains } from '@Entities/Domains';
import { DomainEntity } from '@Entities/DomainEntity';
import { Places } from '@Entities/Places';
import { PlaceEntity } from '@Entities/PlaceEntity';
import { GenericFilter } from '@Entities/EntityFilters/GenericFilter';
import { createPublicKey } from 'crypto';
import { VKeyedCollection, VKeyValue } from '@Tools/vTypes';
import { IsNotNullOrEmpty } from '@Tools/Misc';
import { Logger } from '@Tools/Logging';
import { Maturity } from '@Entities/Sets/Maturity';
import { Visibility } from '@Entities/Sets/Visibility';
// The public_key is sent as a binary (DER) form of a PKCS1 key.
// To keep backward compatibility, we convert the PKCS1 key into a SPKI key in PEM format
// ("PEM" format is "Privacy Enhanced Mail" format and has the "BEGIN" and "END" text included).
export function convertBinKeyToPEM(pBinKey: Buffer): string {
// Convert the passed binary into a crypto.KeyObject
const publicKey = createPublicKey( {
key: pBinKey,
format: 'der',
type: 'pkcs1'
});
// Convert the public key to 'SubjectPublicKeyInfo' (SPKI) format as a PEM string
const convertedKey = publicKey.export({ type: 'spki', format: 'pem' });
return convertedKey as string;
};
// The legacy interface returns public keys as a stripped PEM key.
// "stripped" in that the bounding "BEGIN" and "END" lines have been removed.
// This routine returns a stripped key string from a properly PEM formatted public key string.
export function createSimplifiedPublicKey(pPubKey: string): string {
let keyLines: string[] = [];
if (pPubKey) {
keyLines = pPubKey.split('\n');
keyLines.shift(); // Remove the "BEGIN" first line
while (keyLines.length > 1
&& ( keyLines[keyLines.length-1].length < 1 || keyLines[keyLines.length-1].includes('END PUBLIC KEY') ) ) {
keyLines.pop(); // Remove the "END" last line
};
}
return keyLines.join(''); // Combine all lines into one long string
};
// Process the user request data and update fields fields in the account.
// This is infomation from the user from heartbeats or location updates.
// Return a VKeyedCollection of the updates made so it can be used to update the database.
export async function updateLocationInfo(pReq: Request): Promise<VKeyedCollection> {
let newLoc: VKeyedCollection = {};
if (pReq.vAuthAccount && pReq.body.location) {
try {
// build location from what is in the account already
const loc = pReq.body.location;
for (const field of ['connected', 'path', 'place_id', 'domain_id',
'network_address', 'node_id', 'availability']) {
if (loc.hasOwnProperty(field)) {
await Accounts.setField(pReq.vAuthToken, pReq.vAuthAccount, field, loc[field], pReq.vAuthAccount, newLoc);
};
};
}
catch ( err ) {
Logger.error(`procPutUserLocation: exception parsing put from ${pReq.vAuthAccount.username}: ${err}`);
pReq.vRestResp.respondFailure(`exception parsing request: ${err}`);
newLoc = undefined;
};
};
return newLoc;
};
// The returned location info has many options depending on whether
// the account has set location and/or has an associated domain.
// Return a structure that represents the target account's domain
export async function buildLocationInfo(pAcct: AccountEntity): Promise<any> {
let ret: any = {};
if (pAcct.locationDomainId) {
const aDomain = await Domains.getDomainWithId(pAcct.locationDomainId);
if (IsNotNullOrEmpty(aDomain)) {
ret = {
'root': {
'domain': await buildDomainInfo(aDomain),
},
'path': pAcct.locationPath,
};
}
else {
// The domain doesn't have an ID
ret = {
'root': {
'domain': {
'network_address': pAcct.locationNetworkAddress,
'network_port': pAcct.locationNetworkPort
}
}
};
};
};
ret.node_id = pAcct.locationNodeId;
ret.online = Accounts.isOnline(pAcct)
return ret;
};
export async function buildDomainInfo(pDomain: DomainEntity): Promise<any> {
return {
'id': pDomain.id,
'domainId': pDomain.id,
'name': pDomain.name,
'visibility': pDomain.visibility ?? Visibility.OPEN,
'sponsorAccountId': pDomain.sponsorAccountId,
'label': pDomain.name,
'network_address': pDomain.networkAddr,
'network_port': pDomain.networkPort,
'ice_server_address': pDomain.iceServerAddr,
'time_of_last_heartbeat': pDomain.timeOfLastHeartbeat ? pDomain.timeOfLastHeartbeat.toISOString() : undefined,
'num_users': pDomain.numUsers
};
}
// Return a structure with the usual domain information.
export async function buildDomainInfoV1(pDomain: DomainEntity): Promise<any> {
return {
'domainId': pDomain.id,
'id': pDomain.id, // legacy
'name': pDomain.name,
'visibility': pDomain.visibility ?? Visibility.OPEN,
'world_name': pDomain.name, // legacy
'label': pDomain.name, // legacy
'public_key': pDomain.publicKey ? createSimplifiedPublicKey(pDomain.publicKey) : undefined,
'owner_places': await buildPlacesForDomain(pDomain),
'sponsor_account_id': pDomain.sponsorAccountId,
'ice_server_address': pDomain.iceServerAddr,
'version': pDomain.version,
'protocol_version': pDomain.protocol,
'network_address': pDomain.networkAddr,
'network_port': pDomain.networkPort,
'automatic_networking': pDomain.networkingMode,
'restricted': pDomain.restricted,
'num_users': pDomain.numUsers,
'anon_users': pDomain.anonUsers,
'total_users': pDomain.numUsers + pDomain.anonUsers,
'capacity': pDomain.capacity,
'description': pDomain.description,
'maturity': pDomain.maturity ?? Maturity.UNRATED,
'restriction': pDomain.restriction,
'managers': pDomain.managers,
'tags': pDomain.tags,
'meta': {
'capacity': pDomain.capacity,
'contact_info': pDomain.contactInfo,
'description': pDomain.description,
'images': pDomain.images,
'managers': pDomain.managers,
'restriction': pDomain.restriction,
'tags': pDomain.tags,
'thumbnail': pDomain.thumbnail,
'world_name': pDomain.name
},
'users': {
'num_anon_users': pDomain.anonUsers,
'num_users': pDomain.numUsers,
'user_hostnames': pDomain.hostnames
},
'time_of_last_heartbeat': pDomain.timeOfLastHeartbeat ? pDomain.timeOfLastHeartbeat.toISOString() : undefined,
'last_sender_key': pDomain.lastSenderKey,
'addr_of_first_contact': pDomain.iPAddrOfFirstContact,
'when_domain_entry_created': pDomain.whenCreated ? pDomain.whenCreated.toISOString() : undefined
};
};
// Return the limited "user" info.. used by /api/v1/users
export async function buildUserInfo(pAccount: AccountEntity): Promise<any> {
return {
'accountId': pAccount.id,
'id': pAccount.id,
'username': pAccount.username,
'images': await buildImageInfo(pAccount),
'location': await buildLocationInfo(pAccount),
};
};
export async function buildImageInfo(pAccount: AccountEntity): Promise<any> {
const ret: VKeyedCollection = {};
if (pAccount.imagesTiny) ret.tiny = pAccount.imagesTiny;
if (pAccount.imagesHero) ret.hero = pAccount.imagesHero;
if (pAccount.imagesThumbnail) ret.thumbnail = pAccount.imagesThumbnail;
return ret;
};
// Return the block of account information.
// Used by several of the requests to return the complete account information.
export async function buildAccountInfo(pReq: Request, pAccount: AccountEntity): Promise<any> {
return {
'accountId': pAccount.id,
'id': pAccount.id,
'username': pAccount.username,
'email': pAccount.email,
'administrator': Accounts.isAdmin(pAccount),
'roles': pAccount.roles,
'availability': pAccount.availability,
'public_key': createSimplifiedPublicKey(pAccount.sessionPublicKey),
'images': {
'hero': pAccount.imagesHero,
'tiny': pAccount.imagesTiny,
'thumbnail': pAccount.imagesThumbnail
},
'profile_detail': pAccount.profileDetail,
'location': await buildLocationInfo(pAccount),
'friends': pAccount.friends,
'connections': pAccount.connections,
'when_account_created': pAccount.whenCreated ? pAccount.whenCreated.toISOString() : undefined,
'time_of_last_heartbeat': pAccount.timeOfLastHeartbeat ? pAccount.timeOfLastHeartbeat.toISOString() : undefined
};
};
// Return the block of account information used as the account 'profile'.
// Anyone can fetch a profile (if 'availability' is 'any') so not all info is returned
export async function buildAccountProfile(pReq: Request, pAccount: AccountEntity): Promise<any> {
return {
'accountId': pAccount.id,
'id': pAccount.id,
'username': pAccount.username,
'images': {
'hero': pAccount.imagesHero,
'tiny': pAccount.imagesTiny,
'thumbnail': pAccount.imagesThumbnail
},
'profile_detail': pAccount.profileDetail,
'location': await buildLocationInfo(pAccount),
'when_account_created': pAccount.whenCreated ? pAccount.whenCreated.toISOString() : undefined,
'time_of_last_heartbeat': pAccount.timeOfLastHeartbeat ? pAccount.timeOfLastHeartbeat.toISOString() : undefined
};
};
// Return an object with the formatted place information
// Pass the PlaceEntity and the place's domain if known.
export async function buildPlaceInfo(pPlace: PlaceEntity, pDomain?: DomainEntity): Promise<any> {
const ret = await buildPlaceInfoSmall(pPlace, pDomain);
// if the place points to a domain, add that information also
if (IsNotNullOrEmpty(pPlace.domainId)) {
const aDomain = pDomain ?? await Domains.getDomainWithId(pPlace.domainId);
if (aDomain) {
ret.domain = await buildDomainInfo(aDomain);
};
};
return ret;
};
// Return the basic information block for a Place
export async function buildPlaceInfoSmall(pPlace: PlaceEntity, pDomain?: DomainEntity): Promise<any> {
const ret: VKeyedCollection = {
'placeId': pPlace.id,
'id': pPlace.id,
'name': pPlace.name,
'visibility': pPlace.visibility ?? Visibility.OPEN,
'address': await Places.getAddressString(pPlace),
'path': pPlace.path,
'description': pPlace.description,
'maturity': pPlace.maturity ?? Maturity.UNRATED,
'tags': pPlace.tags,
'thumbnail': pPlace.thumbnail,
'images': pPlace.images,
'current_attendance': await Places.getCurrentAttendance(pPlace, pDomain),
'current_images': pPlace.currentImages,
'current_info': pPlace.currentInfo,
'current_last_update_time': pPlace.currentLastUpdateTime
};
return ret;
};
// Return an array of Places names that are associated with the passed domain
export async function buildPlacesForDomain(pDomain: DomainEntity): Promise<any[]> {
const ret: any[] = [];
for await (const aPlace of Places.enumerateAsync(new GenericFilter({ 'domainId': pDomain.id }))) {
ret.push(await buildPlaceInfoSmall(aPlace));
};
return ret;
};