mirror of
https://github.com/the-draupnir-project/Draupnir.git
synced 2026-03-31 19:35:39 +00:00
We have a lot of verbose headers, and i think now is the best opportunity we have to become reuse compliant given that we just did two other similar maintenance changes (prettier, typescirpt5 & eslint9 & typescript-eslint). * synapse_antispam resuse headers. * delete old unused tslint.json. * Add REUSE to pre-commit config. * reuse info for config directory.
83 lines
2.1 KiB
TypeScript
83 lines
2.1 KiB
TypeScript
// Copyright 2022 Gnuxie <Gnuxie@protonmail.com>
|
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
//
|
|
// SPDX-License-Identifier: AFL-3.0 AND Apache-2.0
|
|
//
|
|
// SPDX-FileAttributionText: <text>
|
|
// This modified file incorporates work from mjolnir
|
|
// https://github.com/matrix-org/mjolnir
|
|
// </text>
|
|
|
|
import * as request from "request";
|
|
import { MatrixClient } from "matrix-bot-sdk";
|
|
|
|
interface OpenIDTokenInfo {
|
|
access_token: string;
|
|
expires_in: number;
|
|
matrix_server_name: string;
|
|
token_type: string;
|
|
}
|
|
|
|
async function getOpenIDToken(client: MatrixClient): Promise<string> {
|
|
const tokenInfo: OpenIDTokenInfo = await client.doRequest(
|
|
"POST",
|
|
`/_matrix/client/v3/user/${await client.getUserId()}/openid/request_token`,
|
|
undefined,
|
|
{}
|
|
);
|
|
return tokenInfo.access_token;
|
|
}
|
|
|
|
export interface CreateMjolnirResponse {
|
|
mjolnirUserId: string;
|
|
managementRoomId: string;
|
|
}
|
|
|
|
export class MjolnirWebAPIClient {
|
|
private constructor(
|
|
private readonly openIDToken: string,
|
|
private readonly baseURL: string
|
|
) {}
|
|
|
|
public static async makeClient(
|
|
client: MatrixClient,
|
|
baseUrl: string
|
|
): Promise<MjolnirWebAPIClient> {
|
|
const token = await getOpenIDToken(client);
|
|
return new MjolnirWebAPIClient(token, baseUrl);
|
|
}
|
|
|
|
public async createMjolnir(
|
|
roomToProtectId: string
|
|
): Promise<CreateMjolnirResponse> {
|
|
const body: { mxid: string; roomId: string } = await new Promise(
|
|
(resolve, reject) => {
|
|
request.post(
|
|
`${this.baseURL}/create`,
|
|
{
|
|
json: {
|
|
openId: this.openIDToken,
|
|
roomId: roomToProtectId,
|
|
},
|
|
},
|
|
(error, response) => {
|
|
if (error === null || error === undefined) {
|
|
resolve(response.body);
|
|
} else if (error instanceof Error) {
|
|
reject(error);
|
|
} else {
|
|
reject(
|
|
new TypeError(`Someone is throwing things that aren't errors`)
|
|
);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
);
|
|
return {
|
|
mjolnirUserId: body.mxid,
|
|
managementRoomId: body.roomId,
|
|
};
|
|
}
|
|
}
|