registration.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. obj,
  8. clientCertificateCn,
  9. clientCertificateFingerprint,
  10. ) {
  11. logger.info(
  12. 'registerParty',
  13. obj,
  14. clientCertificateCn,
  15. clientCertificateFingerprint,
  16. );
  17. const requestVenId = obj.venId;
  18. if (!requestVenId) {
  19. const error = new Error('No VenID in request');
  20. error.responseCode = 452;
  21. throw error;
  22. }
  23. if (requestVenId !== clientCertificateFingerprint) {
  24. // as per certification item #512, venId MUST be case-sensitive
  25. const error = new Error('VenID does not match certificate');
  26. error.responseCode = 452;
  27. throw error;
  28. }
  29. if (!clientCertificateCn) {
  30. const error = new Error('Could not determine CN from client certificate');
  31. error.responseCode = 452;
  32. throw error;
  33. }
  34. let nantumRegistration = await nantum.fetchRegistration(requestVenId);
  35. if (nantumRegistration) {
  36. if (nantumRegistration.common_name !== clientCertificateCn) {
  37. const error = new Error('Client certificate CN mismatch');
  38. error.responseCode = 452;
  39. throw error;
  40. }
  41. if (nantumRegistration.registration_id == null) {
  42. const registrationId = v4().replace(/-/g, '');
  43. nantumRegistration.registration_id = registrationId;
  44. await nantum.updateRegistration(nantumRegistration);
  45. }
  46. } else {
  47. const registrationId = v4().replace(/-/g, '');
  48. nantumRegistration = {
  49. common_name: clientCertificateCn,
  50. ven_id: requestVenId,
  51. registration_id: registrationId,
  52. };
  53. await nantum.updateRegistration(nantumRegistration);
  54. }
  55. return nantumRegistrationToOadrRegistrationCreated(
  56. obj.requestId,
  57. nantumRegistration,
  58. );
  59. }
  60. async function query(obj, clientCertificateCn, clientCertificateFingerprint) {
  61. logger.info('query', obj, clientCertificateCn, clientCertificateFingerprint);
  62. const requestVenId = clientCertificateFingerprint;
  63. let nantumRegistration = await nantum.fetchRegistration(requestVenId);
  64. if (nantumRegistration) {
  65. if (nantumRegistration.common_name !== clientCertificateCn) {
  66. const error = new Error('Client certificate CN mismatch');
  67. error.responseCode = 452;
  68. throw error;
  69. }
  70. } else {
  71. // response payload should not contain ven_id or registration_id
  72. nantumRegistration = {};
  73. }
  74. return nantumRegistrationToOadrRegistrationCreated(
  75. obj.requestId,
  76. nantumRegistration,
  77. );
  78. }
  79. async function cancelParty(
  80. obj,
  81. clientCertificateCn,
  82. clientCertificateFingerprint,
  83. ) {
  84. logger.info(
  85. 'cancelParty',
  86. obj,
  87. clientCertificateCn,
  88. clientCertificateFingerprint,
  89. );
  90. const registrationId = obj.registrationId;
  91. if (!registrationId) {
  92. const error = new Error('No registrationID in request');
  93. error.responseCode = 452;
  94. throw error;
  95. }
  96. const requestVenId = obj.venId;
  97. if (requestVenId && requestVenId !== clientCertificateFingerprint) {
  98. // as per certification item #512, venId MUST be case-sensitive
  99. const error = new Error('VenID does not match certificate');
  100. error.responseCode = 452;
  101. throw error;
  102. }
  103. const venId = clientCertificateFingerprint;
  104. if (!clientCertificateCn) {
  105. const error = new Error('Could not determine CN from client certificate');
  106. error.responseCode = 452;
  107. throw error;
  108. }
  109. let nantumRegistration = await nantum.fetchRegistration(requestVenId);
  110. let cancelledRegistrationId;
  111. if (nantumRegistration) {
  112. if (nantumRegistration.common_name !== clientCertificateCn) {
  113. const error = new Error('Client certificate CN mismatch');
  114. error.responseCode = 452;
  115. throw error;
  116. }
  117. cancelledRegistrationId = nantumRegistration.registration_id;
  118. // clear all registration data
  119. nantumRegistration = {
  120. ven_id: requestVenId,
  121. common_name: clientCertificateCn,
  122. };
  123. await nantum.updateRegistration(nantumRegistration);
  124. }
  125. if (cancelledRegistrationId == null) {
  126. const error = new Error('No current registration for VenID');
  127. error.responseCode = 452;
  128. throw error;
  129. }
  130. return {
  131. responseRequestId: obj.requestId || '',
  132. responseCode: '200',
  133. responseDescription: 'OK',
  134. venId: venId,
  135. registrationId: cancelledRegistrationId,
  136. };
  137. }
  138. function nantumRegistrationToOadrRegistrationCreated(
  139. requestId,
  140. nantumRegistration,
  141. ) {
  142. return {
  143. responseRequestId: requestId || '',
  144. responseCode: '200',
  145. responseDescription: 'OK',
  146. registrationId: nantumRegistration.registration_id,
  147. venId: nantumRegistration.ven_id,
  148. vtnId: vtnId,
  149. pollFreqDuration: 'PT10S',
  150. };
  151. }
  152. module.exports = {
  153. cancelParty,
  154. query,
  155. registerParty,
  156. };