Skip to content

Commit

Permalink
fix: auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Jun 6, 2024
1 parent e8d3ca2 commit 0f0f58c
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 35 deletions.
9 changes: 4 additions & 5 deletions dist/browser/chats/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/browser/chats/index.js.map

Large diffs are not rendered by default.

34 changes: 30 additions & 4 deletions dist/browser/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/browser/index.js.map

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions dist/node/chats/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/node/chats/index.js.map

Large diffs are not rendered by default.

34 changes: 30 additions & 4 deletions dist/node/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/node/index.js.map

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export declare class FilenSDK {
private _contacts;
private _user;
socket: Socket;
private _updateKeyPairTries;
/**
* Creates an instance of FilenSDK.
* @date 2/21/2024 - 8:58:43 AM
Expand Down Expand Up @@ -380,7 +381,14 @@ export declare class FilenSDK {
authVersion: number;
salt: string;
hasRecoveryKeys: boolean;
newMasterKeys: string;
newMasterKeys: string; /**
* Creates an instance of FilenSDK.
* @date 2/21/2024 - 8:58:43 AM
*
* @constructor
* @public
* @param {?FilenSDKConfig} [params]
*/
}) => Promise<void>;
};
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filen/sdk",
"version": "0.1.114",
"version": "0.1.115",
"description": "Filen SDK",
"main": "dist/node/index.js",
"browser": "dist/browser/index.js",
Expand Down
15 changes: 8 additions & 7 deletions src/chats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,14 @@ export class Chats {
this._chatKeyCache.set(uuidToUse, key)

if (contacts) {
const promises: Promise<void>[] = []

for (const contact of contacts) {
promises.push(this.addParticipant({ conversation: uuidToUse, contact }))
}

await promiseAllChunked(promises)
await promiseAllChunked(
contacts.map(contact =>
this.addParticipant({
conversation: uuidToUse,
contact
})
)
)
}

return uuidToUse
Expand Down
41 changes: 37 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class FilenSDK {
private _contacts: Contacts
private _user: User
public socket: Socket = new Socket()
private _updateKeyPairTries = 0

/**
* Creates an instance of FilenSDK.
Expand Down Expand Up @@ -282,7 +283,32 @@ export class FilenSDK {
}

if (!privateKey) {
throw new Error("Could not decrypt private key.")
// If the user for example changed his password and did not properly import the old master keys, it could be that we cannot decrypt the private key anymore.
// We try to decrypt it 3 times (might be network/API related) and if it still does not work, we generate a new keypair.
if (this._updateKeyPairTries < 3) {
this._updateKeyPairTries += 1

await new Promise<void>(resolve => setTimeout(resolve, 250))

return await this.__updateKeyPair({
apiKey,
masterKeys
})
}

const generatedKeyPair = await this._crypto.utils.generateKeyPair()

await this._updateKeyPair({
apiKey,
publicKey: generatedKeyPair.publicKey,
privateKey: generatedKeyPair.privateKey,
masterKeys
})

return {
publicKey: generatedKeyPair.publicKey,
privateKey: generatedKeyPair.privateKey
}
}

await this._updateKeyPair({
Expand Down Expand Up @@ -320,9 +346,16 @@ export class FilenSDK {
apiKey: string
masterKeys: string[]
}): Promise<{ masterKeys: string[]; publicKey: string; privateKey: string }> {
const encryptedMasterKeys = await this._crypto
.encrypt()
.metadata({ metadata: masterKeys.join("|"), key: masterKeys[masterKeys.length - 1] })
const currentLastMasterKey = masterKeys[masterKeys.length - 1]

if (!currentLastMasterKey || currentLastMasterKey.length < 16) {
throw new Error("Invalid current master key.")
}

const encryptedMasterKeys = await this._crypto.encrypt().metadata({
metadata: masterKeys.join("|"),
key: currentLastMasterKey
})

const masterKeysResponse = await this._api.v3().user().masterKeys({
encryptedMasterKeys,
Expand Down

0 comments on commit 0f0f58c

Please sign in to comment.