-
Notifications
You must be signed in to change notification settings - Fork 0
/
bottalk-login.go
182 lines (146 loc) · 4.02 KB
/
bottalk-login.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
package main
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"github.com/urfave/cli"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
"time"
)
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)
}
}
func bAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func getTokenByCode(code string) string {
var username = "4"
var passwd = "bottalk-cli"
client := &http.Client{}
body := url.Values{
"grant_type": {"authorization_code"},
"code": {code},
"redirect_uri": {"http://localhost:2876/auth"},
}
reqBody := bytes.NewBufferString(body.Encode())
req, err := http.NewRequest("POST", "https://auth.bottalk.de/token", reqBody)
req.Header.Add("Authorization", "Basic "+bAuth(username, passwd))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
s := string(bodyText)
homedir := userHomeDir()
d1 := []byte(s)
err = ioutil.WriteFile(homedir+"/.bottalk-cli.token", d1, 0644)
if err != nil {
log.Println(err)
} else {
log.Println("Successfully wrote credentials.")
}
return s
}
func getTokenRefresh() string {
var username = "4"
var passwd = "bottalk-cli"
client := &http.Client{}
body := url.Values{
"grant_type": {"refresh_token"},
"refresh_token": {token.Refresh},
"scope": {token.Scope},
}
reqBody := bytes.NewBufferString(body.Encode())
req, err := http.NewRequest("POST", "https://auth.bottalk.de/token", reqBody)
req.Header.Add("Authorization", "Basic "+bAuth(username, passwd))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
s := string(bodyText)
homedir := userHomeDir()
d1 := []byte(s)
err = ioutil.WriteFile(homedir+"/.bottalk-cli.token", d1, 0644)
if err != nil {
log.Println(err)
} else {
log.Println("Successfully wrote credentials.")
}
loadToken()
return s
}
func getLoginCommands() []cli.Command {
s := &http.Server{
Addr: ":2876",
}
return []cli.Command{{
Name: "login",
Usage: "Login into your bottalk account",
Action: func(c *cli.Context) error {
log.Println("Please log in into your account in window that was opened in your browser")
openbrowser("https://auth.bottalk.de/authorize?client_id=4&response_type=code&scope=all&redirect_uri=http%3A%2F%2Flocalhost%3A2876%2Fauth")
log.Println("If it didn't open, please follow this link: https://auth.bottalk.de/authorize?client_id=4&response_type=code&scope=all&redirect_uri=http%3A%2F%2Flocalhost%3A2876%2Fauth")
http.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
code := r.FormValue("code")
fmt.Fprint(w, " <script>setTimeout(function(){window.close();},5000);</script> This window will close in 5 seconds. You can return to your console.")
go getTokenByCode(code)
go func() {
time.Sleep(1 * time.Second)
s.Shutdown(context.TODO())
}()
})
err := s.ListenAndServe()
if err != http.ErrServerClosed {
log.Fatal(err)
}
return nil
},
}, {
Name: "info",
Usage: "Information about your bottalk account",
Action: func(c *cli.Context) error {
log.Println("Fetching info about your account")
getInfo()
return nil
},
}}
}
func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
} else if runtime.GOOS == "linux" {
home := os.Getenv("XDG_CONFIG_HOME")
if home != "" {
return home
}
}
return os.Getenv("HOME")
}