-
Notifications
You must be signed in to change notification settings - Fork 55
/
DiscordProvider.ts
224 lines (199 loc) · 6.16 KB
/
DiscordProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import {
AuthMethod,
BaseProviderOptions,
OAuthProviderOptions,
} from '@lit-protocol/types';
import { AuthMethodType } from '@lit-protocol/constants';
import { BaseProvider } from './BaseProvider';
import {
prepareLoginUrl,
parseLoginParams,
getStateParam,
decode,
LIT_LOGIN_GATEWAY,
} from '../utils';
import { ethers } from 'ethers';
export default class DiscordProvider extends BaseProvider {
/**
* The redirect URI that Lit's login server should send the user back to
*/
public redirectUri: string;
/**
* OAuth client ID. Defaults to one used by Lit
*/
private clientId?: string;
constructor(options: BaseProviderOptions & OAuthProviderOptions) {
super(options);
this.redirectUri = options.redirectUri || window.location.origin;
this.clientId = options.clientId || '1052874239658692668';
}
/**
* Redirect user to the Lit's Discord login page
*
* @returns {Promise<void>} - Redirects user to Lit login page
*/
public async signIn(): Promise<void> {
// Get login url
const loginUrl = await prepareLoginUrl('discord', this.redirectUri);
// Redirect to login url
window.location.assign(loginUrl);
}
/**
* Validate the URL parameters returned from Lit's login server and return the authentication data
*
* @returns {Promise<AuthMethod>} - Auth method object that contains OAuth token
*/
public async authenticate(): Promise<AuthMethod> {
// Check if current url matches redirect uri
if (!window.location.href.startsWith(this.redirectUri)) {
throw new Error(
`Current url "${window.location.href}" does not match provided redirect uri "${this.redirectUri}"`
);
}
// Check url for params
const { provider, accessToken, state, error } = parseLoginParams(
window.location.search
);
// Check if there's an error
if (error) {
throw new Error(error);
}
// Check if provider is Discord
if (!provider || provider !== 'discord') {
throw new Error(
`OAuth provider "${provider}" passed in redirect callback URL does not match "discord"`
);
}
// Check if state param matches
if (!state || decode(decodeURIComponent(state)) !== getStateParam()) {
throw new Error(
`Invalid state parameter "${state}" passed in redirect callback URL`
);
}
// Clear params from url
window.history.replaceState(
null,
window.document.title,
window.location.pathname
);
// Check if access token is present in url
if (!accessToken) {
throw new Error(
`Missing access token in redirect callback URL for Discord OAuth"`
);
}
const authMethod = {
authMethodType: AuthMethodType.Discord,
accessToken: accessToken,
};
return authMethod;
}
/**
* Sign in using popup window
*
* @param baseURL
*/
public async signInUsingPopup(baseURL: string): Promise<AuthMethod> {
const width = 500;
const height = 600;
const left = window.screen.width / 2 - width / 2;
const top = window.screen.height / 2 - height / 2;
const url = await prepareLoginUrl('discord', this.redirectUri, baseURL);
const popup = window.open(
`${url}&caller=${window.location.origin}`,
'popup',
`toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no, width=${width}, height=${height}, top=${top}, left=${left}`
);
if (!popup) {
throw new Error('Failed to open popup window');
}
return new Promise((resolve, reject) => {
// window does not have a closed event, so we need to poll using a timer
const interval = setInterval(() => {
if (popup.closed) {
clearInterval(interval);
reject(new Error('User closed popup window'));
}
}, 1000);
window.addEventListener('message', (event) => {
if (event.origin !== (baseURL || LIT_LOGIN_GATEWAY)) {
return;
}
const { provider, token, error } = event.data;
if (error) {
clearInterval(interval);
reject(new Error(error));
}
if (provider === 'discord' && token) {
clearInterval(interval);
popup.close();
resolve({
authMethodType: AuthMethodType.Discord,
accessToken: token,
});
}
});
});
}
/**
* Get auth method id that can be used to look up and interact with
* PKPs associated with the given auth method
*
* @param {AuthMethod} authMethod - Auth method object
*
* @returns {Promise<string>} - Auth method id
*/
public async getAuthMethodId(authMethod: AuthMethod): Promise<string> {
const userId = await this._fetchDiscordUser(authMethod.accessToken);
const authMethodId = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(`${userId}:${this.clientId}`)
);
return authMethodId;
}
public static async authMethodId(
authMethod: AuthMethod,
clientId?: string
): Promise<string> {
const _clientId = clientId || '1052874239658692668';
// -- get user id from access token
let userId;
const meResponse = await fetch('https://discord.com/api/users/@me', {
method: 'GET',
headers: {
authorization: `Bearer ${authMethod.accessToken}`,
},
});
if (meResponse.ok) {
const user = await meResponse.json();
userId = user.id;
} else {
throw new Error('Unable to verify Discord account');
}
// -- get auth method id
const authMethodId = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(`${userId}:${_clientId}`)
);
return authMethodId;
}
/**
* Fetch Discord user ID
*
* @param {string} accessToken - Discord access token
*
* @returns {Promise<string>} - Discord user ID
*/
private async _fetchDiscordUser(accessToken: string): Promise<string> {
const meResponse = await fetch('https://discord.com/api/users/@me', {
method: 'GET',
headers: {
authorization: `Bearer ${accessToken}`,
},
});
if (meResponse.ok) {
const user = await meResponse.json();
return user.id;
} else {
throw new Error('Unable to verify Discord account');
}
}
}