adds trace log to http, fixes login flow (#14)
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Reviewed-on: https://gitea.asdf.cafe/abs3nt/gospt/pulls/14 Co-authored-by: a <a@tuxpa.in> Co-committed-by: a <a@tuxpa.in>
This commit is contained in:
parent
7e65581333
commit
a1e9cc6dee
@ -1,15 +1,16 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"tuxpa.in/a/zlog/log"
|
||||||
|
|
||||||
"gitea.asdf.cafe/abs3nt/gospt/src/config"
|
"gitea.asdf.cafe/abs3nt/gospt/src/config"
|
||||||
"gitea.asdf.cafe/abs3nt/gospt/src/gctx"
|
"gitea.asdf.cafe/abs3nt/gospt/src/gctx"
|
||||||
|
|
||||||
@ -25,6 +26,12 @@ var (
|
|||||||
configDir, _ = os.UserConfigDir()
|
configDir, _ = os.UserConfigDir()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
||||||
|
|
||||||
|
func (fn roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
return fn(req)
|
||||||
|
}
|
||||||
|
|
||||||
func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
||||||
if config.Values.ClientId == "" || config.Values.ClientSecret == "" || config.Values.Port == "" {
|
if config.Values.ClientId == "" || config.Values.ClientSecret == "" || config.Values.Port == "" {
|
||||||
fmt.Println("PLEASE WRITE YOUR CONFIG FILE IN", filepath.Join(configDir, "gospt/client.yml"))
|
fmt.Println("PLEASE WRITE YOUR CONFIG FILE IN", filepath.Join(configDir, "gospt/client.yml"))
|
||||||
@ -57,42 +64,36 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
if _, err := os.Stat(filepath.Join(configDir, "gospt/auth.json")); err == nil {
|
if _, err := os.Stat(filepath.Join(configDir, "gospt/auth.json")); err == nil {
|
||||||
authFile, err := os.Open(filepath.Join(configDir, "gospt/auth.json"))
|
authFilePath := filepath.Join(configDir, "gospt/auth.json")
|
||||||
|
authFile, err := os.Open(authFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer authFile.Close()
|
defer authFile.Close()
|
||||||
authValue, err := io.ReadAll(authFile)
|
tok := &oauth2.Token{}
|
||||||
|
err = json.NewDecoder(authFile).Decode(tok)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var tok *oauth2.Token
|
ctx.Context = context.WithValue(ctx.Context, oauth2.HTTPClient, &http.Client{
|
||||||
err = json.Unmarshal(authValue, &tok)
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||||
if err != nil {
|
log.Trace().Interface("path", r.URL.Path).Msg("request")
|
||||||
return nil, err
|
return http.DefaultTransport.RoundTrip(r)
|
||||||
}
|
}),
|
||||||
client := spotify.New(auth.Client(ctx, tok))
|
})
|
||||||
|
authClient := auth.Client(ctx, tok)
|
||||||
|
client := spotify.New(authClient)
|
||||||
new_token, err := client.Token()
|
new_token, err := client.Token()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if new_token != tok {
|
out, err := json.MarshalIndent(new_token, "", " ")
|
||||||
out, err := json.MarshalIndent(new_token, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
err = os.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
|
|
||||||
if err != nil {
|
|
||||||
panic("FAILED TO SAVE AUTH")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out, err := json.MarshalIndent(tok, "", " ")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
return nil, err
|
||||||
}
|
}
|
||||||
err = os.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
|
err = os.WriteFile(authFilePath, out, 0o644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("FAILED TO SAVE AUTH")
|
return nil, fmt.Errorf("failed to save auth")
|
||||||
}
|
}
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
@ -104,7 +105,7 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
|||||||
go func() {
|
go func() {
|
||||||
err := http.ListenAndServe(fmt.Sprintf(":%s", config.Values.Port), nil)
|
err := http.ListenAndServe(fmt.Sprintf(":%s", config.Values.Port), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
url := auth.AuthURL(state)
|
url := auth.AuthURL(state)
|
||||||
@ -127,7 +128,6 @@ func completeAuth(w http.ResponseWriter, r *http.Request) {
|
|||||||
tok, err := auth.Token(r.Context(), state, r)
|
tok, err := auth.Token(r.Context(), state, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Couldn't get token", http.StatusForbidden)
|
http.Error(w, "Couldn't get token", http.StatusForbidden)
|
||||||
log.Fatal(err)
|
|
||||||
}
|
}
|
||||||
if st := r.FormValue("state"); st != state {
|
if st := r.FormValue("state"); st != state {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
|
Loading…
Reference in New Issue
Block a user