forked from markbates/goth
/
session.go
47 lines (40 loc) · 1.16 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package spotify
import (
"encoding/json"
"errors"
"github.com/markbates/goth"
"golang.org/x/oauth2"
)
// Session stores data during the auth process with Spotify.
type Session struct {
AuthURL string
AccessToken string
}
// GetAuthURL will return the URL set by calling the `BeginAuth` function on the
// Spotify provider.
func (s Session) GetAuthURL() (string, error) {
if s.AuthURL == "" {
return "", errors.New("spotify: AuthURL has not been set")
}
return s.AuthURL, nil
}
// Authorize completes the the authorization with Spotify and returns the access
// token to be stored for future use.
func (s Session) Authorize(provider goth.Provider, params goth.Params) (string, error) {
p := provider.(*Provider)
token, err := p.config.Exchange(oauth2.NoContext, params.Get("code"))
if err != nil {
return "", err
}
s.AccessToken = token.AccessToken
return token.AccessToken, err
}
// Marshal marshals a session into a JSON string.
func (s Session) Marshal() string {
j, _ := json.Marshal(s)
return string(j)
}
// String is equivalent to Marshal. It returns a JSON representation of the session.
func (s Session) String() string {
return s.Marshal()
}