working get and post request with opengroup api v2
parent
c07271109f
commit
b68338e26c
@ -0,0 +1,72 @@
|
||||
export const defaultServer = 'https://sessionopengroup.com';
|
||||
export const defaultServerPublicKey =
|
||||
'658d29b91892a2389505596b135e76a53db6e11d613a51dbd3d0816adffb231b';
|
||||
|
||||
export type OpenGroupV2Request = {
|
||||
method: 'GET' | 'POST' | 'DELETE' | 'PUT';
|
||||
room: string;
|
||||
server: string;
|
||||
endpoint: string;
|
||||
// queryParams are used for post or get, but not the same way
|
||||
queryParams?: Record<string, string>;
|
||||
headers?: Record<string, string>;
|
||||
isAuthRequired: boolean;
|
||||
// Always `true` under normal circumstances. You might want to disable this when running over Lokinet.
|
||||
useOnionRouting?: boolean;
|
||||
};
|
||||
|
||||
export type OpenGroupV2Info = {
|
||||
id: string;
|
||||
name: string;
|
||||
imageId?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Try to build an full url and check it for validity.
|
||||
* @returns null if the check failed. the built URL otherwise
|
||||
*/
|
||||
export const buildUrl = (request: OpenGroupV2Request): URL | null => {
|
||||
let rawURL = `${request.server}/${request.endpoint}`;
|
||||
if (request.method === 'GET') {
|
||||
const entries = Object.entries(request.queryParams || {});
|
||||
|
||||
if (entries.length) {
|
||||
const queryString = entries
|
||||
.map(([key, value]) => `${key}=${value}`)
|
||||
.join('&');
|
||||
rawURL += `?${queryString}`;
|
||||
}
|
||||
}
|
||||
// this just check that the URL is valid
|
||||
try {
|
||||
return new URL(`${rawURL}`);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Map of serverUrl to roomId to list of moderators as a Set
|
||||
*/
|
||||
export const cachedModerators: Map<
|
||||
string,
|
||||
Map<string, Set<string>>
|
||||
> = new Map();
|
||||
|
||||
export const setCachedModerators = (
|
||||
serverUrl: string,
|
||||
roomId: string,
|
||||
newModerators: Array<string>
|
||||
) => {
|
||||
const allRoomsMods = cachedModerators.get(serverUrl);
|
||||
if (!allRoomsMods) {
|
||||
cachedModerators.set(serverUrl, new Map());
|
||||
}
|
||||
// tslint:disable: no-non-null-assertion
|
||||
if (!allRoomsMods!.get(roomId)) {
|
||||
allRoomsMods!.set(roomId, new Set());
|
||||
}
|
||||
newModerators.forEach(m => {
|
||||
allRoomsMods!.get(roomId)?.add(m);
|
||||
});
|
||||
};
|
@ -0,0 +1,100 @@
|
||||
import { getSodium } from '../../session/crypto';
|
||||
import { UserUtils } from '../../session/utils';
|
||||
import {
|
||||
fromBase64ToArray,
|
||||
fromHex,
|
||||
fromHexToArray,
|
||||
toHex,
|
||||
} from '../../session/utils/String';
|
||||
|
||||
export class OpenGroupMessageV2 {
|
||||
public serverId?: number;
|
||||
public sender?: string;
|
||||
public sentTimestamp: number;
|
||||
public base64EncodedData: string;
|
||||
public base64EncodedSignature?: string;
|
||||
|
||||
constructor(messageData: {
|
||||
serverId?: number;
|
||||
sender?: string;
|
||||
sentTimestamp: number;
|
||||
base64EncodedData: string;
|
||||
base64EncodedSignature?: string;
|
||||
}) {
|
||||
const {
|
||||
base64EncodedData,
|
||||
sentTimestamp,
|
||||
base64EncodedSignature,
|
||||
sender,
|
||||
serverId,
|
||||
} = messageData;
|
||||
|
||||
this.base64EncodedData = base64EncodedData;
|
||||
this.sentTimestamp = sentTimestamp;
|
||||
this.base64EncodedSignature = base64EncodedSignature;
|
||||
this.sender = sender;
|
||||
this.serverId = serverId;
|
||||
}
|
||||
|
||||
public async sign() {
|
||||
const ourKeyPair = await UserUtils.getUserED25519KeyPair();
|
||||
if (!ourKeyPair) {
|
||||
window.log.warn("Couldn't find user X25519 key pair.");
|
||||
return null;
|
||||
}
|
||||
const data = fromBase64ToArray(this.base64EncodedData);
|
||||
const sodium = await getSodium();
|
||||
const signature = sodium.crypto_sign_detached(
|
||||
data,
|
||||
fromHexToArray(ourKeyPair.privKey)
|
||||
);
|
||||
if (!signature || signature.length === 0) {
|
||||
throw new Error("Couldn't sign message");
|
||||
}
|
||||
return new OpenGroupMessageV2({
|
||||
base64EncodedData: this.base64EncodedData,
|
||||
sentTimestamp: this.sentTimestamp,
|
||||
base64EncodedSignature: toHex(signature),
|
||||
sender: this.sender,
|
||||
serverId: this.serverId,
|
||||
});
|
||||
}
|
||||
|
||||
public toJson() {
|
||||
const json = {
|
||||
data: this.base64EncodedData,
|
||||
timestamp: this.sentTimestamp,
|
||||
} as Record<string, any>;
|
||||
if (this.serverId) {
|
||||
json.server_id = this.serverId;
|
||||
}
|
||||
if (this.sender) {
|
||||
json.public_key = this.sender;
|
||||
}
|
||||
if (this.base64EncodedSignature) {
|
||||
json.signature = this.base64EncodedSignature;
|
||||
}
|
||||
}
|
||||
|
||||
public fromJson(json: Record<string, any>) {
|
||||
const {
|
||||
data: base64EncodedData,
|
||||
timestamp: sentTimestamp,
|
||||
server_id: serverId,
|
||||
public_key: sender,
|
||||
signature: base64EncodedSignature,
|
||||
} = json;
|
||||
|
||||
if (!base64EncodedData || !sentTimestamp) {
|
||||
window.log.info('invalid json to build OpenGroupMessageV2');
|
||||
return null;
|
||||
}
|
||||
return new OpenGroupMessageV2({
|
||||
base64EncodedData,
|
||||
base64EncodedSignature,
|
||||
sentTimestamp,
|
||||
serverId,
|
||||
sender,
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue