Skip to content

Commit

Permalink
Merge pull request #12 from chvarkov/develop
Browse files Browse the repository at this point in the history
 Added option to use recaptcha.net as api root and agent support
  • Loading branch information
chvarkov authored Oct 30, 2020
2 parents 9c90354 + 4beebfb commit f7d8df8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,26 @@ $ npm i @nestlab/google-recaptcha
secretKey: process.env.GOOGLE_RECAPTCHA_SECRET_KEY,
response: req => req.headers.recaptcha,
skipIf: () => process.env.NODE_ENV !== 'production',
useRecaptchaNet: false,
agent: null
})
],
})
export class AppModule {
}
```

**Configuration options**

| Property | | Type | Description |
|-------------------|---|----------------------------|-------------|
| `secretKey` || string | Google recaptcha secret key |
| `response` || (request) => string | Function that returns response (recaptcha token) by request |
| `skipIf` || () => boolean | Function that returns true if you need skip check for development or testing |
| `useRecaptchaNet` || boolean | If your server has trouble connecting to https://www.google.com. You can use https://recaptcha.net instead, just set true |
| `agent` || https.Agent | If you need to use an agent |


If you want import configs from your [ConfigService](https://docs.nestjs.com/techniques/configuration#getting-started) via [custom getter function](https://docs.nestjs.com/techniques/configuration#custom-getter-functions) that will return `GoogleRecaptchaModuleOptions` object.

```typescript
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestlab/google-recaptcha",
"version": "1.1.3",
"version": "1.1.4",
"description": "Google recaptcha module for NestJS.",
"keywords": [
"nest",
Expand Down
12 changes: 12 additions & 0 deletions src/interfaces/google-recaptcha-validator-options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import * as https from "https";

export interface GoogleRecaptchaValidatorOptions {
secretKey: string;
/**
* If your server has trouble connecting to https://www.google.com,
* set it to `true` to use https://recaptcha.net instead.
*/
useRecaptchaNet?: boolean;
/**
* If your server has trouble connecting to https://www.google.com,
* you can use an agent (`proxy-agent` or other NPM modules)
*/
agent?: https.Agent;
}
9 changes: 8 additions & 1 deletion src/services/google-recaptcha.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ErrorCode } from '../enums/error-code';
@Injectable()
export class GoogleRecaptchaValidator {
private readonly apiUrl = 'https://www.google.com/recaptcha/api/siteverify';
private readonly apiUrlUseRecaptchaNet = 'https://recaptcha.net/recaptcha/api/siteverify';
private readonly headers = {'Content-Type': 'application/x-www-form-urlencoded'};

constructor(private readonly http: HttpService,
Expand All @@ -17,7 +18,13 @@ export class GoogleRecaptchaValidator {
validate(response: string): Promise<GoogleRecaptchaValidationResult> {
const data = qs.stringify({secret: this.options.secretKey, response});

return this.http.post(this.apiUrl, data, {headers: this.headers})
const url = this.options.useRecaptchaNet ? this.apiUrlUseRecaptchaNet : this.apiUrl;

return this.http.post(url, data, {
headers: this.headers,
httpsAgent: this.options.agent
}
)
.toPromise()
.then(res => res.data)
.then(result => ({
Expand Down
2 changes: 2 additions & 0 deletions test/google-recaptcha-async-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class TestConfigService {
secretKey: 'secret',
response: req => req.body.recaptcha,
skipIf: () => true,
useRecaptchaNet: false,
agent: null
};
}
}
Expand Down
17 changes: 16 additions & 1 deletion test/google-recaptcha-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { INestApplication } from '@nestjs/common';
import { GoogleRecaptchaValidator } from '../src/services/google-recaptcha.validator';
import { GoogleRecaptchaGuard } from '../src/guards/google-recaptcha.guard';
import { GoogleRecaptchaModule } from '../src/google-recaptcha.module';
import { Agent } from 'https';
import { RECAPTCHA_OPTIONS } from '../src/provider.declarations';
import { GoogleRecaptchaModuleOptions } from '../src';

describe('Google recaptcha module', () => {
let app: INestApplication;
Expand All @@ -14,7 +17,9 @@ describe('Google recaptcha module', () => {
secretKey: process.env.GOOGLE_RECAPTCHA_SECRET_KEY,
response: req => req.headers.authorization,
skipIf: () => process.env.NODE_ENV !== 'production',
})
useRecaptchaNet: true,
agent: new Agent({maxFreeSockets: 10}),
}),
],
}).compile();

Expand All @@ -32,4 +37,14 @@ describe('Google recaptcha module', () => {

expect(guard).toBeInstanceOf(GoogleRecaptchaGuard);
});

test('Test use recaptcha net options', async () => {
const options: GoogleRecaptchaModuleOptions = app.get(RECAPTCHA_OPTIONS);

expect(options).toBeDefined();
expect(options.useRecaptchaNet).toBeTruthy();
expect(options.agent).toBeDefined();
expect(options.agent).toBeInstanceOf(Agent);
expect(options.agent.maxFreeSockets).toBe(10);
});
});

0 comments on commit f7d8df8

Please sign in to comment.