Skip to content

Commit

Permalink
Account command: separate the create and sign-in subcommands, and all…
Browse files Browse the repository at this point in the history
…ow the provision of a specific device ID while creating a session
  • Loading branch information
ba1uev committed Apr 19, 2024
1 parent 810b4f5 commit 5e579b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
33 changes: 22 additions & 11 deletions src/commands/account.ts
Expand Up @@ -63,12 +63,22 @@ export async function runAccountCommand(
const userId = localpartToUserId(userLocalpart)

// Create a new account
if (command === Command.Create || command === Command.SignIn) {
if (command === Command.Create) {
validateMasConfiguration()
const user = await adminApi.getUserAccount(userId)
if (command === Command.Create && user) {
if (user) {
throw new CommandError(`User ${userId} already exists.`)
} else if (command === Command.SignIn && !user) {
}
await getOrCreateUser(userLocalpart)
await adminApi.markUserAsBot(userId)
await client.sendHtmlText(roomId, `Created a new account ${userId}.`)
}

// Sign in
if (command === Command.SignIn) {
validateMasConfiguration()
const user = await adminApi.getUserAccount(userId)
if (!user) {
throw new CommandError(`User ${userId} doesn't exist.`)
}

Expand All @@ -81,15 +91,15 @@ export async function runAccountCommand(
return
}

const permanent = extraArgs[0] === "permanent"
const userDetails = await getOrCreateUser(userLocalpart)

if (command === Command.Create) {
await adminApi.markUserAsBot(userId)
await client.sendHtmlText(roomId, `Created a new account for user ${userId}.`)
const permanentArg = extraArgs[0]
if (!permanentArg || !["permanent", "refreshable"].includes(permanentArg)) {
throw new CommandError(`Missing or invalid token type argument. Should be one of: permanent, refreshable`)
}
const permanent = permanentArg === "permanent"
const deviceIdArg = extraArgs[1] || undefined

const sessionDetails = await createOauth2Session(userDetails.id, permanent)
const userDetails = await getOrCreateUser(userLocalpart)
const sessionDetails = await createOauth2Session(userDetails.id, permanent, deviceIdArg)
const { accessToken, refreshToken, deviceId } = sessionDetails
await client.sendHtmlText(
dmRoomId,
Expand Down Expand Up @@ -272,6 +282,7 @@ async function getOrCreateUser(username: string): Promise<{ id: string; username
async function createOauth2Session(
userId: string,
permanent: boolean,
_deviceId?: string,
): Promise<{ sessionId: string; accessToken: string; refreshToken?: string; deviceId: string }> {
type Response = {
data: {
Expand All @@ -286,7 +297,7 @@ async function createOauth2Session(
}
} | null
}
const deviceId = generateDeviceId()
const deviceId = _deviceId || generateDeviceId()
const scope = getOAuth2SessionScope(deviceId)
try {
const res = await makeGraphqlRequest<Response>(
Expand Down
13 changes: 8 additions & 5 deletions src/commands/help.ts
Expand Up @@ -132,14 +132,17 @@ ${commandPrefix} ${DEACTIVATE_USER_COMMAND} <userId> [recover]
${commandPrefix} ${ACCOUNT_COMMAND} ${AccountSubcommand.List}
Lists all bot accounts on the server.
${commandPrefix} ${ACCOUNT_COMMAND} ${AccountSubcommand.Create} <userId> [permanent]
Creates a bot account and a new OAuth2 session. By default an access and refresh tokens will be sent to a requester in the E2EE DM chat.
${commandPrefix} ${ACCOUNT_COMMAND} ${AccountSubcommand.Create} <userId>
Creates a bot account
<userId> - Matrix user id @username:${config.MATRIX_SERVER_DOMAIN} or just username
[permanent] - (Optional) flag making the access token permanent
${commandPrefix} ${ACCOUNT_COMMAND} ${AccountSubcommand.SignIn} <userId> [permanent]
Does the same as the create command, but for an existing account
${commandPrefix} ${ACCOUNT_COMMAND} ${AccountSubcommand.SignIn} <userId> <permanent | refreshable> [deviceId]
Creates a new OAuth2 session. An access and refresh tokens, and device ID will be sent to a requester in the E2EE DM chat.
<userId> - Matrix user id @username:${config.MATRIX_SERVER_DOMAIN} or just username
<permanent | refreshable> - Flag defining the token type
[deviceId] - (Optional) flag for creating a session for a specific device
${commandPrefix} ${ACCOUNT_COMMAND} ${AccountSubcommand.ListSessions} <userId>
List all active OAuth2 sessions in the format: ID, CreatedAt, LastActiveAt.
Expand Down

0 comments on commit 5e579b2

Please sign in to comment.