-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue challenges, check them, increase complexity, and test it
- Loading branch information
1 parent
bc51fe3
commit e83c349
Showing
2 changed files
with
148 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,69 @@ | ||
import { createHash } from 'crypto'; | ||
import type { Request, Response } from 'express'; | ||
|
||
const outstandingHashes: { [challenge: string]: Date } = {}; | ||
const recentChallenges: { [challenge: string]: number } = {}; | ||
const CHALLENGE_TIMEOUT = 60 * 1000; | ||
|
||
setInterval(() => { | ||
for (const challenge of Object.keys(recentChallenges)) { | ||
if ( | ||
recentChallenges[challenge] && | ||
Date.now() - recentChallenges[challenge] > CHALLENGE_TIMEOUT | ||
) { | ||
console.log('challenge expired:', challenge); | ||
delete recentChallenges[challenge]; | ||
} | ||
} | ||
}, 30000); | ||
|
||
export async function getChallenge( | ||
_req: Request, | ||
res: Response, | ||
): Promise<void> { | ||
// sign a JWT with an expiration date shortly in the future (1-2 min) and a complexity | ||
// make the complexity go up when get challenge requests increase in frequency | ||
|
||
res.status(500).json({ error: 'unimplemented' }); | ||
const challenge = generateChallenge(); | ||
recentChallenges[challenge] = Date.now(); | ||
res.json({ | ||
challenge: challenge, | ||
complexity: getComplexity(), | ||
}); | ||
} | ||
|
||
export async function verifyChallenge( | ||
req: Request, | ||
res: Response, | ||
): Promise<void> { | ||
console.log(req.body); | ||
console.log('verifying challenge:', req.body); | ||
const body: { | ||
challenge: string; | ||
response: string; | ||
} = req.body; | ||
|
||
const hash = createHash('sha1'); | ||
const hash = createHash('sha256'); | ||
hash.update(body.challenge); | ||
hash.update(body.response); | ||
|
||
const hex = hash.digest('hex'); | ||
const complexity = Object.keys(outstandingHashes).length; | ||
for (let i = 0; i < complexity; i++) { | ||
if (hex[i] != '0') { | ||
res.status(401).json({ success: false }); | ||
return; | ||
} | ||
const complexity = getComplexity(); | ||
if (!hash.digest('hex').startsWith('0'.repeat(complexity))) { | ||
res.status(401).json({ success: false }); | ||
return; | ||
} | ||
|
||
res.json({ success: true }); | ||
} | ||
|
||
const challengeCharacters = | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
|
||
function generateChallenge(): string { | ||
let challenge = ''; | ||
while (challenge.length < 10) { | ||
challenge += challengeCharacters.charAt( | ||
Math.floor(Math.random() * challengeCharacters.length), | ||
); | ||
} | ||
return challenge; | ||
} | ||
|
||
function getComplexity(): number { | ||
return Object.keys(recentChallenges).length; | ||
} |
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