TeaWeb/shared/js/profiles/identities/NameIdentity.ts

97 lines
3 KiB
TypeScript
Raw Normal View History

2020-03-30 13:44:18 +02:00
import {
AbstractHandshakeIdentityHandler,
HandshakeCommandHandler,
IdentitifyType,
Identity
} from "../../profiles/Identity";
import * as log from "../../log";
import {LogCategory} from "../../log";
import {CommandResult} from "../../connection/ServerConnectionDeclaration";
import {AbstractServerConnection} from "../../connection/ConnectionBase";
import {HandshakeIdentityHandler} from "../../connection/HandshakeHandler";
import { tr } from "tc-shared/i18n/localize";
2020-03-30 13:44:18 +02:00
class NameHandshakeHandler extends AbstractHandshakeIdentityHandler {
readonly identity: NameIdentity;
handler: HandshakeCommandHandler<NameHandshakeHandler>;
constructor(connection: AbstractServerConnection, identity: NameIdentity) {
super(connection);
this.identity = identity;
this.handler = new HandshakeCommandHandler(connection, this);
this.handler["handshakeidentityproof"] = () => this.trigger_fail("server requested unexpected proof");
}
2020-03-30 13:44:18 +02:00
start_handshake() {
this.connection.command_handler_boss().register_handler(this.handler);
this.connection.send_command("handshakebegin", {
intention: 0,
authentication_method: this.identity.type(),
client_nickname: this.identity.name()
}).catch(error => {
log.error(LogCategory.IDENTITIES, tr("Failed to initialize name based handshake. Error: %o"), error);
if(error instanceof CommandResult)
error = error.extra_message || error.message;
this.trigger_fail("failed to execute begin (" + error + ")");
}).then(() => this.trigger_success());
}
2020-03-30 13:44:18 +02:00
protected trigger_fail(message: string) {
this.connection.command_handler_boss().unregister_handler(this.handler);
super.trigger_fail(message);
}
2020-03-30 13:44:18 +02:00
protected trigger_success() {
this.connection.command_handler_boss().unregister_handler(this.handler);
super.trigger_success();
}
}
2020-03-30 13:44:18 +02:00
export class NameIdentity implements Identity {
private _name: string;
2019-10-19 17:13:40 +02:00
2020-03-30 13:44:18 +02:00
constructor(name?: string) {
this._name = name;
}
2020-03-30 13:44:18 +02:00
set_name(name: string) { this._name = name; }
2020-03-30 13:44:18 +02:00
name() : string { return this._name; }
2020-03-30 13:44:18 +02:00
fallback_name(): string | undefined {
return this._name;
}
2020-03-30 13:44:18 +02:00
uid(): string {
return btoa(this._name); //FIXME hash!
}
2020-03-30 13:44:18 +02:00
type(): IdentitifyType {
return IdentitifyType.NICKNAME;
}
2020-03-30 13:44:18 +02:00
valid(): boolean {
return this._name != undefined && this._name.length >= 5;
}
decode(data) : Promise<void> {
data = JSON.parse(data);
if(data.version !== 1)
throw "invalid version";
this._name = data["name"];
return;
}
encode?() : string {
return JSON.stringify({
version: 1,
name: this._name
});
}
2020-03-30 13:44:18 +02:00
spawn_identity_handshake_handler(connection: AbstractServerConnection) : HandshakeIdentityHandler {
return new NameHandshakeHandler(connection, this);
}
}