前言
上一篇文章我們已經(jīng)啟動了fabric網(wǎng)絡(luò)/node服務(wù)匙赞,這篇文章主要來詳細(xì)分析用戶注冊的過程梨熙。
具體實現(xiàn)
找到app.js
中關(guān)于users的路由:
//注冊用戶
app.post('/users', function(req, res) {
// 1 參數(shù)校驗
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;
}
// 2 生成jwt 添加用戶名以及組織信息
var token = jwt.sign({
exp: Math.floor(Date.now() / 1000) + parseInt(hfc.getConfigSetting('jwt_expiretime')),
username: username,
orgName: orgName
}, app.get('secret'));
// 3 注冊用戶 如果成功連同jwt一起返回
helper.getRegisteredUsers(username, orgName, true).then(function(response) {
if (response && typeof response !== 'string') {
response.token = token;
res.json(response);
} else {
res.json({
success: false,
message: response
});
}
});
});
從上面得知要看具體實現(xiàn),還需要進入helper.js
中查看getRegisteredUsers()
:
var getRegisteredUsers = function(username, userOrg, isJson) {
var member;
var client = getClientForOrg(userOrg);
var enrollmentSecret = null;
// 設(shè)置公私鑰存儲的地方
return hfc.newDefaultKeyValueStore({
// 這里只要求path參數(shù) 來作為存儲的地方
path: getKeyStoreForOrg(getOrgName(userOrg))
}).then((store) => {
// 設(shè)置狀態(tài)持久化存儲(如(證書艇搀,私鑰缴挖。繁成。))
client.setStateStore(store);
// clearing the user context before switching
client._userContext = null;
//1 獲取用戶信息 (從 內(nèi)存-->statestore 依次查找)
return client.getUserContext(username, true).then((user) => {
if (user && user.isEnrolled()) {
// 從持久化中 成功加載此 user
logger.info('Successfully loaded member from persistence');
return user;
} else {
let caClient = caClients[userOrg];
//2 獲得adminuser 來注冊賬戶
return getAdminUser(userOrg).then(function(adminUserObj) {
member = adminUserObj;
return caClient.register({
enrollmentID: username,
// 與此用戶關(guān)聯(lián)的隸屬關(guān)系
affiliation: userOrg + '.department1'
}, member); // 注冊員
}).then((secret) => {
enrollmentSecret = secret;
logger.debug(username + ' registered successfully');
//3 注冊成功后 拿著secret 來enroll
return caClient.enroll({
enrollmentID: username,
enrollmentSecret: secret
});
}, (err) => {
logger.debug(username + ' failed to register');
return '' + err;
//return 'Failed to register '+username+'. Error: ' + err.stack ? err.stack : err;
}).then((message) => { // enroll 返回的信息
if (message && typeof message === 'string' && message.includes(
'Error:')) {
logger.error(username + ' enrollment failed');
return message;
}
logger.debug(username + ' enrolled successfully');
// 構(gòu)建user對象
member = new User(username);
member._enrollmentSecret = enrollmentSecret;
//4 設(shè)置此User實例的注冊對象 來持久化在client中
return member.setEnrollment(message.key, message.certificate, getMspID(userOrg));
}).then(() => {
//5 保存用戶的上下文環(huán)境(e.g 證書 私鑰 ) 用戶簽名 各種請求
client.setUserContext(member);
return member;
}, (err) => {
logger.error(util.format('%s enroll failed: %s', username, err.stack ? err.stack : err));
return '' + err;
});;
}
});
}).then((user) => {
// 6 返回用戶信息
if (isJson && isJson === true) {
var response = {
success: true,
secret: user._enrollmentSecret,
message: username + ' enrolled Successfully',
};
return response;
}
return user;
}, (err) => {
logger.error(util.format('Failed to get registered user: %s, error: %s', username, err.stack ? err.stack : err));
return '' + err;
});
};
基本流程 :
API訪問
為org1
組織建立一個用戶名為Jim
的用戶。在命令行里輸入:
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
控制面板打印結(jié)果:
POST request Enroll on Org1 ...
{"success":true,"secret":"xDfluXIPURbu","message":"Jim enrolled Successfully","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDgxNTkyMjQsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE1MDgxMjMyMjR9.rE5UuViSzmDcdHNZRXt22izpopY5eVkJ7YbfXILjSQk"}
ORG1 token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDgxNTkyMjQsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE1MDgxMjMyMjR9.rE5UuViSzmDcdHNZRXt22izpopY5eVkJ7YbfXILjSQk
// 上面的token是 訪問后面API的必要參數(shù)
可以看出 Jim
已經(jīng)成功被注冊了己儒,其公私鑰存放在/tmp/fabric-client-kvs_peerOrg1
中(可在config.json
中自定義keyValueStore
路徑):
node服務(wù)后臺打悠槠瘛:
[2017-10-16 11:07:04.222] [DEBUG] SampleWebApp - End point : /users
[2017-10-16 11:07:04.222] [DEBUG] SampleWebApp - User name : Jim
[2017-10-16 11:07:04.222] [DEBUG] SampleWebApp - Org name : org1
[2017-10-16 11:07:04.226] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:04.228] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- getValue
[2017-10-16 11:07:04.230] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:04.231] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- getValue
//----->公私鑰存放路徑
[2017-10-16 11:07:04.310] [DEBUG] Helper - [utils.CryptoKeyStore]: This class requires a CryptoKeyStore to save keys, using the store: {"opts":{"path":"/tmp/fabric-client-kvs_peerOrg1"}}
[2017-10-16 11:07:04.311] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:04.311] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore returning ks
[2017-10-16 11:07:04.312] [DEBUG] Helper - [crypto_ecdsa_aes]: generateKey, store.setValue
[2017-10-16 11:07:04.313] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 5f8df5637db9f5e75af2990de09b57214b1eefcf834002cd79e6e7a634a23b68
[2017-10-16 11:07:04.313] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: bc8ce6511008bff20bb18ad6f105af889eb3c880cd2275cd97184d3378c47751
[2017-10-16 11:07:04.319] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
//----->獲取admin
[2017-10-16 11:07:04.715] [INFO] Helper - Successfully enrolled user 'admin'
[2017-10-16 11:07:04.716] [DEBUG] Helper - Msp ID : Org1MSP
[2017-10-16 11:07:04.717] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start
[2017-10-16 11:07:04.721] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular]
[2017-10-16 11:07:04.722] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store
[2017-10-16 11:07:04.723] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 5f8df5637db9f5e75af2990de09b57214b1eefcf834002cd79e6e7a634a23b68
[2017-10-16 11:07:04.723] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: bc8ce6511008bff20bb18ad6f105af889eb3c880cd2275cd97184d3378c47751
[2017-10-16 11:07:04.724] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:04.726] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 5f8df5637db9f5e75af2990de09b57214b1eefcf834002cd79e6e7a634a23b68
[2017-10-16 11:07:04.726] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: bc8ce6511008bff20bb18ad6f105af889eb3c880cd2275cd97184d3378c47751
[2017-10-16 11:07:04.727] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 5f8df5637db9f5e75af2990de09b57214b1eefcf834002cd79e6e7a634a23b68
[2017-10-16 11:07:04.727] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: bc8ce6511008bff20bb18ad6f105af889eb3c880cd2275cd97184d3378c47751
[2017-10-16 11:07:04.728] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 5f8df5637db9f5e75af2990de09b57214b1eefcf834002cd79e6e7a634a23b68
[2017-10-16 11:07:04.728] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: bc8ce6511008bff20bb18ad6f105af889eb3c880cd2275cd97184d3378c47751
[2017-10-16 11:07:04.728] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:04.743] [DEBUG] Helper - [crypto_ecdsa_aes]: ecdsa signature: Signature {
r: <BN: f1034e3347d230585a94f1ea21318d6217a20774754798641f599e33b8a9571f>,
s: <BN: 5e4d05280fe82c2bbe93eb0e8fefb8998bacf160b6422e370b561c109b1c0ef2>,
recoveryParam: 0 }
//-----> Jim register 成功
[2017-10-16 11:07:04.950] [DEBUG] Helper - Jim registered successfully
[2017-10-16 11:07:05.033] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store
[2017-10-16 11:07:05.034] [DEBUG] Helper - [crypto_ecdsa_aes]: generateKey, store.setValue
[2017-10-16 11:07:05.034] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:05.034] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
//-----> Jim enrolled 成功
[2017-10-16 11:07:05.391] [DEBUG] Helper - Jim enrolled successfully
[2017-10-16 11:07:05.391] [DEBUG] Helper - Msp ID : Org1MSP
[2017-10-16 11:07:05.392] [DEBUG] Helper - [crypto_ecdsa_aes]: constructor, keySize: 256
[2017-10-16 11:07:05.392] [DEBUG] Helper - [crypto_ecdsa_aes]: Hash algorithm: SHA2, hash output size: 256
[2017-10-16 11:07:05.392] [DEBUG] Helper - [utils.CryptoKeyStore]: CryptoKeyStore, constructor - start
[2017-10-16 11:07:05.393] [DEBUG] Helper - [utils.CryptoKeyStore]: constructor, no super class specified, using config: fabric-client/lib/impl/FileKeyValueStore.js
[2017-10-16 11:07:05.393] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start
[2017-10-16 11:07:05.393] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular]
[2017-10-16 11:07:05.393] [DEBUG] Helper - [utils.CryptoKeyStore]: This class requires a CryptoKeyStore to save keys, using the store: {"opts":{"path":"/home/jiang/.hfc-key-store"}}
[2017-10-16 11:07:05.393] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:05.394] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore returning ks
[2017-10-16 11:07:05.394] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:05.394] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:05.395] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:05.396] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:05.397] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:05.398] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:05.398] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:05.399] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:05.400] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:05.400] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue