forked from hyperledger-labs/cckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_after.go
37 lines (31 loc) · 1.11 KB
/
middleware_after.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
package encryption
import (
"github.com/hyperledger-labs/cckit/router"
)
func encryptIfEncodeKeyExists(encryptionRequired bool) router.MiddlewareFunc {
return func(pre router.HandlerFunc, pos ...int) router.HandlerFunc {
return func(ctx router.Context) (interface{}, error) {
res, err := pre(ctx)
// encrypt response only if method in Invoked, not queried
if err != nil || ctx.Handler().Type != router.MethodInvoke {
return res, err
}
if _, err = KeyFromTransient(ctx); err != nil {
if err == ErrKeyNotDefinedInTransientMap && !encryptionRequired {
return res, nil
}
return nil, err
}
return EncryptWithTransientKey(ctx, res)
}
}
}
// EncryptInvokeResponse returns middleware function for encrypt chaincode invocation response
func EncryptInvokeResponse() router.MiddlewareFunc {
return encryptIfEncodeKeyExists(true)
}
// EncryptInvokeResponseIfKeyProvided returns middleware function for encrypt chaincode invocation response if
// encryption key provided in transient map
func EncryptInvokeResponseIfKeyProvided() router.MiddlewareFunc {
return encryptIfEncodeKeyExists(false)
}