-
Notifications
You must be signed in to change notification settings - Fork 0
/
certifier.go
59 lines (51 loc) · 1.2 KB
/
certifier.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
package certify
import (
"net/http"
"strings"
)
type Certifier struct {
RA *RegistrationAuthority
}
func NewCertifier() (*Certifier, error) {
// TODO(tdaniels): use existing RA
ra, err := NewRegistrationAuthority()
if err != nil {
return nil, err
}
return &Certifier{RA: ra}, nil
}
func (c *Certifier) HandleRequest(w http.ResponseWriter, r *http.Request) {
// TODO(tdaniels): user model
cn := r.Header.Get("X-Certify-User")
uriElems := strings.Split(r.RequestURI, "/")
action := uriElems[len(uriElems)-1]
switch action {
case "new":
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
response, err := c.HandleNewCertificateRequest(cn, r)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(response)
return
default:
http.NotFound(w, r)
}
}
func (c *Certifier) HandleNewCertificateRequest(cn string, r *http.Request) ([]byte, error) {
cert, err := c.RA.IssueCertificate(r, cn)
if err != nil {
return nil, err
}
response, err := cert.Json()
if err != nil {
return nil, err
}
return response, nil
}