Skip to content

Commit

Permalink
fix: doc and api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DhananjayPurohit committed Mar 13, 2024
1 parent 910bc56 commit b26f6fd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 78 deletions.
20 changes: 6 additions & 14 deletions doc/mainstay_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,27 +575,15 @@ const pvtKey =
const commitment =
'F01111111111111111111111111111111111111111111111111111111111110F';


let keyPair = ec.keyFromPrivate("97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a");
let privKey = keyPair.getPrivate("hex");
let pubKey = keyPair.getPublic();

let signature = ec.sign(commitment, privKey, "hex", {canonical: true}).toDER('base64');

var payload = {
commitment: commitment,
position: 0,
token: '4c8c006d-4cee-4fef-8e06-bb8112db6314',
};

payload = new Buffer(JSON.stringify(payload)).toString('base64');

const options = {
url: url + route,
headers: {
'X-MAINSTAY-PAYLOAD': payload,
'X-MAINSTAY-SIGNATURE': signature
}
body: payload
};

request.post(options, (error, response, body) => {
Expand All @@ -607,7 +595,11 @@ request.post(options, (error, response, body) => {

**Curl example**
```perl
curl --header "Content-Type: application/json" --request POST --data '{"X-MAINSTAY-PLAYLOAD":"eyJwb3NpdGlvbiI6MCwiY29tbWl0bWVudCI6IkYwMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMEYifQ==","X-MAINSTAY-SIGNATURE":"IJbqe50XtfZbQ1b0jr+J1tswSPfZlWwZugXCpYbwYMPuRl+htqSb7wTLYY9RtQ6Bw9Ym5dw0vMNRaDwR8pked2Y="}' http://localhost:9000/api/v1/commitment/send
curl --header "Content-Type: application/json" --request POST --data '{
"position": "0",
"token": "4c8c006d-4cee-4fef-8e06-bb8112db6314",
"commitment": "f3d424bf830dbd59eebc3f0a23491a266b7158635188e47b0e2abf7dbcc8931",
}' http://localhost:9000/api/v1/commitment/send
```

*response*
Expand Down
102 changes: 44 additions & 58 deletions src/controllers/api_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,77 +408,63 @@ module.exports = {

commitment_send: async (req, res) => {
const startTime = start_time();
let rawRequestData = '';
req.on('data', chunk => {
rawRequestData += chunk.toString();
});

req.on('end', async () => {
// test payload in base64 format and defined
let data;
let payload;
try {
data = JSON.parse(rawRequestData);
payload = JSON.parse(base64decode(data[MAINSTAY_PAYLOAD]));
} catch (e) {
return reply_err(res, BAD_ARG_PAYLOAD, startTime);
}
const payload = req.body;

if (payload === undefined) {
return reply_err(res, MISSING_ARG_PAYLOAD, startTime);
}
if (payload === undefined) {
return reply_err(res, MISSING_ARG_PAYLOAD, startTime);
}

// check payload components are defined
if (payload.commitment === undefined) {
return reply_err(res, MISSING_PAYLOAD_COMMITMENT, startTime);
}
if (payload.position === undefined) {
return reply_err(res, MISSING_PAYLOAD_POSITION, startTime);
// check payload components are defined
if (payload.commitment === undefined) {
return reply_err(res, MISSING_PAYLOAD_COMMITMENT, startTime);
}
if (payload.position === undefined) {
return reply_err(res, MISSING_PAYLOAD_POSITION, startTime);
}
if (payload.token === undefined) {
return reply_err(res, MISSING_PAYLOAD_TOKEN, startTime);
}

if (/[0-9A-Fa-f]{64}/g.test(payload.commitment) === false) {
return reply_err(res, BAD_COMMITMENT, startTime);
}

try {
// try get client details
const data = await models.clientDetails.find({client_position: payload.position});
if (data.length === 0) {
return reply_err(res, POSITION_UNKNOWN, startTime);
}
if (payload.token === undefined) {
return reply_err(res, MISSING_PAYLOAD_TOKEN, startTime);
if (data[0].auth_token !== payload.token) {
return reply_err(res, PAYLOAD_TOKEN_ERROR, startTime);
}

if (/[0-9A-Fa-f]{64}/g.test(payload.commitment) === false) {
return reply_err(res, BAD_COMMITMENT, startTime);
if (data[0].expiry_date && new Date(data[0].expiry_date) < new Date()) {
return reply_err(res, EXPIRY_DATE_ERROR, startTime);
}

try {
// try get client details
const data = await models.clientDetails.find({client_position: payload.position});
if (data.length === 0) {
return reply_err(res, POSITION_UNKNOWN, startTime);
}
if (data[0].auth_token !== payload.token) {
return reply_err(res, PAYLOAD_TOKEN_ERROR, startTime);
}
if (data[0].expiry_date && new Date(data[0].expiry_date) < new Date()) {
return reply_err(res, EXPIRY_DATE_ERROR, startTime);
}

if (data[0].service_level === 'free') {
if (data[0].service_level === 'free') {

const latest_com = await models.clientCommitment.find({client_position: payload.position});
let today = new Date().toLocaleDateString();
const latest_com = await models.clientCommitment.find({client_position: payload.position});
let today = new Date().toLocaleDateString();

if (latest_com[0].date === today) {
return reply_err(res, FREE_TIER_LIMIT, startTime);
} else {
await models.clientCommitment.findOneAndUpdate({client_position: payload.position}, {
commitment: payload.commitment,
date: today
}, {upsert: true});
reply_msg(res, 'Commitment added', startTime);
}
if (latest_com[0].date === today) {
return reply_err(res, FREE_TIER_LIMIT, startTime);
} else {
await models.clientCommitment.findOneAndUpdate({client_position: payload.position}, {commitment: payload.commitment}, {upsert: true});
await models.clientCommitment.findOneAndUpdate({client_position: payload.position}, {
commitment: payload.commitment,
date: today
}, {upsert: true});
reply_msg(res, 'Commitment added', startTime);
}

} catch (error) {
return reply_err(res, INTERNAL_ERROR_API, startTime);
} else {
await models.clientCommitment.findOneAndUpdate({client_position: payload.position}, {commitment: payload.commitment}, {upsert: true});
reply_msg(res, 'Commitment added', startTime);
}
});

} catch (error) {
return reply_err(res, INTERNAL_ERROR_API, startTime);
}
},

commitment_add: async (req, res) => {
Expand Down
35 changes: 29 additions & 6 deletions test/controllers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const mongoose = require('mongoose');
const controllers = require('../src/controllers/api_controller');
const ctrlControllers = require('../src/controllers/ctrl_controller');
const models = require('../src/models/models');
const { BAD_COMMITMENT } = require('../src/utils/constants');
////////////////////////////////////////////////////////////////////////////////
/// Connect to MongoBD fot Test ///
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -249,26 +250,48 @@ describe('Test Api Controllers', () => {
/// pubKey = 1CsSceq9GWnmozaky3DGa24UER6gRDgibf
/// pvtKey = bac52bbea2194e7ea1cd3da6585b66d28f1a7a3683eca91af4ba6373d323d24f
///

// non 64 byte string
it('Route: /api/v1/commitment/send', () => {
const req = mockHttp.createRequest(
{
method: 'POST',
url: '/api/v1/commitment/send',
headers: {
'X-MAINSTAY-PAYLOAD': 'b',
'X-MAINSTAY-SIGNATURE': 'd'
body: {
position: 0,
token: '4c8c006d-4cee-4fef-8e06-bb8112db6314',
commitment: '1',
}
});
const res = mockHttp.createResponse();
controllers.commitment_send(req, res);
const json = JSON.parse(res._getData());
assert(json.error === BAD_COMMITMENT);
});

// non 64 byte string
it('Route: /api/v1/commitment/send', () => {
const req = mockHttp.createRequest(
{
method: 'POST',
url: '/api/v1/commitment/send',
body: {
position: 0,
token: '4c8c006d-4cee-4fef-8e06-bb8112db6314',
commitment: 'f3d424bf830dbd59eebc3f0a23491a266b7158635188e47b0e2abf7dbcc8*&/',
}
});
const res = mockHttp.createResponse();
controllers.commitment_send(req, res);
const json = JSON.parse(res._getData());
assert(json.error === BAD_COMMITMENT);
});

// non 64 byte string
it('Route: /ctrl/sendcommitment', () => {
const req = mockHttp.createRequest(
{
method: 'POST',
url: '/ctrl/sendcommitment',
body: {
position: 0,
token: '4c8c006d-4cee-4fef-8e06-bb8112db6314',
Expand All @@ -282,11 +305,11 @@ describe('Test Api Controllers', () => {
});

// non hex string
it('Route: /api/v1/commitment/send', () => {
it('Route: /ctrl/sendcommitment', () => {
const req = mockHttp.createRequest(
{
method: 'POST',
url: '/api/v1/commitment/send',
url: '/ctrl/sendcommitment',
body: {
position: 0,
token: '4c8c006d-4cee-4fef-8e06-bb8112db6314',
Expand Down

0 comments on commit b26f6fd

Please sign in to comment.