|
| 1 | +package firebase |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "crypto/x509" |
| 12 | + "encoding/json" |
| 13 | + "encoding/pem" |
| 14 | + "net/http" |
| 15 | + |
| 16 | + "golang.org/x/net/context" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + defaultCertsCacheTime = 1 * time.Hour |
| 21 | + |
| 22 | + // URL containing the public keys for the Google certs |
| 23 | + clientCertURL = "https://www.googleapis.com/robot/v1/metadata/x509/[email protected]" |
| 24 | +) |
| 25 | + |
| 26 | +type ( |
| 27 | + certificateStore struct { |
| 28 | + sync.RWMutex |
| 29 | + url string |
| 30 | + certs map[string]*x509.Certificate |
| 31 | + exp time.Time |
| 32 | + } |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + certs *certificateStore |
| 37 | +) |
| 38 | + |
| 39 | +func init() { |
| 40 | + certs = newCertificateStore("") |
| 41 | +} |
| 42 | + |
| 43 | +func newCertificateStore(url string) *certificateStore { |
| 44 | + if url == "" { |
| 45 | + url = clientCertURL |
| 46 | + } |
| 47 | + return &certificateStore{ |
| 48 | + url: url, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (c *certificateStore) Get(ctx context.Context, kid string) (*x509.Certificate, error) { |
| 53 | + if err := c.ensureLoaded(ctx); err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + c.RLock() |
| 58 | + defer c.RUnlock() |
| 59 | + |
| 60 | + cert, found := c.certs[kid] |
| 61 | + if !found { |
| 62 | + return nil, fmt.Errorf("certificate not found for key ID: %s", kid) |
| 63 | + } |
| 64 | + return cert, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (c *certificateStore) ensureLoaded(ctx context.Context) error { |
| 68 | + c.RLock() |
| 69 | + if c.exp.After(clock.Now()) { |
| 70 | + c.RUnlock() |
| 71 | + return nil |
| 72 | + } |
| 73 | + c.RUnlock() |
| 74 | + |
| 75 | + certs, cacheTime, err := c.download(ctx) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + c.Lock() |
| 81 | + defer c.Unlock() |
| 82 | + c.certs = certs |
| 83 | + c.exp = clock.Now().Add(cacheTime) |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +// TODO: pass in transport, provide appengine stub to automatically get it from context |
| 88 | +func (c *certificateStore) download(ctx context.Context) (map[string]*x509.Certificate, time.Duration, error) { |
| 89 | + client, err := ContextClient(ctx) |
| 90 | + if err != nil { |
| 91 | + return nil, 0, err |
| 92 | + } |
| 93 | + |
| 94 | + resp, err := client.Get(c.url) |
| 95 | + if err != nil { |
| 96 | + return nil, 0, err |
| 97 | + } |
| 98 | + defer resp.Body.Close() |
| 99 | + if resp.StatusCode != http.StatusOK { |
| 100 | + return nil, 0, fmt.Errorf("download %s fails: %s", c.url, resp.Status) |
| 101 | + } |
| 102 | + certs, err := parse(resp.Body) |
| 103 | + if err != nil { |
| 104 | + return nil, 0, err |
| 105 | + } |
| 106 | + return certs, cacheTime(resp), nil |
| 107 | +} |
| 108 | + |
| 109 | +// parse parses the certificates response in JSON format. |
| 110 | +// The response has the format: |
| 111 | +// { |
| 112 | +// "kid1": "-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----", |
| 113 | +// "kid2": "-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----", |
| 114 | +// } |
| 115 | +func parse(r io.Reader) (map[string]*x509.Certificate, error) { |
| 116 | + m := make(map[string]string) |
| 117 | + dec := json.NewDecoder(r) |
| 118 | + if err := dec.Decode(&m); err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + certs := make(map[string]*x509.Certificate) |
| 122 | + for k, v := range m { |
| 123 | + block, _ := pem.Decode([]byte(v)) |
| 124 | + c, err := x509.ParseCertificate(block.Bytes) |
| 125 | + if err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + certs[k] = c |
| 129 | + } |
| 130 | + return certs, nil |
| 131 | +} |
| 132 | + |
| 133 | +// cacheTime extracts the cache time from the HTTP response header. |
| 134 | +// A default cache time is returned if extraction fails. |
| 135 | +func cacheTime(resp *http.Response) time.Duration { |
| 136 | + cc := strings.Split(resp.Header.Get("Cache-Control"), ",") |
| 137 | + const maxAge = "max-age=" |
| 138 | + for _, c := range cc { |
| 139 | + c = strings.TrimSpace(c) |
| 140 | + if strings.HasPrefix(c, maxAge) { |
| 141 | + if d, err := strconv.Atoi(c[len(maxAge):]); err == nil { |
| 142 | + return time.Duration(d) * time.Second |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + return defaultCertsCacheTime |
| 147 | +} |
0 commit comments