Created
June 30, 2022 02:15
-
-
Save shrmpy/377ba462e76d29305f0d32af94cb5a82 to your computer and use it in GitHub Desktop.
itch CLI command butler login is the normal way to obtain the wharf scope key, but there is no arm; culled from mansion/authenticate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"net/http" | |
"net/url" | |
"regexp" | |
) | |
func main() { | |
var err = AuthenticateViaOauth() | |
if err != nil { | |
fmt.Printf("DEBUG bad outcome, %v\n", err.Error()) | |
} | |
fmt.Println("INFO done") | |
} | |
const ( | |
authHTML = ` | |
<!DOCTYPE html> | |
<html> | |
<head> | |
</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\/(.*)$`) | |
func AuthenticateViaOauth() error { | |
var err error | |
var done = make(chan string) | |
defer close(done) | |
var handler = func(w http.ResponseWriter, r *http.Request) { | |
matches := callbackRe.FindStringSubmatch(r.RequestURI) | |
if matches != nil { | |
w.Header().Set("Content-Type", "text/plain") | |
fmt.Fprintf(w, "todo request /wharf/status") | |
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 listener net.Listener | |
if listener, err = net.Listen("tcp", "127.0.0.1:0"); err != nil { | |
panic(err) | |
} | |
addr = listener.Addr().String() | |
go func() { | |
// spin up local web srv to expect the redirect | |
if err := http.Serve(listener, nil); err != nil { | |
panic(err) | |
} | |
}() | |
var params = url.Values{} | |
params.Add("client_id", "butler") | |
params.Add("scope", "wharf") | |
params.Add("response_type", "token") | |
params.Add("redirect_uri", fmt.Sprintf("http://%s/oauth/callback", addr)) | |
var query = params.Encode() | |
var uri = fmt.Sprintf("https://itch.io/user/oauth?%s", query) | |
fmt.Printf("DEBUG to oauth use web browser to visit, %s \n", uri) | |
select { | |
case key := <-done: | |
// todo request /wharf/status to exercise the API (token) | |
fmt.Printf("DEBUG token rcvd, %s \n", key) | |
return nil | |
} | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment