mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-24 21:47:55 +02:00
lastfm: authentication
This commit is contained in:
parent
3ccbb20a9e
commit
5b8f4788f9
10 changed files with 158 additions and 15 deletions
|
@ -27,6 +27,7 @@ import (
|
|||
"go.uploadedlobster.com/scotty/internal/backends/dump"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/funkwhale"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/jspf"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/lastfm"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/listenbrainz"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/maloja"
|
||||
"go.uploadedlobster.com/scotty/internal/backends/scrobblerlog"
|
||||
|
@ -80,6 +81,7 @@ var knownBackends = map[string]func() models.Backend{
|
|||
"dump": func() models.Backend { return &dump.DumpBackend{} },
|
||||
"funkwhale": func() models.Backend { return &funkwhale.FunkwhaleApiBackend{} },
|
||||
"jspf": func() models.Backend { return &jspf.JSPFBackend{} },
|
||||
"lastfm": func() models.Backend { return &lastfm.LastfmApiBackend{} },
|
||||
"listenbrainz": func() models.Backend { return &listenbrainz.ListenBrainzApiBackend{} },
|
||||
"maloja": func() models.Backend { return &maloja.MalojaApiBackend{} },
|
||||
"scrobbler-log": func() models.Backend { return &scrobblerlog.ScrobblerLogBackend{} },
|
||||
|
|
|
@ -33,8 +33,13 @@ func (s deezerStrategy) Config() oauth2.Config {
|
|||
return s.conf
|
||||
}
|
||||
|
||||
func (s deezerStrategy) AuthCodeURL(verifier string, state string) string {
|
||||
return s.conf.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
|
||||
func (s deezerStrategy) AuthCodeURL(verifier string, state string) auth.AuthUrl {
|
||||
url := s.conf.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
|
||||
return auth.AuthUrl{
|
||||
Url: url,
|
||||
State: state,
|
||||
Param: "code",
|
||||
}
|
||||
}
|
||||
|
||||
func (s deezerStrategy) ExchangeToken(code auth.CodeResponse, verifier string) (*oauth2.Token, error) {
|
||||
|
|
51
internal/backends/lastfm/auth.go
Normal file
51
internal/backends/lastfm/auth.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
Scotty is free software: you can redistribute it and/or modify it under the
|
||||
terms of the GNU General Public License as published by the Free Software
|
||||
Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
|
||||
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
Scotty. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lastfm
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/shkh/lastfm-go/lastfm"
|
||||
"go.uploadedlobster.com/scotty/internal/auth"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
type lastfmStrategy struct {
|
||||
client *lastfm.Api
|
||||
redirectUrl *url.URL
|
||||
}
|
||||
|
||||
func (s lastfmStrategy) Config() oauth2.Config {
|
||||
return oauth2.Config{}
|
||||
}
|
||||
|
||||
func (s lastfmStrategy) AuthCodeURL(verifier string, state string) auth.AuthUrl {
|
||||
// Last.fm does not use OAuth2, but the provided authorization flow with
|
||||
// callback URL is close enough we can shoehorn it into the existing
|
||||
// authentication strategy.
|
||||
// TODO: Investigate and use callback-less flow with api.GetAuthTokenUrl(token)
|
||||
url := s.client.GetAuthRequestUrl(s.redirectUrl.String())
|
||||
return auth.AuthUrl{
|
||||
Url: url,
|
||||
State: "", // last.fm does not use state
|
||||
Param: "token",
|
||||
}
|
||||
}
|
||||
|
||||
func (s lastfmStrategy) ExchangeToken(code auth.CodeResponse, verifier string) (*oauth2.Token, error) {
|
||||
// The token is directly valid
|
||||
return &oauth2.Token{AccessToken: code.Code}, nil
|
||||
}
|
54
internal/backends/lastfm/lastfm.go
Normal file
54
internal/backends/lastfm/lastfm.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
Scotty is free software: you can redistribute it and/or modify it under the
|
||||
terms of the GNU General Public License as published by the Free Software
|
||||
Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
|
||||
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
Scotty. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lastfm
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/shkh/lastfm-go/lastfm"
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/internal/auth"
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
type LastfmApiBackend struct {
|
||||
client *lastfm.Api
|
||||
}
|
||||
|
||||
func (b *LastfmApiBackend) Name() string { return "lastfm" }
|
||||
|
||||
func (b *LastfmApiBackend) FromConfig(config *viper.Viper) models.Backend {
|
||||
clientId := config.GetString("client-id")
|
||||
clientSecret := config.GetString("client-secret")
|
||||
b.client = lastfm.New(clientId, clientSecret)
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *LastfmApiBackend) OAuth2Strategy(redirectUrl *url.URL) auth.OAuth2Strategy {
|
||||
return lastfmStrategy{
|
||||
client: b.client,
|
||||
redirectUrl: redirectUrl,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *LastfmApiBackend) OAuth2Setup(token oauth2.TokenSource) error {
|
||||
t, err := token.Token()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return b.client.LoginWithToken(t.AccessToken)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue