Skip to content

Commit a3b4573

Browse files
authored
Merge pull request #52 from fbsamples/debug-access-token
View Access Token Details
2 parents 5526a91 + 4d7b81b commit a3b4573

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"dotenv": "^16.0.1",
1515
"express": "^4.17.3",
1616
"express-session": "^1.17.2",
17+
"luxon": "^3.7.1",
1718
"multer": "^1.4.5-lts.1",
1819
"pug": "^3.0.2"
1920
},

src/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@ const path = require('path');
1414
const fs = require('fs');
1515
const { URLSearchParams, URL } = require('url');
1616
const multer = require('multer');
17+
const { DateTime } = require('luxon');
1718

1819
const app = express();
1920
const upload = multer();
2021

2122
const DEFAULT_THREADS_QUERY_LIMIT = 10;
2223

2324
const FIELD__ALT_TEXT = 'alt_text';
25+
const FIELD__APPLICATION = 'application';
26+
const FIELD__APP_SCOPED_USER_ID = 'user_id';
2427
const FIELD__CLICKS = 'clicks';
2528
const FIELD__ERROR_MESSAGE = 'error_message';
29+
const FIELD__EXPIRES_AT = 'expires_at';
2630
const FIELD__FOLLOWERS_COUNT = 'followers_count';
2731
const FIELD__HIDE_STATUS = 'hide_status';
2832
const FIELD__ID = 'id';
33+
const FIELD__ISSUED_AT = 'issued_at';
2934
const FIELD__IS_REPLY = 'is_reply';
3035
const FIELD__IS_VERIFIED = 'is_verified';
3136
const FIELD__LIKES = 'likes';
@@ -40,6 +45,7 @@ const FIELD__REPLIES = 'replies';
4045
const FIELD__REPOSTS = 'reposts';
4146
const FIELD__QUOTES = 'quotes';
4247
const FIELD__REPLY_AUDIENCE = 'reply_audience';
48+
const FIELD__SCOPES = 'scopes';
4349
const FIELD__SHARES = 'shares';
4450
const FIELD__STATUS = 'status';
4551
const FIELD__TEXT = 'text';
@@ -63,6 +69,7 @@ const PARAMS__DELETE_CONFIG = 'delete_config';
6369
const PARAMS__DELETE_QUOTA_USAGE = 'delete_quota_usage';
6470
const PARAMS__FIELDS = 'fields';
6571
const PARAMS__HIDE = 'hide';
72+
const PARAMS__INPUT_TOKEN = 'input_token';
6673
const PARAMS__LINK_ATTACHMENT = 'link_attachment';
6774
const PARAMS__LOCATION_SEARCH_CONFIG = 'location_search_config';
6875
const PARAMS__LOCATION_SEARCH_QUOTA_USAGE = 'location_search_quota_usage';
@@ -805,6 +812,37 @@ app.get('/mentions', loggedInUserChecker, async (req, res) => {
805812
});
806813
});
807814

815+
app.get('/debug', loggedInUserChecker, async (req, res) => {
816+
const params = {
817+
[PARAMS__INPUT_TOKEN]: req.session.access_token,
818+
};
819+
820+
const debugAccessTokenUrl = buildGraphAPIURL(`debug_token`, params, req.session.access_token);
821+
822+
let data = {};
823+
try {
824+
const response = await axios.get(debugAccessTokenUrl, { httpsAgent: agent });
825+
data = response.data.data;
826+
} catch (e) {
827+
console.error(e?.response?.data?.error?.message ?? e.message);
828+
}
829+
830+
const applicationName = data[FIELD__APPLICATION];
831+
const expiresAt = formatTimestamp(data[FIELD__EXPIRES_AT]);
832+
const issuedAt = formatTimestamp(data[FIELD__ISSUED_AT]);
833+
const scopes = data[FIELD__SCOPES].join(', ');
834+
const appScopedUserId = data[FIELD__APP_SCOPED_USER_ID];
835+
836+
return res.render('debug', {
837+
title: 'Inspect Access Token',
838+
applicationName,
839+
expiresAt,
840+
issuedAt,
841+
scopes,
842+
appScopedUserId,
843+
});
844+
});
845+
808846
app.get('/keywordSearch', loggedInUserChecker, async (req, res) => {
809847
const { keyword, searchType } = req.query;
810848

@@ -1083,6 +1121,15 @@ function addAttachmentFields(target, attachmentType, url, altText) {
10831121
}
10841122
}
10851123

1124+
/**
1125+
* @param {int} timestamp
1126+
*/
1127+
function formatTimestamp(timestamp) {
1128+
const userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
1129+
return DateTime.fromSeconds(timestamp, { zone: userTimeZone })
1130+
.toLocaleString(DateTime.DATETIME_FULL_WITH_SECONDS);
1131+
}
1132+
10861133
/**
10871134
* @param {URL} sourceUrl
10881135
* @param {URL} destinationUrl

views/account.pug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ block content
2323
button(onclick="location.href='/profileLookup'") Profile Lookup
2424
button(onclick="location.href='/userInsights'") My Insights
2525
button(onclick="location.href='/publishingLimit'") Publishing Limit
26+
button(onclick="location.href='/debug'") Debug Access Token

views/debug.pug

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
extends layout_with_account
2+
3+
block content
4+
table
5+
tbody
6+
tr
7+
th(colspan=2) App Name
8+
td(colspan=2)=applicationName
9+
tr
10+
th(colspan=2) Issued At
11+
td(colspan=2)=issuedAt
12+
tr
13+
th(colspan=2) Expires At
14+
td(colspan=2)=expiresAt
15+
tr
16+
th(colspan=2) Scopes
17+
td(colspan=2)=scopes
18+
tr
19+
th(colspan=2) User ID
20+
td(colspan=2)=appScopedUserId

0 commit comments

Comments
 (0)