Advertisement
Doc_Holliwood

Untitled

Sep 2nd, 2019
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'esversion: 8';
  2. 'use strict';
  3.  
  4. async function FetchUserWallet (Username)
  5. {
  6.     const walletPath = path.join(process.cwd(), 'wallet');
  7.     const wallet = new FileSystemWallet(walletPath);
  8.     let appAdmin = "FIXME";
  9.    
  10.     if ( await wallet.exists(Username) )
  11.         return [undefined, "An identity for the  ${Username} already exists in the wallet"];
  12.    
  13.     if (!await wallet.exists(appAdmin))
  14.         return [undefined, "Am admin identity is not registered . please register admin first"];
  15.    
  16.     return [wallet, undefined];
  17. }  
  18.  
  19. async function connectToGateway(wallet)
  20. {
  21.     const gateway = new Gateway();
  22.        
  23.     let ccp = "FIXME";
  24.     let appAdmin = "FIXME";
  25.     await gateway.connect(ccp, {
  26.         wallet,
  27.         identity: appAdmin,
  28.         discovery: {
  29.             enabled: false,
  30.             asLocalhost: true
  31.         }
  32.     });
  33.    
  34.     return gateway;
  35. }
  36.  
  37. function BadRegistration(code, error)
  38. {
  39.     let response = {};
  40.     response.data = null;
  41.     response.httpstatus = code;
  42.     response.message = error;
  43.     return response;
  44. }
  45.  
  46. async function FetchNameFromDFARM (config, roleid) {
  47.     let query = { RoleId: roleid };
  48.    
  49.     MongoClient.connect(config.Database.DFARM.connectString, function (err, client) {
  50.         if (err)
  51.             return [ undefined, "Error connecting to DFARM database: " + err ];
  52.  
  53.         client
  54.         .db(config.Database.DFARM.dbName)
  55.         .collection("Role")
  56.         .find( query )
  57.     // I don't know, this seems to be a strange construct
  58.     // Are you sure that's correct?
  59.         .toArray( function (err, docs) {
  60.             if (err)
  61.                 return [ undefined, "Error with DB :" + err ];          
  62.  
  63.             return [docs[0].name, undefined];
  64.         });
  65.        
  66.         //client.close();
  67.     });
  68. }  
  69.  
  70. async function DoRegistration(Username, name, wallet, ca, adminIdentity)
  71. {
  72.     const secret = await ca.register({
  73.         enrollmentID: Username,
  74.         role: 'client',
  75.         attrs: [{name: "approle", value: name, ecert: true }]
  76.     }, adminIdentity);
  77.     console.log('secret', secret);   // return secret;                            
  78.  
  79.     const enrollment = await ca.enroll({
  80.         enrollmentID: Username,enrollmentSecret: secret
  81.     });
  82.     // console.log('enrollment', enrollment);
  83.     const userIdentity = X509WalletMixin.createIdentity('DfarmadminMSP', enrollment.certificate, enrollment.key.toBytes());
  84.     // console.log("userIdentity", userIdentity)
  85.     await wallet.import(Username, userIdentity);
  86.     console.log(`Successfully  enrolled  user ${Username}  and imported it into the wallet`);
  87.  
  88.     return [secret, userIdentity];
  89. }                          
  90.  
  91. async function RegisterUser(Username, roleid, callback)
  92. {
  93.     try
  94.     {
  95.       var fs = require('fs');
  96.       var config = JSON.parse(fs.readFileSync('./config/Config.json', 'utf8'));
  97.      
  98.       const [wallet, wallet_error] = await FetchUserWallet( Username );
  99.       if ( !wallet )
  100.         return BadRegistration( 400, wallet_error );
  101.      
  102.       let gateway = await connectToGateway(wallet);
  103.       if ( !gateway )
  104.         return BadRegistration( 502, "Bad Gateway" );
  105.      
  106.       let [name, db_error] = await FetchNameFromDFARM( config, roleid );
  107.       if ( !name )
  108.         return BadRegistration( 500, db_error );
  109.      
  110.       const ca = gateway.getClient().getCertificateAuthority();
  111.       const adminIdentity = gateway.getCurrentIdentity();
  112.       if ( adminIdentity != "" ) // or whatever is appropriate here
  113.         return BadRegistration( 4711, "Unknown Admin" );
  114.      
  115.       let [secret, userIdentity] = await DoRegistration(Username, name, wallet, ca, adminIdentity);
  116.       if ( ! (secret && userIdentity) )
  117.         return BadRegistration( 500, "Failed to enroll admin due to above error" );
  118.      
  119.       return {
  120.         secret: secret,
  121.         data: userIdentity,
  122.         httpstatus: 200,
  123.         message: "Successfully registered admin user ${Username} and imported it into the wallet"
  124.       };
  125.      
  126.     }
  127.     catch (error)
  128.     {
  129.       return BadRegistration( 500, "Failed to enroll admin due to above error" );
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement