-
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.
Add optional (disabled by default) support for requiring new DIDs to …
…complete a proof-of-work challenge before using the server
- Loading branch information
1 parent
44f2c93
commit 6e0509f
Showing
10 changed files
with
519 additions
and
142 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { createHash } from 'crypto'; | ||
import type { Request, Response } from 'express'; | ||
import type { Express } from 'express'; | ||
import type { Dialect } from 'kysely'; | ||
import { Kysely } from 'kysely'; | ||
|
||
const recentChallenges: { [challenge: string]: number } = {}; | ||
const CHALLENGE_TIMEOUT = 60 * 1000; | ||
|
||
export class ProofOfWork { | ||
#db: Kysely<PowDatabase>; | ||
|
||
constructor(dialect: Dialect) { | ||
this.#db = new Kysely<PowDatabase>({ dialect: dialect }); | ||
} | ||
|
||
async initialize(): Promise<void> { | ||
setInterval(() => { | ||
for (const challenge of Object.keys(recentChallenges)) { | ||
if ( | ||
recentChallenges[challenge] && | ||
Date.now() - recentChallenges[challenge] > CHALLENGE_TIMEOUT | ||
) { | ||
delete recentChallenges[challenge]; | ||
} | ||
} | ||
}, CHALLENGE_TIMEOUT / 4); | ||
|
||
await this.#db.schema | ||
.createTable('authorizedTenants') | ||
.ifNotExists() | ||
.addColumn('did', 'text', (column) => column.primaryKey()) | ||
.execute(); | ||
} | ||
|
||
setupRoutes(server: Express): void { | ||
server.get('/register', (req: Request, res: Response) => | ||
this.getChallenge(req, res), | ||
); | ||
server.post('/register', (req: Request, res: Response) => | ||
this.verifyChallenge(req, res), | ||
); | ||
} | ||
|
||
async isAuthorized(tenant: string): Promise<boolean> { | ||
const result = await this.#db | ||
.selectFrom('authorizedTenants') | ||
.select('did') | ||
.where('did', '=', tenant) | ||
.execute(); | ||
|
||
return result.length > 0; | ||
} | ||
|
||
private async getChallenge(_req: Request, res: Response): Promise<void> { | ||
const challenge = generateChallenge(); | ||
recentChallenges[challenge] = Date.now(); | ||
res.json({ | ||
challenge: challenge, | ||
complexity: getComplexity(), | ||
}); | ||
} | ||
|
||
private async verifyChallenge(req: Request, res: Response): Promise<void> { | ||
const body: { | ||
did: string; | ||
challenge: string; | ||
response: string; | ||
} = req.body; | ||
|
||
const hash = createHash('sha256'); | ||
hash.update(body.challenge); | ||
hash.update(body.response); | ||
|
||
const complexity = getComplexity(); | ||
const digest = hash.digest('hex'); | ||
console.log('digest: ', digest); | ||
if (!digest.startsWith('0'.repeat(complexity))) { | ||
res.status(401).json({ success: false }); | ||
return; | ||
} | ||
|
||
try { | ||
await this.#db | ||
.insertInto('authorizedTenants') | ||
.values({ did: body.did }) | ||
.executeTakeFirst(); | ||
} catch (e) { | ||
console.log('error inserting did', e); | ||
res.status(500).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; | ||
} | ||
interface AuthorizedTenants { | ||
did: string; | ||
} | ||
|
||
interface PowDatabase { | ||
authorizedTenants: AuthorizedTenants; | ||
} |
Oops, something went wrong.