| 1 | // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt |
| 2 | |
| 3 | type ApiCall = (cmd:string, params:any) => any |
| 4 | type Srp = typeof import('tssrp6a') |
| 5 | |
| 6 | export async function srpClientSequence(srp: Srp, username:string, password:string, apiCall: ApiCall, extra?: object) { |
| 7 | const { pubKey, salt } = await apiCall('loginSrp1', { username }) |
| 8 | if (!salt) throw Error('salt') |
| 9 | const client = await srpClientPart(srp, username, password, salt, pubKey) |
| 10 | const res = await apiCall('loginSrp2', { pubKey: String(client.A), proof: String(client.M1), ...extra }) // bigint-s must be cast to string to be json-ed |
| 11 | await client.step3(BigInt(res.proof)).catch(() => Promise.reject('trust')) |
| 12 | return res |
| 13 | } |
| 14 | |
| 15 | export async function srpClientPart(srp: Srp, username: string, password: string, salt: string, pubKey: string) { |
| 16 | const srp6aNimbusRoutines = new srp.SRPRoutines(new srp.SRPParameters()) |
| 17 | const srpClient = new srp.SRPClientSession(srp6aNimbusRoutines) |
| 18 | const res = await srpClient.step1(username, password) |
| 19 | return await res.step2(BigInt(salt), BigInt(pubKey)) |
| 20 | } |