/
authenticate.go
303 lines (258 loc) · 7.64 KB
/
authenticate.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package mansion
import (
"bufio"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/itchio/butler/art"
"github.com/itchio/butler/comm"
"github.com/itchio/go-itchio"
"github.com/pkg/errors"
)
// read+write for owner, no permissions for others
const keyFileMode = 0600
const (
authHTML = `
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet" type="text/css">
<style>
body {
text-align: center;
margin: 50px 0;
}
p {
line-height: 1.6;
font-size: 18px;
font-family: Lato, sans-serif;
}
a, a:active, a:visited, a:hover {
color: #FA5B5B;
}
/* A a pastel rainbow palette */
@keyframes rainbow {
from { color: #FFB3BA; }
25% { color: #FFDFBA; }
50% { color: #FFFFBA; }
75% { color: #BAFFC9; }
to { color: #BAE1FF; }
}
pre {
animation: rainbow alternate 5s infinite linear;
background: #1C1C1D;
padding: 2em 0;
line-height: 1.3;
font-size: 16px;
color: #FFB3BA;
text-shadow: 0 0 20px;
color: white;
}
</style>
</head>
<body>
<pre id="art"></pre>
<p id="message">
Authenticating...
</p>
<script>
'use strict'
var key = location.hash.replace(/^#/, '')
location.hash = 'ok'
var xhr = new XMLHttpRequest()
var $message = document.querySelector("#message")
var $art = document.querySelector("#art")
xhr.onload = function () {
$art.innerHTML = xhr.responseText
$message.innerHTML = "You're successfully authenticated! You can close this page."
}
xhr.onerror = function () {
$message.innerHTML = "Copy the following code back in your terminal: " + key
}
xhr.open("POST", "/oauth/callback/" + key)
xhr.send()
</script>
</body>
</html>`
)
var callbackRe = regexp.MustCompile(`^\/oauth\/callback\/(.*)$`)
const environmentApiKeyVariable = "BUTLER_API_KEY"
func (ctx *Context) HasSavedCredentials() bool {
// environment has priority
if os.Getenv(environmentApiKeyVariable) != "" {
return true
}
// then file at usual or specified path
var identity = ctx.Identity
_, err := os.Lstat(identity)
exists := !os.IsNotExist(err)
return exists
}
func readKeyFile(path string) (string, error) {
stats, err := os.Lstat(path)
if err != nil && os.IsNotExist(err) {
// no key file
return "", nil
}
if stats.Mode()&077 > 0 {
if runtime.GOOS == "windows" {
// windows won't let you 0600, because it's ACL-based
// we can make it 0644, and go will report 0666, but
// it doesn't matter since other users can't access it anyway.
// empirical evidence: https://github.com/itchio/butler/issues/65
} else {
comm.Logf("[Warning] Key file had wrong permissions (%#o), resetting to %#o\n", stats.Mode()&0777, keyFileMode)
err = os.Chmod(path, keyFileMode)
if err != nil {
comm.Logf("[Warning] Couldn't chmod keyfile: %s\n", err.Error())
}
}
}
buf, err := ioutil.ReadFile(path)
if err != nil && !os.IsNotExist(err) {
return "", errors.WithStack(err)
}
return strings.TrimSpace(string(buf)), nil
}
func writeKeyFile(path string, key string) error {
return ioutil.WriteFile(path, []byte(key), os.FileMode(keyFileMode))
}
func (ctx *Context) AuthenticateViaOauth() (*itchio.Client, error) {
var err error
var identity = ctx.Identity
var key string
envKey := os.Getenv(environmentApiKeyVariable)
if envKey != "" {
return ctx.NewClient(envKey), nil
}
if os.Getenv("CI") != "" {
comm.Logf(" ~~~ ")
comm.Logf("It looks like you're running butler on a CI server.")
comm.Logf("It's strongly recommended to pass credentials via the %s environment variable.", environmentApiKeyVariable)
comm.Logf("See https://itch.io/docs/butler/login.html for more info.")
comm.Logf(" ~~~ ")
}
key, err = readKeyFile(identity)
if err != nil {
return nil, errors.Wrap(err, "reading key file")
}
if key == "" {
if !IsTerminal() {
comm.Logf("Please set %s to your API key, see https://itch.io/docs/butler/login.html for more info.", environmentApiKeyVariable)
comm.Dief("No credentials and stdin is not a terminal - terminating.")
}
done := make(chan string)
errs := make(chan error)
handler := func(w http.ResponseWriter, r *http.Request) {
matches := callbackRe.FindStringSubmatch(r.RequestURI)
if matches != nil {
client := ctx.NewClient(matches[1])
client.WharfStatus()
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, art.ItchLogo)
done <- matches[1]
return
}
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "%s", authHTML)
}
http.HandleFunc("/", handler)
// if we're running `butler login` remotely, we're asking the user to copy-paste
var addr = "127.0.0.1:226"
var doManualOauth = os.Getenv("BUTLER_MANUAL_OAUTH") == "1"
if !doManualOauth {
var listener net.Listener
listener, err = net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, errors.Wrap(err, "listening on local address for oauth process")
}
addr = listener.Addr().String()
go func() {
err = http.Serve(listener, nil)
if err != nil {
errs <- errors.Wrap(err, "serving local http server for oauth process")
}
}()
}
form := url.Values{}
form.Add("client_id", "butler")
form.Add("scope", "wharf")
form.Add("response_type", "token")
form.Add("redirect_uri", fmt.Sprintf("http://%s/oauth/callback", addr))
query := form.Encode()
uri := fmt.Sprintf("%s/user/oauth?%s", ctx.WebAddress(), query)
comm.Login(uri)
go func() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
line := strings.TrimSpace(s.Text())
u, err := url.Parse(line)
if err != nil {
// not a valid url
continue
}
if u.Fragment != "" {
// user pasted the url!
done <- u.Fragment
return
}
}
}()
select {
case err = <-errs:
return nil, errors.WithStack(err)
case key = <-done:
err = nil
client := ctx.NewClient(key)
_, err = client.WharfStatus()
if err != nil {
return nil, errors.Wrap(err, "retrieving wharf status")
}
comm.Logf("\nAuthenticated successfully! Saving key in %s...\n", identity)
err = os.MkdirAll(filepath.Dir(identity), os.FileMode(0755))
if err != nil {
comm.Logf("\nCould not create directory for storing API key: %s\n\n", err)
err = nil
} else {
err = writeKeyFile(identity, key)
if err != nil {
comm.Logf("\nCould not save API key: %s\n\n", err)
err = nil
}
}
}
}
if err != nil {
err = errors.WithStack(err)
}
return ctx.NewClient(key), nil
}
func stripApiSubdomain(address string) (string, error) {
u, err := url.Parse(address)
if err != nil {
return "", err
}
u.Host = strings.TrimPrefix(u.Host, "api.")
return u.String(), nil
}
var (
// Simple regular expression for IPv4 values, see https://github.com/goadesign/goa/blob/master/validation.go
ipv4Regex = regexp.MustCompile(`^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`)
)
func addApiSubdomain(address string) (string, error) {
u, err := url.Parse(address)
if err != nil {
return "", err
}
if !strings.HasPrefix(u.Host, "api.") && !ipv4Regex.MatchString(u.Hostname()) {
u.Host = "api." + u.Host
}
return u.String(), nil
}