-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
218 lines (182 loc) · 5.35 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"context"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"path"
"github.com/caarlos0/env/v6"
"golang.org/x/oauth2"
"golang.org/x/oauth2/spotify"
)
const id = "SpotifyAuthProxy"
var (
source oauth2.TokenSource
envCfg *EnvConfig
token string
config *oauth2.Config
)
type EnvConfig struct {
ClientID string `env:"SPOTIFY_CLIENT_ID,required"`
ClientSecret string `env:"SPOTIFY_CLIENT_SECRET,required"`
BaseURL *url.URL `env:"SPOTIFY_PROXY_BASE_URI" envDefault:"http://localhost:8080"`
APIKey string `env:"SPOTIFY_PROXY_API_KEY"`
}
func main() {
envCfg = new(EnvConfig)
if err := env.Parse(envCfg); err != nil {
fmt.Printf("%+v\n", err)
os.Exit(1)
}
if envCfg.APIKey == "" {
envCfg.APIKey = randString()
fmt.Println("APIKey: ", envCfg.APIKey)
}
token = randString()
authUrl := *envCfg.BaseURL
authUrl.Path = path.Join(authUrl.Path, "authorize")
authUrl.RawQuery = url.Values{"token": {token}}.Encode()
fmt.Println("Auth URL:", authUrl.String())
redirectUrl := *envCfg.BaseURL
redirectUrl.Path = path.Join(redirectUrl.Path, "spotify_callback")
config = &oauth2.Config{
ClientID: envCfg.ClientID,
ClientSecret: envCfg.ClientSecret,
RedirectURL: redirectUrl.String(),
Endpoint: spotify.Endpoint,
Scopes: []string{
"user-read-email",
"user-read-private",
// "playlist-read-collaborative",
"playlist-read-private",
"playlist-modify-private",
"playlist-modify-public",
"user-library-read",
"user-library-modify",
// "ugc-image-upload",
},
}
http.HandleFunc("/authorize", Authorize)
http.HandleFunc("/api/v1/token/terraform", APIToken)
http.HandleFunc("/spotify_callback", SpotifyCallback)
http.HandleFunc("/health", HealthCheck)
log.Fatal(http.ListenAndServe(":8080", nil))
}
// APIToken is the endpoint for refreshing API tokens
func APIToken(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || username != id || password != envCfg.APIKey {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintln(w, "APIToken: invalid authorization")
return
}
if source == nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "APIToken: token not available")
return
}
token, err := source.Token()
if err != nil {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintln(w, "APIToken: could not retrieve token")
return
}
if err := json.NewEncoder(w).Encode(token); err != nil {
fmt.Fprintf(w, "APIToken: could not encode JSON response: %s\n", err)
w.WriteHeader(http.StatusInternalServerError)
}
fmt.Println("Token Retrieved")
}
// Authorize takes a user through the auth flow to get a new access token
func Authorize(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("token")
if key != token {
fmt.Fprintln(w, "Authorize: you are not authorized to sign in to this platform")
w.WriteHeader(http.StatusUnauthorized)
}
state := randString()
mac := hmac.New(sha256.New, []byte(envCfg.APIKey))
mac.Write([]byte(state))
state += base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
http.Redirect(w, r, config.AuthCodeURL(state), http.StatusSeeOther)
}
// SpotifyCallback handles the redirect from spotify after authorizing
func SpotifyCallback(w http.ResponseWriter, r *http.Request) {
if err := r.FormValue("error"); err != "" {
fmt.Fprintf(w, "Could not complete authorization: %s\n", err)
w.WriteHeader(http.StatusBadRequest)
return
}
code := r.FormValue("code")
if code == "" {
fmt.Fprintln(w, "Could not complete authorization: no auth code present")
w.WriteHeader(http.StatusBadRequest)
return
}
state := r.FormValue("state")
if len(state) < 64 {
fmt.Fprintln(w, "Could not complete authorization: invalid state value")
w.WriteHeader(http.StatusUnauthorized)
return
}
state1 := []byte(state[:64])
stateMac, err := base64.RawURLEncoding.DecodeString(state[64:])
if err != nil {
fmt.Fprintln(w, "Could not complete authorization: invalid state value")
w.WriteHeader(http.StatusUnauthorized)
return
}
mac := hmac.New(sha256.New, []byte(envCfg.APIKey))
mac.Write(state1)
expectedMac := mac.Sum(nil)
if !hmac.Equal(stateMac, expectedMac) {
fmt.Fprintln(w, "Could not complete authorization: invalid state value")
w.WriteHeader(http.StatusUnauthorized)
return
}
token, err := config.Exchange(r.Context(), code)
if err != nil {
fmt.Fprintln(w, "Could not complete authorization: invalid auth code")
w.WriteHeader(http.StatusUnauthorized)
return
}
source = config.TokenSource(context.Background(), token)
fmt.Fprintln(w, "Authorization successful")
fmt.Println("Authorization successful")
}
func HealthCheck(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
status := "healthy"
response := struct {
Status string `json:"status"`
}{
Status: status,
}
jsonResponse, err := json.Marshal(response)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "Error encoding JSON response:", err)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResponse)
}
func randString() string {
output := make([]byte, 48)
n, err := rand.Reader.Read(output)
if err != nil {
panic(err)
}
if n != 48 {
panic("could not read 48 bytes")
}
return base64.RawURLEncoding.EncodeToString(output)
}