forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
require_cert.go
40 lines (34 loc) · 853 Bytes
/
require_cert.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/
package middleware
import (
"net/http"
)
type requireCert struct {
next http.Handler
}
// RequireCert is used to ensure that a verified TLS client certificate was
// used for authentication.
func RequireCert() Middleware {
return func(next http.Handler) http.Handler {
return &requireCert{next: next}
}
}
func (r *requireCert) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch {
case req.TLS == nil:
fallthrough
case len(req.TLS.VerifiedChains) == 0:
fallthrough
case len(req.TLS.VerifiedChains[0]) == 0:
w.WriteHeader(http.StatusUnauthorized)
default:
r.next.ServeHTTP(w, req)
}
}