@syphin/cli 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/commands/login.js +103 -16
- package/src/lib/api.js +21 -0
package/package.json
CHANGED
package/src/commands/login.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* syphin login [--token <t>] [--force]
|
|
3
|
+
*
|
|
4
|
+
* Without --token: starts OAuth device flow (opens browser).
|
|
5
|
+
* With --token: direct token authentication.
|
|
3
6
|
*/
|
|
4
7
|
|
|
5
8
|
import ora from 'ora'
|
|
9
|
+
import open from 'open'
|
|
6
10
|
import { saveToken, loadToken } from '../lib/config.js'
|
|
7
11
|
import { createApiClient } from '../lib/api.js'
|
|
8
|
-
import { success, fail, info, teal } from '../lib/output.js'
|
|
12
|
+
import { success, fail, info, teal, dim } from '../lib/output.js'
|
|
13
|
+
|
|
14
|
+
const POLL_INTERVAL = 5000 // 5 seconds
|
|
15
|
+
const MAX_POLLS = 120 // 10 minutes / 5s
|
|
9
16
|
|
|
10
17
|
export function registerLogin(program) {
|
|
11
18
|
program
|
|
@@ -20,30 +27,110 @@ export function registerLogin(program) {
|
|
|
20
27
|
return
|
|
21
28
|
}
|
|
22
29
|
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
info('Get a token from the Syphin dashboard at https://app.syphin.dev/settings/tokens')
|
|
27
|
-
process.exit(1)
|
|
30
|
+
// Direct token mode
|
|
31
|
+
if (opts.token) {
|
|
32
|
+
return directLogin(opts.token)
|
|
28
33
|
}
|
|
29
34
|
|
|
30
|
-
|
|
35
|
+
// Device flow
|
|
36
|
+
return deviceFlowLogin()
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function directLogin(token) {
|
|
41
|
+
const spinner = ora('Verifying token...').start()
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const api = createApiClient(token)
|
|
45
|
+
const result = await api.verify()
|
|
46
|
+
|
|
47
|
+
saveToken(token)
|
|
48
|
+
spinner.succeed('Authenticated successfully!')
|
|
49
|
+
|
|
50
|
+
console.log('')
|
|
51
|
+
info(`Organization: ${teal(result.orgName)} (${result.orgSlug})`)
|
|
52
|
+
info(`Plan: ${result.orgPlan}`)
|
|
53
|
+
console.log('')
|
|
54
|
+
} catch (err) {
|
|
55
|
+
spinner.fail('Authentication failed')
|
|
56
|
+
fail(err.message)
|
|
57
|
+
process.exit(1)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function deviceFlowLogin() {
|
|
62
|
+
const api = createApiClient(null)
|
|
63
|
+
let deviceData
|
|
64
|
+
|
|
65
|
+
const spinner = ora('Starting authentication...').start()
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
deviceData = await api.requestDeviceCode()
|
|
69
|
+
spinner.stop()
|
|
70
|
+
} catch (err) {
|
|
71
|
+
spinner.fail('Failed to start authentication')
|
|
72
|
+
fail(err.message)
|
|
73
|
+
process.exit(1)
|
|
74
|
+
}
|
|
31
75
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
76
|
+
// Display the code
|
|
77
|
+
console.log('')
|
|
78
|
+
console.log(` Open this URL in your browser:`)
|
|
79
|
+
console.log('')
|
|
80
|
+
console.log(` ${teal(deviceData.verificationUrl)}`)
|
|
81
|
+
console.log('')
|
|
82
|
+
console.log(` And enter this code:`)
|
|
83
|
+
console.log('')
|
|
84
|
+
console.log(` ${teal(deviceData.userCode)}`)
|
|
85
|
+
console.log('')
|
|
86
|
+
info(dim('Code expires in 10 minutes'))
|
|
87
|
+
console.log('')
|
|
35
88
|
|
|
36
|
-
|
|
37
|
-
|
|
89
|
+
// Try to open the browser
|
|
90
|
+
try {
|
|
91
|
+
await open(deviceData.verificationUrl)
|
|
92
|
+
} catch {
|
|
93
|
+
// Browser open failed — user can manually navigate
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Poll for approval
|
|
97
|
+
const pollSpinner = ora('Waiting for browser authorization...').start()
|
|
98
|
+
|
|
99
|
+
for (let i = 0; i < MAX_POLLS; i++) {
|
|
100
|
+
await sleep(POLL_INTERVAL)
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
const result = await api.pollDeviceToken(deviceData.deviceCode)
|
|
104
|
+
|
|
105
|
+
if (result.status === 'pending') {
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (result.status === 'approved') {
|
|
110
|
+
saveToken(result.token)
|
|
111
|
+
pollSpinner.succeed('Authenticated successfully!')
|
|
38
112
|
|
|
39
113
|
console.log('')
|
|
40
114
|
info(`Organization: ${teal(result.orgName)} (${result.orgSlug})`)
|
|
41
115
|
info(`Plan: ${result.orgPlan}`)
|
|
42
116
|
console.log('')
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
} catch (err) {
|
|
120
|
+
if (err.message.includes('expired') || err.message.includes('EXPIRED')) {
|
|
121
|
+
pollSpinner.fail('Code expired')
|
|
122
|
+
fail('Run `syphin login` to try again.')
|
|
46
123
|
process.exit(1)
|
|
47
124
|
}
|
|
48
|
-
|
|
125
|
+
// Other errors — keep polling
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
pollSpinner.fail('Timed out waiting for authorization')
|
|
130
|
+
fail('Run `syphin login` to try again.')
|
|
131
|
+
process.exit(1)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function sleep(ms) {
|
|
135
|
+
return new Promise(resolve => setTimeout(resolve, ms))
|
|
49
136
|
}
|
package/src/lib/api.js
CHANGED
|
@@ -28,9 +28,30 @@ export function createApiClient(token, apiUrl) {
|
|
|
28
28
|
return json.data
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
// Device flow endpoints don't need auth
|
|
32
|
+
async function requestNoAuth(method, path, body) {
|
|
33
|
+
const url = `${baseUrl}${path}`
|
|
34
|
+
const opts = {
|
|
35
|
+
method,
|
|
36
|
+
headers: { 'Content-Type': 'application/json' },
|
|
37
|
+
}
|
|
38
|
+
if (body) opts.body = JSON.stringify(body)
|
|
39
|
+
|
|
40
|
+
const res = await fetch(url, opts)
|
|
41
|
+
const json = await res.json()
|
|
42
|
+
|
|
43
|
+
if (!res.ok && res.status !== 202) {
|
|
44
|
+
throw new Error(json.error || `API error ${res.status}`)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return json.data
|
|
48
|
+
}
|
|
49
|
+
|
|
31
50
|
return {
|
|
32
51
|
verify: () => request('POST', '/api/auth/verify'),
|
|
33
52
|
getManifest: (slug, env) => request('GET', `/api/projects/${slug}/manifest?env=${env}`),
|
|
34
53
|
createProject: (name, slug) => request('POST', '/api/projects', { name, slug }),
|
|
54
|
+
requestDeviceCode: () => requestNoAuth('POST', '/api/auth/device/code'),
|
|
55
|
+
pollDeviceToken: (deviceCode) => requestNoAuth('POST', '/api/auth/device/token', { deviceCode }),
|
|
35
56
|
}
|
|
36
57
|
}
|