2020-02-22 23:29:33 +00:00
|
|
|
import { IAuthentication } from '../../types/Authentication';
|
|
|
|
import {
|
2020-02-25 10:54:57 +00:00
|
|
|
IPacket,
|
2020-02-22 23:29:33 +00:00
|
|
|
IPacketHandler,
|
|
|
|
IPacketHandlerResult,
|
2020-05-14 13:02:15 +00:00
|
|
|
PacketResponseCode,
|
2020-02-22 23:29:33 +00:00
|
|
|
} from '../../types/PacketHandler';
|
2021-10-24 20:37:16 +00:00
|
|
|
import PackageInterface from '../../interface';
|
2020-02-22 23:29:33 +00:00
|
|
|
|
2021-10-24 20:37:16 +00:00
|
|
|
const packageInterface = PackageInterface.get();
|
|
|
|
const log = (...args) => packageInterface.log(...args);
|
2020-02-25 10:54:57 +00:00
|
|
|
|
|
|
|
export class UserPasswordPacketHandler implements IPacketHandler {
|
2020-02-22 23:29:33 +00:00
|
|
|
constructor(private authentication: IAuthentication) {}
|
|
|
|
|
2020-02-25 10:54:57 +00:00
|
|
|
async handlePacket(packet: IPacket): Promise<IPacketHandlerResult> {
|
|
|
|
const username = packet.attributes['User-Name'];
|
2021-05-28 10:33:26 +00:00
|
|
|
let password = packet.attributes['User-Password'];
|
|
|
|
|
2021-05-28 10:36:37 +00:00
|
|
|
if (Buffer.isBuffer(password) && password.indexOf(0x00) > 0) {
|
2021-05-28 10:33:26 +00:00
|
|
|
// check if there is a 0x00 in it, and trim it from there
|
|
|
|
password = password.slice(0, password.indexOf(0x00));
|
|
|
|
}
|
2020-02-22 23:29:33 +00:00
|
|
|
|
|
|
|
if (!username || !password) {
|
|
|
|
// params missing, this handler cannot continue...
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-02-25 10:54:57 +00:00
|
|
|
log('username', username, username.toString());
|
|
|
|
log('token', password, password.toString());
|
|
|
|
|
2021-10-24 20:37:16 +00:00
|
|
|
const [strUsername, strPassword] = packet.credentialMiddleware
|
|
|
|
? packet.credentialMiddleware(username.toString(), password.toString())
|
|
|
|
: [username.toString(), password.toString()];
|
|
|
|
|
|
|
|
const authenticated = await this.authentication.authenticate(strUsername, strPassword);
|
2020-02-22 23:29:33 +00:00
|
|
|
if (authenticated) {
|
|
|
|
// success
|
|
|
|
return {
|
2020-02-25 10:54:57 +00:00
|
|
|
code: PacketResponseCode.AccessAccept,
|
2020-05-14 13:02:15 +00:00
|
|
|
attributes: [['User-Name', username]],
|
2020-02-22 23:29:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failed
|
|
|
|
return {
|
2020-05-14 13:02:15 +00:00
|
|
|
code: PacketResponseCode.AccessReject,
|
2020-02-22 23:29:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|