2017-12-19 04:44:01 +00:00
|
|
|
import { GET, Path, QueryParam } from "typescript-rest";
|
|
|
|
import { ScalarService } from "../scalar/ScalarService";
|
|
|
|
import config from "../../config";
|
|
|
|
import { ApiError } from "../ApiError";
|
2017-12-24 02:45:34 +00:00
|
|
|
import { MatrixLiteClient } from "../../matrix/MatrixLiteClient";
|
|
|
|
import { CURRENT_VERSION } from "../../version";
|
2018-03-24 23:09:34 +00:00
|
|
|
import { getFederationUrl } from "../../matrix/helpers";
|
2017-12-19 04:44:01 +00:00
|
|
|
|
2017-12-24 02:45:34 +00:00
|
|
|
interface DimensionVersionResponse {
|
|
|
|
version: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DimensionConfigResponse {
|
|
|
|
admins: string[];
|
|
|
|
widgetBlacklist: string[];
|
|
|
|
homeserver: {
|
|
|
|
name: string;
|
|
|
|
userId: string;
|
|
|
|
federationUrl: string;
|
2018-03-24 23:09:34 +00:00
|
|
|
clientServerUrl: string;
|
2017-12-24 02:45:34 +00:00
|
|
|
};
|
2017-12-19 04:44:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 21:17:39 +00:00
|
|
|
/**
|
|
|
|
* Administrative API for general information about Dimension
|
|
|
|
*/
|
2017-12-19 04:44:01 +00:00
|
|
|
@Path("/api/v1/dimension/admin")
|
2018-03-24 03:36:43 +00:00
|
|
|
export class AdminService {
|
2017-12-19 04:44:01 +00:00
|
|
|
|
2018-03-30 21:17:39 +00:00
|
|
|
/**
|
|
|
|
* Determines if a given user is an administrator
|
|
|
|
* @param {string} userId The user ID to validate
|
|
|
|
* @returns {boolean} True if the user is an administrator
|
|
|
|
*/
|
2017-12-19 04:44:01 +00:00
|
|
|
public static isAdmin(userId: string) {
|
|
|
|
return config.admins.indexOf(userId) >= 0;
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:17:39 +00:00
|
|
|
/**
|
|
|
|
* Validates the given scalar token to ensure the owner is an administrator. If the
|
|
|
|
* given scalar token does not belong to an administrator, an ApiError is raised.
|
|
|
|
* @param {string} scalarToken The scalar token to validate
|
|
|
|
* @returns {Promise<string>} Resolves to the owner's user ID if they are an administrator
|
|
|
|
* @throws {ApiError} Thrown with a status code of 401 if the owner is not an administrator
|
|
|
|
*/
|
2018-03-24 03:01:56 +00:00
|
|
|
public static async validateAndGetAdminTokenOwner(scalarToken: string): Promise<string> {
|
|
|
|
const userId = await ScalarService.getTokenOwner(scalarToken, true);
|
2018-03-24 03:36:43 +00:00
|
|
|
if (!AdminService.isAdmin(userId))
|
2018-03-24 03:01:56 +00:00
|
|
|
throw new ApiError(401, "You must be an administrator to use this API");
|
|
|
|
return userId;
|
2017-12-19 04:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
2017-12-24 02:45:34 +00:00
|
|
|
@Path("check")
|
2018-03-24 03:01:56 +00:00
|
|
|
public async checkIfAdmin(@QueryParam("scalar_token") scalarToken: string): Promise<{}> {
|
2018-03-24 03:36:43 +00:00
|
|
|
await AdminService.validateAndGetAdminTokenOwner(scalarToken);
|
2018-03-24 03:01:56 +00:00
|
|
|
return {}; // A 200 OK essentially means "you're an admin".
|
2017-12-24 02:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path("version")
|
2018-03-24 03:01:56 +00:00
|
|
|
public async getVersion(@QueryParam("scalar_token") scalarToken: string): Promise<DimensionVersionResponse> {
|
2018-03-24 03:36:43 +00:00
|
|
|
await AdminService.validateAndGetAdminTokenOwner(scalarToken);
|
2018-03-24 03:01:56 +00:00
|
|
|
return {version: CURRENT_VERSION};
|
2017-12-19 04:44:01 +00:00
|
|
|
}
|
2017-12-24 02:45:34 +00:00
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path("config")
|
2018-03-24 03:01:56 +00:00
|
|
|
public async getConfig(@QueryParam("scalar_token") scalarToken: string): Promise<DimensionConfigResponse> {
|
2018-03-24 03:36:43 +00:00
|
|
|
await AdminService.validateAndGetAdminTokenOwner(scalarToken);
|
2018-03-24 03:01:56 +00:00
|
|
|
|
2018-03-24 23:09:34 +00:00
|
|
|
const client = new MatrixLiteClient(config.homeserver.accessToken);
|
2018-03-24 03:01:56 +00:00
|
|
|
return {
|
2017-12-24 02:45:34 +00:00
|
|
|
admins: config.admins,
|
|
|
|
widgetBlacklist: config.widgetBlacklist,
|
|
|
|
homeserver: {
|
|
|
|
name: config.homeserver.name,
|
2018-03-24 03:01:56 +00:00
|
|
|
userId: await client.whoAmI(),
|
2018-03-24 23:09:34 +00:00
|
|
|
federationUrl: await getFederationUrl(config.homeserver.name),
|
|
|
|
clientServerUrl: config.homeserver.clientServerUrl,
|
2017-12-24 02:45:34 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2017-12-19 04:44:01 +00:00
|
|
|
}
|