Skip to content

Commit

Permalink
Merge pull request #6 from LacunaSoftware/feature/issue-software-cert…
Browse files Browse the repository at this point in the history
…ificate

Consuming new certificate API
  • Loading branch information
rufiss authored Nov 7, 2023
2 parents 0e62436 + ec6f72b commit c761c28
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 2 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const {
HttpMethods,
} = require('./lib/enums');
const { IssueCertificateRequest } = require('./lib/issue-certificate-request');
const { IssueSoftwareCertificateRequest } = require('./lib/issue-software-certificate-request');
const { IssueSoftwareCertificateResponse } = require('./lib/issue-software-certificate-response');
const { IssuePkcs12CertificateRequest } = require('./lib/issue-pkcs12-cert-request');
const { IssuePkcs12CertificateResponse } = require('./lib/issue-pkcs12-cert-response');
const { KeyModel } = require('./lib/key-model');
Expand Down Expand Up @@ -92,6 +94,8 @@ exports.PaginationOrders = PaginationOrders;
exports.AmpliaErrorCodes = AmpliaErrorCodes;
exports.HttpMethods = HttpMethods;
exports.IssueCertificateRequest = IssueCertificateRequest;
exports.IssueSoftwareCertificateRequest = IssueSoftwareCertificateRequest;
exports.IssueSoftwareCertificateResponse = IssueSoftwareCertificateResponse;
exports.IssuePkcs12CertificateRequest = IssuePkcs12CertificateRequest;
exports.IssuePkcs12CertificateResponse = IssuePkcs12CertificateResponse;
exports.Order = Order;
Expand Down
17 changes: 17 additions & 0 deletions lib/amplia-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const { PaginatedSearchResponse } = require('./paginated-search-response');
const { RestClient } = require('./rest-client');
const { SignHashResponse } = require('./sign-hash-response');
const { KeyModel } = require('./key-model');
const { IssueSoftwareCertificateRequest } = require('./issue-software-certificate-request');
const { IssueSoftwareCertificateResponse } = require('./issue-software-certificate-response');
const { IssuePkcs12CertificateResponse } = require('./issue-pkcs12-cert-response');
const { IssuePkcs12CertificateRequest } = require('./issue-pkcs12-cert-request');

Expand Down Expand Up @@ -232,7 +234,22 @@ class AmpliaClient {
.catch((err) => reject(err));
});
}
/**
* Issues a Certificate order from Amplia
*
* @param {string} orderId the ID of the desired order
* @param {string} password the password for the certificate
* @param {number} keySize the size of the key for this certificate
* @returns {Promise<IssueSoftwareCertificateResponse>} the response containing the certificate in many formats
*/

