Skip to content

Commit

Permalink
feat: Added templates for config, middleware and contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
NerdyLuffy committed Nov 4, 2021
1 parent b2c27ab commit a7c0ce6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
41 changes: 41 additions & 0 deletions instructions.ts
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)
}
}
17 changes: 17 additions & 0 deletions templates/config/hcaptcha.txt
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
7 changes: 7 additions & 0 deletions templates/contracts/hcaptcha.txt
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
}
}
26 changes: 26 additions & 0 deletions templates/middleware/Hcaptcha.txt
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()
}
}

0 comments on commit a7c0ce6

Please sign in to comment.