github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/cauthdsl/policy.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cauthdsl 18 19 import ( 20 "errors" 21 "fmt" 22 23 "github.com/hyperledger/fabric/common/policies" 24 cb "github.com/hyperledger/fabric/protos/common" 25 26 "github.com/golang/protobuf/proto" 27 "github.com/hyperledger/fabric/msp" 28 ) 29 30 type provider struct { 31 deserializer msp.IdentityDeserializer 32 } 33 34 // NewProviderImpl provides a policy generator for cauthdsl type policies 35 func NewPolicyProvider(deserializer msp.IdentityDeserializer) policies.Provider { 36 return &provider{ 37 deserializer: deserializer, 38 } 39 } 40 41 // NewPolicy creates a new policy based on the policy bytes 42 func (pr *provider) NewPolicy(data []byte) (policies.Policy, proto.Message, error) { 43 sigPolicy := &cb.SignaturePolicyEnvelope{} 44 if err := proto.Unmarshal(data, sigPolicy); err != nil { 45 return nil, nil, fmt.Errorf("Error unmarshaling to SignaturePolicy: %s", err) 46 } 47 48 if sigPolicy.Version != 0 { 49 return nil, nil, fmt.Errorf("This evaluator only understands messages of version 0, but version was %d", sigPolicy.Version) 50 } 51 52 compiled, err := compile(sigPolicy.Policy, sigPolicy.Identities, pr.deserializer) 53 if err != nil { 54 return nil, nil, err 55 } 56 57 return &policy{ 58 evaluator: compiled, 59 }, sigPolicy, nil 60 61 } 62 63 type policy struct { 64 evaluator func([]*cb.SignedData, []bool) bool 65 } 66 67 // Evaluate takes a set of SignedData and evaluates whether this set of signatures satisfies the policy 68 func (p *policy) Evaluate(signatureSet []*cb.SignedData) error { 69 if p == nil { 70 return fmt.Errorf("No such policy") 71 } 72 73 ok := p.evaluator(signatureSet, make([]bool, len(signatureSet))) 74 if !ok { 75 return errors.New("Failed to authenticate policy") 76 } 77 return nil 78 }