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