lastfm: authentication

This commit is contained in:
Philipp Wolfer 2023-11-23 23:14:47 +01:00
parent 3ccbb20a9e
commit 5b8f4788f9
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
10 changed files with 158 additions and 15 deletions

View file

@ -43,10 +43,6 @@ var authCmd = &cobra.Command{
redirectURL, err := backends.BuildRedirectURL(viper.GetViper(), backend.Name())
cobra.CheckErr(err)
// Start an HTTP server to listen for the response
responseChan := make(chan auth.CodeResponse)
auth.RunOauth2CallbackServer(*redirectURL, responseChan)
// The backend must provide an authentication strategy
strategy := backend.OAuth2Strategy(redirectURL)
@ -56,14 +52,20 @@ var authCmd = &cobra.Command{
state := "somestate" // FIXME: Should be a random string
// Redirect user to consent page to ask for permission specified scopes.
url := strategy.AuthCodeURL(verifier, state)
fmt.Printf("Visit the URL for the auth dialog: %v\n", url)
err = browser.OpenURL(url)
authUrl := strategy.AuthCodeURL(verifier, state)
// Start an HTTP server to listen for the response
responseChan := make(chan auth.CodeResponse)
auth.RunOauth2CallbackServer(*redirectURL, authUrl.Param, responseChan)
// Open the URL
fmt.Printf("Visit the URL for the auth dialog: %v\n", authUrl.Url)
err = browser.OpenURL(authUrl.Url)
cobra.CheckErr(err)
// Retrieve the code from the authentication callback
code := <-responseChan
if code.State != state {
if code.State != authUrl.State {
cobra.CompErrorln("Error: oauth state mismatch")
os.Exit(1)
}