-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
59 lines (48 loc) · 1.49 KB
/
auth.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
package spotifyauth
import (
"context"
"golang.org/x/oauth2"
"os"
)
const (
// AuthURL is the URL to Spotify Accounts Service's OAuth2 endpoint.
AuthURL = "https://accounts.spotify.com/authorize"
// TokenURL is the URL to the Spotify Accounts Service's OAuth2
// token endpoint.
TokenURL = "https://accounts.spotify.com/api/token"
)
type Authenticator struct {
config *oauth2.Config
}
type AuthenticatorOption func(a *Authenticator)
func New(opts ...AuthenticatorOption) *Authenticator {
cfg := &oauth2.Config{
ClientID: os.Getenv("SPOTIFY_ID"),
ClientSecret: os.Getenv("SPOTIFY_SECRET"),
Endpoint: oauth2.Endpoint{
AuthURL: AuthURL,
TokenURL: TokenURL,
},
}
a := &Authenticator{
config: cfg,
}
for _, opt := range opts {
opt(a)
}
return a
}
func (a Authenticator) RefreshToken(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error) {
src := a.config.TokenSource(ctx, token)
return src.Token()
}
// Exchange is like Token, except it allows you to manually specify the access
// code instead of pulling it out of an HTTP request.
func (a Authenticator) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) {
return a.config.Exchange(ctx, code, opts...)
}
// Client creates a *http.Client that will use the specified access token for its API requests.
// Combine this with spotify.HTTPClientOpt.
func (a Authenticator) Client(ctx context.Context, token *oauth2.Token) *http.Client {
return a.config.Client(ctx, token)
}