This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
health_authority.go
113 lines (99 loc) · 3.3 KB
/
health_authority.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
// Copyright 2020 the Exposure Notifications Server authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package model is a model abstraction of health authorities.
package model
import (
"crypto/ecdsa"
"errors"
"fmt"
"time"
"github.com/google/exposure-notifications-server/internal/project"
"github.com/google/exposure-notifications-server/pkg/keys"
)
// HealthAuthority represents a public health authority that is authorized to
// issue diagnosis verification certificates accepted by this server.
type HealthAuthority struct {
ID int64
Issuer string
Audience string
Name string
Keys []*HealthAuthorityKey
JwksURI *string
EnableStatsAPI bool
}
// JWKSEnabled returns true if JWKS discovery is enabled for this health authority.
func (ha *HealthAuthority) JWKSEnabled() bool {
return !(ha.JwksURI == nil || len(*ha.JwksURI) == 0)
}
// SetJWKS sets the optional JwksURI property of the HealthAuthority.
func (ha *HealthAuthority) SetJWKS(uri string) {
uri = project.TrimSpaceAndNonPrintable(uri)
if uri == "" {
ha.JwksURI = nil
return
}
ha.JwksURI = &uri
}
// Validate returns an error if the HealthAuthority struct is not valid.
func (ha *HealthAuthority) Validate() error {
if ha.Issuer == "" {
return errors.New("issuer cannot be empty")
}
if ha.Audience == "" {
return errors.New("audience cannot be empty")
}
if ha.Name == "" {
return errors.New("name cannot be empty")
}
return nil
}
// HealthAuthorityKey represents a public key version for a given health authority.
type HealthAuthorityKey struct {
AuthorityID int64
Version string
From time.Time
Thru time.Time
PublicKeyPEM string
}
// Validate returns an error if the HealthAuthorityKey is not valid.
func (k *HealthAuthorityKey) Validate() error {
if _, err := k.PublicKey(); err != nil {
return fmt.Errorf("invalid public key PEM block: %w", err)
}
return nil
}
func (k *HealthAuthorityKey) IsFuture() bool {
return k.From.After(time.Now())
}
// IsValid returns true if the key is valid based on the current time.
func (k *HealthAuthorityKey) IsValid() bool {
return k.IsValidAt(time.Now())
}
// IsValidAt returns true if the key is valid at a specific point in time.
func (k *HealthAuthorityKey) IsValidAt(t time.Time) bool {
return t.After(k.From) && (k.Thru.IsZero() || k.Thru.After(t))
}
// Revoke revokes a key.
func (k *HealthAuthorityKey) Revoke() {
k.Thru = time.Now().UTC()
if !k.Thru.After(k.From) {
// make it so that the key doesn't expire before it is active.
k.Thru = k.From
}
}
// PublicKey decodes the PublicKeyPEM text and returns the `*ecdsa.PublicKey`
// This system only supports verifying ECDSA JWTs, `alg: ES256`.
func (k *HealthAuthorityKey) PublicKey() (*ecdsa.PublicKey, error) {
return keys.ParseECDSAPublicKey(k.PublicKeyPEM)
}