@smartsoft001/google 2.183.0 → 2.185.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,11 +14,28 @@ Methods:
14
14
 
15
15
  <table>
16
16
  <tr>
17
- <td>getUserId</td>
18
- <td>Makes a GET request to Google's OAuth tokeninfo endpoint to validate the token and get user information</td>
17
+ <td>getUserId(token, clientIds)</td>
18
+ <td>Makes a GET request to Google's OAuth tokeninfo endpoint to validate the token and returns its user_id</td>
19
19
  </tr>
20
20
  <tr>
21
- <td>getData</td>
21
+ <td>getData(token, clientIds)</td>
22
22
  <td>Makes the same GET request to the tokeninfo endpoint, but processes the response data to return an object containing id and email</td>
23
23
  </tr>
24
24
  </table>
25
+
26
+ ### Access-token client binding
27
+
28
+ `clientIds` is required: the OAuth client ids your application trusts, loaded from server
29
+ configuration. Never use a list supplied by the login request. A token is accepted only when the
30
+ tokeninfo answer has a non-empty `user_id`, `audience` and `issued_to` both in `clientIds`, and a
31
+ positive finite `expires_in`. An empty list fails closed before any request is made.
32
+
33
+ Every failure is an `UnauthorizedException`. The token is URL-encoded, and HTTP errors are replaced
34
+ with `Invalid Google token`, so the token-bearing request URL never reaches logs. Validity still
35
+ depends on Google's tokeninfo response: this is not an ID-token (JWT) verifier and does not change
36
+ the login protocol. The returned fields are documented by
37
+ [Google](https://developers.google.com/resources/api-libraries/documentation/oauth2/v2/java/latest/com/google/api/services/oauth2/model/Tokeninfo.html).
38
+
39
+ **Migrating from 2.182.0 or earlier:** calls without `clientIds` no longer compile. Pass your
40
+ actual OAuth client ids before rolling out. With the auth stack, set `TokenConfig.googleClientIds`;
41
+ the `google` grant fails with `Google client IDs must be configured` until it is set.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smartsoft001/google",
3
- "version": "2.183.0",
3
+ "version": "2.185.0",
4
4
  "type": "commonjs",
5
5
  "main": "./src/index.js",
6
6
  "typings": "./src/index.d.ts",
@@ -2,9 +2,10 @@ import { HttpService } from '@nestjs/axios';
2
2
  export declare class GoogleService {
3
3
  private http;
4
4
  constructor(http: HttpService);
5
- getUserId(token: string): Promise<string>;
6
- getData(token: string): Promise<{
5
+ getUserId(token: string, clientIds: readonly string[]): Promise<string>;
6
+ getData(token: string, clientIds: readonly string[]): Promise<{
7
7
  email: string;
8
8
  id: string;
9
9
  }>;
10
+ private verify;
10
11
  }
@@ -10,17 +10,44 @@ let GoogleService = class GoogleService {
10
10
  constructor(http) {
11
11
  this.http = http;
12
12
  }
13
- async getUserId(token) {
14
- const { data } = await (0, rxjs_1.firstValueFrom)(this.http.get('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token));
13
+ async getUserId(token, clientIds) {
14
+ const data = await this.verify(token, clientIds);
15
15
  return data.user_id;
16
16
  }
17
- async getData(token) {
18
- const { data } = await (0, rxjs_1.firstValueFrom)(this.http.get('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token));
17
+ async getData(token, clientIds) {
18
+ const data = await this.verify(token, clientIds);
19
19
  return {
20
20
  id: data.user_id,
21
21
  email: data.email,
22
22
  };
23
23
  }
24
+ async verify(token, clientIds) {
25
+ if (!token ||
26
+ typeof token !== 'string' ||
27
+ token.length > 8192 ||
28
+ !clientIds.length) {
29
+ throw new common_1.UnauthorizedException('Invalid Google token or client configuration');
30
+ }
31
+ try {
32
+ const { data } = await (0, rxjs_1.firstValueFrom)(this.http.get('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' +
33
+ encodeURIComponent(token)));
34
+ if (!data ||
35
+ typeof data.user_id !== 'string' ||
36
+ !data.user_id ||
37
+ !clientIds.includes(data.audience) ||
38
+ !clientIds.includes(data.issued_to) ||
39
+ typeof data.expires_in !== 'number' ||
40
+ !Number.isFinite(data.expires_in) ||
41
+ data.expires_in <= 0) {
42
+ throw new common_1.UnauthorizedException('Invalid Google token');
43
+ }
44
+ return data;
45
+ }
46
+ catch {
47
+ // The HTTP error may contain the access token in its request URL.
48
+ throw new common_1.UnauthorizedException('Invalid Google token');
49
+ }
50
+ }
24
51
  };
25
52
  exports.GoogleService = GoogleService;
26
53
  exports.GoogleService = GoogleService = tslib_1.__decorate([
@@ -1 +1 @@
1
- {"version":3,"file":"google.service.js","sourceRoot":"","sources":["../../../../../../packages/shared/google/src/lib/google.service.ts"],"names":[],"mappings":";;;;AAAA,yCAA4C;AAC5C,2CAA4C;AAC5C,+BAAsC;AAG/B,IAAM,aAAa,GAAnB,MAAM,aAAa;IACJ;IAApB,YAAoB,IAAiB;QAAjB,SAAI,GAAJ,IAAI,CAAa;IAAG,CAAC;IAEzC,KAAK,CAAC,SAAS,CAAC,KAAa;QAC3B,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAA,qBAAc,EACnC,IAAI,CAAC,IAAI,CAAC,GAAG,CACX,8DAA8D,GAAG,KAAK,CACvE,CACF,CAAC;QAEF,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,KAAa;QACzB,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAA,qBAAc,EACnC,IAAI,CAAC,IAAI,CAAC,GAAG,CACX,8DAA8D,GAAG,KAAK,CACvE,CACF,CAAC;QAEF,OAAO;YACL,EAAE,EAAE,IAAI,CAAC,OAAO;YAChB,KAAK,EAAE,IAAI,CAAC,KAAK;SAClB,CAAC;IACJ,CAAC;CACF,CAAA;AAzBY,sCAAa;wBAAb,aAAa;IADzB,IAAA,mBAAU,GAAE;6CAEe,mBAAW;GAD1B,aAAa,CAyBzB"}
1
+ {"version":3,"file":"google.service.js","sourceRoot":"","sources":["../../../../../../packages/shared/google/src/lib/google.service.ts"],"names":[],"mappings":";;;;AAAA,yCAA4C;AAC5C,2CAAmE;AACnE,+BAAsC;AAG/B,IAAM,aAAa,GAAnB,MAAM,aAAa;IACJ;IAApB,YAAoB,IAAiB;QAAjB,SAAI,GAAJ,IAAI,CAAa;IAAG,CA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