sign-in.js - Sign in to CA Server

Q

How to write a Node.js script to sign in to the CA Server?

✍: FYIcenter.com

A

Hyperledger Fabric network is permission based server application. All client applications must use an authenticated user identity to access the network.

User authentication is controlled by the CA (Certificate Authority) server using PKI technology.

Here is sample Node.js script, sign-in.js, that takes a user ID and password and tries to sign in to the CA server.

/* Copyright (c) FYIcenter.com
 */
'use strict';
const FabricCAServices = require('fabric-ca-client');

// Build the connection setting object
const fs = require('fs');
const ccp = JSON.parse(fs.readFileSync("connection.json", 'utf8'));

// Get user name and password from the command line
const user = process.argv[2];
const pass = process.argv[3];

async function main() {
   try {
      // Create a new CA client for interacting with the CA.
      const caURL = ccp.certificateAuthorities['ca.example.com'].url;
      const ca = new FabricCAServices(caURL);

      // Enroll the user and dump the output
      const enrollment = await ca.enroll({ enrollmentID: user, enrollmentSecret: pass });
      console.log(enrollment.certificate);
      console.log(enrollment.key);
      console.log("User (%s) authenticated!", user);
   } catch (error) {
      console.error("Failed to authenticate user %s", user);
      process.exit(1);
   }
}

main();

To run this application script on your client system, you need to make sure that the WYFA (Write Your First Application) network is up and running on your server system.

This script also needs a copy of connection.json which contains IP addresses and port numbers of the WYFA network services. You may need to review them and update them accordingly.

$ cp ../../basic-network/connection.json .
$ more connection.json

...
    "orderers": {
        "orderer.example.com": {
            "url": "grpc://localhost:7050"
        }
    },
    "peers": {
        "peer0.org1.example.com": {
            "url": "grpc://localhost:7051"
        }
    },
    "certificateAuthorities": {
        "ca.example.com": {
            "url": "http://localhost:7054",
            "caName": "ca.example.com"
        }
    }
}

Now try with the default admin user and password pre-defined in the WYFA network:

$ node sign-in.js admin adminpw
-----BEGIN CERTIFICATE-----
MIICATCCAaigAwIBAgIUNPNbAS6CFLNahRFy8dIyHmZD4bQwCgYIKoZIzj0EAwIw
...
-----END CERTIFICATE-----

ECDSA_KEY {
  _key:
   { type: 'EC',
     isPrivate: true,
     isPublic: false,
     ...
     curveName: 'secp256r1',
     ecparams:
      { name: 'secp256r1',
        keylen: 256,
        curve: [Object],
        G: [Object],
        n: [Object],
        h: [Object],
        oid: undefined,
        info: undefined },
     prvKeyHex: '09424cecea10a8b52364dc32cedbcfce8bb34e100c2a617ef6cd2c73919e5b17',
     pubKeyHex: '0411cfe977bc08a07bbabe88554ef546c98565762819c73e633e453fa76...' } }

User (admin) authenticated!

As you can see, the enroll() method is really an authentication method. After you are authenticated, ca.example.com issues you a key and certificate, using EC (Elliptic Curve) cryotography technology on the "secp256r1" curve.

You can also try it again with a wrong password:

$ node sign-in.js admin junk

error: [FabricCAClientService.js]: Failed to enroll admin, error:%o \
   message=Enrollment failed with errors [[{"code":20,"message":"Authentication failure"}]], 
   ...
Failed to authenticate user admin

 

wallet-import.js - Import Keys to Wallet

FabricCAServices Node.js Class

Interfaces to Communicate with Ledger Peer

⇑⇑ Hyperledger Tutorials

2019-11-08, 267👍, 0💬