-
Notifications
You must be signed in to change notification settings - Fork 2
/
verification_service.go
75 lines (58 loc) · 1.95 KB
/
verification_service.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
package http_server
import (
"context"
"net/http"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
type Verify func(ctx context.Context, hash []byte) HTTPResponse
type VerifyOffline func(upp []byte, hash []byte) HTTPResponse
type VerificationService struct {
Verify
VerifyOffline
}
// HandleVerificationRequest unpacks an incoming HTTP request and calls either Verify or VerifyOffline depending
// on the endpoint the request was received at.
//
// There are online and offline verification endpoints, as well as endpoints for direct hash injection and JSON
// data packages. For that reason, the function is nested in a way that it can be passed to the AddServiceEndpoint
// function with the following signature:
// func (srv *HTTPServer) AddServiceEndpoint(endpointPath string, handle func(offline bool, isHash bool) http.HandlerFunc, supportOffline bool)
func (s *VerificationService) HandleVerificationRequest(offline, isHashRequest bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var resp HTTPResponse
if offline {
upp, hash, err := unpackOfflineVerificationRequest(r, isHashRequest)
if err != nil {
ClientError(uuid.Nil, r, w, err.Error(), http.StatusBadRequest)
return
}
resp = s.VerifyOffline(upp, hash[:])
} else {
hash, err := GetHash(r, isHashRequest)
if err != nil {
ClientError(uuid.Nil, r, w, err.Error(), http.StatusBadRequest)
return
}
resp = s.Verify(ctx, hash[:])
}
select {
case <-ctx.Done():
log.Warnf("verification response could not be sent: http request %s", ctx.Err())
default:
SendResponse(w, resp)
}
}
}
func unpackOfflineVerificationRequest(r *http.Request, isHashRequest bool) (upp []byte, hash Sha256Sum, err error) {
upp, err = getUPP(r.Header)
if err != nil {
return nil, Sha256Sum{}, err
}
hash, err = GetHash(r, isHashRequest)
if err != nil {
return nil, Sha256Sum{}, err
}
return upp, hash, nil
}