Skip to content

Commit

Permalink
Add test "account" command
Browse files Browse the repository at this point in the history
  • Loading branch information
ba1uev committed Jan 18, 2024
1 parent 97d33b0 commit e9f3902
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/admin-api/index.ts
Expand Up @@ -148,6 +148,14 @@ class AdminApi {
throw err
}
}

async createDevice(userId: string, deviceId: string): Promise<unknown> {
try {
return await this.makeRequest("POST", `/v2/users/${userId}/devices`, { device_id: deviceId })
} catch (err) {
throw err
}
}
}

export const adminApi = new AdminApi({ host: config.MATRIX_SERVER_URL, accessToken: config.ACCESS_TOKEN })
6 changes: 3 additions & 3 deletions src/bot.ts
Expand Up @@ -13,7 +13,7 @@ import { INVITE_COMMAND, runInviteCommand } from "./commands/invite"
import { INVITE_ROOM, runInviteRoomCommand } from "./commands/invite-room"
import { PROMOTE_COMMAND, runPromoteCommand } from "./commands/promote"
import { runSpaceCommand, SPACE_COMMAND } from "./commands/space"
import { runRotateTokenCommand, ROTATE_TOKEN_COMMAND } from "./commands/rotate-token"
import { runAccountCommand, ACCOUNT_COMMAND } from "./commands/account"
import { CommandError } from "./utils"

/* This is the maximum allowed time between time on matrix server
Expand Down Expand Up @@ -114,8 +114,8 @@ export default class Bot {
return await runDeactivateUserCommand(roomId, event, args, this.client)
case SPACE_COMMAND:
return await runSpaceCommand(roomId, event, args, this.client)
case ROTATE_TOKEN_COMMAND:
return await runRotateTokenCommand(roomId, event, args, this.client)
case ACCOUNT_COMMAND:
return await runAccountCommand(roomId, event, args, this.client)
default:
return await runHelpCommand(roomId, event, this.client)
}
Expand Down
54 changes: 41 additions & 13 deletions src/commands/rotate-token.ts → src/commands/account.ts
Expand Up @@ -6,9 +6,14 @@ import config from "src/config/env"
import { canExecuteCommand, CommandError } from "src/utils"

const moduleName = "RotateTokenCommand"
export const ROTATE_TOKEN_COMMAND = "rotate-token"
export const ACCOUNT_COMMAND = "account"

export async function runRotateTokenCommand(
enum Command {
LoginUser = "login",
CreateDevice = "create-device",
}

export async function runAccountCommand(
roomId: string,
event: MessageEvent<MessageEventContent>,
args: string[],
Expand All @@ -21,10 +26,13 @@ export async function runRotateTokenCommand(
}

// 1. Retrive and validate arguments
const [, targetUserId] = args
const [, command, targetUserId, ...extraArgs] = args
if (!event.sender.includes(`:${config.MATRIX_SERVER_DOMAIN}`)) {
throw new CommandError(`Access denied.`)
}
if (!Object.values(Command).includes(command as Command)) {
throw new CommandError(`Invalid subcommand. Should be one of: ${Object.values(Command).join(", ")}`)
}
if (!targetUserId || !targetUserId.includes(`:${config.MATRIX_SERVER_DOMAIN}`)) {
const [, wrongHomeServer] = targetUserId.split(":")
throw new CommandError(
Expand All @@ -44,15 +52,35 @@ export async function runRotateTokenCommand(
throw new CommandError(`The user "${targetUserId}" cannot be found.`)
}

// 3. Generate an access token
try {
const response = await adminApi.loginUser(targetUserId)
await client.sendHtmlText(
roomId,
`New access token for user "${targetUserId}": <code>${JSON.stringify(response)}</code>`,
)
} catch (err) {
LogService.error(moduleName, err)
throw new CommandError(`Unable to retrieve user access token.`)
// 3.1 Generate an access token
if (command === Command.LoginUser) {
try {
const response = await adminApi.loginUser(targetUserId)
await client.sendHtmlText(
roomId,
`New access token for user "${targetUserId}": <code>${JSON.stringify(response)}</code>`,
)
} catch (err) {
LogService.error(moduleName, err)
throw new CommandError(`Unable to retrieve user access token.`)
}
}

// 3.2 Create a new device
if (command === Command.CreateDevice) {
const deviceId = extraArgs[0]
if (!deviceId) {
throw new CommandError(`Missing device ID argument.`)
}
try {
const response = await adminApi.createDevice(targetUserId, deviceId)
await client.sendHtmlText(
roomId,
`Created a new device for user "${targetUserId}": <code>${JSON.stringify(response)}</code>`,
)
} catch (err) {
LogService.error(moduleName, err)
throw new CommandError(`Unable to retrieve user access token.`)
}
}
}

0 comments on commit e9f3902

Please sign in to comment.