@@ -7,9 +7,9 @@ import { encrypt, decrypt, keyToBuffer } from './crypto'
77
88export type ILambdaCaptcha = {
99 /**
10- * An unencrypted string representation of the captcha
10+ * An unencrypted representation of the captcha
1111 */
12- expr : string
12+ expr : any
1313 /**
1414 * An unencrypted string representation of the captcha
1515 */
@@ -18,6 +18,11 @@ export type ILambdaCaptcha = {
1818 * Captcha SVG
1919 */
2020 captchaSvg : string
21+
22+ /**
23+ * Unix timestamp when the captcha expires (UTC)
24+ */
25+ validUntil : number
2126}
2227
2328export function create ( config : ILambdaCaptchaConfig ) : ILambdaCaptcha {
@@ -31,27 +36,39 @@ export function create(config: ILambdaCaptchaConfig): ILambdaCaptcha {
3136 throw new Error ( `unknown captcha mode ${ config . mode } ` )
3237 }
3338
34- const expressionJson = expression . toJSON ( )
39+ // TODO: Generate timestamp
40+
41+ const validUntil = 0
42+ const validationInfo = JSON . stringify ( { expression : expression . toObject ( ) , validUntil } )
3543
3644 return {
37- expr : expressionJson ,
38- encryptedExpr : encrypt ( expressionJson , config . cryptoKey ) ,
39- captchaSvg : renderExpression ( expression , config )
45+ expr : expression ,
46+ encryptedExpr : encrypt ( validationInfo , config . cryptoKey ) ,
47+ captchaSvg : renderExpression ( expression , config ) ,
48+ validUntil
4049 }
4150}
4251
4352export function verify (
44- encryptedExpression : string ,
53+ validationInfo : string ,
4554 solution : any ,
4655 key : string
4756) {
4857 try {
49- const expressionJson = decrypt ( encryptedExpression , keyToBuffer ( key ) )
50- const o = JSON . parse ( expressionJson )
58+ const json = decrypt ( validationInfo , keyToBuffer ( key ) )
59+ const { expression : o , validUntil } = JSON . parse ( json )
5160
61+
5262 switch ( o . type ) {
5363 case 'math' :
5464 const expression = LambdaCaptchaMathExpression . fromJSON ( o )
65+
66+ const currentTimestamp = Math . floor ( Date . now ( ) / 1000 )
67+ if ( validUntil < currentTimestamp ) {
68+ console . log ( 'got' , validUntil , 'which is <' , currentTimestamp )
69+ return false
70+ }
71+
5572 return expression . solve ( ) == solution
5673 default :
5774 throw new Error ( `unknown captcha type ${ o . type } ` )
0 commit comments