implemented handling of password resets

This commit is contained in:
Sammy 2023-06-04 03:14:45 +02:00
parent 11dac10a07
commit d3fff38e36
2 changed files with 42 additions and 18 deletions

View File

@ -19,6 +19,7 @@ export interface Config {
jellfin_token: string
jellyfin_url: string
port: number
workaround_token: string
}
}
export const config: Config = {
@ -47,6 +48,7 @@ export const config: Config = {
guild_id: process.env.GUILD_ID ?? "",
client_id: process.env.CLIENT_ID ?? "",
jellfin_token: process.env.JELLYFIN_TOKEN ?? "",
jellyfin_url: process.env.JELLYFIN_URL ?? ""
jellyfin_url: process.env.JELLYFIN_URL ?? "",
workaround_token: process.env.TOKEN ?? ""
}
}

View File

@ -14,7 +14,7 @@ export class JellyfinHandler {
private userApi: UserApi
private systemApi: SystemApi
private token: string
private authHeader: { headers: { 'X-MediaBrowser-Token': string } }
private authHeader: { headers: { 'X-Emby-Authorization': string } }
private config: Config
private serverName = "";
@ -30,7 +30,7 @@ export class JellyfinHandler {
this.token = this.config.bot.jellfin_token
this.authHeader = {
headers: {
"X-MediaBrowser-Token": this.token
"X-Emby-Authorization": this.config.bot.workaround_token
}
}
const userApiConfigurationParams: ConfigurationParameters = {
@ -69,19 +69,19 @@ export class JellyfinHandler {
}
logger.debug(JSON.stringify(req))
const createResult = await this.userApi.createUserByName(req)
if (createResult){
(await discordUser.createDM()).send(`Ich hab dir mal nen Account angelegt :)\nDein Username ist ${createResult.name}, dein Password ist "${req.createUserByNameRequest.password}"!` )
if (createResult) {
(await discordUser.createDM()).send(`Ich hab dir mal nen Account angelegt :)\nDein Username ist ${createResult.name}, dein Password ist "${req.createUserByNameRequest.password}"!`)
return createResult
}
else throw new Error('Could not create User in Jellyfin')
}
public async isUserAlreadyPresent(discordUser: GuildMember, requestId?: string): Promise<boolean> {
const jfuser = await this.getUser(discordUser, requestId)
logger.debug(`Presence for DiscordUser ${discordUser.id}:${jfuser !== undefined}`)
return jfuser !== undefined
}
public async getCurrentUsers(guildId: string, requestId?: string): Promise<UserDto[]> {
try {
logger.info(`Fetching current users from Jellyfin`, { requestId, guildId })
@ -92,16 +92,17 @@ export class JellyfinHandler {
}
return []
}
public async getUser(discordUser: GuildMember, requestId?: string): Promise<Maybe<UserDto>> {
logger.info(`Getting user for discord member ${discordUser.displayName}`, {requestId})
const jfUsernameFromDiscordUsername = this.generateJFUserName(discordUser)
const jfUsers = await this.getCurrentUsers(discordUser.guild.id, requestId)
const foundUser = jfUsers.find(x => x.name === jfUsernameFromDiscordUsername)
return foundUser
}
public async removeUser(newMember: GuildMember, requestId?: string) {
logger.error(`Trying to remove user ${newMember.displayName}, but method is not implemented`)
logger.error(`Trying to remove user ${newMember.displayName}, but method is not implemented`, {requestId})
const jfuser = await this.getUser(newMember, requestId)
if (jfuser) {
await this.disableUser(jfuser, newMember.guild.id, requestId)
@ -109,20 +110,41 @@ export class JellyfinHandler {
}
public async resetUserPasswort(member: GuildMember, requestId?: string) {
logger.info(`Resetting password for user ${member.displayName}`, {requestId})
const jfUser = await this.getUser(member, requestId)
if (jfUser && jfUser.id) {
const r: UpdateUserPasswordRequest = {
newPw: this.generatePasswordForUser()
// const reset: UpdateUserPasswordRequest = {
// resetPassword: true
// }
// const shit: UpdateUserPasswordOperationRequest = {
// updateUserPasswordRequest: reset,
// userId: jfUser.id
// }
// logger.info(JSON.stringify(jfUser.policy, null, 2))
// logger.info("Resetting password", {requestId})
// await this.userApi.updateUserPassword(shit);
const password = this.generatePasswordForUser()
const passwordRequest: UpdateUserPasswordRequest = {
// resetPassword: true,
currentPw: "",
newPw: password
}
const shit: UpdateUserPasswordOperationRequest = {
updateUserPasswordRequest: r,
const passwordOperationRequest: UpdateUserPasswordOperationRequest = {
updateUserPasswordRequest: passwordRequest,
userId: jfUser.id
}
this.userApi.updateUserPassword(shit)
logger.info("Setting new password", {requestId})
await this.userApi.updateUserPassword(passwordOperationRequest);
(await member.createDM()).send(`Hier ist dein neues Passwort: ${password}`)
} else {
(await member.createDM()).send("Ich konnte leider keinen User von dir auf Jellyfin finden. Bitte melde dich bei Markus oder Samantha!")
}
@ -132,7 +154,7 @@ export class JellyfinHandler {
public async disableUser(user: UserDto, guildId?: string, requestId?: string): Promise<void> {
if (user.id) {
const jfUser = await this.getUser(<GuildMember>{displayName: user.name, guild: {id: guildId}}, requestId)
const jfUser = await this.getUser(<GuildMember>{ displayName: user.name, guild: { id: guildId } }, requestId)
logger.info(`Trying to disable user: ${user.name}|${user.id}|${JSON.stringify(jfUser, null, 2)}`)
const r: UpdateUserPolicyOperationRequest = {
userId: user.id ?? "",
@ -148,10 +170,10 @@ export class JellyfinHandler {
logger.error(`Can not disable user ${JSON.stringify(user)}, has no id?!`, { requestId, guildId })
}
}
public async enableUser(user: UserDto, guildId: string, requestId?: string): Promise<void> {
if (user.id) {
const jfUser = await this.getUser(<GuildMember>{displayName: user.name, guild: {id: guildId}}, requestId)
const jfUser = await this.getUser(<GuildMember>{ displayName: user.name, guild: { id: guildId } }, requestId)
logger.info(`Trying to enable user: ${user.name}|${user.id}|${JSON.stringify(jfUser, null, 2)}`)
const r: UpdateUserPolicyOperationRequest = {
userId: user.id ?? "",