node-radius-server/src/radius/handler/UserPasswordPacketHandler.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

import { IAuthentication } from '../../types/Authentication';
import {
IPacket,
IPacketHandler,
IPacketHandlerResult,
2020-05-14 13:02:15 +00:00
PacketResponseCode,
} from '../../types/PacketHandler';
import PackageInterface from '../../interface';
const packageInterface = PackageInterface.get();
const log = (...args) => packageInterface.log(...args);
export class UserPasswordPacketHandler implements IPacketHandler {
constructor(private authentication: IAuthentication) {}
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'];
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));
}
if (!username || !password) {
// params missing, this handler cannot continue...
return {};
}
log('username', username, username.toString());
log('token', password, password.toString());
const [strUsername, strPassword] = packet.credentialMiddleware
? packet.credentialMiddleware(username.toString(), password.toString())
: [username.toString(), password.toString()];
const authenticated = await this.authentication.authenticate(strUsername, strPassword);
if (authenticated) {
// success
return {
code: PacketResponseCode.AccessAccept,
2020-05-14 13:02:15 +00:00
attributes: [['User-Name', username]],
};
}
// Failed
return {
2020-05-14 13:02:15 +00:00
code: PacketResponseCode.AccessReject,
};
}
}