首先,我对超级账本非常陌生,可能在某些地方我想错了。
现在,我正在使用Hyperledger Fabric,Hyperledger Composer和hyperledger-rest-server开发的应用程序上工作。
现在,我阅读了一些有关身份验证过程的信息,但我只停留在一点。
在我提出问题之前,我先告诉您一些我想要的东西:
我想创建一个带有角度的前端应用程序,以便用户可以使用其用户名/密码登录。
我读到的是可以使用Passport.js进行hyperledger-rest-server身份验证。但是我了解的是,这是为了与Twitter,Facebook等第三方进行身份验证。
我的问题:
答案 0 :(得分:0)
是的,这是一种这样的选择,并且完全可行。您可以将passportjs
与composer-rest-server
结合使用,并将username
链接到唯一的composer business card
。这样,当用户登录时,它将从服务器获取令牌。随后的每个API调用都必须具有令牌,基于此令牌,其余服务器将使用所需的composer card
您首先需要follow this并使用composer-rest-server -c ADMIN_CARD_NAME
设置服务器。然后,您将需要为服务器激活身份验证。转到http://0.0.0.0:3000/explorer查看API,包括用于身份验证的API。
根据身份验证方法,您需要安装所需的软件包。 This tutorial是一个不错的起点。使用wallet import
端点,管理员可以将Facebook帐户或用户名映射到特定的composer card
。
现在,我的诚实观点是,composer-rest-server
不适合进行试生产。我使用了它,最终决定处理自己的API和身份验证。
async function createParticipant() {
const businessNetworkConnection = new BusinessNetworkConnection();
await businessNetworkConnection.connect(adminCardName);
let registry= await businessNetworkConnection.getParticipantRegistry(`${BASE_NS}.SampleUser`);
const factory = businessNetworkConnection.getBusinessNetwork().getFactory();
let user= factory.newResource(BASE_NS, 'SampleUser', 'username123');
await businessNetworkConnection.save(user);
// User is registered, time to import an identity
let creds = await businessNetworkConnection.issueIdentity(`${BASE_NS}.SampleUser#username123`, 'username123);
const adminConnection = new AdminConnection();
await adminConnection.connect(adminCardName);
const card = new IdCard({
userName: creds.userID,
version: 1,
enrollmentSecret: creds.userSecret,
businessNetwork: businessName
}, connectionProfile);
await adminConnection.importCard(`username123@my-network`, card);
await adminConnection.disconnect();
await businessNetworkConnection.disconnect();
}
composer card
。对于此示例,我使用了mongoDB。模式将是这样的let newUser = new userSchema({
username: 'Alice,
password: 'topSecretPassword,
emailId: 'foo@bar.com',
participantId: 'username123,
composerCard: `username123@my-network`
});
jsonwebtoken
,她必须在所有后续API调用中使用它。const jwt = require('jsonwebtoken');
app.post('/login', auth());
async function auth(req, res, next) {
let username = req.body.username;
let password = req.body.password;
// User whatever authentication method you want here and return a jwt
// In my project I created a new token db and kept a mapping
const authToken = jwt.sign(tokenSeed, jwtSecret, { });
const newToken = new tokenSchema({
authToken: authToken,
username: req.body.username
});
await newToken.save();
res.send({ authToken: authToken, username: username });
}
composer card
const chooseCard = async (req, res, next) => {
// Retrieve the token from the DB
let token = await tokenSchema.findOne({ authToken: req.headers['x-access-token'] });
// Get the user information
let user = await userSchema.findOne({ username: token.username });
let card = user.composerCard;
req.composerCardToUse = card;
next();
};
const someFancyTx = async (req, res, next) => {
let card = req.composerCardToUse;
await businessNetworkConnection.connect(card);
// And the transactions submitted with this instance will use the required card
}
app.post('/sendMoney', chooseCard, somefancyTx);
注意:我已经跳过了一些验证步骤(检查jwt的真实性,用户会话),并跳过了一些样本代码,这些代码会使答案变得不必要地冗长。我已经使用同一系统成功运行了飞行员和PoC。