Hojjat Jashnniloofar
Hojjat Jashnniloofar

Reputation: 145

How to add roles to user's certificate and use them in chaincode?

I develop an application on hyperledger fabric with "IBM‌ Blockchain Platform" extension and nodejs. I use "fabric-ca-client v1.3.0" module to enroll users and admins certificate. I want to add roles to users and have access control based on user roles on my chaincode. I try to enroll multiple certificate but always this module enroll certificate with empty roles and affiliation like this:

{"name":"admin","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"...","identity":{"..."}}}

I found this on this on hyperledger shim documentation about use of roles on chaincode:

const ClientIdentity = require('fabric-shim').ClientIdentity;

let cid = new ClientIdentity(stub); 
object passed to Init() and Invoke() methods
if (cid.assertAttributeValue('hf.role', 'auditor')) {
   // proceed to carry out auditing
}

but I didn't find any solution to how I can register and enroll user that have 'hf.role' attribute

Upvotes: 1

Views: 914

Answers (1)

SandeepR
SandeepR

Reputation: 146

you have to register user 1st and then enroll the user. You can do this via cmdline as well as REST. For cmdline while registering user you need to pass additional attributes as:

--id.attrs 'role=writer:ecert,[email protected]'

then at the time of enroll:

--enrollment.attrs "role=writer,email,phone:opt"

In the chaincode you can access the attribute as:

let cid = new ClientIdentity(stub); 
if (cid.assertAttributeValue('role', 'writer')) { .. }

Upvotes: 1

Related Questions