-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clerk-js): Decouple captcha heartbeat from token refresh (#4630)
- Loading branch information
1 parent
3f890cf
commit 7623a99
Showing
12 changed files
with
144 additions
and
88 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,6 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
'@clerk/types': patch | ||
--- | ||
|
||
Decouple captcha heartbeat from token refresh mechanism |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { createWorkerTimers } from '@clerk/shared/workerTimers'; | ||
|
||
import { CaptchaChallenge } from '../../utils/captcha/CaptchaChallenge'; | ||
import type { Clerk } from '../resources/internal'; | ||
|
||
export class CaptchaHeartbeat { | ||
constructor( | ||
private clerk: Clerk, | ||
private captchaChallenge = new CaptchaChallenge(clerk), | ||
private timers = createWorkerTimers(), | ||
) {} | ||
|
||
public async start() { | ||
if (!this.isEnabled()) { | ||
return; | ||
} | ||
|
||
await this.challengeAndSend(); | ||
this.timers.setInterval(() => { | ||
void this.challengeAndSend(); | ||
}, this.intervalInMs()); | ||
} | ||
|
||
private async challengeAndSend() { | ||
if (!this.clerk.client) { | ||
return; | ||
} | ||
|
||
try { | ||
const params = await this.captchaChallenge.invisible(); | ||
await this.clerk.client.sendCaptchaToken(params); | ||
} catch (e) { | ||
// Ignore unhandled errors | ||
} | ||
} | ||
|
||
private isEnabled() { | ||
return !!this.clerk.__unstable__environment?.displayConfig.captchaHeartbeat; | ||
} | ||
|
||
private intervalInMs() { | ||
return this.clerk.__unstable__environment?.displayConfig.captchaHeartbeatIntervalMs ?? 10 * 60 * 1000; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { Clerk } from '../../core/resources/internal'; | ||
import { getCaptchaToken } from './getCaptchaToken'; | ||
import { retrieveCaptchaInfo } from './retrieveCaptchaInfo'; | ||
|
||
export class CaptchaChallenge { | ||
public constructor(private clerk: Clerk) {} | ||
|
||
/** | ||
* Triggers an invisible challenge. | ||
* This will always use the non-interactive variant of the CAPTCHA challenge and will | ||
* always use the fallback key. | ||
*/ | ||
public async invisible() { | ||
const { captchaSiteKey, canUseCaptcha, captchaURL, captchaPublicKeyInvisible } = retrieveCaptchaInfo(this.clerk); | ||
|
||
if (canUseCaptcha && captchaSiteKey && captchaURL && captchaPublicKeyInvisible) { | ||
return getCaptchaToken({ | ||
siteKey: captchaPublicKeyInvisible, | ||
invisibleSiteKey: captchaPublicKeyInvisible, | ||
widgetType: 'invisible', | ||
scriptUrl: captchaURL, | ||
captchaProvider: 'turnstile', | ||
}).catch(e => { | ||
if (e.captchaError) { | ||
return { captchaError: e.captchaError }; | ||
} | ||
return undefined; | ||
}); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
/** | ||
* Triggers a smart challenge if the user is required to solve a CAPTCHA. | ||
* Depending on the environment settings, this will either trigger an | ||
* invisible or smart (managed) CAPTCHA challenge. | ||
* Managed challenged start as non-interactive and escalate to interactive if necessary. | ||
* Important: For this to work at the moment, the instance needs to be using SMART protection | ||
* as we need both keys (visible and invisible) to be present. | ||
*/ | ||
public async managed() { | ||
const { captchaSiteKey, canUseCaptcha, captchaURL, captchaWidgetType, captchaProvider, captchaPublicKeyInvisible } = | ||
retrieveCaptchaInfo(this.clerk); | ||
|
||
if (canUseCaptcha && captchaSiteKey && captchaURL && captchaPublicKeyInvisible) { | ||
return getCaptchaToken({ | ||
siteKey: captchaSiteKey, | ||
widgetType: captchaWidgetType, | ||
invisibleSiteKey: captchaPublicKeyInvisible, | ||
scriptUrl: captchaURL, | ||
captchaProvider, | ||
modalWrapperQuerySelector: '#cl-modal-captcha-wrapper', | ||
modalContainerQuerySelector: '#cl-modal-captcha-container', | ||
openModal: () => this.clerk.__internal_openBlankCaptchaModal(), | ||
closeModal: () => this.clerk.__internal_closeBlankCaptchaModal(), | ||
}).catch(e => { | ||
if (e.captchaError) { | ||
return { captchaError: e.captchaError }; | ||
} | ||
return undefined; | ||
}); | ||
} | ||
|
||
return {}; | ||
} | ||
} |
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