131 lines
4.3 KiB
Go
131 lines
4.3 KiB
Go
/*
|
|
Copyright © 2023 Jonas Möller <mail@jonasmoeller.de>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/dchest/uniuri"
|
|
"github.com/spf13/viper"
|
|
"github.com/zmb3/spotify/v2"
|
|
spotifyauth "github.com/zmb3/spotify/v2/auth"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// spotifyAuthCmd represents the spotifyAuth command
|
|
var spotifyAuthCmd = &cobra.Command{
|
|
Use: "spotifyAuth",
|
|
Short: "Authenticate your Spotify account",
|
|
Long: `Authenticate spotigram to access your Spotify account.
|
|
|
|
This will add the client configuration for Spotify to your config file.
|
|
It is meant to run just once for initialization.
|
|
|
|
You can also use this command to recover your config with a new set of client credentials.
|
|
|
|
Client credentials for the Spotify application must be present as environment variables:
|
|
"SPOTIFY_CLIENT_ID" and "SPOTIFY_CLIENT_SECRET".`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
spotifyLogin()
|
|
},
|
|
}
|
|
var (
|
|
auth = spotifyauth.New()
|
|
ch = make(chan *spotify.Client)
|
|
state = uniuri.New()
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(spotifyAuthCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// spotifyAuthCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// spotifyAuthCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|
|
|
|
func spotifyLogin() {
|
|
viper.SetDefault("spotify_callback_listen_port", ":8888")
|
|
viper.SetDefault("spotify_callback_redirect_url", "http://localhost:8888/callback")
|
|
|
|
auth = spotifyauth.New(spotifyauth.WithRedirectURL(viper.GetString("spotify_callback_redirect_url")),
|
|
spotifyauth.WithScopes(spotifyauth.ScopePlaylistModifyPublic),
|
|
spotifyauth.WithClientID(viper.GetString("spotify_client_id")),
|
|
spotifyauth.WithClientSecret(viper.GetString("spotify_client_secret")),
|
|
)
|
|
|
|
http.HandleFunc("/callback", completeAuth)
|
|
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
|
|
log.Println("Got request for ", request.URL.String())
|
|
})
|
|
|
|
go func() {
|
|
err := http.ListenAndServe(viper.GetString("spotify_callback_listen_port"), nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}()
|
|
|
|
url := auth.AuthURL(state)
|
|
fmt.Println("Visit this URL to authenticate Spotify: ", url)
|
|
|
|
client := <-ch
|
|
|
|
token, err := client.Token()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
viper.Set("spotify_access_token", token.AccessToken)
|
|
viper.Set("spotify_refresh_token", token.RefreshToken)
|
|
viper.Set("spotify_token_type", token.TokenType)
|
|
viper.Set("spotify_token_expiry", token.Expiry)
|
|
if err = viper.WriteConfig(); err != nil {
|
|
cobra.CheckErr(err)
|
|
}
|
|
|
|
fmt.Printf("\nLogin complete ✅\n\nSession: %s\n", state)
|
|
}
|
|
|
|
func completeAuth(writer http.ResponseWriter, request *http.Request) {
|
|
token, err := auth.Token(request.Context(), state, request)
|
|
if err != nil {
|
|
http.Error(writer, "Could not get token.", http.StatusForbidden)
|
|
cobra.CheckErr(err)
|
|
}
|
|
if st := request.FormValue("state"); st != state {
|
|
http.NotFound(writer, request)
|
|
log.Fatalf("State mismatch: %s != %s\n", st, state)
|
|
}
|
|
|
|
client := spotify.New(auth.Client(request.Context(), token))
|
|
_, _ = fmt.Fprintf(writer, "Login complete ✅\n\nSession: %s", state)
|
|
ch <- client
|
|
}
|