-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
auth.ts
304 lines (264 loc) · 8.2 KB
/
auth.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import jwtDecode from 'jwt-decode'
import { shell } from 'electron'
import * as url from 'url'
// import envVariables from '../env-variables';
import {
BrowserWindow,
IpcMainInvokeEvent,
ipcMain,
webContents,
} from 'electron'
import { API_ROOT, HOMEPAGE_ROOT } from '../utils'
import crypto from 'crypto'
import fetch from 'node-fetch'
import Store from 'electron-store'
const store = new Store()
const win: BrowserWindow | null = null
const auth0Domain = 'cursor.us.auth0.com'
const clientId = 'KbZUR41cY7W6zRSdpSUJ7I7mLYBKOCmB'
// Test domain/client
// const auth0Domain = 'dev-27d5cph2nbetfllb.us.auth0.com'
// const clientId = 'OzaBXLClY5CAGxNzUhQ2vlknpi07tGuE'
let accessToken: string | null = null
let profile: any | null = null
const openAISecretKey: string | null = null
let refreshToken: string | null = null
let stripeProfile: string | null = null
const verifier: string | null = null
const STRIPE_SUCCESS_URL = 'electron-fiddle://success/'
const STRIPE_FAILURE_URL = 'electron-fiddle://failure/'
const AUTH0_CALLBACK_URL = `${API_ROOT}/auth/auth0_callback`
const redirectUri = AUTH0_CALLBACK_URL
const DUMMY_URL = `${API_ROOT}/dummy/*`
const API_AUDIENCE = `https://${auth0Domain}/api/v2/`
// These are routes that exist on our homepage
const loginUrl = `${HOMEPAGE_ROOT}/loginDeep`
const signUpUrl = `${HOMEPAGE_ROOT}/loginDeep`
const settingsUrl = `${HOMEPAGE_ROOT}/settings`
const payUrl = `${HOMEPAGE_ROOT}/pricing?fromCursor=true`
const supportUrl = `${API_ROOT}/auth/support`
// These are api routes
const logoutUrl = `${API_ROOT}/api/auth/logout`
const storeWrapper = {
get: async (key: string) => {
return store.get('AUTH_STORE_' + key)
},
set: async (key: string, value: any) => {
return store.set('AUTH_STORE_' + key, value)
},
has: async (key: string) => {
return store.has('AUTH_STORE_' + key)
},
delete: async (key: string) => {
return store.delete('AUTH_STORE_' + key)
},
clear: async () => {
// Iterate through the keys of store that should be deleted and remove
Object.keys(store.store).forEach((key) => {
if (key.startsWith('AUTH_STORE_')) {
store.delete(key)
}
})
},
}
function base64URLEncode(str: Buffer) {
return str
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '')
}
function sha256(buffer: Buffer) {
return crypto.createHash('sha256').update(buffer).digest()
}
export async function stripeUrlRequest(window: BrowserWindow) {
const response = await fetch(`${API_ROOT}/auth/create-checkout-session`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
profile,
}),
})
const newUrl = (await response.json()) as string
window.loadURL(newUrl)
}
export async function refreshTokens(event?: IpcMainInvokeEvent) {
const refreshToken = await storeWrapper.get('refreshToken')
if (refreshToken) {
const refreshOptions = {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
grant_type: 'refresh_token',
client_id: clientId,
refresh_token: refreshToken,
// audience: API_AUDIENCE,
// state: 'thisisatest',
}),
}
const response = await fetch(
`https://${auth0Domain}/oauth/token`,
refreshOptions
)
const data = (await response.json()) as {
access_token: string
id_token: string
}
accessToken = data.access_token
const idToken = data.id_token
profile = jwtDecode(idToken)
} else {
// No refresh token
//throw new Error('No available refresh token.')
}
if (event) {
event.sender.send('updateAuthStatus', { accessToken, profile })
}
}
export async function setupTokens(
callbackURL: string
// window: BrowserWindow
) {
const urlParts = url.parse(callbackURL, true)
const query = urlParts.query
const host = urlParts.host
//
if (host?.toLowerCase() === 'changetokens') {
accessToken = query.accessToken as string
refreshToken = query.refreshToken as string
await storeWrapper.set('refreshToken', refreshToken)
}
// Get the profile id from this
await refreshTokens()
await loadStripeProfile()
webContents.getAllWebContents().forEach((wc) => {
wc.send('updateAuthStatus', { accessToken, profile, stripeProfile })
wc.send('closeErrors')
})
}
export async function loadStripeProfile() {
if (!accessToken) {
return
}
const response = await fetch(`${API_ROOT}/auth/stripe_profile`, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
const resp = await response.json()
if (resp) {
stripeProfile = resp as string
}
}
export async function logout(window: BrowserWindow) {
await storeWrapper.clear()
accessToken = null
profile = null
refreshToken = null
stripeProfile = null
window.webContents.send('updateAuthStatus', {
accessToken,
profile,
stripeProfile,
})
}
export async function logoutEvent(event: IpcMainInvokeEvent) {
await storeWrapper.clear()
accessToken = null
profile = null
refreshToken = null
stripeProfile = null
event.sender.send('updateAuthStatus', {
accessToken,
profile,
stripeProfile,
})
}
export function getLogOutUrl() {
return `https://${auth0Domain}/v2/logout`
}
export function addRandomQueryParam(url: string): string {
const randomParam = `random=${Math.random().toString(36).substring(7)}`
const parsedUrl = new URL(url)
parsedUrl.searchParams.append(randomParam, '')
return parsedUrl.toString()
}
export async function login() {
// const { url, state, } = getAuthenticationURL()
await shell.openExternal(addRandomQueryParam(loginUrl))
}
export async function signup() {
await shell.openExternal(addRandomQueryParam(signUpUrl))
}
export async function pay() {
await shell.openExternal(payUrl)
}
export async function settings() {
await shell.openExternal(settingsUrl)
}
export async function support() {
await shell.openExternal(supportUrl)
}
export function createLogoutWindow(event: IpcMainInvokeEvent) {
const logoutWindow = new BrowserWindow({
show: false,
})
logoutWindow.loadURL(getLogOutUrl())
logoutWindow.on('ready-to-show', async () => {
await logoutEvent(event)
logoutWindow.close()
})
}
export function authPackage() {
// Simple browser opening functions
ipcMain.handle('loginCursor', login)
ipcMain.handle('signupCursor', signup)
ipcMain.handle('payCursor', pay)
ipcMain.handle('settingsCursor', settings)
ipcMain.handle('logoutCursor', createLogoutWindow)
// Functions to handle electron-fiddle
ipcMain.handle(
'loginData',
async (
event: IpcMainInvokeEvent,
data: {
accessToken: string
profile: any
stripeProfile: string
}
) => {
// Set the global values
accessToken = data.accessToken
profile = data.profile
stripeProfile = data.stripeProfile
await refreshTokens(event)
await loadStripeProfile()
event.sender.send('updateAuthStatus', {
accessToken,
profile,
stripeProfile,
})
}
)
ipcMain.handle('refreshTokens', async (event: IpcMainInvokeEvent) => {
await refreshTokens(event)
await loadStripeProfile()
event.sender.send('updateAuthStatus', {
accessToken,
profile,
stripeProfile,
})
})
ipcMain.handle('getUserCreds', async (event: IpcMainInvokeEvent) => {
await refreshTokens(event)
await loadStripeProfile()
return {
accessToken,
profile,
stripeProfile,
}
})
}