Refactor password encoding and error handling
This commit is contained in:
parent
21b800f431
commit
50c33eb172
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
|
|
|
@ -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({
|
||||
|
|
Loading…
Reference in New Issue
Block a user