@@ -7,41 +7,6 @@ import * as storage from './storage.js';
7
7
* Code specific to communicating with the Discord API.
8
8
*/
9
9
10
- export interface OAuth2TokenResponse {
11
- access_token : string ;
12
- expires_at : number ;
13
- expires_in : number ;
14
- refresh_token : string ;
15
- scope : string ;
16
- token_type : string ;
17
- }
18
-
19
- export interface OAuth2UserInfo {
20
- application : {
21
- id : string ;
22
- name : string ;
23
- icon : string | null ;
24
- description : string ;
25
- summary : string ;
26
- type : string | null ;
27
- hook : boolean ;
28
- bot_public : boolean ;
29
- bot_require_code_grant : boolean ;
30
- verify_key : string ;
31
- flags : number ;
32
- } ;
33
- scopes : string [ ] ;
34
- expires : string ;
35
- user : {
36
- id : string ;
37
- username : string ;
38
- avatar : string ;
39
- avatar_decoration : string | null ;
40
- discriminator : string ;
41
- public_flags : number ;
42
- } ;
43
- }
44
-
45
10
/**
46
11
* The following methods all facilitate OAuth2 communication with Discord.
47
12
* See https://discord.com/developers/docs/topics/oauth2 for more details.
@@ -55,8 +20,8 @@ export function getOAuthUrl() {
55
20
const state = crypto . randomUUID ( ) ;
56
21
57
22
const url = new URL ( 'https://discord.com/api/oauth2/authorize' ) ;
58
- url . searchParams . set ( 'client_id' , process . env . DISCORD_TOKEN ! ) ;
59
- url . searchParams . set ( 'redirect_uri' , process . env . DISCORD_REDIRECT_URI ! ) ;
23
+ url . searchParams . set ( 'client_id' , process . env . DISCORD_CLIENT_ID ) ;
24
+ url . searchParams . set ( 'redirect_uri' , process . env . DISCORD_REDIRECT_URI ) ;
60
25
url . searchParams . set ( 'response_type' , 'code' ) ;
61
26
url . searchParams . set ( 'state' , state ) ;
62
27
url . searchParams . set ( 'scope' , 'role_connections.write identify' ) ;
@@ -68,16 +33,14 @@ export function getOAuthUrl() {
68
33
* Given an OAuth2 code from the scope approval page, make a request to Discord's
69
34
* OAuth2 service to retrieve an access token, refresh token, and expiration.
70
35
*/
71
- export async function getOAuthTokens (
72
- code : string
73
- ) : Promise < OAuth2TokenResponse > {
36
+ export async function getOAuthTokens ( code ) {
74
37
const url = 'https://discord.com/api/v10/oauth2/token' ;
75
38
const body = new URLSearchParams ( {
76
- client_id : process . env . DISCORD_CLIENT_ID ! ,
77
- client_secret : process . env . DISCORD_CLIENT_SECRET ! ,
39
+ client_id : process . env . DISCORD_CLIENT_ID ,
40
+ client_secret : process . env . DISCORD_CLIENT_SECRET ,
78
41
grant_type : 'authorization_code' ,
79
42
code,
80
- redirect_uri : process . env . DISCORD_REDIRECT_URI ! ,
43
+ redirect_uri : process . env . DISCORD_REDIRECT_URI ,
81
44
} ) ;
82
45
83
46
const response = await fetch ( url , {
@@ -88,31 +51,26 @@ export async function getOAuthTokens(
88
51
} ,
89
52
} ) ;
90
53
if ( response . ok ) {
91
- const data = await response . json ( ) as OAuth2TokenResponse ;
54
+ const data = await response . json ( ) ;
92
55
return data ;
93
56
} else {
94
57
throw new Error ( `Error fetching OAuth tokens: [${ response . status } ] ${ response . statusText } ` ) ;
95
58
}
96
59
}
97
60
98
-
99
61
/**
100
62
* The initial token request comes with both an access token and a refresh
101
63
* token. Check if the access token has expired, and if it has, use the
102
64
* refresh token to acquire a new, fresh access token.
103
65
*/
104
- export async function getAccessToken (
105
- userId : string ,
106
- data : storage . DiscordData
107
- ) {
108
- let tokens : OAuth2TokenResponse ; // Define tokens outside the if block
109
- if ( Date . now ( ) > data . expires_at ) {
66
+ export async function getAccessToken ( userId , tokens ) {
67
+ if ( Date . now ( ) > tokens . expires_at ) {
110
68
const url = 'https://discord.com/api/v10/oauth2/token' ;
111
69
const body = new URLSearchParams ( {
112
- client_id : process . env . DISCORD_CLIENT_ID ! ,
113
- client_secret : process . env . DISCORD_CLIENT_SECRET ! ,
70
+ client_id : process . env . DISCORD_CLIENT_ID ,
71
+ client_secret : process . env . DISCORD_CLIENT_SECRET ,
114
72
grant_type : 'refresh_token' ,
115
- refresh_token : data . refresh_token ,
73
+ refresh_token : tokens . refresh_token ,
116
74
} ) ;
117
75
const response = await fetch ( url , {
118
76
body,
@@ -122,47 +80,21 @@ export async function getAccessToken(
122
80
} ,
123
81
} ) ;
124
82
if ( response . ok ) {
125
- tokens = await response . json ( ) as OAuth2TokenResponse ;
83
+ const tokens = await response . json ( ) ;
126
84
tokens . expires_at = Date . now ( ) + tokens . expires_in * 1000 ;
127
85
await storage . storeDiscordTokens ( userId , tokens ) ;
86
+ return tokens . access_token ;
128
87
} else {
129
88
throw new Error ( `Error refreshing access token: [${ response . status } ] ${ response . statusText } ` ) ;
130
89
}
131
- } else {
132
- tokens = data as OAuth2TokenResponse ; // Assign the value of data to tokens
133
90
}
134
91
return tokens . access_token ;
135
92
}
136
93
137
- /**
138
- * Revoke the given user's Discord access and refresh tokens.
139
- * @param userId The Discord User ID
140
- */
141
- export async function revokeAccess ( userId : string ) {
142
- const tokens = await storage . getDiscordTokens ( userId ) ;
143
- const url = 'https://discord.com/api/v10/oauth2/token' ;
144
- const body = new URLSearchParams ( {
145
- client_id : process . env . DISCORD_CLIENT_ID ! ,
146
- client_secret : process . env . DISCORD_CLIENT_SECRET ! ,
147
- token : tokens . refresh_token ,
148
- token_type_hint : 'refresh_token' ,
149
- } ) ;
150
- await fetch ( url , {
151
- body,
152
- method : 'POST' ,
153
- headers : {
154
- 'Content-Type' : 'application/x-www-form-urlencoded' ,
155
- } ,
156
- } ) ;
157
-
158
- // remove the tokens from storage
159
- await storage . deleteDiscordTokens ( userId ) ;
160
- }
161
-
162
94
/**
163
95
* Given a user based access token, fetch profile information for the current user.
164
96
*/
165
- export async function getUserData ( tokens : OAuth2TokenResponse ) {
97
+ export async function getUserData ( tokens ) {
166
98
const url = 'https://discord.com/api/v10/oauth2/@me' ;
167
99
const response = await fetch ( url , {
168
100
headers : {
@@ -181,16 +113,12 @@ export async function getUserData(tokens: OAuth2TokenResponse) {
181
113
* Given metadata that matches the schema, push that data to Discord on behalf
182
114
* of the current user.
183
115
*/
184
- export async function pushMetadata (
185
- userId : string ,
186
- data : storage . DiscordData ,
187
- metadata : Record < string , string >
188
- ) {
116
+ export async function pushMetadata ( userId , tokens , metadata ) {
189
117
// PUT /users/@me /applications/:id/role-connection
190
118
const url = `https://discord.com/api/v10/users/@me/applications/${ process . env . DISCORD_CLIENT_ID } /role-connection` ;
191
- const accessToken = await getAccessToken ( userId , data ) ;
119
+ const accessToken = await getAccessToken ( userId , tokens ) ;
192
120
const body = {
193
- platform_name : 'PrismBot Linked Roles' ,
121
+ platform_name : 'Prism Linked Roles' ,
194
122
metadata,
195
123
} ;
196
124
const response = await fetch ( url , {
@@ -210,13 +138,10 @@ export async function pushMetadata(
210
138
* Fetch the metadata currently pushed to Discord for the currently logged
211
139
* in user, for this specific bot.
212
140
*/
213
- export async function getMetadata (
214
- userId : string ,
215
- data : storage . DiscordData
216
- ) {
141
+ export async function getMetadata ( userId , tokens ) {
217
142
// GET /users/@me /applications/:id/role-connection
218
143
const url = `https://discord.com/api/v10/users/@me/applications/${ process . env . DISCORD_CLIENT_ID } /role-connection` ;
219
- const accessToken = await getAccessToken ( userId , data ) ;
144
+ const accessToken = await getAccessToken ( userId , tokens ) ;
220
145
const response = await fetch ( url , {
221
146
headers : {
222
147
Authorization : `Bearer ${ accessToken } ` ,
@@ -228,4 +153,4 @@ export async function getMetadata(
228
153
} else {
229
154
throw new Error ( `Error getting discord metadata: [${ response . status } ] ${ response . statusText } ` ) ;
230
155
}
231
- }
156
+ }
0 commit comments