-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (174 loc) · 4.25 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"syscall"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/oauth2"
)
// Provider indicates the hosting provider
type Provider string
const (
// GitHub indicates that the required provider is github
GitHub Provider = "github"
// GitLab indicates that the required provider is gitlab
GitLab Provider = "gitlab"
)
var code = make(chan string, 1)
func prettyPrintJSON(body []byte) {
var prettyJSON bytes.Buffer
error := json.Indent(&prettyJSON, body, "", "\t")
if error != nil {
log.Fatal("JSON parse error: ", error)
return
}
fmt.Println(string(prettyJSON.Bytes()))
}
func readSecretFromStdin(prompt string) string {
fmt.Println(prompt)
byteSecret, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
log.Fatal(err)
}
return string(byteSecret)
}
func openBrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
case "windows":
err = exec.Command(
"rundll32", "url.dll,FileProtocolHandler", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}
func extractCode(w http.ResponseWriter, r *http.Request) {
codes, ok := r.URL.Query()["code"]
if !ok || len(codes) < 1 {
io.WriteString(w, "Url Param 'code' missing")
} else {
c := codes[0]
io.WriteString(w, "Code: "+c)
go func() { code <- c }()
}
}
func startServer(done chan int) {
errs := make(chan error)
go func() {
c := make(chan os.Signal, 1)
defer close(c)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("SIGNAL (%s)", <-c)
}()
http.HandleFunc("/", extractCode)
go func() {
errs <- http.ListenAndServe(":8000", nil)
}()
select {
case status := <-done:
os.Exit(status)
case err := <-errs:
fmt.Printf("\nReceived: %v\n", err)
}
}
func generateToken(
endpoint oauth2.Endpoint, scopes []string, testURL string, newAppURL string,
done chan<- int, code <-chan string,
) {
ctx := context.Background()
fmt.Println("Create your OAuth application here (" + newAppURL + ")")
clientID := readSecretFromStdin("Enter your client ID please: ")
clientSecret := readSecretFromStdin("Enter your client secret please: ")
fmt.Print("Enter your OAuth scopes please (seperated by space, enter EOF " +
"or ^D to stop): ")
for {
var scope string
if _, err := fmt.Scan(&scope); err != nil {
break
}
if scope != "" {
scopes = append(scopes, scope)
}
}
conf := &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Scopes: scopes,
RedirectURL: "http://localhost:8000",
Endpoint: endpoint,
}
// Redirect user to consent page to ask for permission
// for the scopes specified above.
url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
fmt.Println("\nVisit the following URL for the auth dialog:")
fmt.Printf("%s\n", url)
fmt.Println("Awaiting authentication...")
openBrowser(url)
// Use the authorization code that is pushed to the redirect
// URL. Exchange will do the handshake to retrieve the
// initial access token. The HTTP Client returned by
// conf.Client will refresh the token as necessary.
tok, err := conf.Exchange(ctx, <-code)
if err != nil {
log.Fatal(err)
}
if byteToken, err := json.Marshal(*tok); err != nil {
log.Fatal(err)
} else {
prettyPrintJSON(byteToken)
}
client := conf.Client(ctx, tok)
resp, err := client.Get(testURL)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
prettyPrintJSON(body)
done <- 0
}
func main() {
var provider string
flag.StringVar(
&provider,
"provider",
"github",
"the provider for which oauth token is to be generated, one of "+
"('github', 'gitlab')")
flag.Parse()
fmt.Println("Use OAuth application server homepage as " +
"'http://localhost:8000/'")
done := make(chan int)
switch Provider(provider) {
case GitHub:
go doGithubOAuthDance(done, code)
case GitLab:
go doGitlabOAuthDance(done, code)
default:
fmt.Println("generate-oauth-token: Incorrect usage")
flag.Usage()
os.Exit(1)
}
startServer(done)
}