2020-02-24 17:52:21 +00:00
|
|
|
// https://tools.ietf.org/html/rfc5281 TTLS v0
|
|
|
|
// https://tools.ietf.org/html/draft-funk-eap-ttls-v1-00 TTLS v1 (not implemented)
|
|
|
|
/* eslint-disable no-bitwise */
|
|
|
|
import debug from 'debug';
|
|
|
|
import { IPacketHandlerResult, PacketResponseCode } from '../../../../types/PacketHandler';
|
|
|
|
import { IEAPMethod } from '../../../../types/EAPMethod';
|
|
|
|
import { IAuthentication } from '../../../../types/Authentication';
|
|
|
|
import { buildEAPResponse, decodeEAPHeader } from '../EAPHelper';
|
|
|
|
|
|
|
|
const log = debug('radius:eap:gtc');
|
|
|
|
|
|
|
|
export class EAPGTC implements IEAPMethod {
|
|
|
|
getEAPType(): number {
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
2020-02-25 10:54:57 +00:00
|
|
|
extractValue(msg: Buffer) {
|
2020-05-14 13:02:15 +00:00
|
|
|
let tillBinary0 = msg.findIndex((v) => v === 0) || msg.length;
|
2020-02-25 10:54:57 +00:00
|
|
|
if (tillBinary0 < 0) {
|
|
|
|
tillBinary0 = msg.length - 1;
|
2020-02-24 17:52:21 +00:00
|
|
|
}
|
2020-02-25 10:54:57 +00:00
|
|
|
return msg.slice(0, tillBinary0 + 1); // use token til binary 0.
|
|
|
|
}
|
2020-02-24 17:52:21 +00:00
|
|
|
|
2020-02-25 10:54:57 +00:00
|
|
|
identify(identifier: number, _stateID: string): IPacketHandlerResult {
|
2020-02-24 17:52:21 +00:00
|
|
|
return buildEAPResponse(identifier, 6, Buffer.from('Password: '));
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(private authentication: IAuthentication) {}
|
|
|
|
|
|
|
|
async handleMessage(
|
|
|
|
_identifier: number,
|
2020-02-25 10:54:57 +00:00
|
|
|
_stateID: string,
|
|
|
|
msg: Buffer,
|
|
|
|
_,
|
|
|
|
identity?: string
|
2020-02-24 17:52:21 +00:00
|
|
|
): Promise<IPacketHandlerResult> {
|
2020-02-25 10:54:57 +00:00
|
|
|
const username = identity; // this.loginData.get(stateID) as Buffer | undefined;
|
2020-02-24 17:52:21 +00:00
|
|
|
|
2020-06-25 09:17:19 +00:00
|
|
|
try {
|
|
|
|
const { data } = decodeEAPHeader(msg);
|
2020-02-24 17:52:21 +00:00
|
|
|
|
2020-06-25 09:17:19 +00:00
|
|
|
const token = this.extractValue(data);
|
2020-02-24 17:52:21 +00:00
|
|
|
|
2020-06-25 09:17:19 +00:00
|
|
|
if (!username) {
|
|
|
|
throw new Error('no username');
|
|
|
|
}
|
2020-02-24 17:52:21 +00:00
|
|
|
|
2020-06-25 09:17:19 +00:00
|
|
|
log('username', username, username.toString());
|
|
|
|
log('token', token, token.toString());
|
2020-02-24 17:52:21 +00:00
|
|
|
|
2020-06-25 09:17:19 +00:00
|
|
|
const success = await this.authentication.authenticate(username.toString(), token.toString());
|
2020-02-24 17:52:21 +00:00
|
|
|
|
2020-06-25 09:17:19 +00:00
|
|
|
return {
|
|
|
|
code: success ? PacketResponseCode.AccessAccept : PacketResponseCode.AccessReject,
|
|
|
|
attributes: (success && [['User-Name', username]]) || undefined,
|
|
|
|
};
|
|
|
|
} catch (err) {
|
|
|
|
console.error('decoding of EAP-GTC package failed', msg, err);
|
|
|
|
return {
|
|
|
|
code: PacketResponseCode.AccessReject,
|
|
|
|
};
|
|
|
|
}
|
2020-02-24 17:52:21 +00:00
|
|
|
}
|
|
|
|
}
|