-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
auth.js
227 lines (187 loc) · 5.41 KB
/
auth.js
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
const _ = require('lodash')
const debug = require('debug')('cypress:server:auth')
const express = require('express')
const os = require('os')
const pkg = require('@packages/root')
const Promise = require('bluebird')
const { shell } = require('electron')
const url = require('url')
const errors = require('../errors')
const machineId = require('../util/machine_id')
const random = require('../util/random')
const user = require('../user')
const windows = require('./windows')
let app
let authCallback
let authState
let openExternalAttempted = false
let authRedirectReached = false
let server
let utm
const _buildLoginRedirectUrl = (server) => {
const { port } = server.address()
return `http://127.0.0.1:${port}/redirect-to-auth`
}
const _buildFullLoginUrl = (baseLoginUrl, server, utmCode) => {
const { port } = server.address()
if (!authState) {
authState = random.id(32)
}
const authUrl = url.parse(baseLoginUrl)
return machineId.machineId()
.then((id) => {
authUrl.query = {
port,
state: authState,
machineId: id,
cypressVersion: pkg.version,
platform: os.platform(),
}
if (utmCode) {
authUrl.query = {
utm_source: 'Test Runner',
utm_medium: 'Login Button',
utm_campaign: 'TR-Dashboard',
utm_content: utmCode,
...authUrl.query,
}
}
return authUrl.format()
})
}
const _getOriginFromUrl = (originalUrl) => {
const parsedUrl = url.parse(originalUrl)
return url.format(_.pick(parsedUrl, ['protocol', 'slashes', 'hostname', 'port']))
}
/**
* @returns a promise that is resolved with a user when auth is complete or rejected when it fails
*/
const start = (onMessage, utmCode) => {
function sendMessage (type, name, arg1) {
onMessage({
type,
name,
message: errors.getMsgByType(name, arg1),
browserOpened: authRedirectReached,
})
}
utm = utmCode
authRedirectReached = false
return user.getBaseLoginUrl()
.then((baseLoginUrl) => {
return _launchServer(baseLoginUrl, sendMessage)
})
.then(() => {
return _buildLoginRedirectUrl(server)
})
.then((loginRedirectUrl) => {
debug('Trying to open native auth to URL ', loginRedirectUrl)
return _launchNativeAuth(loginRedirectUrl, sendMessage)
.then(() => {
debug('openExternal completed')
})
})
.then(() => {
return Promise.fromCallback((cb) => {
authCallback = cb
})
})
.finally(() => {
_stopServer()
windows.focusMainWindow()
})
}
/**
* @returns the currently running auth server instance, launches one if there is not one
*/
const _launchServer = (baseLoginUrl, sendMessage) => {
if (!server) {
// launch an express server to listen for the auth callback from dashboard
const origin = _getOriginFromUrl(baseLoginUrl)
debug('Launching auth server with origin', origin)
app = express()
app.get('/redirect-to-auth', (req, res) => {
authRedirectReached = true
_buildFullLoginUrl(baseLoginUrl, server, utm)
.then((fullLoginUrl) => {
debug('Received GET to /redirect-to-auth, redirecting: %o', { fullLoginUrl })
res.redirect(303, fullLoginUrl)
sendMessage('info', 'AUTH_BROWSER_LAUNCHED')
})
})
app.get('/auth', (req, res) => {
debug('Received GET to /auth with query params %o', req.query)
const redirectToStatus = (status) => {
res.redirect(`${baseLoginUrl}?status=${status}`)
}
/**
* Cypress Dashboard can redirect to us with an error; or, if Electron's shell.openExternal
* is bugging out, `authCallback` can be undefined and reaching this point makes no sense.
* @see https://github.com/cypress-io/cypress/pull/5243
*/
if (_.get(req.query, 'status') === 'error' || !authCallback) {
if (authCallback) {
authCallback(new Error('There was an error authenticating to the Cypress dashboard.'))
}
return redirectToStatus('error')
}
const { state, name, email, access_token } = req.query
if (state === authState && access_token) {
const userObj = {
name,
email,
authToken: access_token,
}
return user.set(userObj)
.then(() => {
authCallback(undefined, userObj)
redirectToStatus('success')
})
.catch((err) => {
authCallback(err)
redirectToStatus('error')
})
}
redirectToStatus('error')
})
return new Promise.fromCallback((cb) => {
server = app.listen(0, '127.0.0.1', cb)
})
}
return Promise.resolve()
}
const _stopServer = () => {
debug('Closing auth server')
if (server) {
server.close()
server = undefined
}
app = undefined
authState = undefined
authCallback = undefined
openExternalAttempted = false
authRedirectReached = false
}
const _launchNativeAuth = Promise.method((loginUrl, sendMessage) => {
const warnCouldNotLaunch = () => {
if (openExternalAttempted && !authRedirectReached) {
sendMessage('warning', 'AUTH_COULD_NOT_LAUNCH_BROWSER', loginUrl)
}
}
warnCouldNotLaunch()
setTimeout(warnCouldNotLaunch, 4000)
openExternalAttempted = true
return shell.openExternal(loginUrl)
.catch((err) => {
debug('Error launching native auth: %o', { err })
warnCouldNotLaunch()
})
})
module.exports = {
_buildFullLoginUrl,
_getOriginFromUrl,
_launchServer,
_launchNativeAuth,
_stopServer,
start,
}