registration.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. 'use strict';
  2. const { v4 } = require('uuid');
  3. const { vtnId } = require('../config');
  4. const logger = require('../logger');
  5. const nantum = require('../modules/nantum');
  6. async function registerParty(
  7. oadrCreatePartyRegistration,
  8. clientCertificateCn,
  9. clientCertificateFingerprint,
  10. ) {
  11. logger.info(
  12. 'registerParty',
  13. oadrCreatePartyRegistration,
  14. clientCertificateCn,
  15. clientCertificateFingerprint,
  16. );
  17. const requestVenId = oadrCreatePartyRegistration.venId;
  18. validateVenId(requestVenId, clientCertificateFingerprint, true);
  19. validateCreatePartyRegistration(oadrCreatePartyRegistration);
  20. let nantumRegistration = await nantum.fetchRegistration(requestVenId);
  21. if (nantumRegistration) {
  22. if (nantumRegistration.common_name !== clientCertificateCn) {
  23. const error = new Error('Client certificate CN mismatch');
  24. error.responseCode = 452;
  25. throw error;
  26. }
  27. if (nantumRegistration.registration_id == null) {
  28. const registrationId = v4().replace(/-/g, '');
  29. nantumRegistration.registration_id = registrationId;
  30. await nantum.updateRegistration(nantumRegistration);
  31. }
  32. } else {
  33. const registrationId = v4().replace(/-/g, '');
  34. nantumRegistration = {
  35. common_name: clientCertificateCn,
  36. ven_id: requestVenId,
  37. registration_id: registrationId,
  38. };
  39. await nantum.updateRegistration(nantumRegistration);
  40. }
  41. return nantumRegistrationToOadrRegistrationCreated(
  42. oadrCreatePartyRegistration.requestId,
  43. nantumRegistration,
  44. );
  45. }
  46. function validateCreatePartyRegistration(oadrCreatePartyRegistration) {
  47. if (oadrCreatePartyRegistration.oadrTransportName !== 'simpleHttp') {
  48. const error = new Error('Transport name must be simpleHttp');
  49. error.responseCode = 459;
  50. throw error;
  51. }
  52. if (oadrCreatePartyRegistration.oadrProfileName !== '2.0b') {
  53. const error = new Error('Profile name must be 2.0b');
  54. error.responseCode = 459;
  55. throw error;
  56. }
  57. if (oadrCreatePartyRegistration.oadrReportOnly) {
  58. const error = new Error('Report-only mode is not supported');
  59. error.responseCode = 459;
  60. throw error;
  61. }
  62. if (oadrCreatePartyRegistration.oadrXmlSignature) {
  63. const error = new Error('XML signature mode is not supported');
  64. error.responseCode = 459;
  65. throw error;
  66. }
  67. if (
  68. oadrCreatePartyRegistration.oadrHttpPullModel != null &&
  69. !oadrCreatePartyRegistration.oadrHttpPullModel
  70. ) {
  71. const error = new Error('simpleHttp push mode is not supported');
  72. error.responseCode = 459;
  73. throw error;
  74. }
  75. if (oadrCreatePartyRegistration.oadrTransportAddress) {
  76. const error = new Error('oadrTransportAddress is not supported');
  77. error.responseCode = 459;
  78. throw error;
  79. }
  80. }
  81. async function query(
  82. oadrQueryRegistration,
  83. clientCertificateCn,
  84. clientCertificateFingerprint,
  85. ) {
  86. logger.info(
  87. 'query',
  88. oadrQueryRegistration,
  89. clientCertificateCn,
  90. clientCertificateFingerprint,
  91. );
  92. const requestVenId = clientCertificateFingerprint;
  93. let nantumRegistration = await nantum.fetchRegistration(requestVenId);
  94. if (nantumRegistration) {
  95. if (nantumRegistration.common_name !== clientCertificateCn) {
  96. const error = new Error('Client certificate CN mismatch');
  97. error.responseCode = 452;
  98. throw error;
  99. }
  100. } else {
  101. // response payload should not contain ven_id or registration_id
  102. nantumRegistration = {};
  103. }
  104. return nantumRegistrationToOadrRegistrationCreated(
  105. oadrQueryRegistration.requestId,
  106. nantumRegistration,
  107. );
  108. }
  109. async function cancelParty(
  110. oadrCancelPartyRegistration,
  111. clientCertificateCn,
  112. clientCertificateFingerprint,
  113. ) {
  114. logger.info(
  115. 'cancelParty',
  116. oadrCancelPartyRegistration,
  117. clientCertificateCn,
  118. clientCertificateFingerprint,
  119. );
  120. const requestVenId = oadrCancelPartyRegistration.venId;
  121. validateVenId(requestVenId, clientCertificateFingerprint, false);
  122. const venId = clientCertificateFingerprint;
  123. let nantumRegistration = await nantum.fetchRegistration(requestVenId);
  124. let cancelledRegistrationId;
  125. if (nantumRegistration) {
  126. if (nantumRegistration.common_name !== clientCertificateCn) {
  127. const error = new Error('Client certificate CN mismatch');
  128. error.responseCode = 452;
  129. throw error;
  130. }
  131. cancelledRegistrationId = nantumRegistration.registration_id;
  132. // clear all registration data
  133. nantumRegistration = {
  134. ven_id: requestVenId,
  135. common_name: clientCertificateCn,
  136. };
  137. await nantum.updateRegistration(nantumRegistration);
  138. }
  139. if (cancelledRegistrationId == null) {
  140. const error = new Error('No current registration for VenID');
  141. error.responseCode = 452;
  142. throw error;
  143. }
  144. return {
  145. responseRequestId: oadrCancelPartyRegistration.requestId || '',
  146. responseCode: '200',
  147. responseDescription: 'OK',
  148. venId: venId,
  149. registrationId: cancelledRegistrationId,
  150. };
  151. }
  152. function nantumRegistrationToOadrRegistrationCreated(
  153. requestId,
  154. nantumRegistration,
  155. ) {
  156. return {
  157. responseRequestId: requestId || '',
  158. responseCode: '200',
  159. responseDescription: 'OK',
  160. registrationId: nantumRegistration.registration_id,
  161. venId: nantumRegistration.ven_id,
  162. vtnId: vtnId,
  163. pollFreqDuration: 'PT10S',
  164. };
  165. }
  166. function validateVenId(requestVenId, clientCertificateFingerprint, required) {
  167. if (requestVenId === clientCertificateFingerprint) {
  168. return;
  169. }
  170. if (!required && requestVenId == null) {
  171. return;
  172. }
  173. if (required && requestVenId == null) {
  174. const error = new Error('VenID is missing');
  175. error.responseCode = 452;
  176. throw error;
  177. }
  178. const error = new Error('VenID does not match certificate');
  179. error.responseCode = 452;
  180. throw error;
  181. }
  182. module.exports = {
  183. cancelParty,
  184. query,
  185. registerParty,
  186. };