forked from rejetto/hfs
-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
76 additions
and
81 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 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 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.
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 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,40 @@ | ||
import { Account, getAccount, normalizeUsername } from './perm' | ||
import { HTTP_NOT_ACCEPTABLE, HTTP_SERVER_ERROR } from './cross-const' | ||
import { SRPParameters, SRPRoutines, SRPServerSession } from 'tssrp6a' | ||
import { Context } from 'koa' | ||
import { prepareState } from './middlewares' | ||
import { srpClientPart } from './srp' | ||
|
||
const srp6aNimbusRoutines = new SRPRoutines(new SRPParameters()) | ||
|
||
export async function srpStep1(account: Account) { | ||
if (!account.srp) | ||
throw HTTP_NOT_ACCEPTABLE | ||
const [salt, verifier] = account.srp.split('|') | ||
if (!salt || !verifier) | ||
throw Error("malformed account") | ||
const srpSession = new SRPServerSession(srp6aNimbusRoutines) | ||
const step1 = await srpSession.step1(account.username, BigInt(salt), BigInt(verifier)) | ||
return { step1, salt, pubKey: String(step1.B) } // cast to string cause bigint can't be jsonized | ||
} | ||
|
||
export async function srpCheck(username: string, password: string) { | ||
const account = getAccount(username) | ||
if (!account?.srp || !password) return false | ||
const { step1, salt, pubKey } = await srpStep1(account) | ||
const client = await srpClientPart(username, password, salt, pubKey) | ||
return await step1.step2(client.A, client.M1).then(() => true, () => false) | ||
} | ||
|
||
// centralized log-in state | ||
export async function loggedIn(ctx: Context, username: string | false) { | ||
const s = ctx.session | ||
if (!s) | ||
return ctx.throw(HTTP_SERVER_ERROR,'session') | ||
if (username === false) { | ||
delete s.username | ||
return | ||
} | ||
s.username = normalizeUsername(username) | ||
await prepareState(ctx, async ()=>{}) // updating the state is necessary to send complete session data so that frontend shows admin button | ||
} |
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 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,19 @@ | ||
// This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt | ||
|
||
import { SRPClientSession, SRPParameters, SRPRoutines } from 'tssrp6a' | ||
|
||
export async function srpClientSequence(username:string, password:string, apiCall: (cmd:string, params:any) => any) { | ||
const { pubKey, salt } = await apiCall('loginSrp1', { username }) | ||
if (!salt) throw Error('salt') | ||
const client = await srpClientPart(username, password, salt, pubKey) | ||
const res = await apiCall('loginSrp2', { pubKey: String(client.A), proof: String(client.M1) }) // bigint-s must be cast to string to be json-ed | ||
await client.step3(BigInt(res.proof)).catch(() => Promise.reject('trust')) | ||
return res | ||
} | ||
|
||
export async function srpClientPart(username: string, password: string, salt: string, pubKey: string) { | ||
const srp6aNimbusRoutines = new SRPRoutines(new SRPParameters()) | ||
const srp = new SRPClientSession(srp6aNimbusRoutines); | ||
const res = await srp.step1(username, password) | ||
return await res.step2(BigInt(salt), BigInt(pubKey)) | ||
} |
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