registration.js 5.5 KB

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