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

135 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-03-30 11:44:18 +00:00
import {
AbstractHandshakeIdentityHandler,
HandshakeCommandHandler,
IdentitifyType,
Identity
} from "../../profiles/Identity";
import {LogCategory, logError} from "../../log";
import {CommandResult} from "../../connection/ServerConnectionDeclaration";
import {AbstractServerConnection} from "../../connection/ConnectionBase";
import {HandshakeIdentityHandler} from "../../connection/HandshakeHandler";
2020-03-30 11:44:18 +00:00
import * as forum from "./teaspeak-forum";
import { tr } from "tc-shared/i18n/localize";
2020-03-30 11:44:18 +00:00
class TeaForumHandshakeHandler extends AbstractHandshakeIdentityHandler {
readonly identity: TeaForumIdentity;
handler: HandshakeCommandHandler<TeaForumHandshakeHandler>;
constructor(connection: AbstractServerConnection, identity: TeaForumIdentity) {
super(connection);
this.identity = identity;
this.handler = new HandshakeCommandHandler(connection, this);
this.handler["handshakeidentityproof"] = this.handle_proof.bind(this);
}
executeHandshake() {
2020-03-30 11:44:18 +00:00
this.connection.command_handler_boss().register_handler(this.handler);
this.connection.send_command("handshakebegin", {
intention: 0,
authentication_method: this.identity.type(),
data: this.identity.data().data_json()
}).catch(error => {
logError(LogCategory.IDENTITIES, tr("Failed to initialize TeaForum based handshake. Error: %o"), error);
2020-03-30 11:44:18 +00:00
if(error instanceof CommandResult)
error = error.extra_message || error.message;
this.trigger_fail("failed to execute begin (" + error + ")");
});
}
2020-03-30 11:44:18 +00:00
private handle_proof(json) {
this.connection.send_command("handshakeindentityproof", {
proof: this.identity.data().data_sign()
}).catch(error => {
logError(LogCategory.IDENTITIES, tr("Failed to proof the identity. Error: %o"), error);
2020-03-30 11:44:18 +00:00
if(error instanceof CommandResult)
error = error.extra_message || error.message;
this.trigger_fail("failed to execute proof (" + error + ")");
}).then(() => this.trigger_success());
}
2020-03-30 11:44:18 +00:00
protected trigger_fail(message: string) {
this.connection.command_handler_boss().unregister_handler(this.handler);
super.trigger_fail(message);
}
2020-03-30 11:44:18 +00:00
protected trigger_success() {
this.connection.command_handler_boss().unregister_handler(this.handler);
super.trigger_success();
}
}
2020-03-30 11:44:18 +00:00
export class TeaForumIdentity implements Identity {
private readonly identity_data: forum.Data;
2020-03-30 11:44:18 +00:00
valid() : boolean {
return !!this.identity_data && !this.identity_data.is_expired();
}
2020-03-30 11:44:18 +00:00
constructor(data: forum.Data) {
this.identity_data = data;
}
2020-03-30 11:44:18 +00:00
data() {
return this.identity_data;
}
2020-03-30 11:44:18 +00:00
decode(data) : Promise<void> {
data = JSON.parse(data);
if(data.version !== 1)
throw "invalid version";
2020-03-30 11:44:18 +00:00
return;
}
2020-03-30 11:44:18 +00:00
encode() : string {
return JSON.stringify({
version: 1
});
}
2020-03-30 11:44:18 +00:00
spawn_identity_handshake_handler(connection: AbstractServerConnection) : HandshakeIdentityHandler {
return new TeaForumHandshakeHandler(connection, this);
}
2020-03-30 11:44:18 +00:00
fallback_name(): string | undefined {
return this.identity_data ? this.identity_data.name() : undefined;
}
2019-08-21 08:00:01 +00:00
2020-03-30 11:44:18 +00:00
type(): IdentitifyType {
return IdentitifyType.TEAFORO;
}
2019-08-21 08:00:01 +00:00
2020-03-30 11:44:18 +00:00
uid(): string {
//FIXME: Real UID!
return "TeaForo#" + ((this.identity_data ? this.identity_data.name() : "Another TeaSpeak user"));
}
2019-08-21 08:00:01 +00:00
2020-03-30 11:44:18 +00:00
public static identity() {
return static_identity;
}
2020-03-30 11:44:18 +00:00
}
2020-03-30 11:44:18 +00:00
let static_identity: TeaForumIdentity;
2020-03-30 11:44:18 +00:00
export function set_static_identity(identity: TeaForumIdentity) {
static_identity = identity;
}
2019-01-02 10:38:48 +00:00
2020-03-30 11:44:18 +00:00
export function update_forum() {
if(forum.logged_in() && (!static_identity || static_identity.data() !== forum.data())) {
static_identity = new TeaForumIdentity(forum.data());
} else {
static_identity = undefined;
}
2020-03-30 11:44:18 +00:00
}
2020-03-30 11:44:18 +00:00
export function valid_static_forum_identity() : boolean {
return static_identity && static_identity.valid();
}
2020-03-30 11:44:18 +00:00
export function static_forum_identity() : TeaForumIdentity | undefined {
return static_identity;
}