-
Notifications
You must be signed in to change notification settings - Fork 1
/
certificates_resource.go
61 lines (55 loc) · 1.72 KB
/
certificates_resource.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
package server
import (
"net/http"
"github.com/leanovate/microzon-auth-go/certificates"
"github.com/leanovate/microzon-auth-go/logging"
"github.com/untoldwind/routing"
)
type certificatesResource struct {
certificateManager *certificates.CertificateValidator
logger logging.Logger
}
func CertificatesRoutes(certificateManager *certificates.CertificateValidator, parent logging.Logger) routing.Matcher {
logger := parent.WithContext(map[string]interface{}{"resource": "certificates"})
resource := &certificatesResource{
certificateManager: certificateManager,
logger: logger,
}
return routing.PrefixSeq("/certificates",
routing.EndSeq(
routing.GETFunc(wrap(resource.logger, resource.QueryCertificates)),
SendError(logger, MethodNotAllowed()),
),
routing.StringPart(
func(x5t string) routing.Matcher {
return routing.EndSeq(
routing.GETFunc(wrap(resource.logger, resource.GetCertByThumbprint(x5t))),
SendError(logger, MethodNotAllowed()),
)
},
),
)
}
func (r *certificatesResource) QueryCertificates(req *http.Request) (interface{}, error) {
certs, err := r.certificateManager.ListAllCertificates()
if err != nil {
return nil, err
}
result := make([]*certificates.CertificateVO, 0, len(certs))
for _, cert := range certs {
result = append(result, certificates.NewCertificateVO(cert))
}
return result, nil
}
func (r *certificatesResource) GetCertByThumbprint(x5t string) func(req *http.Request) (interface{}, error) {
return func(req *http.Request) (interface{}, error) {
cert, err := r.certificateManager.FindCertificate(x5t)
if err != nil {
return nil, err
}
if cert == nil {
return nil, NotFound()
}
return certificates.NewCertificateVO(cert), nil
}
}