Files
magpie/internal/provider/copilot_ghe.go
T
yetone 9f94b2156b copilot: an account on GitHub Enterprise Cloud with data residency (*.ghe.com) signs in, is read from copilot login --host and is served at its enterprise (#723, guitaoliu: magpie skipped a Copilot CLI sign-in whose host wasn't github.com and its own sign-in was fixed to github.com) — a Copilot account now remembers its host (copilotApp.Host, "host" in its logins.json auth; none for github.com) and every URL it uses follows it, as VS Code's Copilot Chat, copilot.lua and avante do (checked against the issue's references and GitHub's docs): the device flow at https://<name>.ghe.com/login/device/code and /login/oauth/access_token with the same Copilot OAuth app, the device page there, whose account at https://api.<name>.ghe.com/user, its plan and usage at api.<name>.ghe.com/copilot_internal/user, its session token at api.<name>.ghe.com/copilot_internal/v2/token, and its requests, model list, Auto and terms at the session's endpoints.api, or https://copilot-api.<name>.ghe.com when it names none (the provider's base too). A github.com account has no host and goes where it always did, the same vars and the same bytes. CopilotHost (copilot_ghe.go) takes only github.com or one <name>.ghe.com label (a scheme and a trailing slash are dropped; a port, path, user, deeper subdomain or any other host is refused), in the sign-in, the CLI and a host read back from logins.json, so a GitHub token is never sent to a host of the user's choosing. The Copilot CLI's sign-in: a github.com account first, as before; then one copilot login --host <name>.ghe.com signed in, its token under "https://<name>.ghe.com:<login>" in copilotTokens or the keychain; a GitHub Enterprise Server host is still left out. An editor's apps.json entry keyed "<name>.ghe.com:…" is read after the editors' github.com one and the CLI's. GUI: a Copilot device code's panel has "Account on GHE.com?" (zh 账号在 GHE.com 上?), which ends that sign-in and asks "Your enterprise on GHE.com" (你的企业在 GHE.com 上的地址) in a field (acme.ghe.com; Sign in stays off until it reads as <name>.ghe.com), then signs in there; that sign-in's panel offers "Sign in on github.com instead" (改为在 github.com 登录), as does a failed one. CLI: magpie accounts add copilot [--host <name>.ghe.com]. TestCopilotHost, TestCopilotGHESignIn (an httptest server standing in for acme.ghe.com, api.acme.ghe.com and copilot-api.acme.ghe.com through copilotGHEOrigin: sign-in, what is kept, models, a signed request, usage, nothing sent to github.com, a bad host refused) and TestCopilotCLIGHELogin are new and fail on the old code; copilot-ghe.test.cjs (webkit and chromium, en and zh: the link, the field refusing evil.com, a.b.ghe.com, ports and the like, the posted site, the way back, cancels, no native select, no left-border accent) fails on the old code. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass; signin-link, signin-close, signin-again and routing-signin pass in webkit, signin-callback fails before and after the change alike. Not tried with a real GHE.com account: the URLs follow the references, and the CLI's token key for a ghe.com host is taken to be the github.com one's form. Accounts are still told apart by login alone; an EMU login carries its enterprise's suffix.
2026-10-04 06:56:04 +08:00

126 lines
3.8 KiB
Go

package provider
// Copilot on GitHub Enterprise Cloud with data residency (#723): an
// enterprise's accounts live at <name>.ghe.com, not github.com. They sign in
// with the same device flow and OAuth app at https://<name>.ghe.com, trade
// their token at https://api.<name>.ghe.com/copilot_internal/v2/token, are
// looked up at api.<name>.ghe.com, and are served at the API their session
// names (endpoints.api: https://copilot-api.<name>.ghe.com), as VS Code's
// Copilot Chat, copilot.lua and `copilot login --host` do. An account
// remembers its host; one on github.com has none, and every URL it uses is
// the one it always was.
import (
"errors"
"net/url"
"regexp"
"strings"
)
// gheName is an enterprise's subdomain of ghe.com: one DNS label.
var gheName = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$`)
// CopilotHost is where a Copilot account signs in, from what a user typed
// ("acme.ghe.com", "https://acme.ghe.com/", "github.com"): "" for
// github.com, "<name>.ghe.com" for an enterprise on GHE.com. Nothing else
// is taken, so a GitHub token is never sent to a host of the user's choosing.
func CopilotHost(s string) (string, error) {
h := strings.ToLower(strings.TrimSpace(s))
if strings.Contains(h, "://") {
u, err := url.Parse(h)
if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.User != nil || u.Port() != "" || (u.Path != "" && u.Path != "/") || u.RawQuery != "" || u.Fragment != "" {
return "", errors.New("not a GitHub host: " + strings.TrimSpace(s))
}
h = u.Hostname()
}
h = strings.TrimSuffix(h, "/")
if h == "" || h == "github.com" || h == "www.github.com" {
return "", nil
}
if name, ok := strings.CutSuffix(h, ".ghe.com"); ok && gheName.MatchString(name) {
return h, nil
}
return "", errors.New("Copilot signs in on github.com or an enterprise's <name>.ghe.com, not " + strings.TrimSpace(s))
}
// copilotGHEOrigin is https://[sub.]host; a var so tests can point an
// enterprise's hosts elsewhere.
var copilotGHEOrigin = func(host, sub string) string {
if sub == "" {
return "https://" + host
}
return "https://" + sub + "." + host
}
// copilotHostOf is an account's host from the Copilot CLI's
// ("https://acme.ghe.com"); ok is false for one magpie can't serve.
func copilotHostOf(cli string) (string, bool) {
if cli == "" {
return "", true
}
h, err := CopilotHost(cli)
return h, err == nil
}
// The URLs an account uses: github.com's own (vars tests point elsewhere)
// with no host, its enterprise's otherwise.
func copilotDeviceURL(host string) string {
if host == "" {
return gitHubDeviceURL
}
return copilotGHEOrigin(host, "") + "/login/device/code"
}
func copilotOAuthURL(host string) string {
if host == "" {
return gitHubTokenURL
}
return copilotGHEOrigin(host, "") + "/login/oauth/access_token"
}
func copilotDevicePage(host string) string {
if host == "" {
return "https://github.com/login/device"
}
return copilotGHEOrigin(host, "") + "/login/device"
}
func copilotTokenURL(host string) string {
if host == "" {
return CopilotTokenURL
}
return copilotGHEOrigin(host, "api") + "/copilot_internal/v2/token"
}
func copilotUserURL(host string) string {
if host == "" {
return CopilotUserURL
}
return copilotGHEOrigin(host, "api") + "/copilot_internal/user"
}
func gitHubUserURL(host string) string {
if host == "" {
return GitHubUserURL
}
return copilotGHEOrigin(host, "api") + "/user"
}
// copilotBaseOf is the Copilot API an account is served at when its
// session names none.
func copilotBaseOf(host string) string {
if host == "" {
return copilotBase
}
return copilotGHEOrigin(host, "copilot-api")
}
// apiBase is where this session's requests go.
func (s copilotSession) apiBase(host string) string {
if s.Endpoints.API != "" {
return s.Endpoints.API
}
return copilotBaseOf(host)
}