-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
189 lines (143 loc) · 3.84 KB
/
setup.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package chatsh
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
)
type AuthResponse struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationUri string `json:"verification_uri"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
}
type Cache struct {
GithubToken string `json:"github_token"`
CopilotToken string `json:"copilot_token"`
ExpiresAt string `json:"expires_at"`
}
func CacheTokenNewAuth(token string) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
filePath := filepath.Join(homeDir, ".config", ".chatsh")
f, err := os.Create(filePath)
if err != nil {
return err
}
cache := Cache{
GithubToken: token,
ExpiresAt: "",
CopilotToken: "",
}
defer f.Close()
return json.NewEncoder(f).Encode(cache)
}
func VerifyGithub(deviceCode string) error {
url := "https://github.com/login/oauth/access_token"
client := http.DefaultClient
body := map[string]string{
"client_id": "Iv1.b507a08c87ecfe98",
"device_code":deviceCode,
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
}
loginHeader := map[string]string{
"accept": "application/json",
"content-type": "application/json",
"editor-version": "Neovim/0.9.2",
"editor-plugin-version": "chat.sh/0.1",
"user-agent": "GithubCopilot/1.133.0",
}
jsonBody, err := json.Marshal(body)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
for k, v := range loginHeader {
req.Header.Set(k, v)
}
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
var results map[string]string
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(data, &results)
if _, ok := results["access_token"]; !ok{
return errors.New("please do the instruction")
}
err = CacheTokenNewAuth(results["access_token"])
if err != nil {
return err
}
return nil
}
func githubAuthenticate() error {
req, err := githubRequestAuth()
if err != nil {
return err
}
fmt.Printf("please visit %v\nand enter %v code\n", req.VerificationUri, req.UserCode)
for {
err := VerifyGithub(req.DeviceCode)
if err.Error() == "please do the instruction"{
println("please do the instruction\n")
time.Sleep(5 * time.Second)
continue
}
if err == nil {
break
}
}
return nil
}
func githubRequestAuth() (AuthResponse, error){
url := "https://github.com/login/device/code"
client := &http.Client{}
loginHeader := map[string]string{
"accept": "application/json",
"content-type": "application/json",
"editor-version": "Neovim/0.9.2",
"editor-plugin-version": "chat.sh/0.1",
"user-agent": "GithubCopilot/1.133.0",
}
body := map[string]string{
"client_id": "Iv1.b507a08c87ecfe98",
"scope": "read:user",
}
jsonBody, err := json.Marshal(body)
if err != nil {
return AuthResponse{}, err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
if err != nil {
return AuthResponse{}, err
}
for k, v:= range loginHeader{
req.Header.Set(k,v)
}
resp, err := client.Do(req)
if err != nil {
return AuthResponse{}, err
}
var results AuthResponse
data, err := io.ReadAll(resp.Body)
err = json.Unmarshal(data, &results)
if err != nil {
return AuthResponse{}, err
}
return results, nil
}