adds trace log to http, fixes login flow #14
@ -1,14 +1,16 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"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"
|
||||||
|
|
||||||
@ -24,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"))
|
||||||
@ -67,6 +75,12 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
ctx.Context = context.WithValue(ctx.Context, oauth2.HTTPClient, &http.Client{
|
||||||
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||||
|
log.Trace().Interface("path", r.URL.Path).Msg("request")
|
||||||
|
return http.DefaultTransport.RoundTrip(r)
|
||||||
|
}),
|
||||||
|
})
|
||||||
authClient := auth.Client(ctx, tok)
|
authClient := auth.Client(ctx, tok)
|
||||||
client := spotify.New(authClient)
|
client := spotify.New(authClient)
|
||||||
new_token, err := client.Token()
|
new_token, err := client.Token()
|
||||||
@ -91,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)
|
||||||
@ -114,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