Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | import { NextcloudServer } from "./Model/NextcloudServer";
import { DefaultLoggingService } from "./Service/DefaultLoggingService";
import { DefaultPersistenceService } from "./Service/DefaultPersistenceService";
import { PassmanServerConnection } from "./Model/PassmanServerConnection";
/**
* PassmanClient manages one or more {@link PassmanServerConnection} instances.
*
* A backward-compatible single-server facade (server, preloadedVaults, preloadVaults, createVault,
* getFullVaultByGuid, getVaultByGuid, getTranslation, createInstance) delegates to the active
* connection, so existing single-server consumers are unaffected.
*/
export class PassmanClient {
logger;
/**
* Managed server connections, keyed by their stable connectionId.
*/
_connections = new Map();
/**
* connectionId of the connection the single-server facade delegates to.
*/
_activeConnectionId;
/**
* Create PassmanClient instance. Deprecated!
* @param serverData
* @param nextcloudServer
* @param logger
* @param persistence
* @throws ConfigurationError from nextcloud server configuration data
* @deprecated use PassmanClient.createInstance() instead in external/public libs
*/
constructor(serverData, nextcloudServer, logger, persistence) {
this.logger = logger ?? new DefaultLoggingService();
const connection = new PassmanServerConnection(serverData, nextcloudServer, this.logger, persistence);
this._registerConnection(connection);
}
/**
* Create PassmanClient instance.
* To use "auto restore on reconstruction" provide a custom persistence instance. This will never auto-unlock vaults.
* @param serverData
* @param nextcloudServer
* @param logger
* @param persistence
*/
static async createInstance(serverData, nextcloudServer, logger, persistence) {
// automatic probing if backendAppId is not explicitly set
if (!serverData.backendAppId) {
const successfulProbing = await PassmanClient.getServerBackendAppId(serverData);
if (successfulProbing) {
serverData.backendAppId = successfulProbing;
nextcloudServer?.setTemporaryBackendAppId(successfulProbing);
}
}
if (persistence?.autoRestoreOnReconstruction()) {
let passmanClient = new this(serverData, nextcloudServer ?? new NextcloudServer(serverData, logger, persistence), logger, persistence);
if (persistence.getModelStore() === undefined && persistence.getRequestCacheHandler() === undefined) {
throw new Error("autoRestoreOnReconstruction() is enabled but neither a model store nor a request cache handler is configured. Provide getModelStore() (preferred) or getRequestCacheHandler(), or disable autoRestoreOnReconstruction() from the PersistenceInterface.");
}
await passmanClient.activeConnection.restore();
return passmanClient;
}
else {
return new this(serverData, nextcloudServer, logger, persistence);
}
}
/**
* Add an additional server connection to this client.
* Performs backendAppId probing (if not explicitly set) and optional cache restore, mirroring createInstance.
* The first added connection becomes the active connection.
* @param serverData
* @param nextcloudServer
* @param logger
* @param persistence
*/
async addConnection(serverData, nextcloudServer, logger, persistence) {
// automatic probing if backendAppId is not explicitly set
if (!serverData.backendAppId) {
const successfulProbing = await PassmanClient.getServerBackendAppId(serverData);
if (successfulProbing) {
serverData.backendAppId = successfulProbing;
nextcloudServer?.setTemporaryBackendAppId(successfulProbing);
}
}
const connection = new PassmanServerConnection(serverData, nextcloudServer, logger ?? this.logger, persistence);
if (persistence?.autoRestoreOnReconstruction()) {
if (persistence.getModelStore() === undefined && persistence.getRequestCacheHandler() === undefined) {
throw new Error("autoRestoreOnReconstruction() is enabled but neither a model store nor a request cache handler is configured. Provide getModelStore() (preferred) or getRequestCacheHandler(), or disable autoRestoreOnReconstruction() from the PersistenceInterface.");
}
await connection.restore();
}
this._registerConnection(connection);
return connection;
}
_registerConnection(connection) {
this._connections.set(connection.connectionId, connection);
if (this._activeConnectionId === undefined) {
this._activeConnectionId = connection.connectionId;
}
}
/**
* Returns the managed connection for the given connectionId, if present.
* @param connectionId
*/
getConnection(connectionId) {
return this._connections.get(connectionId);
}
/**
* Removes the managed connection for the given connectionId.
* If the active connection is removed, another remaining connection (if any) becomes active.
* @param connectionId
* @returns whether a connection was removed
*/
removeConnection(connectionId) {
const removed = this._connections.delete(connectionId);
if (removed && this._activeConnectionId === connectionId) {
const next = this._connections.keys().next();
this._activeConnectionId = next.done ? undefined : next.value;
}
return removed;
}
/**
* All managed server connections.
*/
get connections() {
return Array.from(this._connections.values());
}
/**
* The connection the single-server facade delegates to.
* @throws Error if no connection is available
*/
get activeConnection() {
if (this._activeConnectionId === undefined) {
throw new Error("No active connection available.");
}
const connection = this._connections.get(this._activeConnectionId);
if (connection === undefined) {
throw new Error("Active connection is unexpectedly missing.");
}
return connection;
}
/**
* Select which managed connection the single-server facade delegates to.
* @param connectionId
* @throws Error if no connection with the given id is managed
*/
setActiveConnection(connectionId) {
if (!this._connections.has(connectionId)) {
throw new Error(`No connection with id ${connectionId} is managed.`);
}
this._activeConnectionId = connectionId;
}
/**
* Test and fix the provided NextcloudServerBackendAppId within the serverData
* @param serverData
*/
static async getServerBackendAppId(serverData) {
let testBackendId = serverData.backendAppId ?? 'passman';
try {
const server1 = new NextcloudServer({
...serverData,
backendAppId: testBackendId
}, new DefaultLoggingService(), new DefaultPersistenceService());
const vaultsResponse1 = await server1.getJson('/vaults', () => { }, false);
if (vaultsResponse1) {
serverData.backendAppId = testBackendId;
return testBackendId;
}
}
catch (_) {
// handle error case below
}
try {
testBackendId = testBackendId === 'passman-next' ? 'passman' : 'passman-next';
const server2 = new NextcloudServer({
...serverData,
backendAppId: testBackendId
}, new DefaultLoggingService(), new DefaultPersistenceService());
const vaultsResponse2 = await server2.getJson('/vaults', () => { }, false);
if (vaultsResponse2) {
serverData.backendAppId = testBackendId;
return testBackendId;
}
}
catch (_) {
// handle error case below
}
return undefined;
}
// --- backward-compatible single-server facade (delegates to the active connection) ---
get server() {
return this.activeConnection.server;
}
get preloadedVaults() {
return this.activeConnection.preloadedVaults;
}
set preloadedVaults(preloadedVaults) {
this.activeConnection.preloadedVaults = preloadedVaults;
}
/**
* Preloads vaults or refreshes the preloaded vaults array, if getCachedIfPossible = false.
* @param throwError
* @param getCachedIfPossible
*/
async preloadVaults(throwError = false, getCachedIfPossible = false) {
return this.activeConnection.preloadVaults(throwError, getCachedIfPossible);
}
async createVault(vaultName, vaultPassword) {
return this.activeConnection.createVault(vaultName, vaultPassword);
}
/**
* @deprecated use getFullVaultByGuid instead
* @param guid
* @param getCachedIfPossible
*/
async getVaultByGuid(guid, getCachedIfPossible = false) {
return this.activeConnection.getVaultByGuid(guid, getCachedIfPossible);
}
/**
* Returns full vault from cache, or fetches it from the server (and updates the full-featured vault cache afterward).
* If a vault key is provided, it tries not only to unlock, also to restore decrypted data if getCachedIfPossible is set.
* @param guid
* @param getCachedIfPossible
*/
async getFullVaultByGuid(guid, getCachedIfPossible = false, vaultKey) {
return this.activeConnection.getFullVaultByGuid(guid, getCachedIfPossible, vaultKey);
}
async getTranslation(lang = 'en') {
return this.activeConnection.getTranslation(lang);
}
}
|