Skip to content

Commit 006cec9

Browse files
committed
feat: recaptcha-enterprise-callback
add a recaptcha enterprise callback
1 parent 8b4656c commit 006cec9

File tree

5 files changed

+132
-1
lines changed

5 files changed

+132
-1
lines changed

packages/javascript-sdk/src/auth/enums.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ enum CallbackType {
3636
PingOneProtectInitializeCallback = 'PingOneProtectInitializeCallback',
3737
PollingWaitCallback = 'PollingWaitCallback',
3838
ReCaptchaCallback = 'ReCaptchaCallback',
39-
ReCaptchaEnterpriseCallback = 'ReCaptchaEnterpriseCallback ',
39+
ReCaptchaEnterpriseCallback = 'ReCaptchaEnterpriseCallback',
4040
RedirectCallback = 'RedirectCallback',
4141
SelectIdPCallback = 'SelectIdPCallback',
4242
StringAttributeInputCallback = 'StringAttributeInputCallback',
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { describe, expect, it, beforeAll } from 'vitest';
2+
import ReCaptchaEnterpriseCallback from './recaptcha-enterprise-callback';
3+
import { CallbackType } from '../../auth/enums';
4+
import { Callback } from '../../auth/interfaces';
5+
6+
const recaptchaCallback: Callback = {
7+
type: 'ReCaptchaEnterpriseCallback' as CallbackType.ReCaptchaEnterpriseCallback,
8+
output: [
9+
{
10+
name: 'recaptchaSiteKey',
11+
value: '6LdSu_spAAAAAKz3UhIy4JYQld2lm_WRt7dEhf9T',
12+
},
13+
{
14+
name: 'captchaApiUri',
15+
value: 'https://www.google.com/recaptcha/enterprise.js',
16+
},
17+
{
18+
name: 'captchaDivClass',
19+
value: 'g-recaptcha',
20+
},
21+
],
22+
input: [
23+
{
24+
name: 'IDToken1token',
25+
value: '',
26+
},
27+
{
28+
name: 'IDToken1action',
29+
value: '',
30+
},
31+
{
32+
name: 'IDToken1clientError',
33+
value: '',
34+
},
35+
{
36+
name: 'IDToken1payload',
37+
value: '',
38+
},
39+
],
40+
};
41+
describe('enterprise recaptcha', () => {
42+
let callback: ReCaptchaEnterpriseCallback;
43+
beforeAll(() => {
44+
callback = new ReCaptchaEnterpriseCallback(recaptchaCallback);
45+
});
46+
it('should get the site key', () => {
47+
const siteKey = callback.getSiteKey();
48+
expect(siteKey).toEqual('6LdSu_spAAAAAKz3UhIy4JYQld2lm_WRt7dEhf9T');
49+
});
50+
it('should get captchaApiUri', () => {
51+
const url = callback.getApiUrl();
52+
expect(url).toEqual('https://www.google.com/recaptcha/enterprise.js');
53+
});
54+
it('should set the action', () => {
55+
callback.setAction('my_action');
56+
expect(callback.getInputValue('IDToken1action')).toEqual('my_action');
57+
});
58+
it('should set the client error', () => {
59+
callback.setClientError('error here');
60+
expect(callback.getInputValue('IDToken1clientError')).toEqual('error here');
61+
});
62+
it('should set the payload', () => {
63+
callback.setPayload({ test: 'here' });
64+
expect(callback.getInputValue('IDToken1payload')).toEqual({ test: 'here' });
65+
});
66+
it('should set the token', () => {
67+
callback.setResult('12345');
68+
expect(callback.getInputValue('IDToken1token')).toEqual('12345');
69+
});
70+
it('should get the class', () => {
71+
const className = callback.getElementClass();
72+
expect(className).toBe('g-recaptcha');
73+
});
74+
});

packages/javascript-sdk/src/fr-auth/callbacks/recaptcha-enterprise-callback.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@
1010

1111
import FRCallback from '.';
1212
import type { Callback } from '../../auth/interfaces';
13+
//"input": [
14+
// {
15+
// "name": "IDToken1token",
16+
// "value": ""
17+
// },
18+
// {
19+
// "name": "IDToken1action",
20+
// "value": ""
21+
// },
22+
// {
23+
// "name": "IDToken1clientError",
24+
// "value": ""
25+
// },
26+
// {
27+
// "name": "IDToken1payload",
28+
// "value": ""
29+
// }
1330

1431
/**
1532
* Represents a callback used to integrate reCAPTCHA.
@@ -29,12 +46,45 @@ class ReCaptchaEnterpriseCallback extends FRCallback {
2946
return this.getOutputByName<string>('recaptchaSiteKey', '');
3047
}
3148

49+
/**
50+
* Get the api url
51+
*/
52+
public getApiUrl(): string {
53+
return this.getOutputByName<string>('captchaApiUri', '');
54+
}
55+
/**
56+
* Get the class name
57+
*/
58+
public getElementClass(): string {
59+
return this.getOutputByName<string>('captchaDivClass', '');
60+
}
3261
/**
3362
* Sets the reCAPTCHA result.
3463
*/
3564
public setResult(result: string): void {
3665
this.setInputValue(result);
3766
}
67+
68+
/**
69+
* Set client client error
70+
*/
71+
public setClientError(error: string) {
72+
this.setInputValue(error, 'IDToken1clientError');
73+
}
74+
75+
/**
76+
* Set the recaptcha payload
77+
*/
78+
public setPayload(payload: unknown) {
79+
this.setInputValue(payload, 'IDToken1payload');
80+
}
81+
82+
/**
83+
* Set the recaptcha action
84+
*/
85+
public setAction(action: string) {
86+
this.setInputValue(action, 'IDToken1action');
87+
}
3888
}
3989

4090
export default ReCaptchaEnterpriseCallback;

packages/javascript-sdk/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { StepType } from './fr-auth/enums';
4242
import FRLoginFailure from './fr-auth/fr-login-failure';
4343
import PingOneProtectEvaluationCallback from './fr-auth/callbacks/ping-protect-evaluation-callback';
4444
import PingOneProtectInitializeCallback from './fr-auth/callbacks/ping-protect-initialize-callback';
45+
import ReCaptchaEnterpriseCallback from './fr-auth/callbacks/recaptcha-enterprise-callback';
4546
import FRLoginSuccess from './fr-auth/fr-login-success';
4647
import type { FRStepHandler } from './fr-auth/fr-step';
4748
import FRStep from './fr-auth/fr-step';
@@ -140,6 +141,7 @@ export {
140141
PolicyKey,
141142
PollingWaitCallback,
142143
ReCaptchaCallback,
144+
ReCaptchaEnterpriseCallback,
143145
RedirectCallback,
144146
ResponseType,
145147
SelectIdPCallback,

packages/javascript-sdk/tsconfig.spec.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
44
"outDir": "../../dist/out-tsc",
5+
"module": "ES2020",
6+
"target": "ES2020",
7+
"esModuleInterop": true,
8+
"moduleResolution": "Node16",
9+
"lib": ["DOM", "DOM.Iterable", "es2023"],
510
"types": [
611
"vitest/globals",
712
"vitest/importMeta",

0 commit comments

Comments
 (0)