diff --git a/src/server/lib/encodePassword.ts b/src/server/lib/encodePassword.ts index 4bde902..b87de0b 100644 --- a/src/server/lib/encodePassword.ts +++ b/src/server/lib/encodePassword.ts @@ -1,12 +1,11 @@ export function encodePassword(password: string): string { - let encodedPassword = ""; - password = '"' + password + '"'; - - for (let i = 0; i < password.length; i++) - encodedPassword += String.fromCharCode( + let newPassword = '' + password = '"' + password + '"' + for (let i = 0; i < password.length; i++) { + newPassword += String.fromCharCode( password.charCodeAt(i) & 0xff, (password.charCodeAt(i) >>> 8) & 0xff - ); - - return encodedPassword; + ) + } + return newPassword } diff --git a/src/server/lib/updatePassword.ts b/src/server/lib/updatePassword.ts index e2f89e5..564819a 100644 --- a/src/server/lib/updatePassword.ts +++ b/src/server/lib/updatePassword.ts @@ -1,4 +1,10 @@ -import { Client, Change, Attribute } from 'ldapts' +import { + Client, + Change, + Attribute, + InvalidCredentialsError, + UnwillingToPerformError +} from 'ldapts' import { encodePassword } from './encodePassword' const ldapClient = new Client({ @@ -40,8 +46,11 @@ export async function updatePassword({ }): Promise<'SUCCESS' | 'FAIL'> { try { const userDN = await getUserDN(username) + await ldapClient.bind(userDN, password) + console.log('binded') + await ldapClient.modify(userDN, [ new Change({ operation: 'delete', @@ -60,10 +69,21 @@ export async function updatePassword({ ]) return 'SUCCESS' - } catch (err) { - console.error(err) + } catch (err: any) { + console.log(err) + + if (err instanceof InvalidCredentialsError) { + throw new Error('Usuário ou senha atual incorreta.') + } + + if (err instanceof UnwillingToPerformError) { + throw new Error( + 'A senha atual está correta, mas o servidor recusou a alteração. Verifique se a nova senha atende aos requisitos de complexidade.' + ) + } else throw err } finally { await ldapClient.unbind() + console.log('unbinded') } return 'FAIL' } diff --git a/src/server/trpc.ts b/src/server/trpc.ts index 12e25a5..d258681 100644 --- a/src/server/trpc.ts +++ b/src/server/trpc.ts @@ -20,6 +20,8 @@ export const appRouter = router({ newPassword: z.string().min(8) }) ).mutation(async ({ input }) => { + console.log('input', input) + const { username, password, newPassword } = input try { await updatePassword({