-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
110 lines (87 loc) · 2.6 KB
/
api.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"context"
"fmt"
"net/http"
"sync"
"github.com/zmb3/spotify"
)
type API struct {
sync.RWMutex
Users map[string]*LameUser
SpotifyRedirectURI string
YoutubeRedirectURI string
}
func (this *API) GetUser(username string) (*LameUser, bool) {
this.RLock()
result, ok := this.Users[username]
this.RUnlock()
return result, ok
}
// LoginUser has some unique functionality. We decided to allow anyone to login with any username. First check if the user exists, if it doesn't, create it.
func (this *API) LoginUser(username string) {
if _, exists := this.GetUser(username); exists == true {
// the user already exists, ideally we have stronger account security :P
return
}
this.Lock()
this.Users[username] = &LameUser{
ServiceAccounts: make(map[string]ServiceAccount),
}
this.Unlock()
return
}
func (this *API) RegisterYoutube(user string) (string, error) {
this.LoginUser(user)
lameUser, ok := this.GetUser(user)
if !ok {
return "", fmt.Errorf("could not get user when registering youtube")
}
ys, err := NewYoutubeService(context.Background(), user, this.YoutubeRedirectURI)
if err != nil {
return "", err
}
// add youtube service to user
this.Lock()
lameUser.ServiceAccounts["youtube"] = ys
this.Unlock()
return ys.authUrl, nil
}
func (this *API) YoutubeAuthCallback(r *http.Request) error {
code := r.FormValue("code")
user := r.FormValue("state")
fmt.Println("youtube callback data: ")
fmt.Println("code: ", code)
fmt.Println("user: ", user)
lameUser, ok := this.GetUser(user)
if !ok {
return fmt.Errorf("ERROR: could not find user: %s", user)
}
this.Lock()
ys := lameUser.ServiceAccounts["youtube"].(*YoutubeService)
if err := ys.Authenticate(code); err != nil {
this.Unlock()
return err
}
this.Unlock()
return nil
}
func (this *API) RegisterSpotify(user string) string {
auth := spotify.NewAuthenticator(this.SpotifyRedirectURI, spotify.ScopeUserReadPrivate, spotify.ScopePlaylistReadPrivate, spotify.ScopePlaylistModifyPrivate, spotify.ScopePlaylistModifyPublic)
return auth.AuthURL(user)
}
func (this *API) SpotifyAuthCallback(r *http.Request) error {
auth := spotify.NewAuthenticator(this.SpotifyRedirectURI, spotify.ScopeUserReadPrivate, spotify.ScopePlaylistReadPrivate, spotify.ScopePlaylistModifyPrivate, spotify.ScopePlaylistModifyPublic)
// we save our user in this parameter
user := r.FormValue("state")
tok, err := auth.Token(user, r)
if err != nil {
return err
}
client := auth.NewClient(tok)
this.LoginUser(user)
this.Lock()
this.Users[user].ServiceAccounts["spotify"] = NewSpotifyUser(&client)
this.Unlock()
return nil
}