noguard 1.2.0 → 1.3.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/lib/auth.mjs CHANGED
@@ -1,77 +1,77 @@
1
- import { post } from './api.mjs'
2
- import { setToken, clearToken, getToken, getEndpoint } from './config.mjs'
3
-
4
- export async function login() {
5
- const token = getToken()
6
- if (token) {
7
- console.log('\x1b[33m⚠\x1b[0m Already signed in. Run \x1b[1mnoguard logout\x1b[0m first to switch accounts.')
8
- return
9
- }
10
- console.log('\x1b[36m◆\x1b[0m Starting sign-in…\n')
11
- const flow = await post('/api/device/code', { client: 'cli' }, { token: null })
12
- const code = flow.user_code
13
- const url = flow.verification_uri_complete
14
-
15
- console.log(' Open this URL in your browser:\n')
16
- console.log(` \x1b[4m\x1b[36m${url}\x1b[0m\n`)
17
- console.log(` Or go to \x1b[1m${flow.verification_uri}\x1b[0m and enter: \x1b[1m${code}\x1b[0m\n`)
18
-
19
- try {
20
- const parsed = new URL(url)
21
- const base = new URL(getEndpoint())
22
- if ((parsed.protocol === 'https:' || parsed.protocol === 'http:') && parsed.hostname === base.hostname) {
23
- const { execFile } = await import('node:child_process')
24
- const platform = process.platform
25
- if (platform === 'win32') execFile('rundll32', ['url.dll,FileProtocolHandler', parsed.href])
26
- else if (platform === 'darwin') execFile('open', ['--', parsed.href])
27
- else execFile('xdg-open', [parsed.href])
28
- }
29
- } catch { /* browser open is best-effort */ }
30
-
31
- console.log(' Waiting for approval…')
32
- const interval = (flow.interval ?? 5) * 1000
33
- const deadline = Date.now() + (flow.expires_in ?? 600) * 1000
34
-
35
- while (Date.now() < deadline) {
36
- await sleep(interval)
37
- try {
38
- const result = await post('/api/device/token', { device_code: flow.device_code }, { token: null })
39
- if (result.access_token) {
40
- setToken(result.access_token, result.account)
41
- console.log(`\n\x1b[32m✓\x1b[0m Signed in as \x1b[1m${result.account.email}\x1b[0m`)
42
- console.log(` Credits: \x1b[33m${result.account.credits}\x1b[0m`)
43
- return
44
- }
45
- } catch (err) {
46
- if (err.code === 'authorization_pending' || err.code === 'slow_down') continue
47
- if (err.code === 'access_denied') {
48
- console.log('\n\x1b[31m✗\x1b[0m Sign-in was denied in the browser.')
49
- return
50
- }
51
- if (err.code === 'expired_token') {
52
- console.log('\n\x1b[31m✗\x1b[0m Sign-in code expired. Run \x1b[1mnoguard login\x1b[0m again.')
53
- return
54
- }
55
- throw err
56
- }
57
- }
58
- console.log('\n\x1b[31m✗\x1b[0m Sign-in timed out. Run \x1b[1mnoguard login\x1b[0m again.')
59
- }
60
-
61
- export async function logout() {
62
- const token = getToken()
63
- if (!token) {
64
- console.log('\x1b[33m⚠\x1b[0m Not signed in.')
65
- return
66
- }
67
- try {
68
- const { del } = await import('./api.mjs')
69
- await del('/api/v1/token')
70
- } catch { /* revoke is best-effort; the token is cleared locally either way */ }
71
- clearToken()
72
- console.log('\x1b[32m✓\x1b[0m Signed out.')
73
- }
74
-
75
- function sleep(ms) {
76
- return new Promise(resolve => setTimeout(resolve, ms))
77
- }
1
+ import { post } from './api.mjs'
2
+ import { setToken, clearToken, getToken, getEndpoint } from './config.mjs'
3
+
4
+ export async function login() {
5
+ const token = getToken()
6
+ if (token) {
7
+ console.log('\x1b[33m⚠\x1b[0m Already signed in. Run \x1b[1mnoguard logout\x1b[0m first to switch accounts.')
8
+ return
9
+ }
10
+ console.log('\x1b[36m◆\x1b[0m Starting sign-in…\n')
11
+ const flow = await post('/api/device/code', { client: 'cli' }, { token: null })
12
+ const code = flow.user_code
13
+ const url = flow.verification_uri_complete
14
+
15
+ console.log(' Open this URL in your browser:\n')
16
+ console.log(` \x1b[4m\x1b[36m${url}\x1b[0m\n`)
17
+ console.log(` Or go to \x1b[1m${flow.verification_uri}\x1b[0m and enter: \x1b[1m${code}\x1b[0m\n`)
18
+
19
+ try {
20
+ const parsed = new URL(url)
21
+ const base = new URL(getEndpoint())
22
+ if ((parsed.protocol === 'https:' || parsed.protocol === 'http:') && parsed.hostname === base.hostname) {
23
+ const { execFile } = await import('node:child_process')
24
+ const platform = process.platform
25
+ if (platform === 'win32') execFile('rundll32', ['url.dll,FileProtocolHandler', parsed.href])
26
+ else if (platform === 'darwin') execFile('open', ['--', parsed.href])
27
+ else execFile('xdg-open', [parsed.href])
28
+ }
29
+ } catch { /* browser open is best-effort */ }
30
+
31
+ console.log(' Waiting for approval…')
32
+ const interval = (flow.interval ?? 5) * 1000
33
+ const deadline = Date.now() + (flow.expires_in ?? 600) * 1000
34
+
35
+ while (Date.now() < deadline) {
36
+ await sleep(interval)
37
+ try {
38
+ const result = await post('/api/device/token', { device_code: flow.device_code }, { token: null })
39
+ if (result.access_token) {
40
+ setToken(result.access_token, result.account)
41
+ console.log(`\n\x1b[32m✓\x1b[0m Signed in as \x1b[1m${result.account.email}\x1b[0m`)
42
+ console.log(` Credits: \x1b[33m${result.account.credits}\x1b[0m`)
43
+ return
44
+ }
45
+ } catch (err) {
46
+ if (err.code === 'authorization_pending' || err.code === 'slow_down') continue
47
+ if (err.code === 'access_denied') {
48
+ console.log('\n\x1b[31m✗\x1b[0m Sign-in was denied in the browser.')
49
+ return
50
+ }
51
+ if (err.code === 'expired_token') {
52
+ console.log('\n\x1b[31m✗\x1b[0m Sign-in code expired. Run \x1b[1mnoguard login\x1b[0m again.')
53
+ return
54
+ }
55
+ throw err
56
+ }
57
+ }
58
+ console.log('\n\x1b[31m✗\x1b[0m Sign-in timed out. Run \x1b[1mnoguard login\x1b[0m again.')
59
+ }
60
+
61
+ export async function logout() {
62
+ const token = getToken()
63
+ if (!token) {
64
+ console.log('\x1b[33m⚠\x1b[0m Not signed in.')
65
+ return
66
+ }
67
+ try {
68
+ const { del } = await import('./api.mjs')
69
+ await del('/api/v1/token')
70
+ } catch { /* revoke is best-effort; the token is cleared locally either way */ }
71
+ clearToken()
72
+ console.log('\x1b[32m✓\x1b[0m Signed out.')
73
+ }
74
+
75
+ function sleep(ms) {
76
+ return new Promise(resolve => setTimeout(resolve, ms))
77
+ }