Skip to content

Commit

Permalink
[FAB-1239] register function for fabric-ca-client
Browse files Browse the repository at this point in the history
This CR adds the initial implementation of the register
API for communicating with fabric-ca.  It does not yet
handle security but that will be added in a followup
changeset

Fixes FAB-1239

Change-Id: Idebeaf5a2d170f48afb5f0cf9791656b058d884d
Signed-off-by: Gari Singh <[email protected]>
  • Loading branch information
mastersingh24 committed Jan 29, 2017
1 parent 1f9d5e4 commit a33d1c5
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
98 changes: 98 additions & 0 deletions fabric-ca-client/lib/FabricCAClientImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,104 @@ var FabricCOPClient = class {
this._baseAPI = '/api/v1/cfssl/';


}

/**
* @typedef {Object} KeyValueAttribute
* @property {string} key The key used to reference the attribute
* @property {string} value The value of the attribute
*/

/**
* Register a new user and return the enrollment secret
* @param {string} enrollmentID ID which will be used for enrollment
* @param {string} role Type of role for this user
* @param {string} group Group to which this user will be assigned
* @param {KeyValueAttribute[]} attrs Array of key/value attributes to assign to the user
* @param {string} callerID The ID of the user who is registering this user
* @returns {Promise} The enrollment secret to use when this user enrolls
*/
register(enrollmentID, role, group, attrs, callerID) {

var self = this;
var numArgs = arguments.length;

return new Promise(function (resolve, reject) {
//all arguments are required
if (numArgs < 5) {
reject(new Error('Missing required parameters. \'enrollmentID\', \'role\', \'group\', \'attrs\', \
and \'callerID\' are all required.'));
}


var regRequest = {
'id': enrollmentID,
'type': role,
'group': group,
'attrs': attrs,
'callerID': callerID
};

var requestOptions = {
hostname: self._hostname,
port: self._port,
path: self._baseAPI + 'register',
method: 'POST',
//auth: enrollmentID + ':' + enrollmentSecret,
ca: self._ca
};

var request = self._httpClient.request(requestOptions, function (response) {

const responseBody = [];
response.on('data', function (chunk) {
responseBody.push(chunk);
});

response.on('end', function () {

var payload = responseBody.join('');

if (!payload) {
reject(new Error(
util.format('Registerfailed with HTTP status code ', response.statusCode)));
}
//response should be JSON
try {
var regResponse = JSON.parse(payload);
if (regResponse.success) {
//we want the result field which is Base64-encoded PEM
return resolve(regResponse.result);
} else {
return reject(new Error(
util.format('Register failed with errors [%s]', JSON.stringify(regResponse.errors))));
}

} catch (err) {
reject(new Error(
util.format('Could not parse register response [%s] as JSON due to error [%s]', payload, err)));
}
});

});

request.on('error', function (err) {
reject(new Error(util.format('Calling register endpoint failed with error [%s]', err)));
});

request.write(JSON.stringify(regRequest));
request.end();
});
}

/**
* Generate authorization token required for accessing fabric-cop APIs
* @param {string} privKey The pem-encoded private key used for signing
* @param {string} X509Key The pem-encoded X509 certificate associated with privKey
* @param {string} reqBody The body of the request to sign as part of the token
*/
static generateAuthToken(privKey, X509Key, reqBody) {

}

/**
Expand Down
47 changes: 47 additions & 0 deletions test/unit/fabriccopservices-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,50 @@ test('FabricCOPServices: Test enroll() With Dynamic CSR', function (t) {
);

});

/**
* FabricCOPClient register tests
*/
test('FabricCOPClient: Test register with missing parameters', function (t) {

var client = new FabricCOPClient({
protocol: 'http',
hostname: '127.0.0.1',
port: 7054
});

return client.register()
.then(function (token) {
t.fail('Register must fail when missing required parameters');
})
.catch(function (err) {
if (err.message.startsWith('Missing required parameters')) {
t.pass('Register should fail when missing required parameters');
} else {
t.fail('Register should have failed with \'Missing required parameters\'');
}
});
});

test('FabricCOPClient: Test register', function (t) {

var client = new FabricCOPClient({
protocol: 'http',
hostname: '127.0.0.1',
port: 7054
});

var enrollmentID = 'testRegisterUser';


return client.register(enrollmentID, 'client', 'bank_a', [], 'admin')
.then(function (secret) {
t.comment(secret);
t.pass('Successfully invoked register API with enrollmentID \'' + enrollmentID + '\'');

})
.catch(function (err) {
t.fail('Failed to register \'' + enrollmentID + '\'. ' + err);
});
});

0 comments on commit a33d1c5

Please sign in to comment.