Repository navigation
Expand file tree
/
Copy pathgithub.go
More file actions
85 lines (77 loc) · 3.14 KB
/
Copy pathgithub.go
File metadata and controls
85 lines (77 loc) · 3.14 KB
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
package config
import "strings"
// KeyGitHubToken and KeyGitHubVia are the profile fields the factory's GitHub
// connection is kept in, beside [KeyAPIKey] and written the same way, so a
// token typed on the floor is one value in one owner-readable file.
//
// KeyGitHubToken is a token the person handed over. KeyGitHubVia is `gh` when
// the person said yes to using gh's own login instead: CODEAF READS GH'S TOKEN
// ONLY AFTER THAT YES, because a program that helps itself to another
// program's credentials is a program nobody can audit.
const (
KeyGitHubToken = "github_token"
KeyGitHubVia = "github_via"
)
// GitHubTokenAt is the token kept in the profile, or "".
func GitHubTokenAt(profileDir string) string {
if key, ok := appKey("github"); ok {
return key
}
value, _ := persistedString(profileDir, KeyGitHubToken)
return strings.TrimSpace(value)
}
// GitHubViaAt is `gh` when the person consented to gh's own login, or "".
func GitHubViaAt(profileDir string) string {
value, _ := persistedString(profileDir, KeyGitHubVia)
return strings.TrimSpace(value)
}
// WriteGitHubToken keeps a token in the profile and forgets any yes to gh,
// in one write, so the two can never both be the answer. IN APP MODE THE APP
// HOLDS EVERY KEY AND NONE REACHES THE PROFILE: the write is refused with
// [ErrAppKeys]. THE TOKEN NEVER
// REACHES A LOG; a caller says "a token", never the bytes.
func WriteGitHubToken(profileDir, token string) error {
if AppMode() {
return ErrAppKeys
}
return writeProfileValues(profileDir, map[string]any{
KeyGitHubToken: strings.TrimSpace(token),
KeyGitHubVia: removeProfileKey,
})
}
// WriteGitHubViaGH keeps the person's yes to gh's own login and forgets any
// kept token, in one write.
//
// IN APP MODE IT KEEPS THE YES ALONE. The yes is consent, not a key: gh keeps
// its own token, and the yes is what lets the app's factory read GitHub again
// after a relaunch. A token the person pasted is the app's to keep, in its
// keychain, so no token row is touched here.
func WriteGitHubViaGH(profileDir string) error {
if AppMode() {
return writeProfileValues(profileDir, map[string]any{KeyGitHubVia: "gh"})
}
return writeProfileValues(profileDir, map[string]any{
KeyGitHubVia: "gh",
KeyGitHubToken: removeProfileKey,
})
}
// ForgetGitHubViaGH takes back the yes to gh's own login, for the app's
// factory when the person connects a pasted token instead: the token the app
// keeps is then the one answer, and a keychain that loses it must not quietly
// fall back to gh.
func ForgetGitHubViaGH(profileDir string) error {
return writeProfileValues(profileDir, map[string]any{KeyGitHubVia: removeProfileKey})
}
// ForgetGitHub takes the factory's GitHub connection off the profile: the
// kept token and the yes to gh, in one write. It is the terminal's disconnect;
// in app mode the token is the app's to forget ([AppCredentials.Forget]) and
// only the yes is written here ([ForgetGitHubViaGH]).
func ForgetGitHub(profileDir string) error {
if AppMode() {
return ForgetGitHubViaGH(profileDir)
}
return writeProfileValues(profileDir, map[string]any{
KeyGitHubToken: removeProfileKey,
KeyGitHubVia: removeProfileKey,
})
}