-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathra.go
52 lines (45 loc) · 1.18 KB
/
ra.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
package certify
import (
"errors"
"log"
"net/http"
"strings"
)
type RegistrationAuthority struct {
CA *CertificateAuthority `ca`
}
func NewRegistrationAuthority() (*RegistrationAuthority, error) {
ca, err := NewCertificateAuthority()
if err != nil {
return nil, err
}
return &RegistrationAuthority{CA: ca}, nil
}
func (RA *RegistrationAuthority) IssueCertificate(r *http.Request, cn string) (*Certificate, error) {
err := r.ParseForm()
if err != nil {
return nil, err
}
fqdn := r.PostForm.Get("fqdn")
authorizedDomains, err := RA.getAuthorizedDomains(cn)
if err != nil {
return nil, err
}
for _, domain := range authorizedDomains {
if strings.HasSuffix(fqdn, domain) {
log.Printf("Issuing certificate for CN: %s", fqdn)
csr := NewCertificateRequest(fqdn, "certify")
cert, err := RA.CA.SignCertificateRequest(csr)
if err != nil {
log.Fatalf("Failed to sign CSR: %s\n", err)
return nil, err
}
return cert, nil
}
}
return nil, errors.New("Not authorized for domain.")
}
func (RA *RegistrationAuthority) getAuthorizedDomains(cn string) ([]string, error) {
// TODO(tdaniels): fetch list of authorized domains
return []string{"foobar.com"}, nil
}