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 {
|
export function encodePassword(password: string): string {
|
||||||
let encodedPassword = "";
|
let newPassword = ''
|
||||||
password = '"' + password + '"';
|
password = '"' + password + '"'
|
||||||
|
for (let i = 0; i < password.length; i++) {
|
||||||
for (let i = 0; i < password.length; i++)
|
newPassword += String.fromCharCode(
|
||||||
encodedPassword += String.fromCharCode(
|
|
||||||
password.charCodeAt(i) & 0xff,
|
password.charCodeAt(i) & 0xff,
|
||||||
(password.charCodeAt(i) >>> 8) & 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'
|
import { encodePassword } from './encodePassword'
|
||||||
|
|
||||||
const ldapClient = new Client({
|
const ldapClient = new Client({
|
||||||
|
@ -40,8 +46,11 @@ export async function updatePassword({
|
||||||
}): Promise<'SUCCESS' | 'FAIL'> {
|
}): Promise<'SUCCESS' | 'FAIL'> {
|
||||||
try {
|
try {
|
||||||
const userDN = await getUserDN(username)
|
const userDN = await getUserDN(username)
|
||||||
|
|
||||||
await ldapClient.bind(userDN, password)
|
await ldapClient.bind(userDN, password)
|
||||||
|
|
||||||
|
console.log('binded')
|
||||||
|
|
||||||
await ldapClient.modify(userDN, [
|
await ldapClient.modify(userDN, [
|
||||||
new Change({
|
new Change({
|
||||||
operation: 'delete',
|
operation: 'delete',
|
||||||
|
@ -60,10 +69,21 @@ export async function updatePassword({
|
||||||
])
|
])
|
||||||
|
|
||||||
return 'SUCCESS'
|
return 'SUCCESS'
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
console.error(err)
|
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 {
|
} finally {
|
||||||
await ldapClient.unbind()
|
await ldapClient.unbind()
|
||||||
|
console.log('unbinded')
|
||||||
}
|
}
|
||||||
return 'FAIL'
|
return 'FAIL'
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,8 @@ export const appRouter = router({
|
||||||
newPassword: z.string().min(8)
|
newPassword: z.string().min(8)
|
||||||
})
|
})
|
||||||
).mutation(async ({ input }) => {
|
).mutation(async ({ input }) => {
|
||||||
|
console.log('input', input)
|
||||||
|
|
||||||
const { username, password, newPassword } = input
|
const { username, password, newPassword } = input
|
||||||
try {
|
try {
|
||||||
await updatePassword({
|
await updatePassword({
|
||||||
|
|
Loading…
Reference in New Issue
Block a user