-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeys.go
43 lines (38 loc) · 1.27 KB
/
keys.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
package fireauth
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
)
// HeaderCacheControl Cache-Control field in http response header
const HeaderCacheControl = "Cache-Control"
var myClient = &http.Client{Timeout: 30 * time.Second}
// GetKeys client tokens must be signed by one of the server keys provided via a url.
// The keys expire after a certain amount of time so we need to track that also.
func GetKeys(tokens map[string]interface{}, keyURL string) (int64, error) {
r, err := myClient.Get(keyURL)
if err != nil {
return 0, err
}
maxAge, err := extractMaxAge(r.Header.Get(HeaderCacheControl))
if err != nil {
return maxAge, err
}
defer r.Body.Close()
return maxAge, json.NewDecoder(r.Body).Decode(&tokens)
}
// Extract the max age from the cache control response header value
// The cache control header should look similar to "..., max-age=19008, ..."
func extractMaxAge(cacheControl string) (int64, error) {
// "..., max-age=19008, ..."" to ["..., max-age="]["19008, ..."]
tokens := strings.Split(cacheControl, "max-age=")
if len(tokens) == 1 {
return 0, ErrCacheControlHeaderLacksMaxAge
}
// "19008, ..." to ["19008"][" ..."]
tokens2 := strings.Split(tokens[1], ",")
// convert "19008" to int64
return strconv.ParseInt(tokens2[0], 10, 64)
}