HyperLedger Fabric 中的授权
Authorization in HyperLedger Fabric
在基于 Hyperledger Fabric 的应用程序中实现授权的最佳方式是什么?
从这里考虑 Marbles 演示用例:https://github.com/IBM-Blockchain/marbles
How/Where 我应该实现以下功能吗?
- 只有管理员用户才能创建和分配新弹珠
- 应该只允许用户(本例中的 Amy、Alice、Ava)转出他们拥有的弹珠
可能您需要考虑利用 GetCreator
API 来提取创建交易建议的客户端的证书。一旦获得证书,您就可以实现所需的功能,例如:
Only admin user be able to create and allocate new marbles
Users (Amy, Alice, Ava from this example) should be only allowed to transfer out the marbles that they own
这里是如何在链代码中反序列化证书的例子:
func (*smartContract) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
fmt.Println("Invoke")
serializedID, _ := stub.GetCreator()
sId := &msp.SerializedIdentity{}
err := proto.Unmarshal(serializedID, sId)
if err != nil {
return shim.Error(fmt.Sprintf("Could not deserialize a SerializedIdentity, err %s", err))
}
bl, _ := pem.Decode(sId.IdBytes)
if bl == nil {
return shim.Error(fmt.Sprintf("Could not decode the PEM structure"))
}
cert, err := x509.ParseCertificate(bl.Bytes)
if err != nil {
return shim.Error(fmt.Sprintf("ParseCertificate failed %s", err))
}
fmt.Println(cert)
return shim.Success(nil)
}
在基于 Hyperledger Fabric 的应用程序中实现授权的最佳方式是什么?
从这里考虑 Marbles 演示用例:https://github.com/IBM-Blockchain/marbles
How/Where 我应该实现以下功能吗?
- 只有管理员用户才能创建和分配新弹珠
- 应该只允许用户(本例中的 Amy、Alice、Ava)转出他们拥有的弹珠
可能您需要考虑利用 GetCreator
API 来提取创建交易建议的客户端的证书。一旦获得证书,您就可以实现所需的功能,例如:
Only admin user be able to create and allocate new marbles
Users (Amy, Alice, Ava from this example) should be only allowed to transfer out the marbles that they own
这里是如何在链代码中反序列化证书的例子:
func (*smartContract) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
fmt.Println("Invoke")
serializedID, _ := stub.GetCreator()
sId := &msp.SerializedIdentity{}
err := proto.Unmarshal(serializedID, sId)
if err != nil {
return shim.Error(fmt.Sprintf("Could not deserialize a SerializedIdentity, err %s", err))
}
bl, _ := pem.Decode(sId.IdBytes)
if bl == nil {
return shim.Error(fmt.Sprintf("Could not decode the PEM structure"))
}
cert, err := x509.ParseCertificate(bl.Bytes)
if err != nil {
return shim.Error(fmt.Sprintf("ParseCertificate failed %s", err))
}
fmt.Println(cert)
return shim.Success(nil)
}