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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 4x 4x 4x 4x 4x 21x 21x 17x 5x 4x 4x 2x 2x 4x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 6x 36x 1x 35x 35x 35x 2x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 1x | "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PassmanClient = void 0;
const NextcloudServer_1 = require("./Model/NextcloudServer");
const DefaultLoggingService_1 = require("./Service/DefaultLoggingService");
const DefaultPersistenceService_1 = require("./Service/DefaultPersistenceService");
const PassmanServerConnection_1 = require("./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.
*/
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_1.DefaultLoggingService();
const connection = new PassmanServerConnection_1.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);
}
}
let definitiveLogger = logger ?? new DefaultLoggingService_1.DefaultLoggingService();
if (persistence?.autoRestoreOnReconstruction()) {
let passmanClient = new this(serverData, nextcloudServer ?? new NextcloudServer_1.NextcloudServer(serverData, definitiveLogger, persistence), definitiveLogger, 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, definitiveLogger, 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
Iif (!serverData.backendAppId) {
const successfulProbing = await PassmanClient.getServerBackendAppId(serverData);
if (successfulProbing) {
serverData.backendAppId = successfulProbing;
nextcloudServer?.setTemporaryBackendAppId(successfulProbing);
}
}
const connection = new PassmanServerConnection_1.PassmanServerConnection(serverData, nextcloudServer, logger ?? this.logger, persistence);
Iif (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;
}
/**
* If the server's live identity drifted from the map key (e.g. http to https baseUrl upgrade during fetch redirect),
* refresh the connection's id and rekey the internal map.
* @param previousConnectionId map key used when the connection was registered (needs to be a registered connectionId to be valid)
* @returns the current connectionId (unchanged when no upgrade happened)
*/
syncConnectionIdentity(previousConnectionId) {
const connection = this._connections.get(previousConnectionId);
Iif (connection === undefined) {
this.logger.onThrow(new Error(`No connection with id ${previousConnectionId} is managed.`));
return previousConnectionId;
}
const liveId = connection.server.getConnectionId();
if (liveId === previousConnectionId) {
return previousConnectionId;
}
this._connections.delete(previousConnectionId);
const refreshedId = connection.refreshConnectionId();
this._connections.set(refreshedId, connection);
Eif (this._activeConnectionId === previousConnectionId) {
this._activeConnectionId = refreshedId;
}
return refreshedId;
}
/**
* 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);
Iif (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_1.NextcloudServer({
...serverData,
backendAppId: testBackendId
}, new DefaultLoggingService_1.DefaultLoggingService(), new DefaultPersistenceService_1.DefaultPersistenceService());
const vaultsResponse1 = await server1.getJson('/vaults', () => { }, false);
if (vaultsResponse1) {
serverData.backendAppId = testBackendId;
// probe uses a shallow copy; copy back http to https baseUrl upgrades
if (server1.getBaseUrl() !== serverData.baseUrl) {
serverData.baseUrl = server1.getBaseUrl();
}
return testBackendId;
}
}
catch (_) {
// handle error case below
}
try {
testBackendId = testBackendId === 'passman-next' ? 'passman' : 'passman-next';
const server2 = new NextcloudServer_1.NextcloudServer({
...serverData,
backendAppId: testBackendId
}, new DefaultLoggingService_1.DefaultLoggingService(), new DefaultPersistenceService_1.DefaultPersistenceService());
const vaultsResponse2 = await server2.getJson('/vaults', () => { }, false);
if (vaultsResponse2) {
serverData.backendAppId = testBackendId;
if (server2.getBaseUrl() !== serverData.baseUrl) {
serverData.baseUrl = server2.getBaseUrl();
}
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);
}
}
exports.PassmanClient = PassmanClient;
|