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

@ -21,9 +21,9 @@ import (
"net/url"
)
func RunOauth2CallbackServer(redirectURL url.URL, responseChan chan CodeResponse) {
func RunOauth2CallbackServer(redirectURL url.URL, param string, responseChan chan CodeResponse) {
http.HandleFunc(redirectURL.Path, func(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
code := r.URL.Query().Get(param)
state := r.URL.Query().Get("state")
fmt.Fprint(w, "Token received, you can close this window now.")
responseChan <- CodeResponse{

View file

@ -24,11 +24,21 @@ import (
type OAuth2Strategy interface {
Config() oauth2.Config
AuthCodeURL(verifier string, state string) string
AuthCodeURL(verifier string, state string) AuthUrl
ExchangeToken(code CodeResponse, verifier string) (*oauth2.Token, error)
}
type AuthUrl struct {
// The URL the user must visit to approve access
Url string
// Random state string passed on to the callback.
// Leave empty if the service does not support state.
State string
// Parameter name of the code passed on to the callback (usually "code")
Param string
}
type CodeResponse struct {
Code string
State string
@ -46,8 +56,13 @@ func (s StandardStrategy) Config() oauth2.Config {
return s.conf
}
func (s StandardStrategy) AuthCodeURL(verifier string, state string) string {
return s.conf.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
func (s StandardStrategy) AuthCodeURL(verifier string, state string) AuthUrl {
url := s.conf.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
return AuthUrl{
Url: url,
State: state,
Param: "code",
}
}
func (s StandardStrategy) ExchangeToken(code CodeResponse, verifier string) (*oauth2.Token, error) {