-
Notifications
You must be signed in to change notification settings - Fork 2
/
ghauth.go
183 lines (145 loc) · 3.68 KB
/
ghauth.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
package ghops
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os/exec"
"runtime"
"sync"
"time"
"github.com/atotto/clipboard"
)
const SCOPES = "repo, admin:public_key, admin:gpg_key"
type DeviceFlowFirstPostResponse 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 DeviceFlowAccessPost struct {
ClientId string `json:"client_id"`
DeviceCode string `json:"device_code"`
GrantType string `json:"grant_type"`
}
type DeviceFlowFirstPost struct {
ClientId string `json:"client_id"`
Scope string `json:"scope"`
}
type DeviceFlowAccessResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
}
func AuthToGithub() string {
basicValues := DeviceFlowFirstPost{
ClientId: "bdbcd76255fe7e0ded14",
Scope: SCOPES,
}
data, err := json.Marshal(basicValues)
if err != nil {
log.Fatal(err)
}
res, err := postToGithub("https://github.com/login/device/code", data)
if err != nil {
log.Fatal(err)
}
var b DeviceFlowFirstPostResponse
err = json.Unmarshal(res, &b)
if err != nil {
log.Fatalln(err)
}
access := DeviceFlowAccessPost{
ClientId: basicValues.ClientId,
DeviceCode: b.DeviceCode,
GrantType: "urn:ietf:params:oauth:grant-type:device_code",
}
adata, err := json.Marshal(access)
if err != nil {
log.Fatal(err)
}
fmt.Printf("A browser should automatically open.\n If it doesn't, "+
"please open the following URL in your browser to complete the "+
"authentication process:\n%s\n", b.VerificationUri)
fmt.Printf("I've copied the following code to your clipboard.\nPlease paste it in the browser: \n\t%s\n", b.UserCode)
clipboard.WriteAll(b.UserCode)
openbrowser(b.VerificationUri)
ticker := time.NewTicker((time.Duration(b.Interval) + 1) * time.Second)
timeout := time.After(time.Duration(b.ExpiresIn) * time.Second)
quit := make(chan struct{})
var wg sync.WaitGroup
wg.Add(1)
var authenticated DeviceFlowAccessResponse
fmt.Print("Waiting...")
go func() {
// client := &http.Client{}
for {
select {
case <-ticker.C:
res, err := postToGithub("https://github.com/login/oauth/access_token", adata)
if err != nil {
log.Fatalln(err)
}
var tmp DeviceFlowAccessResponse
err = json.Unmarshal(res, &tmp)
if err != nil {
log.Fatalln(err)
}
if tmp.AccessToken != "" {
authenticated = tmp
wg.Done()
return
} else {
fmt.Print(".")
}
case <-timeout:
log.Fatal("Failed to sign in within 15 minutes. Restart the script to try again.")
ticker.Stop()
return
case <-quit:
ticker.Stop()
return
}
}
}()
wg.Wait()
fmt.Println()
return authenticated.AccessToken
}
func postToGithub(url string, data []byte) ([]byte, error) {
client := &http.Client{}
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(data))
req.Header = http.Header{
"Accept": []string{"application/json"},
"Content-Type": []string{"application/json"},
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
d, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
return d, nil
}
func openbrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}