This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_confirm_nonce.go
59 lines (51 loc) · 1.64 KB
/
main_confirm_nonce.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 main
import (
"html/template"
"log"
"net/http"
"strings"
"github.com/TNG/openpgp-validation-server/validator"
)
var (
confirmAcceptedResponse = template.Must(template.ParseFiles("./templates/confirmAccepted.gohtml"))
confirmErrorResponse = template.Must(template.ParseFiles("./templates/confirmError.gohtml"))
)
func serveNonceConfirmer(address string) error {
nonceChan := make(chan [validator.NonceLength]byte)
go func() {
for {
nonce := <-nonceChan
go handleNonceConfirmation(nonce)
}
}()
http.HandleFunc("/confirm/", func(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
nonce, err := validator.NonceFromString(parts[2])
if err != nil {
log.Printf("BAD REQUEST from %v to %v: %v\n", r.RemoteAddr, r.RequestURI, err)
w.WriteHeader(http.StatusBadRequest)
if err := confirmErrorResponse.Execute(w, struct{}{}); err != nil {
log.Panicf("Cannot execute template 'error': %v\n", err)
}
return
}
log.Printf("ACCEPTED from %v to %v\n", r.RemoteAddr, r.RequestURI)
w.WriteHeader(http.StatusAccepted)
if err := confirmAcceptedResponse.Execute(w, struct{}{}); err != nil {
log.Panicf("Cannot execute template 'accepted': %v\n", err)
}
nonceChan <- nonce
})
return http.ListenAndServe(address, nil)
}
func handleNonceConfirmation(nonce [validator.NonceLength]byte) {
responseMail, err := validator.ConfirmNonce(nonce, store, gpgUtil)
if err != nil {
log.Printf("Cannot confirm nonce: %v\n", err)
return
}
if sendOutgoingMail("signature", responseMail) {
log.Printf("Deleting nonce %v after signed key has been sent successfully.", nonce)
store.Delete(nonce)
}
}