/
client.go
75 lines (63 loc) · 1.95 KB
/
client.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package spotify
import (
"context"
"log"
"github.com/chances/party-server/cache"
"github.com/chances/party-server/session"
"github.com/gin-gonic/gin"
"github.com/zmb3/spotify"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
var partyCache cache.Store
// SetCache sets the Party cache store used by this Spotify client
func SetCache(c cache.Store) {
partyCache = c
}
// DefaultToken gets a Spotify access token via Spotify's client credentials OAuth flow
func DefaultToken(auth clientcredentials.Config) (*oauth2.Token, error) {
tokenEntry, err := partyCache.GetOrDefer("SPOTIFY_TOKEN", func() (*cache.Entry, error) {
token, err := auth.Token(context.Background())
if err != nil {
log.Fatalf("spotify client credentials: %v\n", err)
}
log.Println(token)
tokenEntry := cache.Expires(token.Expiry, *token)
return &tokenEntry, nil
})
if err != nil {
return nil, err
}
token := (*tokenEntry).Value.(oauth2.Token)
return &token, nil
}
// DefaultClient gets a Spotify client from the default token
// The default token is provided via Spotify's Client Credentials auth flow
func DefaultClient(def clientcredentials.Config, auth spotify.Authenticator) (*spotify.Client, error) {
token, err := DefaultToken(def)
if err != nil {
return nil, err
}
newClient := auth.NewClient(token)
return &newClient, nil
}
// ClientFromSession gets a Spotify client from the session's user
func ClientFromSession(c *gin.Context, auth spotify.Authenticator) (*spotify.Client, error) {
user := session.CurrentUser(c)
newClient := auth.NewClient(&oauth2.Token{
AccessToken: user.AccessToken,
RefreshToken: user.RefreshToken,
Expiry: user.TokenExpiryDate,
})
token, err := newClient.Token()
if err != nil {
return nil, err
}
if token.AccessToken != user.AccessToken {
user.AccessToken = token.AccessToken
user.RefreshToken = token.RefreshToken
user.TokenExpiryDate = token.Expiry
user.UpdateGP()
}
return &newClient, nil
}