2023-01-08 05:22:54 +00:00
|
|
|
package auth
|
2023-01-07 07:40:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2023-01-11 02:01:40 +00:00
|
|
|
"os/exec"
|
2023-01-07 07:40:39 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2023-01-08 05:22:54 +00:00
|
|
|
"gospt/internal/config"
|
2023-01-08 05:26:06 +00:00
|
|
|
"gospt/internal/gctx"
|
2023-01-07 07:40:39 +00:00
|
|
|
|
|
|
|
"github.com/zmb3/spotify/v2"
|
|
|
|
spotifyauth "github.com/zmb3/spotify/v2/auth"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-01-08 07:57:33 +00:00
|
|
|
auth *spotifyauth.Authenticator
|
|
|
|
ch = make(chan *spotify.Client)
|
|
|
|
state = "abc123"
|
|
|
|
configDir, _ = os.UserConfigDir()
|
2023-01-07 07:40:39 +00:00
|
|
|
)
|
|
|
|
|
2023-01-08 05:26:06 +00:00
|
|
|
func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
|
2023-01-11 02:01:40 +00:00
|
|
|
if config.Values.ClientId == "" || config.Values.ClientSecret == "" || config.Values.Port == "" {
|
2023-01-09 05:08:51 +00:00
|
|
|
configDir, _ := os.UserConfigDir()
|
|
|
|
fmt.Println("PLEASE WRITE YOUR CONFIG FILE IN", filepath.Join(configDir, "gospt/client.yml"))
|
|
|
|
fmt.Println("GO HERE TO AND MAKE AN APPLICATION: https://developer.spotify.com/dashboard/applications")
|
2023-01-11 02:01:40 +00:00
|
|
|
fmt.Print("\nclient_id: \"idgoesherelikethis\"\nclient_secret: \"secretgoesherelikethis\"\nport:\"8888\"\n\n")
|
2023-01-09 05:08:51 +00:00
|
|
|
return nil, fmt.Errorf("\nINVALID CONFIG")
|
|
|
|
}
|
2023-01-07 07:40:39 +00:00
|
|
|
auth = spotifyauth.New(
|
|
|
|
spotifyauth.WithClientID(config.Values.ClientId),
|
|
|
|
spotifyauth.WithClientSecret(config.Values.ClientSecret),
|
2023-01-11 02:01:40 +00:00
|
|
|
spotifyauth.WithRedirectURL(fmt.Sprintf("http://localhost:%s/callback", config.Values.Port)),
|
2023-01-07 07:40:39 +00:00
|
|
|
spotifyauth.WithScopes(
|
|
|
|
spotifyauth.ScopeImageUpload,
|
|
|
|
spotifyauth.ScopePlaylistReadPrivate,
|
|
|
|
spotifyauth.ScopePlaylistModifyPublic,
|
|
|
|
spotifyauth.ScopePlaylistModifyPrivate,
|
|
|
|
spotifyauth.ScopePlaylistReadCollaborative,
|
|
|
|
spotifyauth.ScopeUserFollowModify,
|
|
|
|
spotifyauth.ScopeUserFollowRead,
|
|
|
|
spotifyauth.ScopeUserLibraryModify,
|
|
|
|
spotifyauth.ScopeUserLibraryRead,
|
|
|
|
spotifyauth.ScopeUserReadPrivate,
|
|
|
|
spotifyauth.ScopeUserReadEmail,
|
|
|
|
spotifyauth.ScopeUserReadCurrentlyPlaying,
|
|
|
|
spotifyauth.ScopeUserReadPlaybackState,
|
|
|
|
spotifyauth.ScopeUserModifyPlaybackState,
|
|
|
|
spotifyauth.ScopeUserReadRecentlyPlayed,
|
|
|
|
spotifyauth.ScopeUserTopRead,
|
|
|
|
spotifyauth.ScopeStreaming,
|
|
|
|
),
|
|
|
|
)
|
2023-01-08 07:57:33 +00:00
|
|
|
if _, err := os.Stat(filepath.Join(configDir, "gospt/auth.json")); err == nil {
|
|
|
|
authFile, err := os.Open(filepath.Join(configDir, "gospt/auth.json"))
|
2023-01-07 07:40:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer authFile.Close()
|
|
|
|
authValue, err := ioutil.ReadAll(authFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var tok *oauth2.Token
|
|
|
|
err = json.Unmarshal(authValue, &tok)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-08 02:19:33 +00:00
|
|
|
client := spotify.New(auth.Client(ctx, tok))
|
2023-01-07 16:19:19 +00:00
|
|
|
new_token, err := client.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if new_token != tok {
|
|
|
|
out, err := json.MarshalIndent(new_token, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
2023-01-08 07:57:33 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
|
2023-01-07 16:19:19 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("FAILED TO SAVE AUTH")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out, err := json.MarshalIndent(tok, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
2023-01-08 07:57:33 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
|
2023-01-07 16:19:19 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("FAILED TO SAVE AUTH")
|
|
|
|
}
|
2023-01-07 07:40:39 +00:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
// first start an HTTP server
|
|
|
|
http.HandleFunc("/callback", completeAuth)
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println("Got request for:", r.URL.String())
|
|
|
|
})
|
|
|
|
go func() {
|
2023-01-11 02:01:40 +00:00
|
|
|
err := http.ListenAndServe(fmt.Sprintf(":%s", config.Values.Port), nil)
|
2023-01-07 07:40:39 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
url := auth.AuthURL(state)
|
2023-01-11 02:01:40 +00:00
|
|
|
fmt.Println(url)
|
|
|
|
cmd := exec.Command("xdg-open", url)
|
|
|
|
cmd.Start()
|
2023-01-07 07:40:39 +00:00
|
|
|
// wait for auth to complete
|
|
|
|
client := <-ch
|
|
|
|
|
|
|
|
// use the client to make calls that require authorization
|
|
|
|
user, err := client.CurrentUser(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fmt.Println("You are logged in as:", user.ID)
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func completeAuth(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tok, err := auth.Token(r.Context(), state, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Couldn't get token", http.StatusForbidden)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if st := r.FormValue("state"); st != state {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
log.Fatalf("State mismatch: %s != %s\n", st, state)
|
|
|
|
}
|
|
|
|
out, err := json.MarshalIndent(tok, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
2023-01-08 07:57:33 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
|
2023-01-07 07:40:39 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("FAILED TO SAVE AUTH")
|
|
|
|
}
|
|
|
|
// use the token to get an authenticated client
|
|
|
|
client := spotify.New(auth.Client(r.Context(), tok))
|
|
|
|
fmt.Fprintf(w, "Login Completed!")
|
|
|
|
ch <- client
|
|
|
|
}
|