github.com/s7techlab/cckit@v0.10.5/identity/entry.go (about)

     1  // Package access contains structs for storing chaincode access control information
     2  package identity
     3  
     4  import (
     5  	"crypto/x509"
     6  
     7  	"github.com/hyperledger/fabric-chaincode-go/shim"
     8  	protomsp "github.com/hyperledger/fabric-protos-go/msp"
     9  )
    10  
    11  // Entry structure for storing identity information
    12  // string representation certificate Subject and Issuer can be used for reach query searching
    13  type Entry struct {
    14  	MSPId   string
    15  	Subject string
    16  	Issuer  string
    17  	PEM     []byte
    18  	Cert    *x509.Certificate `json:"-"` // temporary cert
    19  }
    20  
    21  // Id structure defines short id representation
    22  type Id struct {
    23  	MSP  string
    24  	Cert string
    25  }
    26  
    27  // IdentityEntry interface
    28  type IdentityEntry interface {
    29  	GetIdentityEntry() Entry
    30  }
    31  
    32  // ========  Identity interface ===================
    33  
    34  // GetID identifier by certificate subject and issuer
    35  func (e Entry) GetID() string {
    36  	return ID(e.Subject, e.Issuer)
    37  }
    38  
    39  // Deprecated: use GetMSPIdentifier
    40  // GetMSPID membership service provider identifier
    41  func (e Entry) GetMSPID() string {
    42  	return e.MSPId
    43  }
    44  
    45  func (e Entry) GetMSPIdentifier() string {
    46  	return e.MSPId
    47  }
    48  
    49  // GetSubject certificate subject
    50  func (e Entry) GetSubject() string {
    51  	return e.Subject
    52  }
    53  
    54  // GetIssuer certificate issuer
    55  func (e Entry) GetIssuer() string {
    56  	return e.Issuer
    57  }
    58  
    59  // GetPK certificate issuer
    60  func (e Entry) GetPEM() []byte {
    61  	return e.PEM
    62  }
    63  
    64  func (e Entry) GetPublicKey() interface{} {
    65  
    66  	if e.Cert == nil {
    67  		cert, err := Certificate(e.PEM)
    68  
    69  		if err != nil {
    70  			return err
    71  		}
    72  
    73  		e.Cert = cert
    74  	}
    75  
    76  	return e.Cert.PublicKey
    77  }
    78  
    79  // Is checks IdentityEntry is equal to an other Identity
    80  func (e Entry) Is(id Identity) bool {
    81  	return e.MSPId == id.GetMSPIdentifier() && e.Subject == id.GetSubject()
    82  }
    83  
    84  //func (e Entry) FromBytes(bb []byte) (interface{}, error) {
    85  //	entry := new(Entry)
    86  //	err := json.Unmarshal(bb, entry)
    87  //	return entry, err
    88  //}
    89  
    90  func (e Entry) GetIdentityEntry() Entry {
    91  	return e
    92  }
    93  
    94  // CreateEntry creates IdentityEntry structure from an identity interface
    95  func CreateEntry(i Identity) (g *Entry, err error) {
    96  	return &Entry{
    97  		MSPId:   i.GetMSPIdentifier(),
    98  		Subject: i.GetSubject(),
    99  		Issuer:  i.GetIssuer(),
   100  		PEM:     i.GetPEM(),
   101  	}, nil
   102  }
   103  
   104  func EntryFromStub(stub shim.ChaincodeStubInterface) (g *Entry, err error) {
   105  	id, err := FromStub(stub)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	return CreateEntry(id)
   110  }
   111  
   112  // EntryFromSerialized creates Entry from SerializedEntry
   113  func EntryFromSerialized(s protomsp.SerializedIdentity) (g *Entry, err error) {
   114  	id, err := FromSerialized(s)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	return CreateEntry(id)
   119  }