pinokiod 8.3.2 → 8.3.3
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/kernel/bin/cuda.js +34 -16
- package/kernel/bin/git.js +17 -6
- package/kernel/connect/providers/github/README.md +1 -1
- package/kernel/git.js +4 -20
- package/kernel/gitconfig_template +1 -6
- package/kernel/github_auth.js +277 -0
- package/kernel/index.js +3 -0
- package/package.json +1 -1
- package/server/index.js +18 -190
- package/server/routes/github_auth.js +43 -0
- package/server/views/github.ejs +2 -2
- package/server/views/github_login.ejs +390 -0
- package/test/cuda-bin.test.js +239 -0
- package/test/git-bin.test.js +28 -13
- package/test/git-defaults.test.js +44 -25
- package/test/github-auth.test.js +431 -0
- package/test/github-connection.test.js +6 -129
package/kernel/bin/cuda.js
CHANGED
|
@@ -64,13 +64,15 @@ class Cuda {
|
|
|
64
64
|
await fs.promises.writeFile(script, filtered.join(eol))
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
|
-
async stashActivationScripts() {
|
|
67
|
+
async stashActivationScripts({ strict = false } = {}) {
|
|
68
68
|
if (this.kernel.platform !== "win32") {
|
|
69
69
|
return
|
|
70
70
|
}
|
|
71
71
|
const folder = this.kernel.bin.path("miniforge/etc/conda/activate.d")
|
|
72
72
|
const stash = path.resolve(folder, "pinokio")
|
|
73
|
-
await fs.promises.mkdir(stash, { recursive: true }).catch(() => {
|
|
73
|
+
await fs.promises.mkdir(stash, { recursive: true }).catch((e) => {
|
|
74
|
+
if (strict) throw e
|
|
75
|
+
})
|
|
74
76
|
const scripts = [
|
|
75
77
|
"~cuda-nvcc_activate.bat",
|
|
76
78
|
"vs2019_compiler_vars.bat",
|
|
@@ -84,9 +86,12 @@ class Cuda {
|
|
|
84
86
|
break
|
|
85
87
|
} catch (e) {
|
|
86
88
|
if (e && e.code === "EEXIST") {
|
|
87
|
-
await fs.promises.rm(src).catch(() => {
|
|
89
|
+
await fs.promises.rm(src).catch((e) => {
|
|
90
|
+
if (strict) throw e
|
|
91
|
+
})
|
|
88
92
|
break
|
|
89
93
|
}
|
|
94
|
+
if (strict && e.code !== "ENOENT") throw e
|
|
90
95
|
}
|
|
91
96
|
}
|
|
92
97
|
}
|
|
@@ -95,19 +100,32 @@ class Cuda {
|
|
|
95
100
|
async install(req, ondata) {
|
|
96
101
|
if (this.kernel.gpu === "nvidia") {
|
|
97
102
|
if (this.kernel.platform === "win32") {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
103
|
+
const prefix = this.kernel.bin.path("miniforge")
|
|
104
|
+
// Resolve from the managed working directory without shell-escaping home.
|
|
105
|
+
const conda = ".\\Scripts\\conda.exe"
|
|
106
|
+
// conda.bat reactivates after install, before we can stash newly added
|
|
107
|
+
// compiler hooks. Use the executable without activating base first.
|
|
108
|
+
for (const packages of [
|
|
109
|
+
["cudnn", "libzlib-wapi", "-c", "conda-forge"],
|
|
110
|
+
["nvidia/label/cuda-12.8.1::cuda", "conda-forge::cuda-cccl"],
|
|
111
|
+
]) {
|
|
112
|
+
try {
|
|
113
|
+
const result = await this.kernel.bin.exec({
|
|
114
|
+
path: prefix,
|
|
115
|
+
conda: { skip: true },
|
|
116
|
+
message: [
|
|
117
|
+
{ _: [conda, "clean", "-y", "--all"] },
|
|
118
|
+
{ _: [conda, "install", "-y", "--prefix", ".", ...packages] },
|
|
119
|
+
],
|
|
120
|
+
}, ondata)
|
|
121
|
+
if (result?.error?.length) {
|
|
122
|
+
throw new Error("CUDA dependency installation failed.")
|
|
123
|
+
}
|
|
124
|
+
} finally {
|
|
125
|
+
// Failed transactions can also leave activation scripts behind.
|
|
126
|
+
await this.stashActivationScripts({ strict: true })
|
|
127
|
+
}
|
|
128
|
+
}
|
|
111
129
|
} else {
|
|
112
130
|
await this.kernel.bin.exec({
|
|
113
131
|
message: [
|
package/kernel/bin/git.js
CHANGED
|
@@ -5,9 +5,9 @@ const semver = require('semver')
|
|
|
5
5
|
const { rimraf } = require('rimraf')
|
|
6
6
|
const path = require("path")
|
|
7
7
|
class Git {
|
|
8
|
-
description = "Installs Git, Git LFS,
|
|
8
|
+
description = "Installs Git, Git LFS, and GitHub CLI in the Pinokio environment."
|
|
9
9
|
cmd() {
|
|
10
|
-
const packages = '"git>=2.51.0" git-lfs
|
|
10
|
+
const packages = '"git>=2.51.0" git-lfs gh=2.82.1'
|
|
11
11
|
if (this.kernel.platform === "darwin") {
|
|
12
12
|
return packages
|
|
13
13
|
} else if (this.kernel.platform === "win32") {
|
|
@@ -21,6 +21,10 @@ class Git {
|
|
|
21
21
|
if (this.kernel.platform === "win32") {
|
|
22
22
|
await this.kernel.bin.exec({ message: "conda remove git-bash" }, ondata)
|
|
23
23
|
}
|
|
24
|
+
// Existing installs: drop Git Credential Manager (.NET). Pinokio now stores the GitHub login itself.
|
|
25
|
+
if (this.hasGcm()) {
|
|
26
|
+
await this.kernel.bin.exec({ message: "conda remove -y git-credential-manager" }, ondata)
|
|
27
|
+
}
|
|
24
28
|
await this.kernel.bin.exec({
|
|
25
29
|
message: [
|
|
26
30
|
"conda clean -y --all",
|
|
@@ -41,16 +45,23 @@ class Git {
|
|
|
41
45
|
return false
|
|
42
46
|
}
|
|
43
47
|
}
|
|
48
|
+
// A leftover Git Credential Manager means this install predates the Pinokio GitHub login: reinstall to remove it.
|
|
49
|
+
if (this.hasGcm()) {
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
44
52
|
if (this.kernel.platform === "darwin") {
|
|
45
|
-
return this.kernel.bin.installed.conda && this.kernel.bin.installed.conda.has("git") && this.kernel.bin.installed.conda.has("
|
|
53
|
+
return this.kernel.bin.installed.conda && this.kernel.bin.installed.conda.has("git") && this.kernel.bin.installed.conda.has("gh")
|
|
46
54
|
} else if (this.kernel.platform === "win32") {
|
|
47
|
-
return this.kernel.bin.installed.conda && this.kernel.bin.installed.conda.has("git") && this.kernel.bin.installed.conda.has("
|
|
55
|
+
return this.kernel.bin.installed.conda && this.kernel.bin.installed.conda.has("git") && this.kernel.bin.installed.conda.has("gh") && this.kernel.bin.installed.conda.has("m2-base")
|
|
48
56
|
} else {
|
|
49
|
-
return this.kernel.bin.installed.conda && this.kernel.bin.installed.conda.has("git") && this.kernel.bin.installed.conda.has("
|
|
57
|
+
return this.kernel.bin.installed.conda && this.kernel.bin.installed.conda.has("git") && this.kernel.bin.installed.conda.has("gh")
|
|
50
58
|
}
|
|
51
59
|
}
|
|
60
|
+
hasGcm() {
|
|
61
|
+
return Boolean(this.kernel.bin.installed.conda && this.kernel.bin.installed.conda.has("git-credential-manager"))
|
|
62
|
+
}
|
|
52
63
|
async uninstall(req, ondata) {
|
|
53
|
-
await this.kernel.bin.exec({ message: "conda remove git
|
|
64
|
+
await this.kernel.bin.exec({ message: "conda remove git gh" }, ondata)
|
|
54
65
|
}
|
|
55
66
|
env() {
|
|
56
67
|
let gitconfig_path = path.resolve(this.kernel.homedir, "gitconfig")
|
package/kernel/git.js
CHANGED
|
@@ -75,7 +75,7 @@ class Git {
|
|
|
75
75
|
}
|
|
76
76
|
return { ...env, ...NON_INTERACTIVE_GIT_ENV }
|
|
77
77
|
}
|
|
78
|
-
async getCredentialForUrl(rawUrl) {
|
|
78
|
+
async getCredentialForUrl(rawUrl, options = {}) {
|
|
79
79
|
let target
|
|
80
80
|
try {
|
|
81
81
|
target = new URL(rawUrl)
|
|
@@ -97,7 +97,7 @@ class Git {
|
|
|
97
97
|
{
|
|
98
98
|
cwd: (this.kernel && this.kernel.homedir) || process.cwd(),
|
|
99
99
|
env: this.credentialEnv(),
|
|
100
|
-
timeout: 30000,
|
|
100
|
+
timeout: Number.isFinite(options.timeout) ? options.timeout : 30000,
|
|
101
101
|
maxBuffer: 1024 * 1024,
|
|
102
102
|
windowsHide: true
|
|
103
103
|
},
|
|
@@ -205,11 +205,8 @@ class Git {
|
|
|
205
205
|
ensureDir(this.kernel.path("scripts/git"))
|
|
206
206
|
])
|
|
207
207
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (!(await this.kernel.api.exists(gitconfig_path))) {
|
|
211
|
-
await fs.promises.copyFile(gitconfigTemplate, gitconfig_path)
|
|
212
|
-
}
|
|
208
|
+
// Creates gitconfig from the template, or migrates an existing one (e.g. from Git Credential Manager).
|
|
209
|
+
await this.ensureDefaults()
|
|
213
210
|
|
|
214
211
|
const scripts = [
|
|
215
212
|
"fork",
|
|
@@ -1214,19 +1211,6 @@ class Git {
|
|
|
1214
1211
|
|
|
1215
1212
|
resetCredentialDefaults()
|
|
1216
1213
|
|
|
1217
|
-
const githubCredentialSection = config['credential "https://github'] && config['credential "https://github']['com"']
|
|
1218
|
-
if (githubCredentialSection && typeof githubCredentialSection === "object") {
|
|
1219
|
-
if (!githubCredentialSection.provider) {
|
|
1220
|
-
githubCredentialSection.provider = "github"
|
|
1221
|
-
dirty = true
|
|
1222
|
-
}
|
|
1223
|
-
const helper = typeof githubCredentialSection.helper === "string" ? githubCredentialSection.helper : ""
|
|
1224
|
-
if (!helper) {
|
|
1225
|
-
githubCredentialSection.helper = "manager"
|
|
1226
|
-
dirty = true
|
|
1227
|
-
}
|
|
1228
|
-
}
|
|
1229
|
-
|
|
1230
1214
|
if (dirty) {
|
|
1231
1215
|
await fs.promises.writeFile(gitconfigPath, ini.stringify(config))
|
|
1232
1216
|
}
|
|
@@ -9,13 +9,8 @@
|
|
|
9
9
|
smudge = git-lfs smudge -- %f
|
|
10
10
|
process = git-lfs filter-process
|
|
11
11
|
required = true
|
|
12
|
-
[credential]
|
|
13
|
-
helper = manager
|
|
14
|
-
gitHubAuthModes = oauth
|
|
15
|
-
namespace = pinokio
|
|
16
12
|
[credential "https://github.com"]
|
|
17
|
-
helper =
|
|
18
|
-
provider = github
|
|
13
|
+
helper = !gh auth git-credential
|
|
19
14
|
[user]
|
|
20
15
|
name = pinokio
|
|
21
16
|
email = pinokio@localhost
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// GitHub login for Pinokio.
|
|
2
|
+
//
|
|
3
|
+
// Uses GitHub's OAuth device flow (no client secret, no redirect URL, works from
|
|
4
|
+
// any device) and stores the token where the GitHub CLI keeps it:
|
|
5
|
+
// <home>/config/gh/hosts.yml. Git reads it through "gh auth git-credential",
|
|
6
|
+
// which the Pinokio gitconfig points at, and gh itself is signed in with it.
|
|
7
|
+
// Replaces the Git Credential Manager (.NET) dependency.
|
|
8
|
+
const fs = require("fs")
|
|
9
|
+
const path = require("path")
|
|
10
|
+
const YAML = require("yaml")
|
|
11
|
+
const fetch = require("cross-fetch")
|
|
12
|
+
|
|
13
|
+
const CLIENT_ID = "Ov23cthkE6o0xkxngT2r"
|
|
14
|
+
const SCOPE = "repo workflow"
|
|
15
|
+
const HOST = "github.com"
|
|
16
|
+
const DEVICE_CODE_URL = "https://github.com/login/device/code"
|
|
17
|
+
const ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"
|
|
18
|
+
const USER_URL = "https://api.github.com/user"
|
|
19
|
+
const DEVICE_GRANT = "urn:ietf:params:oauth:grant-type:device_code"
|
|
20
|
+
const USER_AGENT = "pinokio"
|
|
21
|
+
const REQUEST_TIMEOUT = 30000
|
|
22
|
+
|
|
23
|
+
// Changes to hosts.yml run one at a time, across every instance (the kernel makes a new one when
|
|
24
|
+
// it re-initializes), so a login finishing during a logout cannot resurrect the token.
|
|
25
|
+
const writes = { queue: Promise.resolve() }
|
|
26
|
+
|
|
27
|
+
const postForm = async (fetchImpl, url, params) => {
|
|
28
|
+
const response = await fetchImpl(url, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: {
|
|
31
|
+
"Accept": "application/json",
|
|
32
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
33
|
+
"User-Agent": USER_AGENT,
|
|
34
|
+
},
|
|
35
|
+
body: new URLSearchParams(params).toString(),
|
|
36
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT),
|
|
37
|
+
})
|
|
38
|
+
const text = await response.text()
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(text)
|
|
41
|
+
} catch (_) {
|
|
42
|
+
throw new Error(`GitHub returned an unexpected response (${response.status}).`)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
class GithubAuth {
|
|
47
|
+
constructor(kernel, options = {}) {
|
|
48
|
+
this.kernel = kernel
|
|
49
|
+
// Captured at construction (it may still be unset on a first run, before setup picks a home):
|
|
50
|
+
// the kernel changes its homedir when it re-initializes, and a login still finishing then
|
|
51
|
+
// must keep writing to the home it started in, not the new one.
|
|
52
|
+
this.home = kernel.homedir
|
|
53
|
+
this.session = null
|
|
54
|
+
this.fetch = options.fetch || fetch
|
|
55
|
+
// GitHub requires at least 5 seconds between polls; tests lower this.
|
|
56
|
+
this.minInterval = Number.isFinite(options.minInterval) ? options.minInterval : 5
|
|
57
|
+
// Bumped by cancelLogin, so a device-code request still in flight at logout is discarded.
|
|
58
|
+
this.generation = 0
|
|
59
|
+
this.starting = null
|
|
60
|
+
}
|
|
61
|
+
hostsPath() {
|
|
62
|
+
return path.resolve(this.home, "config", "gh", "hosts.yml")
|
|
63
|
+
}
|
|
64
|
+
serialize(fn) {
|
|
65
|
+
const run = writes.queue.then(fn, fn)
|
|
66
|
+
writes.queue = run.catch(() => {})
|
|
67
|
+
return run
|
|
68
|
+
}
|
|
69
|
+
// Parsed hosts.yml; {} when the file does not exist. Throws when it exists but is not valid YAML.
|
|
70
|
+
async readHosts() {
|
|
71
|
+
let text
|
|
72
|
+
try {
|
|
73
|
+
text = await fs.promises.readFile(this.hostsPath(), "utf8")
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (error.code === "ENOENT") return {}
|
|
76
|
+
throw error
|
|
77
|
+
}
|
|
78
|
+
const hosts = YAML.parse(text)
|
|
79
|
+
return hosts && typeof hosts === "object" ? hosts : {}
|
|
80
|
+
}
|
|
81
|
+
// Writes a 0600 temp file, then renames it over hosts.yml, so readers never see a partial file.
|
|
82
|
+
async writeHosts(hosts) {
|
|
83
|
+
const target = this.hostsPath()
|
|
84
|
+
const temp = `${target}.${process.pid}.${Date.now()}.tmp`
|
|
85
|
+
await fs.promises.mkdir(path.dirname(target), { recursive: true, mode: 0o700 })
|
|
86
|
+
try {
|
|
87
|
+
await fs.promises.writeFile(temp, YAML.stringify(hosts), { mode: 0o600, flag: "wx" })
|
|
88
|
+
await fs.promises.rename(temp, target)
|
|
89
|
+
} finally {
|
|
90
|
+
await fs.promises.rm(temp, { force: true }) // only still there when the rename failed
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// Adds or replaces the github.com entry and keeps every other host and account.
|
|
94
|
+
async save(token, login) {
|
|
95
|
+
const hosts = await this.readHosts()
|
|
96
|
+
const entry = hosts[HOST] && typeof hosts[HOST] === "object" ? hosts[HOST] : {}
|
|
97
|
+
const users = entry.users && typeof entry.users === "object" ? entry.users : {}
|
|
98
|
+
users[login] = { oauth_token: token }
|
|
99
|
+
hosts[HOST] = { ...entry, users, oauth_token: token, user: login, git_protocol: "https" }
|
|
100
|
+
await this.writeHosts(hosts)
|
|
101
|
+
}
|
|
102
|
+
// { token, login } for the active github.com account, or null.
|
|
103
|
+
async read() {
|
|
104
|
+
try {
|
|
105
|
+
const entry = (await this.readHosts())[HOST]
|
|
106
|
+
if (!entry || typeof entry !== "object") return null
|
|
107
|
+
const login = entry.user ? String(entry.user) : ""
|
|
108
|
+
const account = entry.users && entry.users[login]
|
|
109
|
+
const token = entry.oauth_token || (account && account.oauth_token)
|
|
110
|
+
return token ? { token: String(token), login } : null
|
|
111
|
+
} catch (_) {
|
|
112
|
+
return null
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// What the GitHub pages show. gh also honours a GH_TOKEN in the environment and the OS keyring
|
|
116
|
+
// of a gh installed outside Pinokio, so this reports what git will actually use (connected,
|
|
117
|
+
// source "pinokio" or "system") next to whether Pinokio has a login saved (saved, login).
|
|
118
|
+
async connection(options = {}) {
|
|
119
|
+
const auth = await this.read()
|
|
120
|
+
const credential = this.kernel.git
|
|
121
|
+
? await this.kernel.git.getCredentialForUrl("https://github.com", { timeout: options.timeout })
|
|
122
|
+
: null
|
|
123
|
+
const saved = Boolean(auth)
|
|
124
|
+
const login = auth ? auth.login || null : null
|
|
125
|
+
if (auth && (!credential || credential.password === auth.token)) {
|
|
126
|
+
return { connected: true, saved, login, display: `github.com: ${login || "connected"}`, source: "pinokio" }
|
|
127
|
+
}
|
|
128
|
+
if (credential) {
|
|
129
|
+
const display = auth
|
|
130
|
+
? `github.com: a GH_TOKEN in your environment overrides the Pinokio login (${login || "saved"})`
|
|
131
|
+
: "github.com: login from outside Pinokio (gh keyring or GH_TOKEN)"
|
|
132
|
+
return { connected: true, saved, login, display, source: "system" }
|
|
133
|
+
}
|
|
134
|
+
return { connected: false, saved, login, display: "", source: null }
|
|
135
|
+
}
|
|
136
|
+
serializeSession(session) {
|
|
137
|
+
if (!session) return null
|
|
138
|
+
return {
|
|
139
|
+
status: session.status,
|
|
140
|
+
user_code: session.user_code,
|
|
141
|
+
verification_uri: session.verification_uri,
|
|
142
|
+
error: session.error || null,
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
async status() {
|
|
146
|
+
return { ...(await this.connection()), session: this.serializeSession(this.session) }
|
|
147
|
+
}
|
|
148
|
+
async startLogin() {
|
|
149
|
+
if (this.session && this.session.status === "pending" && Date.now() < this.session.expires_at) {
|
|
150
|
+
return this.serializeSession(this.session)
|
|
151
|
+
}
|
|
152
|
+
if (!this.starting) {
|
|
153
|
+
// one device-code request at a time, shared by concurrent callers
|
|
154
|
+
const starting = this.requestDeviceCode().finally(() => {
|
|
155
|
+
if (this.starting === starting) this.starting = null
|
|
156
|
+
})
|
|
157
|
+
this.starting = starting
|
|
158
|
+
}
|
|
159
|
+
return this.starting
|
|
160
|
+
}
|
|
161
|
+
async requestDeviceCode() {
|
|
162
|
+
const generation = this.generation
|
|
163
|
+
const response = await postForm(this.fetch, DEVICE_CODE_URL, { client_id: CLIENT_ID, scope: SCOPE })
|
|
164
|
+
if (this.generation !== generation) {
|
|
165
|
+
throw new Error("The GitHub login was cancelled.")
|
|
166
|
+
}
|
|
167
|
+
if (!response.device_code || !response.user_code) {
|
|
168
|
+
throw new Error(response.error_description || response.error || "GitHub did not return a device code.")
|
|
169
|
+
}
|
|
170
|
+
const session = {
|
|
171
|
+
status: "pending",
|
|
172
|
+
device_code: response.device_code,
|
|
173
|
+
user_code: response.user_code,
|
|
174
|
+
verification_uri: response.verification_uri || "https://github.com/login/device",
|
|
175
|
+
interval: Math.max(Number(response.interval) || this.minInterval, this.minInterval),
|
|
176
|
+
expires_at: Date.now() + (Number(response.expires_in) || 900) * 1000,
|
|
177
|
+
timer: null,
|
|
178
|
+
}
|
|
179
|
+
this.session = session
|
|
180
|
+
this.schedulePoll(session)
|
|
181
|
+
return this.serializeSession(session)
|
|
182
|
+
}
|
|
183
|
+
schedulePoll(session) {
|
|
184
|
+
session.timer = setTimeout(() => {
|
|
185
|
+
this.poll(session).catch((error) => {
|
|
186
|
+
this.fail(session, error && error.message ? error.message : String(error))
|
|
187
|
+
})
|
|
188
|
+
}, session.interval * 1000)
|
|
189
|
+
if (session.timer && typeof session.timer.unref === "function") {
|
|
190
|
+
session.timer.unref()
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
fail(session, message) {
|
|
194
|
+
if (this.session !== session) return
|
|
195
|
+
session.status = "error"
|
|
196
|
+
session.error = message
|
|
197
|
+
}
|
|
198
|
+
async poll(session) {
|
|
199
|
+
if (this.session !== session || session.status !== "pending") return
|
|
200
|
+
if (Date.now() >= session.expires_at) {
|
|
201
|
+
this.fail(session, "The GitHub device code expired before it was approved.")
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
const response = await postForm(this.fetch, ACCESS_TOKEN_URL, {
|
|
205
|
+
client_id: CLIENT_ID,
|
|
206
|
+
device_code: session.device_code,
|
|
207
|
+
grant_type: DEVICE_GRANT,
|
|
208
|
+
})
|
|
209
|
+
if (this.session !== session) return
|
|
210
|
+
if (response.access_token) {
|
|
211
|
+
// The account name must be known before anything is saved: gh keys the token by it.
|
|
212
|
+
const login = await this.fetchLogin(response.access_token)
|
|
213
|
+
await this.serialize(async () => {
|
|
214
|
+
if (this.session !== session) return // logged out while this login was finishing
|
|
215
|
+
await this.save(response.access_token, login)
|
|
216
|
+
session.status = "connected"
|
|
217
|
+
})
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
switch (response.error) {
|
|
221
|
+
case "authorization_pending":
|
|
222
|
+
break
|
|
223
|
+
case "slow_down":
|
|
224
|
+
session.interval += 5
|
|
225
|
+
break
|
|
226
|
+
case "expired_token":
|
|
227
|
+
this.fail(session, "The GitHub device code expired before it was approved.")
|
|
228
|
+
return
|
|
229
|
+
case "access_denied":
|
|
230
|
+
this.fail(session, "The GitHub login was cancelled.")
|
|
231
|
+
return
|
|
232
|
+
default:
|
|
233
|
+
this.fail(session, response.error_description || response.error || "GitHub login failed.")
|
|
234
|
+
return
|
|
235
|
+
}
|
|
236
|
+
this.schedulePoll(session)
|
|
237
|
+
}
|
|
238
|
+
async fetchLogin(token) {
|
|
239
|
+
const response = await this.fetch(USER_URL, {
|
|
240
|
+
headers: {
|
|
241
|
+
"Accept": "application/vnd.github+json",
|
|
242
|
+
"Authorization": `Bearer ${token}`,
|
|
243
|
+
"User-Agent": USER_AGENT,
|
|
244
|
+
},
|
|
245
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT),
|
|
246
|
+
})
|
|
247
|
+
const user = await response.json().catch(() => null)
|
|
248
|
+
if (!user || !user.login) {
|
|
249
|
+
throw new Error("GitHub did not return the account name for the new token.")
|
|
250
|
+
}
|
|
251
|
+
return String(user.login)
|
|
252
|
+
}
|
|
253
|
+
cancelLogin() {
|
|
254
|
+
this.generation += 1
|
|
255
|
+
this.starting = null // a start still in flight is discarded; the next start makes a new request
|
|
256
|
+
if (this.session && this.session.timer) {
|
|
257
|
+
clearTimeout(this.session.timer)
|
|
258
|
+
}
|
|
259
|
+
this.session = null
|
|
260
|
+
}
|
|
261
|
+
// Removes the github.com entry (other hosts stay). Throws when the token could not be removed,
|
|
262
|
+
// including when hosts.yml cannot be read: it is left untouched rather than deleted.
|
|
263
|
+
async logout() {
|
|
264
|
+
this.cancelLogin()
|
|
265
|
+
await this.serialize(async () => {
|
|
266
|
+
const hosts = await this.readHosts()
|
|
267
|
+
delete hosts[HOST]
|
|
268
|
+
if (Object.keys(hosts).length === 0) {
|
|
269
|
+
await fs.promises.rm(this.hostsPath(), { force: true })
|
|
270
|
+
} else {
|
|
271
|
+
await this.writeHosts(hosts)
|
|
272
|
+
}
|
|
273
|
+
})
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
module.exports = GithubAuth
|
package/kernel/index.js
CHANGED
|
@@ -38,6 +38,7 @@ const Procs = require('./procs')
|
|
|
38
38
|
const Anywhere = require('./anywhere')
|
|
39
39
|
const Peer = require('./peer')
|
|
40
40
|
const Git = require('./git')
|
|
41
|
+
const GithubAuth = require('./github_auth')
|
|
41
42
|
const Vault = require('./vault')
|
|
42
43
|
const Connect = require('./connect')
|
|
43
44
|
const Favicon = require('./favicon')
|
|
@@ -1131,6 +1132,8 @@ class Kernel {
|
|
|
1131
1132
|
this.peer = new Peer(this)
|
|
1132
1133
|
await this.peer.initialize(this)
|
|
1133
1134
|
this.git = new Git(this)
|
|
1135
|
+
if (this.githubAuth) this.githubAuth.cancelLogin() // re-init: a login still polling must not write later
|
|
1136
|
+
this.githubAuth = new GithubAuth(this)
|
|
1134
1137
|
|
|
1135
1138
|
// if (home) {
|
|
1136
1139
|
// this.homedir = home
|