Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'esversion: 8';
- 'use strict';
- async function FetchUserWallet (Username)
- {
- const walletPath = path.join(process.cwd(), 'wallet');
- const wallet = new FileSystemWallet(walletPath);
- let appAdmin = "FIXME";
- if ( await wallet.exists(Username) )
- return [undefined, "An identity for the ${Username} already exists in the wallet"];
- if (!await wallet.exists(appAdmin))
- return [undefined, "Am admin identity is not registered . please register admin first"];
- return [wallet, undefined];
- }
- async function connectToGateway(wallet)
- {
- const gateway = new Gateway();
- let ccp = "FIXME";
- let appAdmin = "FIXME";
- await gateway.connect(ccp, {
- wallet,
- identity: appAdmin,
- discovery: {
- enabled: false,
- asLocalhost: true
- }
- });
- return gateway;
- }
- function BadRegistration(code, error)
- {
- let response = {};
- response.data = null;
- response.httpstatus = code;
- response.message = error;
- return response;
- }
- async function FetchNameFromDFARM (config, roleid) {
- let query = { RoleId: roleid };
- MongoClient.connect(config.Database.DFARM.connectString, function (err, client) {
- if (err)
- return [ undefined, "Error connecting to DFARM database: " + err ];
- client
- .db(config.Database.DFARM.dbName)
- .collection("Role")
- .find( query )
- // I don't know, this seems to be a strange construct
- // Are you sure that's correct?
- .toArray( function (err, docs) {
- if (err)
- return [ undefined, "Error with DB :" + err ];
- return [docs[0].name, undefined];
- });
- //client.close();
- });
- }
- async function DoRegistration(Username, name, wallet, ca, adminIdentity)
- {
- const secret = await ca.register({
- enrollmentID: Username,
- role: 'client',
- attrs: [{name: "approle", value: name, ecert: true }]
- }, adminIdentity);
- console.log('secret', secret); // return secret;
- const enrollment = await ca.enroll({
- enrollmentID: Username,enrollmentSecret: secret
- });
- // console.log('enrollment', enrollment);
- const userIdentity = X509WalletMixin.createIdentity('DfarmadminMSP', enrollment.certificate, enrollment.key.toBytes());
- // console.log("userIdentity", userIdentity)
- await wallet.import(Username, userIdentity);
- console.log(`Successfully enrolled user ${Username} and imported it into the wallet`);
- return [secret, userIdentity];
- }
- async function RegisterUser(Username, roleid, callback)
- {
- try
- {
- var fs = require('fs');
- var config = JSON.parse(fs.readFileSync('./config/Config.json', 'utf8'));
- const [wallet, wallet_error] = await FetchUserWallet( Username );
- if ( !wallet )
- return BadRegistration( 400, wallet_error );
- let gateway = await connectToGateway(wallet);
- if ( !gateway )
- return BadRegistration( 502, "Bad Gateway" );
- let [name, db_error] = await FetchNameFromDFARM( config, roleid );
- if ( !name )
- return BadRegistration( 500, db_error );
- const ca = gateway.getClient().getCertificateAuthority();
- const adminIdentity = gateway.getCurrentIdentity();
- if ( adminIdentity != "" ) // or whatever is appropriate here
- return BadRegistration( 4711, "Unknown Admin" );
- let [secret, userIdentity] = await DoRegistration(Username, name, wallet, ca, adminIdentity);
- if ( ! (secret && userIdentity) )
- return BadRegistration( 500, "Failed to enroll admin due to above error" );
- return {
- secret: secret,
- data: userIdentity,
- httpstatus: 200,
- message: "Successfully registered admin user ${Username} and imported it into the wallet"
- };
- }
- catch (error)
- {
- return BadRegistration( 500, "Failed to enroll admin due to above error" );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement