Adds saving the token to a file for reuse. Adds logging in via a webview
and reading the auth token out of the browser view to finish the auth dance.
This commit is contained in:
parent
f502f51e59
commit
e3e6c2d0c4
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.token
|
||||||
129
main.go
129
main.go
@ -8,52 +8,46 @@ import (
|
|||||||
// "fmt"
|
// "fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
// "net/url"
|
|
||||||
"time"
|
"time"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
//"github.com/skratchdot/open-golang/open"
|
|
||||||
//"github.com/mxschmitt/playwright-go"
|
|
||||||
"github.com/webview/webview"
|
"github.com/webview/webview"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
conf *oauth2.Config
|
conf *oauth2.Config
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
w webview.WebView
|
w webview.WebView
|
||||||
|
client *http.Client
|
||||||
)
|
)
|
||||||
|
|
||||||
// func callbackHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// queryParts, _ := url.ParseQuery(r.URL.RawQuery)
|
|
||||||
|
|
||||||
// // Use the authorization code that is pushed to the redirect
|
func saveToken(tok *oauth2.Token, filename string) {
|
||||||
// // URL.
|
j, err := json.MarshalIndent(tok, "", "\t")
|
||||||
// code := queryParts["code"][0]
|
if err != nil {
|
||||||
// log.Printf("code: %s\n", code)
|
log.Fatalf("Failed to save token: %v", err)
|
||||||
|
}
|
||||||
|
ioutil.WriteFile(filename, j, 0777)
|
||||||
|
}
|
||||||
|
|
||||||
// // Exchange will do the handshake to retrieve the initial access token.
|
func loadToken(filename string) *oauth2.Token {
|
||||||
// tok, err := conf.Exchange(ctx, code)
|
data, err := ioutil.ReadFile(filename)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Fatal(err)
|
return nil
|
||||||
// }
|
}
|
||||||
// log.Printf("Token: %s", tok)
|
|
||||||
// // The HTTP Client returned by conf.Client will refresh the token as necessary.
|
|
||||||
// client := conf.Client(ctx, tok)
|
|
||||||
|
|
||||||
// resp, err := client.Get("https://embed.gog.com/user/data/games")
|
var v = new(oauth2.Token)
|
||||||
// if err != nil {
|
err = json.Unmarshal(data, &v)
|
||||||
// log.Fatal(err)
|
|
||||||
// } else {
|
|
||||||
// log.Println(color.CyanString("Authentication successful"))
|
|
||||||
// }
|
|
||||||
// defer resp.Body.Close()
|
|
||||||
|
|
||||||
// // show succes page
|
if err != nil {
|
||||||
// msg := "<p><strong>Success!</strong></p>"
|
log.Fatalf("Failed to unmarshal data: %v", err)
|
||||||
// msg = msg + "<p>You are authenticated and can now return to the CLI.</p>"
|
}
|
||||||
// fmt.Fprintf(w, msg)
|
|
||||||
// }
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func giveCode(code string) {
|
func giveCode(code string) {
|
||||||
log.Printf("Code: %s", code)
|
log.Printf("Code: %s", code)
|
||||||
@ -63,8 +57,9 @@ func giveCode(code string) {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
log.Printf("Token: %s", tok)
|
log.Printf("Token: %s", tok)
|
||||||
|
saveToken(tok, "./.token")
|
||||||
// The HTTP Client returned by conf.Client will refresh the token as necessary.
|
// The HTTP Client returned by conf.Client will refresh the token as necessary.
|
||||||
client := conf.Client(ctx, tok)
|
client = conf.Client(ctx, tok)
|
||||||
|
|
||||||
_, err = client.Get("https://embed.gog.com/user/data/games")
|
_, err = client.Get("https://embed.gog.com/user/data/games")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -84,7 +79,7 @@ func giveCode(code string) {
|
|||||||
`)
|
`)
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
w.Destroy()
|
w.Terminate()
|
||||||
}
|
}
|
||||||
|
|
||||||
func openBrowser(url string) {
|
func openBrowser(url string) {
|
||||||
@ -105,33 +100,41 @@ func openBrowser(url string) {
|
|||||||
w.Run()
|
w.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
// func plOpenBrowser(url string) {
|
type GameList struct {
|
||||||
// pw, err := playwright.Run()
|
owned []int64 `json:"owned"`
|
||||||
// if err != nil {
|
}
|
||||||
// log.Fatalf("Could not start playwright: %v", err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// browser, err := pw.Firefox.Launch()
|
func getGameList() *GameList {
|
||||||
// if err != nil {
|
res, err := client.Get("https://embed.gog.com/user/data/games")
|
||||||
// log.Fatalf("Could not launch browser: %v", err)
|
if err != nil {
|
||||||
// }
|
log.Fatalf("Failed to get game list: %v", err)
|
||||||
|
}
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to parse game body: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// page, err := browser.NewPage()
|
log.Println(string(body))
|
||||||
// if err != nil {
|
var gl = new(GameList)
|
||||||
// log.Fatalf("Could not create page: %v", err)
|
err = json.Unmarshal(body, &gl)
|
||||||
// }
|
if err != nil {
|
||||||
// _, err = page.Goto(url)
|
log.Fatalf("Failed to parse game list response: %v", err)
|
||||||
// if err != nil {
|
}
|
||||||
// log.Fatalf("Could not go to %s: %v", url, err)
|
|
||||||
// }
|
return gl
|
||||||
// log.Println("Opened the url")
|
}
|
||||||
// }
|
|
||||||
|
func login(url string) {
|
||||||
|
log.Println(color.CyanString("You will now be taken to your browser for authentication"))
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
openBrowser(url)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
conf = &oauth2.Config{
|
conf = &oauth2.Config{
|
||||||
ClientID: "46899977096215655",
|
ClientID: "46899977096215655",
|
||||||
ClientSecret: "",
|
ClientSecret: "9d85c43b1482497dbbce61f6e4aa173a433796eeae2ca8c5f6129f2dc4de46d9",
|
||||||
Scopes: []string{},
|
Scopes: []string{},
|
||||||
Endpoint: oauth2.Endpoint{
|
Endpoint: oauth2.Endpoint{
|
||||||
AuthURL: "https://auth.gog.com/auth",
|
AuthURL: "https://auth.gog.com/auth",
|
||||||
@ -153,15 +156,21 @@ func main() {
|
|||||||
// for the scopes specified above.
|
// for the scopes specified above.
|
||||||
url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
|
url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
|
||||||
|
|
||||||
log.Println(color.CyanString("You will now be taken to your browser for authentication"))
|
tok := loadToken("./.token")
|
||||||
time.Sleep(1 * time.Second)
|
if tok != nil {
|
||||||
//open.Run(url)
|
client = conf.Client(ctx, tok)
|
||||||
openBrowser(url)
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
log.Printf("Authentication URL: %s\n", url)
|
|
||||||
|
|
||||||
//http.HandleFunc("/oauth/callback", callbackHandler)
|
|
||||||
//log.Fatal(http.ListenAndServe(":9999", nil))
|
|
||||||
|
|
||||||
|
_, err := client.Get("https://embed.gog.com/user/data/games")
|
||||||
|
if err != nil {
|
||||||
|
login(url)
|
||||||
|
} else {
|
||||||
|
log.Println(color.CyanString("Authentication successful"))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
login(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
gl := getGameList()
|
||||||
|
log.Println(gl.owned)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user