mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 05:37:05 +02:00
lastfm: authentication
This commit is contained in:
parent
3ccbb20a9e
commit
5b8f4788f9
10 changed files with 158 additions and 15 deletions
|
@ -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{
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue