import Databox from "./databox/Databox"; import StaticDatabox from "./databox/StaticDatabox"; import Channel from './channel/Channel'; import StaticChannel from './channel/StaticChannel'; import { SignOptions } from 'jsonwebtoken'; import { RawSocket } from '../main/definitions/rawSocket'; import { AuthToken } from '../main/definitions/internal'; import { Agent } from 'useragent'; import { DeepReadonly } from 'ts-essentials'; import { ObjectEditOperation } from '../main/definitions/objectEditOperation'; import { UpgradeRequest } from "ziron-worker"; declare type TokenUpdateListener = (oldToken: null | AuthToken, newToken: null | AuthToken) => void; export default class Socket { private readonly _databoxes; private readonly _channels; private _attachment; /** * @description * Indicates if the socket is authenticated. */ readonly authenticated: boolean; /** * @description * The current userGroup of the socket. */ readonly userGroup: string; /** * @description * The current userId of the socket. */ readonly userId: string | number | undefined; protected _tokenUpdateListener: TokenUpdateListener[]; constructor(rawSocket: RawSocket); /** * Disconnect this socket. */ disconnect(code?: any, data?: any): void; private _connectToRawSocket; /** * @description * This function can be used to recheck the access of this socket to all components * where it is connected to (Databoxes, Channels). * If the access was denied the socket will be kicked out from this component. * It will automatically run whenever the token state of this socket had changed. */ recheckAccess(): Promise; private _connectTokenUpdate; private _loadNewToken; /** * @description * Authenticate this socket. * When the socket is already authenticated, * a brand-new token will be created. * @example * await socket.authenticate('user','tom12',{email: 'example@gmail.com'}); * @param authUserGroup * The authUserGroup must exist in the app config. * Otherwise, an error will be thrown. * @param userId * @param payload * Sets the payload of the token. * Notice that when you call authenticate and the socket is already authenticated * that the complete old token payload will be overridden. * @param signOptions * This optional options argument is an Object which can be used to modify the token's behavior. * Valid properties include any option accepted by the jsonwebtoken library's sign method. * For example, you can change the default expire of the token or add a time before the token gets valid. * @throws UnknownAuthUserGroupError */ authenticate(authUserGroup: string, userId?: string | number, payload?: Partial, signOptions?: SignOptions): Promise; /** * @description * Deauthenticate this socket. */ deauthenticate(): void; /** * @description * Returns if the socket is unauthenticated. */ get unauthenticated(): boolean; /** * @description * Returns the plain auth token. */ get authToken(): DeepReadonly>> | null; /** * @description * Returns the plain auth token. * @throws AuthenticationRequiredError if the socket is not authenticated. */ getAuthToken(): DeepReadonly>>; /** * @description * Returns the raw singed auth token. */ get signedAuthToken(): string | null; /** * @description * Returns the raw signed auth token. * @throws AuthenticationRequiredError if the socket is not authenticated. */ getSignedAuthToken(): string; /** * Sets the user id of this socket. * Notice that it only can be updated when the socket is authenticated. * @param userId */ setUserId(userId: number | string | undefined): Promise; /** * Returns the user id of the socket. * @throws AuthenticationRequiredError if the socket is not authenticated. * @throws UndefinedUserIdError if user id is not defined. */ getUserId(): string | number; /** * @description * Returns the auth user group of the socket. */ get authUserGroup(): string | undefined; /** * @description * Returns the authUserGroup of the socket. * @throws AuthenticationRequiredError if the socket is not authenticated. * @throws UndefinedAuthUserGroupError if auth user group is not defined. * Can happen in only panel tokens. */ getAuthUserGroup(): string; /** * @description * Sets if the socket has panel access. * Notice that it only can be updated when the socket is authenticated. * @param access */ setPanelAccess(access: boolean): Promise; /** * @description * Returns if the socket has panel access. */ hasPanelAccess(): boolean; /** * @description * Returns the auth token payload of this socket. */ get authTokenPayload(): DeepReadonly> | null; /** * @description * Returns the auth token payload of this socket. * @throws AuthenticationRequiredError if this socket is not authenticated. */ getAuthTokenPayload(): DeepReadonly>; /** * @description * Returns a deep clone of the auth token payload of this socket. * @throws AuthenticationRequiredError if the socket is not authenticated. */ getTokenPayloadClone(): Partial; /** * @description * Sets the auth token payload by overriding the old payload. * Every change on the token will update the * authentication of this socket. (Like a new authentication on top). * You can access the token payload on the client and server-side. * But only change, delete or set from the server-side. * Notice that this socket must be authenticated. * @example * await setTokenPayload({name: 'Luc'}); * @param payload * @throws AuthenticationRequiredError if the socket is not authenticated. */ setAuthTokenPayload(payload?: Partial): Promise; /** * Applies edit actions on the token payload. * Only for advance use cases. * Is used internally. * @param operations * @throws AuthenticationRequiredError if this socket is not authenticated. */ applyEditOperationsOnTokenPayload(operations: ObjectEditOperation[]): Promise; /** * @description * Edit the auth token payload. * When using the bundle option, you can make several changes in one action. * Don't forget to call the commit method after your changes in bundle mode. * Every change on the token of a socket will update the authentication * of this socket. (Like a new authentication on top). * You can access the token payload on the client and server-side. * But only delete or set from the server-side. * @example * //Single change * editAuthTokenPayload().name = 'Luc'; * //Multiple changes * const payloadEdit = editAuthTokenPayload(true); * payloadEdit.age = 22; * delete payloadEdit.car; * payloadEdit.commit(); * @param bundle */ editAuthTokenPayload(bundle?: B): true extends B ? import("../main/internalApi/pathEditContentAPICreator").PathEditContentBundle : import("../main/internalApi/pathEditContentAPICreator").PathEditContent; /** * @description * Edit the auth token payload on every token with the * current userId (synchronized on user-id). * When using the bundle option, you can make several changes in one action. * Don't forget to call the commit method after your changes in bundle mode. * Every change on the token of a socket will update the authentication * of this socket. (Like a new authentication on top). * You can access the token payload on the client and server-side. * But only delete or set from the server-side. * @example * //Single change * editAuthTokenPayloadUserIdSync().name = 'Luc'; * //Multiple changes * const payloadEdit = editAuthTokenPayloadUserIdSync(true); * payloadEdit.age = 22; * delete payloadEdit.car; * payloadEdit.commit(); * @param bundle */ editAuthTokenPayloadUserIdSync(bundle?: B): true extends B ? import("../main/internalApi/pathEditContentAPICreator").PathEditContentBundle : import("../main/internalApi/pathEditContentAPICreator").PathEditContent; /** * @description * Edit the auth token payload on every token with the * current auth user group (synchronized on auth user group). * When using the bundle option, you can make several changes in one action. * Don't forget to call the commit method after your changes in bundle mode. * Every change on the token of a socket will update the authentication * of this socket. (Like a new authentication on top). * You can access the token payload on the client and server-side. * But only delete or set from the server-side. * @example * //Single change * editAuthTokenPayloadGroupSync().name = 'Luc'; * //Multiple changes * const payloadEdit = editAuthTokenPayloadGroupSync(true); * payloadEdit.age = 22; * delete payloadEdit.car; * payloadEdit.commit(); * @param bundle */ editAuthTokenPayloadGroupSync(bundle?: B): true extends B ? import("../main/internalApi/pathEditContentAPICreator").PathEditContentBundle : import("../main/internalApi/pathEditContentAPICreator").PathEditContent; get attachment(): Partial; /** * Clears the attachment. */ clearAttachment(): void; /** * Returns this socket id. */ get id(): string; /** * Returns this socket sid. */ get sid(): string; /** * Returns the raw socket. */ get rawSocket(): RawSocket; /** * Returns the Databoxes where this socket is connected to. */ get databoxes(): (Databox | StaticDatabox)[]; /** * Returns the Channels that this socket has subscribed. */ get channels(): (Channel | StaticChannel)[]; /** * @description * Returns the upgrade HTTP request. */ get upgradeRequest(): UpgradeRequest; /** * @description * Returns the socket handshake attachment. * @example * getHandshakeAttachment()?.deviceCode; */ get handshakeAttachment(): any; /** * @description * Returns the accept language of this socket. * Note that it is possible that no accept-language header * was included in the upgrade headers; in this case, an empty string is returned. * @example * en-US,en;q=0.8,et;q=0.6" */ get acceptLanguage(): string; /** * @description * Returns the agent of the client by using the * npm package 'useragent' to parse it. * Note that it is possible that no user agent was included in the header. * @example * //get operating system * getUserAgent().os.toString(); // 'Mac OSX 10.8.1' * //get device * getUserAgent().device.toString(); // 'Asus A100' */ get userAgent(): Agent; /** * @description * Returns the remote IP address of this socket. */ get remoteAddress(): string; /** * @description * Returns the system of the client. */ get clientSystem(): string; /** * @description * Returns the version of the client. */ get clientVersion(): number; /** * Returns the API level of the client. * This API level can be the connection, or default API level. */ get apiLevel(): number; /** * Returns the API level of the connection, * can be undefined if the client did not provide it. */ get connectionApiLevel(): number | undefined; /** * Checks if the API level of the client is compatible with the API level. * @param apiLevel */ isCompatibleApiLevel(apiLevel: number): boolean; } export {};