-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
auth.js
210 lines (172 loc) · 4.94 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
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
const _buildLoginRedirectUrl = (server) => {
const { port } = server.address()
return `http://127.0.0.1:${port}/redirect-to-auth`
}
const _buildFullLoginUrl = (baseLoginUrl, server) => {
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(),
}
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) => {
function sendMessage (type, name, arg1) {
onMessage({
type,
name,
message: errors.getMsgByType(name, arg1),
browserOpened: authRedirectReached,
})
}
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)
.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}`)
}
if (_.get(req.query, 'status') === 'error') {
authCallback(new Error('There was an error authenticating to the Cypress dashboard.'))
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
// wrap openExternal here in case `electron.shell` is not available (during tests)
return Promise.fromCallback((cb) => {
shell.openExternal(loginUrl, {}, cb)
})
.catch((err) => {
debug('Error launching native auth: %o', { err })
warnCouldNotLaunch()
})
})
module.exports = {
_buildFullLoginUrl,
_getOriginFromUrl,
_launchServer,
_launchNativeAuth,
_stopServer,
start,
}