-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(authentication, router,forms): global captcha support from router (
#580)
- Loading branch information
1 parent
ef84def
commit 24b4b81
Showing
13 changed files
with
139 additions
and
69 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
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
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
83 changes: 83 additions & 0 deletions
83
modules/router/src/security/handlers/captcha-validation/index.ts
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,83 @@ | ||
import { NextFunction, Response } from 'express'; | ||
import ConduitGrpcSdk, { | ||
ConfigController, | ||
DatabaseProvider, | ||
} from '@conduitplatform/grpc-sdk'; | ||
import { ConduitRequest } from '@conduitplatform/hermes'; | ||
import { verify as hcaptchaVerify } from 'hcaptcha'; | ||
import axios from 'axios'; | ||
|
||
export class CaptchaValidator { | ||
prod = false; | ||
database: DatabaseProvider; | ||
|
||
constructor(private readonly grpcSdk: ConduitGrpcSdk) { | ||
this.database = this.grpcSdk.database!; | ||
const self = this; | ||
this.grpcSdk.config.get('core').then(res => { | ||
if (res.env === 'production') { | ||
self.prod = true; | ||
} | ||
}); | ||
} | ||
|
||
async recaptchaVerify(secret: string, token: string) { | ||
const googleUrl = `https://www.google.com/siteverify?secret=${secret}&response=${token}`; | ||
const response = await axios.post( | ||
googleUrl, | ||
{}, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', | ||
}, | ||
}, | ||
); | ||
return response.data.success; | ||
} | ||
|
||
async turnstileVerify(secret: string, token: string) { | ||
const response = await axios.post( | ||
`https://challenges.cloudflare.com/turnstile/v0/siteverify`, | ||
{ | ||
secret, | ||
response: token, | ||
}, | ||
); | ||
return response.data.success; | ||
} | ||
|
||
async verifyCaptcha(secretKey: string, provider: string, token: string) { | ||
let success = false; | ||
if (provider === 'recaptcha') { | ||
success = await this.recaptchaVerify(secretKey, token); | ||
} else if (provider === 'hcaptcha') { | ||
const response = await hcaptchaVerify(secretKey, token); | ||
success = response.success; | ||
} else { | ||
success = await this.turnstileVerify(secretKey, token); | ||
} | ||
return success; | ||
} | ||
|
||
async middleware(req: ConduitRequest, res: Response, next: NextFunction) { | ||
const { enabled, provider, secretKey } = | ||
ConfigController.getInstance().config.captcha; | ||
const { captchaToken } = req.body; | ||
if (!enabled) { | ||
req.conduit!.captcha = 'disabled'; | ||
return next(); | ||
} | ||
if (!captchaToken) { | ||
req.conduit!.captcha = 'missing'; | ||
return next(); | ||
} | ||
const success = await this.verifyCaptcha(secretKey, provider, captchaToken); | ||
if (!success) { | ||
req.conduit!.captcha = 'failed'; | ||
return next(); | ||
} else { | ||
req.conduit!.captcha = 'success'; | ||
return next(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.