-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
297 lines (281 loc) · 11.3 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/firestore"
spotify "github.com/chew-z/spotify"
"github.com/coreos/go-oidc"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
"golang.org/x/oauth2"
"google.golang.org/api/option"
)
/*TODO
The big unknown is what happens when user deauthorizes our app in preferences
without letting us know
Token becomes invalid and throws at us errors at each attempt (this is a problem for
Cloud functions which will still attempt to authorize
- We should also get user country and location
*/
/*AuthenticationRequired - is an authentication middleware for selected paths
authPath ("/user" by default) is used for gin router group authorized
*/
func AuthenticationRequired(authPath string) gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
user := session.Get("user")
// if user is nil == user is not logged in with Spotify, so start authorization process
if user == nil {
// c.JSON(http.StatusUnauthorized, gin.H{"error": "user needs to be signed in to access this service"})
url := auth.AuthURL(authPath)
log.Println("/auth: Please log in to Spotify by visiting the following page in your browser:", url)
c.Redirect(303, url)
c.Abort()
// we call c.Abort() if the user is unauthenticated/unauthorized.
// This is because gin calls the next function in the chain even after you write the header
return
}
// add session verification here, like checking if the user and authPath
// combination actually exists if necessary. Try adding caching this
// since this middleware might be called a lot
// if user is set in session authPath and uuid should be (casting to string will fail terribly if nil)
userString := user.(string)
if path := session.Get("authPath").(string); path == authPath {
// log.Printf("/auth: Seems like we are legit as user %s for routes group %s: ", userString, authPath)
}
uuid := session.Get("uuid").(string)
// var client *spotify.Client
// We are only checking if there is a client for this session in cache
if _, foundClient := kaszka.Get(uuid); foundClient {
// client = gclient.(*spotify.Client)
// kaszka.Add(uuid, &client, cache.DefaultExpiration) //Add is like Set or refresh
} else { // and if there is no client in cache we get token from Firestore
location, _ := time.LoadLocation("Europe/Warsaw")
log.Printf("/auth: Cached client NOT found for: %s", uuid)
// create client and put it in cache
var newTok firestoreToken
newTok.user = userString
newTok.path = authPath
tok, _ := getTokenFromDB(&newTok)
log.Printf("/auth: Token expires at: %s", tok.Expiry.In(location).Format("15:04:05"))
spotifyClient := auth.NewClient(tok)
kaszka.Set(uuid, &spotifyClient, cache.DefaultExpiration)
// if token in Firestore is close to or past expiration we refresh token and update in database
if m, _ := time.ParseDuration("4m30s"); time.Until(tok.Expiry) < m {
newTok.token, _ = spotifyClient.Token()
log.Printf("New token expires in: %s", newTok.token.Expiry.Sub(time.Now()).String())
updateTokenInDB(&newTok)
}
}
return
// c.Next() //TODO - philosophical question - Is c.Next() needed here?
// https://github.com/gin-gonic/gin/issues/1169
}
}
/*clientMagic - is how endpoints obtain Spotify client
which is from cache (fast and cheap in resources) or by
retrieving token from Firestore and creating new client (slow)
*/
func clientMagic(c *gin.Context) *spotify.Client {
endpoint := c.Request.URL.Path
var spotifyClient *spotify.Client
session := sessions.Default(c)
// we are past authorization middleware so session
// variables should not be nil and we can safely cast
// them as strings
uuid := session.Get("uuid").(string)
userString := session.Get("user").(string)
authPath := session.Get("authPath").(string)
log.Printf("/clientMagic: session id: %s", uuid)
// If the session is running Spotify client is probably cached
if gClient, foundClient := kaszka.Get(uuid); foundClient {
spotifyClient = gClient.(*spotify.Client)
kaszka.SetDefault(uuid, spotifyClient) // replace existing
return spotifyClient
}
log.Printf("%s: Cached client NOT found for: %s", endpoint, uuid)
// if client isn't in cache get token from database (user should be logged in and Spotify token saved)
var newTok firestoreToken
newTok.user = userString
newTok.path = authPath
tok, err := getTokenFromDB(&newTok)
if err != nil { // This is irregular will lead to I am teapot.
log.Printf("Couldn't find token for %s", newTok.path)
return nil
}
location, _ := time.LoadLocation("Europe/Warsaw")
log.Printf("/clientMagic: Token in Firestore expires at: %s", tok.Expiry.In(location).Format("15:04:05"))
newClient := auth.NewClient(tok)
// if an item doesn't already exist for the given key, or if the existing item has expired
kaszka.Add(uuid, &newClient, cache.DefaultExpiration)
return &newClient
}
/*getTokenFromDB - retrieves token fromFirestore
*/
func getTokenFromDB(token *firestoreToken) (*oauth2.Token, error) {
// we need Spotify user ID and router group path (auth group) to retrieve a token
path := fmt.Sprintf("users/%s/tokens%s", token.user, token.path)
dsnap, err := firestoreClient.Doc(path).Get(ctx)
if err != nil {
log.Printf("Error retrieving token from Firestore for %s %s.\nPossibly it ain't there..", path, err.Error())
return nil, err
}
tok := &oauth2.Token{}
dsnap.DataTo(tok)
token.token = tok // here token is set by reference and also returned in input parameter
location, _ := time.LoadLocation("Europe/Warsaw")
log.Printf("getTokenFromDB: Got token with expiration %s", tok.Expiry.In(location).Format("15:04:05"))
return tok, nil
}
/*saveTokenToDb - saves token to Firestore
*/
func saveTokenToDB(token *firestoreToken) {
// we need Spotify user ID and router group path (auth group) to retrieve a token
path := fmt.Sprintf("users/%s/tokens%s", token.user, token.path)
// TODO - two set operations - ?
_, err := firestoreClient.Doc(path).Set(ctx, token.token)
_, err = firestoreClient.Collection("users").Doc(token.user).Set(ctx, map[string]interface{}{
"userID": token.user,
"user_displayname": token.displayname,
"user_email": token.email,
"country": token.country,
"token_saved": time.Now(), // account created or logged in new browser
}, firestore.MergeAll)
if err != nil {
log.Printf("saveToken: Error saving token for %s %s", token.path, err.Error())
} else {
location, _ := time.LoadLocation("Europe/Warsaw")
log.Printf("saveToken: Saved token for %s into Firestore", token.path)
log.Printf("saveToken: Token expiration %s", token.token.Expiry.In(location).Format("15:04:05"))
}
}
/*updateTokenInDB - updates token in Firestore
*/
func updateTokenInDB(token *firestoreToken) {
// we need Spotify user ID and router group path (auth group) to retrieve a token
path := fmt.Sprintf("users/%s/tokens%s", token.user, token.path)
_, err := firestoreClient.Doc(path).Set(ctx, map[string]interface{}{
"AccessToken": token.token.AccessToken,
"Expiry": token.token.Expiry,
"RefreshToken": token.token.RefreshToken,
"TokenType": token.token.TokenType,
}, firestore.MergeAll)
_, err = firestoreClient.Collection("users").Doc(token.user).Set(ctx, map[string]interface{}{
"token_updated": time.Now(), // last time token had been refreshed
}, firestore.MergeAll)
if err != nil {
log.Printf("updateToken: Error saving token for %s %s", path, err.Error())
} else {
location, _ := time.LoadLocation("Europe/Warsaw")
log.Printf("updateToken: Saved token for %s into Firestore", path)
log.Printf("updateToken: Token expiration %s", token.token.Expiry.In(location).Format("15:04:05"))
}
}
/*initFirestoreDatabase - as the name says creates Firestore client
in Google Cloud it is using project ID, on localhost credentials file
It works for AppEngine, CloudRun/Docker and local testing
*/
func initFirestoreDatabase(ctx context.Context) *firestore.Client {
// Google App Engine
if gae != "" {
// Not possible locally or on Cloud Run/Docker
firestoreClient, err := firestore.NewClient(ctx, os.Getenv("GOOGLE_CLOUD_PROJECT"))
if err != nil {
log.Panic(err)
}
log.Println("GOOGLE_APP_ENGINE")
return firestoreClient
}
// Google Cloud Run
// https://github.com/googleapis/google-cloud-go/blob/master/firestore/client.go#L62
// Read the code and consider that firebase programmers are weird, it's not how it works
// in official Google examples for other parts of ecosystem
if gcr == "YES" {
sa := option.WithCredentialsFile(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")) // this is JSON file path
firestoreClient, err := firestore.NewClient(ctx, "*detect-project-id*", sa)
if err != nil {
log.Panic(err)
}
log.Println("GOOGLE_CLOUD_RUN")
return firestoreClient
}
// Default - local testing
sa := option.WithCredentialsFile(".firebase-credentials.json")
firestoreClient, err := firestore.NewClient(ctx, os.Getenv("GOOGLE_CLOUD_PROJECT"), sa)
if err != nil {
log.Panic(err)
}
log.Println("LOCAL")
return firestoreClient
}
/*getJWToken - make hops to obtain isigned JWT token with service account
giving authorized access to CloudFunction (audience == CloudFunction URL)
Only works from inside Google Cloud not on localhost or Google Cloud Run
https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature
*/
func getJWToken(audience string) string {
tokenURL := fmt.Sprintf("/instance/service-accounts/default/identity?audience=%s", audience)
jwToken, err := metadata.Get(tokenURL)
if err != nil {
log.Printf("metadata.Get: failed to query id_token: %s", err.Error())
}
return jwToken
}
// Get project ID from metadata server
func getProjectID() string {
metaURL := "/project/project-id"
projectID, err := metadata.Get(metaURL)
if err != nil {
log.Printf("metadata.Get: failed to get project ID %s", err.Error())
}
return projectID
}
func getAccountEmail() string {
metaURL := "/instance/service-accounts/default/email"
email, err := metadata.Get(metaURL)
if err != nil {
log.Printf("metadata.Get: failed to get service email %s", err.Error())
}
return email
}
func verifyToken(audience string, token string) bool {
ctx := context.Background()
keySet := oidc.NewRemoteKeySet(ctx, googleRootCertURL)
// https://github.com/coreos/go-oidc/blob/master/verify.go#L36
var config = &oidc.Config{
SkipClientIDCheck: false,
ClientID: audience,
}
verifier := oidc.NewVerifier("https://accounts.google.com", keySet, config)
idt, err := verifier.Verify(ctx, token)
if err != nil {
log.Printf("CAN NOT verify token %s: ", err.Error())
return false
}
log.Printf("Verified id_token with %v: ", idt.Issuer)
return true
}
// Check for network egress configuration (CR-GKE)
func checkNet() bool {
networkEgressError := false
client := &http.Client{
Timeout: 3 * time.Second,
}
// Check to see if we can reach something off the cluster e.g. www.google.com
req, _ := http.NewRequest("HEAD", "https://www.google.com", nil)
res, err := client.Do(req)
if err == nil && res.StatusCode >= 200 && res.StatusCode <= 299 {
// egress worked successfully
log.Print("Verified that network egress is working as expected.")
} else {
log.Print("Network egress appears to be blocked. Unable to access https://www.google.com.")
networkEgressError = true
}
return networkEgressError
}