-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http basic authentication support #57
- Loading branch information
Showing
5 changed files
with
44 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// This file is part of HFS - Copyright 2021-2022, Massimo Melina <[email protected]> - License https://www.gnu.org/licenses/gpl-3.0.txt | ||
|
||
import { getAccount, getCurrentUsername } from './perm' | ||
import { Account, getAccount, getCurrentUsername } from './perm' | ||
import { verifyPassword } from './crypt' | ||
import { ApiError, ApiHandler } from './apiMiddleware' | ||
import { SRPParameters, SRPRoutines, SRPServerSession, SRPServerSessionStep1 } from 'tssrp6a' | ||
|
@@ -12,7 +12,6 @@ import { ctxAdminAccess } from './adminApis' | |
import { prepareState } from './middlewares' | ||
|
||
const srp6aNimbusRoutines = new SRPRoutines(new SRPParameters()) | ||
const srpSession = new SRPServerSession(srp6aNimbusRoutines) | ||
const ongoingLogins:Record<string,SRPServerSessionStep1> = {} // store data that doesn't fit session object | ||
|
||
// centralized log-in state | ||
|
@@ -61,16 +60,26 @@ export const loginSrp1: ApiHandler = async ({ username }, ctx) => { | |
return new ApiError(500) | ||
if (!account) // TODO simulate fake account to prevent knowing valid usernames | ||
return new ApiError(UNAUTHORIZED) | ||
try { | ||
const { step1, ...rest } = await srpStep1(account) | ||
const sid = Math.random() | ||
ongoingLogins[sid] = step1 | ||
setTimeout(()=> delete ongoingLogins[sid], 60_000) | ||
ctx.session.login = { username, sid } | ||
return rest | ||
} | ||
catch (code: any) { | ||
return new ApiError(code) | ||
} | ||
} | ||
|
||
export async function srpStep1(account: Account) { | ||
if (!account.srp) | ||
return new ApiError(406) // unacceptable | ||
throw 406 // unacceptable | ||
const [salt, verifier] = account.srp.split('|') | ||
const srpSession = new SRPServerSession(srp6aNimbusRoutines) | ||
const step1 = await srpSession.step1(account.username, BigInt(salt), BigInt(verifier)) | ||
const sid = Math.random() | ||
ongoingLogins[sid] = step1 | ||
setTimeout(()=> delete ongoingLogins[sid], 60_000) | ||
|
||
ctx.session.login = { username, sid } | ||
return { salt, pubKey: String(step1.B) } // cast to string cause bigint can't be jsonized | ||
return { step1, salt, pubKey: String(step1.B) } // cast to string cause bigint can't be jsonized | ||
} | ||
|
||
export const loginSrp2: ApiHandler = async ({ pubKey, proof }, ctx) => { | ||
|
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