import { HubAuthResponse, TextileStorageAuth } from '@spacehq/utils'; import 'websocket-polyfill'; import { Identity } from './types'; import { Vault, VaultBackupType, VaultServiceConfig } from './vault'; /** * Represents an authenticated KeyPair identity with valid API session token. * */ export interface SpaceUser { identity: Identity; /** * Auth endpoint used to authenticate this user */ endpoint: string; /** * token is the service token. It can be used to interact with the identity service. * */ token: string; storageAuth?: TextileStorageAuth; } /** * An IdentityStorage handles persistence of Identity for the {@link Users} class. * * The sdk provides two implementation for this. * See {@link @spacehq/sdk#BrowserStorage} and {@link @spacehq/sdk#FileStorage}. */ export interface IdentityStorage { list: () => Promise; add: (identity: Identity) => Promise; remove: (key: string) => Promise; } /** * Configuration option provided to the {@link Users} class. * */ export interface UsersConfig { /** * Hub auth service endpoint * */ endpoint: string; /** * Vault Service Configuration. Either this is provided or the `vaultInit` function is provided * or else initializing the users class will throw an error. * */ vaultServiceConfig?: VaultServiceConfig; /** * Optional {@link @spacehq/sdk#Vault} factory function. * * If provided the default VaultService with provided config will not be used for authentication. */ vaultInit?: () => Vault; authChallengeSolver?: (identity: Identity) => Promise; } /** * Users a client wrapper for interacting with the Textile Users API. * * This API has the ability to: * * - Create new identity * * - Authenticate via identity against ws challenge * * * @example * Initialize Users without identity storage * ```typescript * import { Users } from '@spacehq/users' * * const users = new Users({ endpoint: "users.space.storage" }); * * // create new key pair * const id = users.createIdentity(); * * // authenticate against ws challenge, obtaining storageAuth * const user = await users.authenticate(id); * ``` * * @example * Initialize Users with BrowserStorage * ```typescript * import { Users, BrowserStorage } from '@spacehq/users' * * const storage = new BrowserStorage(); * // error is thrown when identity fails to auth * const onErrorCallback = (err, identity) => { ... }; * * // users are automatically restored from stored identities * const users = await Users.withStorage(storage, { endpoint: "users.space.storage" }, onErrorCallback); * * ``` */ export declare class Users { private config; private storage?; private users; private vaultObj?; constructor(config: UsersConfig, storage?: IdentityStorage); /** * Creates a users */ static withStorage(storage: IdentityStorage, config: UsersConfig, onError?: CallableFunction): Promise; /** * createIdentity generates a random keypair identity. * */ createIdentity(): Promise; /** * List all in memory {@link SpaceUser} that have been authenticated so far. * */ list(): SpaceUser[]; /** * Removes the users identity from list of authenticated users. * * It also removes the identity from the {@link IdentityStorage} provided. * * @param publicKey - public key of users identity */ remove(publicKey: string): Promise; /** * Authenticates the random keypair identity against the hub. Generating an appToken API Session token. * * If authentication succeeds, a SpaceUser object that can be used with the UserStorage class is returned. * * @param identity - User identity */ authenticate(identity: Identity): Promise; /** * Recovers users identity key information using the passphrase provided. * * If successfully recovered, the users information is stored in the `IdentityStorage` (if provided) * when initializing the users class. * * @param uuid - users unique vault id * @param passphrase - users passphrase used to recover keys * @param backupType - Type of vault backup the passphrase originates from */ recoverKeysByPassphrase(uuid: string, passphrase: string, backupType: VaultBackupType): Promise; /** * Backup the existing users identity key information using the passphrase provided. * * `Identity` can be gotten from {@link @spacehq/sdk#SpaceUser} gotten after a successful authentication * or recovery. * * @param uuid - users unique vault id * @param passphrase - users passphrase used to recover keys * @param backupType - Type of vault backup the passphrase originates from * @param identity - Identity containing private key of user to backup */ backupKeysByPassphrase(uuid: string, passphrase: string, backupType: VaultBackupType, identity: Identity): Promise; private get vault(); /** * Tries to get the private key from the Identity object provided. * if none found, then tries to get it from the identity storage if provided * * @private */ private getPrivKeyFromIdentity; }