-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
browser-extensions/chrome/src/utilities/EncryptionUtility.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import argon2 from 'argon2-browser/dist/argon2-bundled.min.js'; | ||
|
||
/** | ||
* Utility class for encryption operations which includes Argon2id hashing. | ||
*/ | ||
class EncryptionUtility { | ||
public static async deriveKeyFromPassword( | ||
password: string, | ||
salt: string, | ||
encryptionType: string = 'Argon2id', | ||
encryptionSettings: string = '{"Iterations":1,"MemorySize":1024,"DegreeOfParallelism":4}' | ||
): Promise<string> { | ||
const settings = JSON.parse(encryptionSettings); | ||
|
||
try { | ||
if (encryptionType !== 'Argon2Id') { | ||
throw new Error('Unsupported encryption type'); | ||
} | ||
|
||
const hash = await argon2.hash({ | ||
pass: password, | ||
salt: salt, | ||
time: settings.Iterations, | ||
mem: settings.MemorySize, | ||
parallelism: settings.DegreeOfParallelism, | ||
hashLen: 32, | ||
type: 2, // 0 = Argon2d, 1 = Argon2i, 2 = Argon2id | ||
}); | ||
|
||
return hash.hashHex.toUpperCase(); | ||
} catch (error) { | ||
console.error('Argon2 hashing failed:', error); | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
export default EncryptionUtility; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import srp from 'secure-remote-password/client' | ||
|
||
interface LoginInitiateResponse { | ||
salt: string; | ||
serverEphemeral: string; | ||
encryptionType: string; | ||
encryptionSettings: string; | ||
} | ||
|
||
interface ValidateLoginResponse { | ||
requiresTwoFactor: boolean; | ||
token?: { | ||
token: string; | ||
refreshToken: string; | ||
}; | ||
serverSessionProof: string; | ||
} | ||
|
||
/** | ||
* Utility class for SRP authentication operations. | ||
*/ | ||
class SrpUtility { | ||
public async initiateLogin(username: string): Promise<LoginInitiateResponse> { | ||
// TODO: make base API URL configurable. The extension will have to support both official | ||
// and self-hosted instances. | ||
const response = await fetch('https://localhost:7223/v1/Auth/login', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ username: username.toLowerCase().trim() }) | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error('Login initiation failed'); | ||
} | ||
|
||
return await response.json(); | ||
} | ||
|
||
public async validateLogin( | ||
username: string, | ||
passwordHashString: string, | ||
rememberMe: boolean, | ||
loginResponse: LoginInitiateResponse | ||
): Promise<ValidateLoginResponse> { | ||
// 2. Generate client ephemeral | ||
const clientEphemeral = srp.generateEphemeral() | ||
|
||
// 3. Derive private key | ||
const privateKey = srp.derivePrivateKey(loginResponse.salt, username, passwordHashString); | ||
|
||
// 4. Derive session (simplified for example) | ||
const sessionProof = srp.deriveSession(clientEphemeral.secret, loginResponse.serverEphemeral, loginResponse.salt, username, privateKey); | ||
|
||
// 5. Send validation request | ||
// TODO: make base API URL configurable. The extension will have to support both official | ||
// and self-hosted instances. | ||
const response = await fetch('https://localhost:7223/v1/Auth/validate', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
username: username.toLowerCase().trim(), | ||
rememberMe: rememberMe, | ||
clientPublicEphemeral: clientEphemeral.public, | ||
clientSessionProof: sessionProof.proof, | ||
}) | ||
}); | ||
|
||
return await response.json(); | ||
} | ||
} | ||
|
||
export const srpUtility = new SrpUtility(); |