This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: captcha wip: hcaptcha and turnstile * wip
- Loading branch information
1 parent
82f4f17
commit 1af1013
Showing
8 changed files
with
235 additions
and
26 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,18 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
|
||
export class turnstile1664548052072 implements MigrationInterface { | ||
name = 'turnstile1664548052072' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "meta" ADD "enableTurnstile" boolean NOT NULL DEFAULT false`); | ||
await queryRunner.query(`ALTER TABLE "meta" ADD "turnstileSiteKey" character varying(64)`); | ||
await queryRunner.query(`ALTER TABLE "meta" ADD "turnstileSecretKey" character varying(64)`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "meta" DROP COLUMN "turnstileSecretKey"`); | ||
await queryRunner.query(`ALTER TABLE "meta" DROP COLUMN "turnstileSiteKey"`); | ||
await queryRunner.query(`ALTER TABLE "meta" DROP COLUMN "enableTurnstile"`); | ||
} | ||
|
||
} |
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,121 @@ | ||
<template> | ||
<div> | ||
<span v-if="!available">{{ $t('waiting') }}<mk-ellipsis/></span> | ||
<div ref="captcha"></div> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent } from 'vue'; | ||
import i18n from '../../../i18n'; | ||
type Captcha = { | ||
render(container: string | Node, options: { | ||
readonly [_ in 'sitekey' | 'theme' | 'type' | 'size' | 'tabindex' | 'callback' | 'expired' | 'expired-callback' | 'error-callback' | 'endpoint']?: unknown; | ||
}): string; | ||
remove(id: string): void; | ||
execute(id: string): void; | ||
reset(id: string): void; | ||
getResponse(id: string): string; | ||
}; | ||
type CaptchaProvider = 'hcaptcha' | 'grecaptcha' | 'turnstile'; | ||
type CaptchaContainer = { | ||
readonly [_ in CaptchaProvider]?: Captcha; | ||
}; | ||
declare global { | ||
interface Window extends CaptchaContainer { | ||
} | ||
} | ||
export default defineComponent({ | ||
i18n: i18n(), | ||
props: { | ||
provider: { | ||
type: String, | ||
required: true, | ||
}, | ||
sitekey: { | ||
type: String, | ||
required: true, | ||
}, | ||
value: { | ||
type: String, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
available: false, | ||
}; | ||
}, | ||
computed: { | ||
loaded() { | ||
return !!window[this.provider as CaptchaProvider]; | ||
}, | ||
src() { | ||
const endpoint = ({ | ||
hcaptcha: 'https://hcaptcha.com/1', | ||
grecaptcha: 'https://www.google.com/recaptcha', | ||
turnstile: 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit', | ||
} as Record<PropertyKey, unknown>)[this.provider]; | ||
return `${typeof endpoint === 'string' ? endpoint : 'about:invalid'}/api.js?render=explicit`; | ||
}, | ||
captcha() { | ||
return window[this.provider as CaptchaProvider] || {} as unknown as Captcha; | ||
}, | ||
}, | ||
created() { | ||
if (this.loaded) { | ||
this.available = true; | ||
} else { | ||
(document.getElementById(this.provider) || document.head.appendChild(Object.assign(document.createElement('script'), { | ||
async: true, | ||
id: this.provider, | ||
src: this.src, | ||
}))) | ||
.addEventListener('load', () => this.available = true); | ||
} | ||
}, | ||
mounted() { | ||
if (this.available) { | ||
this.requestRender(); | ||
} else { | ||
this.$watch('available', this.requestRender); | ||
} | ||
}, | ||
beforeDestroy() { | ||
this.reset(); | ||
}, | ||
methods: { | ||
reset() { | ||
this.captcha?.reset(); | ||
}, | ||
requestRender() { | ||
if (this.captcha.render && this.$refs.captcha instanceof Element) { | ||
this.captcha.render(this.$refs.captcha, { | ||
sitekey: this.sitekey, | ||
theme: this.$store.state.device.darkMode ? 'dark' : 'light', | ||
callback: this.callback, | ||
'expired-callback': this.callback, | ||
'error-callback': this.callback, | ||
}); | ||
} else { | ||
setTimeout(this.requestRender.bind(this), 1); | ||
} | ||
}, | ||
callback(response?: string) { | ||
this.$emit('input', typeof response === 'string' ? response : null); | ||
}, | ||
}, | ||
}); | ||
</script> | ||
|
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
Oops, something went wrong.