@smartsoft001/fb 2.188.0 → 2.190.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 +32 -4
- package/package.json +1 -1
- package/src/lib/fb.service.d.ts +14 -2
- package/src/lib/fb.service.js +47 -5
- package/src/lib/fb.service.js.map +1 -1
package/README.md
CHANGED
|
@@ -14,11 +14,39 @@ Methods:
|
|
|
14
14
|
|
|
15
15
|
<table>
|
|
16
16
|
<tr>
|
|
17
|
-
<td>getUserId</td>
|
|
18
|
-
<td>
|
|
17
|
+
<td>getUserId(token, appIds, credentials)</td>
|
|
18
|
+
<td>Verifies the token with Graph's debug_token endpoint and returns its user_id</td>
|
|
19
19
|
</tr>
|
|
20
20
|
<tr>
|
|
21
|
-
<td>getData</td>
|
|
22
|
-
<td>
|
|
21
|
+
<td>getData(token, appIds, credentials)</td>
|
|
22
|
+
<td>Verifies the token the same way, then reads the email and id from /me</td>
|
|
23
23
|
</tr>
|
|
24
24
|
</table>
|
|
25
|
+
|
|
26
|
+
### Access-token app binding
|
|
27
|
+
|
|
28
|
+
A Facebook access token granted to any app identifies its user, so both methods first ask
|
|
29
|
+
`GET https://graph.facebook.com/debug_token?input_token=<token>&access_token=<appId>|<appSecret>`
|
|
30
|
+
whether the token was issued to your application. Both extra parameters are required and come
|
|
31
|
+
from server configuration, never from the login request:
|
|
32
|
+
|
|
33
|
+
- `appIds`: the Facebook app ids whose tokens you accept.
|
|
34
|
+
- `credentials`: `{ appId, appSecret }` (`IFbAppCredentials`) of the app that builds the app access
|
|
35
|
+
token for `debug_token`. Keep the secret on the server.
|
|
36
|
+
|
|
37
|
+
A token is accepted only when the answer has `data.is_valid === true`, a `data.app_id` in `appIds`
|
|
38
|
+
and a non-empty `data.user_id`. `getData` additionally requires the `/me` id to equal that
|
|
39
|
+
`user_id`. An empty `appIds`, a missing app id or secret, or an empty token or one longer than 8192
|
|
40
|
+
characters fails closed before any request is made.
|
|
41
|
+
|
|
42
|
+
Every failure is an `UnauthorizedException`. The token and the app access token are URL-encoded, and
|
|
43
|
+
HTTP errors are replaced with `Invalid Facebook token`, so request URLs that carry the token and the
|
|
44
|
+
app secret never reach logs.
|
|
45
|
+
|
|
46
|
+
**Migrating from 2.188.0 or earlier:** calls without `appIds` and `credentials` no longer compile.
|
|
47
|
+
Pass your Facebook app id(s) and the app id and secret before rolling out. With the auth stack, set
|
|
48
|
+
`TokenConfig.fbAppIds` and `TokenConfig.fbAppCredentials`; the `fb` grant fails with
|
|
49
|
+
`Facebook app IDs and credentials must be configured` until both are set. `getUserId` now makes a
|
|
50
|
+
single `debug_token` request instead of calling `/me`, so test doubles of the HTTP call must answer
|
|
51
|
+
with `{ data: { is_valid, app_id, user_id } }`, and a spec that expected the raw HTTP error must
|
|
52
|
+
expect `Invalid Facebook token`.
|
package/package.json
CHANGED
package/src/lib/fb.service.d.ts
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
import { HttpService } from '@nestjs/axios';
|
|
2
|
+
/** The Facebook app that asks Graph to inspect tokens, as `<appId>|<appSecret>`. */
|
|
3
|
+
export interface IFbAppCredentials {
|
|
4
|
+
appId: string;
|
|
5
|
+
appSecret: string;
|
|
6
|
+
}
|
|
2
7
|
export declare class FbService {
|
|
3
8
|
private http;
|
|
4
9
|
constructor(http: HttpService);
|
|
5
|
-
|
|
6
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Returns the (app-scoped) user id of a token issued to one of `appIds`.
|
|
12
|
+
* `appIds` and `credentials` come from server configuration, never from a request.
|
|
13
|
+
*/
|
|
14
|
+
getUserId(token: string, appIds: readonly string[], credentials: IFbAppCredentials): Promise<string>;
|
|
15
|
+
getData(token: string, appIds: readonly string[], credentials: IFbAppCredentials): Promise<{
|
|
7
16
|
id: string;
|
|
8
17
|
email: string;
|
|
9
18
|
}>;
|
|
19
|
+
/** Asks `debug_token` whether the token is valid and which app it was issued to. */
|
|
20
|
+
private verify;
|
|
21
|
+
private request;
|
|
10
22
|
}
|
package/src/lib/fb.service.js
CHANGED
|
@@ -5,19 +5,61 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
const axios_1 = require("@nestjs/axios");
|
|
6
6
|
const common_1 = require("@nestjs/common");
|
|
7
7
|
const rxjs_1 = require("rxjs");
|
|
8
|
+
const GRAPH = 'https://graph.facebook.com';
|
|
9
|
+
const INVALID_TOKEN = 'Invalid Facebook token';
|
|
8
10
|
let FbService = class FbService {
|
|
9
11
|
http;
|
|
10
12
|
constructor(http) {
|
|
11
13
|
this.http = http;
|
|
12
14
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Returns the (app-scoped) user id of a token issued to one of `appIds`.
|
|
17
|
+
* `appIds` and `credentials` come from server configuration, never from a request.
|
|
18
|
+
*/
|
|
19
|
+
async getUserId(token, appIds, credentials) {
|
|
20
|
+
return this.verify(token, appIds, credentials);
|
|
16
21
|
}
|
|
17
|
-
async getData(token) {
|
|
18
|
-
const
|
|
22
|
+
async getData(token, appIds, credentials) {
|
|
23
|
+
const userId = await this.verify(token, appIds, credentials);
|
|
24
|
+
const data = await this.request(`${GRAPH}/me?fields=email,id&access_token=${encodeURIComponent(token)}`);
|
|
25
|
+
if (!data || data.id !== userId) {
|
|
26
|
+
throw new common_1.UnauthorizedException(INVALID_TOKEN);
|
|
27
|
+
}
|
|
19
28
|
return data;
|
|
20
29
|
}
|
|
30
|
+
/** Asks `debug_token` whether the token is valid and which app it was issued to. */
|
|
31
|
+
async verify(token, appIds, credentials) {
|
|
32
|
+
if (!token ||
|
|
33
|
+
typeof token !== 'string' ||
|
|
34
|
+
token.length > 8192 ||
|
|
35
|
+
!appIds?.length ||
|
|
36
|
+
!credentials?.appId ||
|
|
37
|
+
!credentials?.appSecret) {
|
|
38
|
+
throw new common_1.UnauthorizedException('Invalid Facebook token or app configuration');
|
|
39
|
+
}
|
|
40
|
+
const appAccessToken = `${credentials.appId}|${credentials.appSecret}`;
|
|
41
|
+
const body = await this.request(`${GRAPH}/debug_token?input_token=${encodeURIComponent(token)}` +
|
|
42
|
+
`&access_token=${encodeURIComponent(appAccessToken)}`);
|
|
43
|
+
const data = body?.data;
|
|
44
|
+
if (!data ||
|
|
45
|
+
data.is_valid !== true ||
|
|
46
|
+
!appIds.includes(data.app_id) ||
|
|
47
|
+
typeof data.user_id !== 'string' ||
|
|
48
|
+
!data.user_id) {
|
|
49
|
+
throw new common_1.UnauthorizedException(INVALID_TOKEN);
|
|
50
|
+
}
|
|
51
|
+
return data.user_id;
|
|
52
|
+
}
|
|
53
|
+
async request(url) {
|
|
54
|
+
try {
|
|
55
|
+
const { data } = await (0, rxjs_1.firstValueFrom)(this.http.get(url));
|
|
56
|
+
return data;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
// The HTTP error carries the request URL: the user token and the app secret.
|
|
60
|
+
throw new common_1.UnauthorizedException(INVALID_TOKEN);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
21
63
|
};
|
|
22
64
|
exports.FbService = FbService;
|
|
23
65
|
exports.FbService = FbService = tslib_1.__decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fb.service.js","sourceRoot":"","sources":["../../../../../../packages/shared/fb/src/lib/fb.service.ts"],"names":[],"mappings":";;;;AAAA,yCAA4C;AAC5C,
|
|
1
|
+
{"version":3,"file":"fb.service.js","sourceRoot":"","sources":["../../../../../../packages/shared/fb/src/lib/fb.service.ts"],"names":[],"mappings":";;;;AAAA,yCAA4C;AAC5C,2CAAmE;AACnE,+BAAsC;AAQtC,MAAM,KAAK,GAAG,4BAA4B,CAAC;AAC3C,MAAM,aAAa,GAAG,wBAAwB,CAAC;AAGxC,IAAM,SAAS,GAAf,MAAM,SAAS;IACA;IAApB,YAAoB,IAAiB;QAAjB,SAAI,GAAJ,IAAI,CAAa;IAAG,CAAC;IAEzC;;;OAGG;IACH,KAAK,CAAC,SAAS,CACb,KAAa,EACb,MAAyB,EAC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