注册用户

一, 注册用户的具体实现

用户的操作,依赖于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

转载于:https://www.cnblogs.com/13579net/p/9046491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值