-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added templates for config, middleware and contracts
- Loading branch information
1 parent
b2c27ab
commit a7c0ce6
Showing
4 changed files
with
91 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ApplicationContract } from '@ioc:Adonis/Core/Application' | ||
import * as sinkStatic from '@adonisjs/sink' | ||
import { join } from 'path' | ||
|
||
/** | ||
* Instructions to be executed when setting up the package. | ||
*/ | ||
export default async function instructions( | ||
projectRoot: string, | ||
app: ApplicationContract, | ||
sink: typeof sinkStatic | ||
): Promise<void> { | ||
const middlewareDir = app.resolveNamespaceDirectory('middleware') || 'app/Middleware' | ||
const contractDir = app.resolveNamespaceDirectory('contracts') || 'contracts' | ||
|
||
const middlewarePath = join(middlewareDir, 'Hcaptcha.ts') | ||
const middlewareTemplate = join(__dirname, 'templates/middleware/Hcaptcha.txt') | ||
|
||
const middleware = new sink.files.TemplateLiteralFile( | ||
projectRoot, | ||
middlewarePath, | ||
middlewareTemplate | ||
) | ||
if (middleware.exists()) { | ||
sink.logger.action('create').skipped(`${middlewarePath} file already exists`) | ||
} else { | ||
middleware.apply().commit() | ||
sink.logger.action('create').succeeded(middlewarePath) | ||
} | ||
|
||
const contractPath = join(contractDir, 'hcaptcha.ts') | ||
const contractTemplate = join(__dirname, 'templates/contracts/hcaptcha.txt') | ||
|
||
const contract = new sink.files.TemplateLiteralFile(projectRoot, contractPath, contractTemplate) | ||
if (contract.exists()) { | ||
sink.logger.action('create').skipped(`${contractPath} file already exists`) | ||
} else { | ||
contract.apply().commit() | ||
sink.logger.action('create').succeeded(contractPath) | ||
} | ||
} |
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,17 @@ | ||
import Env from '@ioc:Adonis/Core/Env' | ||
import { HcaptchaConfig } from '@ioc:Hcaptcha' | ||
|
||
const hcaptchaConfig: HcaptchaConfig = { | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Secret Key | ||
|-------------------------------------------------------------------------- | ||
| | ||
| The shared key between your site and reCAPTCHA | ||
| * Don`t tell it to anyone | ||
| | ||
*/ | ||
secretKey: Env.get('secretKey'), | ||
siteKey: Env.get('siteKey'), | ||
} | ||
export default hcaptchaConfig |
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,7 @@ | ||
declare module '@ioc:Adonis/Core/HttpContext' { | ||
import { HcaptchaResponse } from '@ioc:Hcaptcha' | ||
|
||
interface HttpContextContract { | ||
hcaptcha: HcaptchaResponse | null | ||
} | ||
} |
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,26 @@ | ||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' | ||
import { Exception } from '@adonisjs/core/build/standalone' | ||
import HcaptchaValidator from '@ioc:Hcaptcha' | ||
|
||
/** | ||
* ReCAPTCHA middleware is meant to check recaptcha response | ||
* when POST/PUT requests | ||
* | ||
* You must register this middleware inside `start/kernel.ts` file under the list | ||
* of named middleware | ||
*/ | ||
export default class Hcaptcha { | ||
public async handle(ctx: HttpContextContract, next: () => Promise<void>): Promise<void> { | ||
HcaptchaValidator.test() | ||
|
||
const getHcaptchaTokenFromResponse = ctx.request.input('h-captcha-response') | ||
try { | ||
ctx.hcaptcha = await HcaptchaValidator.verifyToken(getHcaptchaTokenFromResponse) | ||
} catch (error) { | ||
console.warn('Error in the package') | ||
throw new Exception('invalid-input-response', 400, 'H_CAPTCHA') | ||
} | ||
|
||
await next() | ||
} | ||
} |