-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TW-1612: Temple Tap AirDrop. + SigAuth middleware
- Loading branch information
Showing
4 changed files
with
85 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { randomStringForEntropy } from '@stablelib/random'; | ||
import { getPkhfromPk, validateAddress, ValidationResult, verifySignature } from '@taquito/utils'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import memoizee from 'memoizee'; | ||
import * as yup from 'yup'; | ||
|
||
import { CodedError } from './utils/errors'; | ||
|
||
const SIGNING_NONCE_TTL = 5 * 60_000; | ||
|
||
export const getSigningNonce = memoizee( | ||
(pkh: string) => { | ||
if (validateAddress(pkh) !== ValidationResult.VALID) throw new CodedError(400, 'Invalid address'); | ||
|
||
return buildNonce(); | ||
}, | ||
{ | ||
max: 1_000_000, | ||
maxAge: SIGNING_NONCE_TTL | ||
} | ||
); | ||
|
||
export function removeSigningNonce(pkh: string) { | ||
getSigningNonce.delete(pkh); | ||
} | ||
|
||
export async function tezosSigAuthMiddleware(req: Request, res: Response, next: NextFunction) { | ||
const sigHeaders = await sigAuthHeadersSchema.validate(req.headers).catch(() => null); | ||
|
||
if (!sigHeaders) return void res.status(StatusCodes.UNAUTHORIZED).send(); | ||
|
||
const { 'tw-tez-sig-pk': publicKey, 'tw-tez-sig-msg': messageBytes, 'tw-tez-sig-sig': signature } = sigHeaders; | ||
|
||
let pkh: string; | ||
try { | ||
pkh = getPkhfromPk(publicKey); | ||
} catch (err) { | ||
console.error(err); | ||
|
||
return void res.status(StatusCodes.BAD_REQUEST).send({ message: 'Invalid public key' }); | ||
} | ||
|
||
// Nonce | ||
const { value: nonce } = getSigningNonce(pkh); | ||
const nonceBytes = Buffer.from(nonce, 'utf-8').toString('hex'); | ||
|
||
console.log('messageBytes=', messageBytes, '|', nonce); | ||
|
||
if (!messageBytes.includes(nonceBytes)) | ||
return void res.status(StatusCodes.UNAUTHORIZED).send({ code: 'INVALID_NONCE', message: 'Invalid message nonce' }); | ||
|
||
// Signature | ||
try { | ||
verifySignature(messageBytes, publicKey, signature); | ||
} catch (error) { | ||
console.error(error); | ||
|
||
return void res | ||
.status(StatusCodes.UNAUTHORIZED) | ||
.send({ code: 'INVALID_SIG', message: 'Invalid signature or message' }); | ||
} | ||
|
||
removeSigningNonce(pkh); | ||
|
||
next(); | ||
} | ||
|
||
const sigAuthHeadersSchema = yup.object({ | ||
'tw-tez-sig-pk': yup.string().required(), | ||
'tw-tez-sig-msg': yup.string().required(), | ||
'tw-tez-sig-sig': yup.string().required() | ||
}); | ||
|
||
function buildNonce() { | ||
// Same as in in SIWE.generateNonce() | ||
const value = randomStringForEntropy(96); | ||
|
||
const expiresAt = new Date(Date.now() + SIGNING_NONCE_TTL).toISOString(); | ||
|
||
return { value, expiresAt }; | ||
} |
This file was deleted.
Oops, something went wrong.