async issueSoftwareCertificate(orderId, password, keySize = null) {
const client = this._getRestClient();
const request = new IssueSoftwareCertificateRequest({ orderId, password, keySize });
const response = await client.post('api/certificates/software', request);
const cert = new IssueSoftwareCertificateResponse(response.getBody());
return cert;
}
/**
* Issues a PKCS#12 certificate order from Amplia
*
Expand Down
61 changes: 61 additions & 0 deletions lib/issue-software-certificate-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

class IssueSoftwareCertificateRequest{

constructor({ orderId, password, keySize } = {}) {
this._orderId = orderId || null;
this._password = password || null;
this._keySize = keySize || null;
}

//region "orderId" Accessors


get orderId() {
return this._orderId;
}


set orderId(value) {
this._orderId = value;
}

//endregion

//region "password" Accessors


get password() {
return this._password;
}


set password(value) {
this._password = value;
}

//endregion

//region "keySize" Accessors


get keySize() {
return this._keySize;
}


set keySize(value) {
this._keySize = value;
}

//endregion

toModel() {
return {
orderId: this._orderId || '00000000-0000-0000-0000-000000000000',
password: this._password,
keySize: this._keySize || 2048
};
}
}
exports.IssueSoftwareCertificateRequest = IssueSoftwareCertificateRequest;
136 changes: 136 additions & 0 deletions lib/issue-software-certificate-response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
'use strict';

class IssueSoftwareCertificateResponse {

constructor({ model,
certificateDer,
certificatePem,
publicKeyDer,
publicKeyPem,
privateKeyPkcs8Der,
privateKeyPkcs8Pem } = {}) {
this._model = model || null;
this._certificateDer = certificateDer || null;
this._certificatePem = certificatePem || null;
this._publicKeyDer = publicKeyDer || null;
this._publicKeyPem = publicKeyPem || null;
this._privateKeyPkcs8Der = privateKeyPkcs8Der || null;
this._privateKeyPkcs8Pem = privateKeyPkcs8Pem || null;
}

//endregion

//region "model" Accessors


get model() {
return this._model;
}


set model(value) {
this._model = value;
}

//endregion

//endregion

//region "certificateDer" Accessors


get certificateDer() {
return this._certificateDer;
}


set certificateDer(value) {
this._certificateDer = value;
}

//endregion

//region "certificatePem" Accessors



get certificatePem() {
return this._certificatePem;
}


set certificatePem(value) {
this._certificatePem = value;
}

//endregion

//region "publicKeyDer" Accessors


get publicKeyDer() {
return this._publicKeyDer;
}


set publicKeyDer(value) {
this._publicKeyDer = value;
}

//endregion

//region "publicKeyPem" Accessors


get publicKeyPem() {
return this._publicKeyPem;
}


set publicKeyPem(value) {
this._publicKeyPem = value;
}

//endregion

//region "privateKeyPkcs8Der" Accessors

get privateKeyPkcs8Der() {
return this._privateKeyPkcs8Der;
}


set privateKeyPkcs8Der(value) {
this._privateKeyPkcs8Der = value;
}

//endregion

//region "privateKeyPkcs8Pem" Accessors



get privateKeyPkcs8Pem() {
return this._privateKeyPkcs8Pem;
}



set privateKeyPkcs8Pem(value) {
this._privateKeyPkcs8Pem = value;
}

toModel() {
return {
model: this._model,
certificateDer: this._certificateDer,
certificatePem: this._certificatePem,
publicKeyDer: this._publicKeyDer,
publicKeyPem: this._publicKeyPem,
privateKeyPkcs8Der: this._privateKeyPkcs8Der,
privateKeyPkcs8Pem: this._privateKeyPkcs8Pem,
};
}
}

exports.IssueSoftwareCertificateResponse = IssueSoftwareCertificateResponse;
16 changes: 14 additions & 2 deletions test/test-cases/orders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const getCertificateParams = (certificateFormat, user) => {

function mountOrderRequest(
hasPresetUser = false,
certificateFormat = CertificateFormats.PKI_BRAZIL
certificateFormat = CertificateFormats.PKI_BRAZIL,
caId = null
) {

const user = hasPresetUser
Expand All @@ -64,7 +65,7 @@ function mountOrderRequest(

// Create an order request.
const request = new CreateOrderRequest({
caId: Config.AMPLIA_CA_ID,
caId: caId != null ? caId : Config.AMPLIA_CA_ID ,
validityEnd: generators.generateDateTwoYearsFromNow(),
kind: CertificateKinds.PUBLIC_KEY,
parameters: getCertificateParams(certificateFormat, user),
Expand Down Expand Up @@ -256,3 +257,14 @@ describe.each(certificateFormats)('Test Cases for Amplia Node Client', ({certifi
);
// end Test Case #3 for Amplia Node Client
});
describe('issueSoftwareCertificate', function(){

test('Should return software certificate', async function(){
// This test require api/certificates/software endpoint to work properly
var request = mountOrderRequest(true,CertificateFormats.PKI_BRAZIL);
const order = await ampliaClient.createOrder(request);
var result = await ampliaClient.issueSoftwareCertificate(order.id,'1234', 2048);
expect(result).not.toBeNull();
});

});

0 comments on commit c761c28

Please sign in to comment.