-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
144 lines (117 loc) · 2.85 KB
/
auth.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package githubcopilotapi
import (
"encoding/json"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
"github.com/google/uuid"
)
type Token struct {
Token string `json:"token"`
ExpiresAt int64 `json:"expires_at"`
}
func (c *Copilot) withAuth() error {
if c.githubOAuthToken == "" {
return errors.New("no GitHub token found")
}
if c.copilotToken.Token == "" || (c.copilotToken.ExpiresAt <= time.Now().Unix()) {
sessionID := uuid.NewString() + strconv.Itoa(int(time.Now().UnixNano()/1000))
url := "https://api.github.com/copilot_internal/v2/token"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "token "+c.githubOAuthToken)
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", c.userAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New("Failed to authenticate: " + resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var token Token
err = json.Unmarshal(body, &token)
if err != nil {
return err
}
c.copilotToken = token
c.sessionID = sessionID
}
return nil
}
type UserData struct {
OAuthToken string `json:"oauth_token"`
}
func getOAuthTokenInLocal() string {
// loading token from the environment only in GitHub Codespaces
token := os.Getenv("GITHUB_TOKEN")
codespaces := os.Getenv("CODESPACES")
if token != "" && codespaces != "" {
return token
}
// loading token from the file
configPath := findConfigPath()
// token can be sometimes in apps.json sometimes in hosts.json
filePaths := []string{
filepath.Join(configPath, "github-copilot", "hosts.json"),
filepath.Join(configPath, "github-copilot", "apps.json"),
}
for _, filePath := range filePaths {
if _, err := os.Stat(filePath); err == nil {
file, err := os.ReadFile(filePath)
if err != nil {
continue
}
var userData map[string]UserData
err = json.Unmarshal(file, &userData)
if err != nil {
continue
}
for _, value := range userData {
if value.OAuthToken != "" {
return value.OAuthToken
}
}
}
}
return ""
}
func findConfigPath() string {
// Check XDG_CONFIG_HOME first
config := os.Getenv("XDG_CONFIG_HOME")
if config != "" && isDirectory(config) {
return config
}
// On Windows, check AppData/Local
if os.Getenv("OS") == "Windows_NT" {
config = filepath.Join(os.Getenv("APPDATA"), "Local")
if isDirectory(config) {
return config
}
}
// Fallback to ~/.config
config = filepath.Join(os.Getenv("HOME"), ".config")
if isDirectory(config) {
return config
}
// If all else fails, return an empty string
return ""
}
func isDirectory(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
}