better code: clearer names, to tell server from client srp
Massimo Melina committed
Jul 23, 2024 at 11:37 UTC
d168467c166a46572d3463586ef5e8b09f4156e4
5 files changed
+12
-12
frontend/src/menu.ts
+1
-1
@@ -18,7 +18,7 @@ import { apiCall } from '@hfs/shared/api'
18
import { reloadList } from './useFetchList'
19
import { t, useI18N } from './i18n'
20
import { cut } from './clip'
21
-import { Btn, BtnProps, CustomCode, iconBtn } from './components'
21
+import { Btn, BtnProps, CustomCode } from './components'
22
23
export function MenuPanel() {
24
const { showFilter, remoteSearch, stopSearch, searchManuallyInterrupted, selected, props, searchOptions } = useSnapState()
src/api.auth.ts
+3
-3
@@ -6,7 +6,7 @@ import { SRPServerSessionStep1 } from 'tssrp6a'
6
import { ADMIN_URI, HTTP_UNAUTHORIZED, HTTP_BAD_REQUEST, HTTP_SERVER_ERROR, HTTP_CONFLICT, HTTP_NOT_FOUND } from './const'
7
import { ctxAdminAccess } from './adminApis'
8
import { sessionDuration } from './middlewares'
9
-import { getCurrentUsername, setLoggedIn, srpStep1 } from './auth'
9
+import { getCurrentUsername, setLoggedIn, srpServerStep1 } from './auth'
10
import { defineConfig } from './config'
11
import events from './events'
12
@@ -26,9 +26,9 @@ export const loginSrp1: ApiHandler = async ({ username }, ctx) => {
26
return new ApiError(HTTP_UNAUTHORIZED)
27
}
28
try {
29
- const { step1, ...rest } = await srpStep1(account)
29
+ const { srpServer, ...rest } = await srpServerStep1(account)
30
const sid = Math.random()
31
- ongoingLogins[sid] = step1
31
+ ongoingLogins[sid] = srpServer
32
setTimeout(()=> delete ongoingLogins[sid], 60_000)
33
ctx.session.loggingIn = { username, sid } // temporarily store until process is complete
34
return rest
src/auth.ts
+5
-5
@@ -9,15 +9,15 @@ import events from './events'
9
10
const srp6aNimbusRoutines = new SRPRoutines(new SRPParameters())
11
12
-export async function srpStep1(account: Account) {
12
+export async function srpServerStep1(account: Account) {
13
if (!account.srp)
14
throw HTTP_NOT_ACCEPTABLE
15
const [salt, verifier] = account.srp.split('|')
16
if (!salt || !verifier)
17
throw Error("malformed account")
18
const srpSession = new SRPServerSession(srp6aNimbusRoutines)
19
- const step1 = await srpSession.step1(account.username, BigInt(salt), BigInt(verifier))
20
- return { step1, salt, pubKey: String(step1.B) } // cast to string cause bigint can't be jsonized
19
+ const srpServer = await srpSession.step1(account.username, BigInt(salt), BigInt(verifier))
20
+ return { srpServer, salt, pubKey: String(srpServer.B) } // cast to string cause bigint can't be jsonized
21
}
22
23
const cache: any = {}
@@ -26,10 +26,10 @@ export async function srpCheck(username: string, password: string) {
26
if (!account?.srp || !password) return
27
const k = createHash('sha256').update(username + password + account.srp).digest("hex")
28
const good = await getOrSet(cache, k, async () => {
29
- const { step1, salt, pubKey } = await srpStep1(account)
29
+ const { srpServer, salt, pubKey } = await srpServerStep1(account)
30
const client = await srpClientPart(username, password, salt, pubKey)
31
setTimeout(() => delete cache[k], 60_000)
32
- return step1.step2(client.A, client.M1).then(() => 1, () => 0)
32
+ return srpServer.step2(client.A, client.M1).then(() => 1, () => 0)
33
})
34
return good ? account : undefined
35
}
src/debounceAsync.ts
+1
-1
@@ -15,7 +15,7 @@ export function debounceAsync<Cancelable extends boolean = false, A extends unkn
15
retain?: number,
16
// for how long do you want to cache last failure value, and return that at next invocation?
17
retainFailure?: number,
18
- // should we offer a cancel method to the returned function?
18
+ // should we offer a cancel method to the returned function? if we do, the awaited-type will include undefined
19
cancelable?: Cancelable
20
} = {}
21
) {
src/srp.ts
+2
-2
@@ -13,7 +13,7 @@ export async function srpClientSequence(username:string, password:string, apiCal
13
14
export async function srpClientPart(username: string, password: string, salt: string, pubKey: string) {
15
const srp6aNimbusRoutines = new SRPRoutines(new SRPParameters())
16
- const srp = new SRPClientSession(srp6aNimbusRoutines);
17
- const res = await srp.step1(username, password)
16
+ const srpClient = new SRPClientSession(srp6aNimbusRoutines);
17
+ const res = await srpClient.step1(username, password)
18
return await res.step2(BigInt(salt), BigInt(pubKey))
19
}
\ No newline at end of file