一, 注册用户的具体实现
用户的操作,依赖于Fabric CAClient:https://fabric-sdk-node.github.io/FabricCAClient.html。
try {
var client = await getClientForOrg(userOrg); // 根据netwrok config中的证书hu 来组建org的client logger.debug('Successfully initialized the credential stores'); // client can now act as an agent for organization Org1 // first check to see if the user is already enrolled var user = await client.getUserContext(username, true); if (user && user.isEnrolled()) { logger.info('Successfully loaded member from persistence'); } else { // user was not enrolled, so we will need an admin user object to register logger.info('User %s was not enrolled, so we will need an admin user object to register',username); var admins = hfc.getConfigSetting('admins'); let adminUserObj = await client.setUserContext({username: admins[0].username, password: admins[0].secret}); let caClient = client.getCertificateAuthority(); //获取ca let secret = await caClient.register({ //注册 enrollmentID: username, affiliation: userOrg.toLowerCase() + '.department1' }, adminUserObj); logger.debug('Successfully got the secret for user %s',username); user = await client.setUserContext({username:username, password:secret}); logger.debug('Successfully enrolled username %s and setUserContext on the client object', username); } if(user && user.isEnrolled) { if (isJson && isJson === true) { var response = { success: true, secret: user._enrollmentSecret, message: username + ' enrolled Successfully', }; return response; } } else { throw new Error('User was not enrolled '); } } catch(error) { logger.error('Failed to get registered user: %s with error: %s', username, error.toString()); return 'failed '+error.toString(); }
二, API和路由
app.post('/users', async function(req, res) { var username = req.body.username; var orgName = req.body.orgName; logger.debug('End point : /users'); logger.debug('User name : ' + username); logger.debug('Org name : ' + orgName); if (!username) { res.json(getErrorMessage('\'username\'')); return; } if (!orgName) { res.json(getErrorMessage('\'orgName\'')); return; } var token = jwt.sign({ exp: Math.floor(Date.now() / 1000) + parseInt(hfc.getConfigSetting('jwt_expiretime')),//根据config js设置token过期时间 username: username, orgName: orgName }, app.get('secret')); let response = await helper.getRegisteredUser(username, orgName, true); logger.debug('-- returned from registering the username %s for organization %s',username,orgName); if (response && typeof response !== 'string') { logger.debug('Successfully registered the username %s for organization %s',username,orgName); response.token = token; res.json(response); } else { logger.debug('Failed to register the username %s for organization %s with::%s',username,orgName,response); res.json({success: false, message: response}); } });
三,调用
echo "POST request Enroll on Org1 ..." echo ORG1_TOKEN=$(curl -s -X POST \ http://localhost:4000/users \ -H "content-type: application/x-www-form-urlencoded" \ -d 'username=Jim&orgName=Org1') echo $ORG1_TOKEN ORG1_TOKEN=$(echo $ORG1_TOKEN | jq ".token" | sed "s/\"//g") echo
返回结果:
ORG1 token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjY0OTMwMDUsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Ik9yZzEiLCJpYXQiOjE1MjY0NTcwMDV9.6jia-jwTOyYKjozUAe6ICLDjR3U_VI6yKsgVilQ3zQU
之后都要使用这个token来调用API