Skip to content

Commit 4d7b81b

Browse files
committed
View Access Token Details
1 parent 7ca8a07 commit 4d7b81b

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';
@@ -803,6 +810,37 @@ app.get('/mentions', loggedInUserChecker, async (req, res) => {
803810
});
804811
});
805812

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

@@ -988,6 +1026,15 @@ function addAttachmentFields(target, attachmentType, url, altText) {
9881026
}
9891027
}
9901028

1029+
/**
1030+
* @param {int} timestamp
1031+
*/
1032+
function formatTimestamp(timestamp) {
1033+
const userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
1034+
return DateTime.fromSeconds(timestamp, { zone: userTimeZone })
1035+
.toLocaleString(DateTime.DATETIME_FULL_WITH_SECONDS);
1036+
}
1037+
9911038
/**
9921039
* @param {URL} sourceUrl
9931040
* @param {URL} destinationUrl

views/account.pug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ block content
2222
button(onclick="location.href='/keywordSearch'") Search for Threads
2323
button(onclick="location.href='/userInsights'") My Insights
2424
button(onclick="location.href='/publishingLimit'") Publishing Limit
25+
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)