| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- 'use strict';
- const { v4 } = require('uuid');
- const { vtnId } = require('../config');
- const logger = require('../logger');
- const nantum = require('../modules/nantum');
- async function registerParty(
- oadrCreatePartyRegistration,
- clientCertificateCn,
- clientCertificateFingerprint,
- ) {
- logger.info(
- 'registerParty',
- oadrCreatePartyRegistration,
- clientCertificateCn,
- clientCertificateFingerprint,
- );
- const requestVenId = oadrCreatePartyRegistration.venId;
- validateVenId(requestVenId, clientCertificateFingerprint, true);
- validateCreatePartyRegistration(oadrCreatePartyRegistration);
- let nantumRegistration = await nantum.fetchRegistration(requestVenId);
- if (nantumRegistration) {
- if (nantumRegistration.common_name !== clientCertificateCn) {
- const error = new Error('Client certificate CN mismatch');
- error.responseCode = 452;
- throw error;
- }
- if (nantumRegistration.registration_id == null) {
- const registrationId = v4().replace(/-/g, '');
- nantumRegistration.registration_id = registrationId;
- await nantum.updateRegistration(nantumRegistration);
- }
- } else {
- const registrationId = v4().replace(/-/g, '');
- nantumRegistration = {
- common_name: clientCertificateCn,
- ven_id: requestVenId,
- registration_id: registrationId,
- };
- await nantum.updateRegistration(nantumRegistration);
- }
- return nantumRegistrationToOadrRegistrationCreated(
- oadrCreatePartyRegistration.requestId,
- nantumRegistration,
- );
- }
- function validateCreatePartyRegistration(oadrCreatePartyRegistration) {
- if (oadrCreatePartyRegistration.oadrTransportName !== 'simpleHttp') {
- const error = new Error('Transport name must be simpleHttp');
- error.responseCode = 459;
- throw error;
- }
- if (oadrCreatePartyRegistration.oadrProfileName !== '2.0b') {
- const error = new Error('Profile name must be 2.0b');
- error.responseCode = 459;
- throw error;
- }
- if (oadrCreatePartyRegistration.oadrReportOnly) {
- const error = new Error('Report-only mode is not supported');
- error.responseCode = 459;
- throw error;
- }
- if (oadrCreatePartyRegistration.oadrXmlSignature) {
- const error = new Error('XML signature mode is not supported');
- error.responseCode = 459;
- throw error;
- }
- if (
- oadrCreatePartyRegistration.oadrHttpPullModel != null &&
- !oadrCreatePartyRegistration.oadrHttpPullModel
- ) {
- const error = new Error('simpleHttp push mode is not supported');
- error.responseCode = 459;
- throw error;
- }
- if (oadrCreatePartyRegistration.oadrTransportAddress) {
- const error = new Error('oadrTransportAddress is not supported');
- error.responseCode = 459;
- throw error;
- }
- }
- async function query(
- oadrQueryRegistration,
- clientCertificateCn,
- clientCertificateFingerprint,
- ) {
- logger.info(
- 'query',
- oadrQueryRegistration,
- clientCertificateCn,
- clientCertificateFingerprint,
- );
- const requestVenId = clientCertificateFingerprint;
- let nantumRegistration = await nantum.fetchRegistration(requestVenId);
- if (nantumRegistration) {
- if (nantumRegistration.common_name !== clientCertificateCn) {
- const error = new Error('Client certificate CN mismatch');
- error.responseCode = 452;
- throw error;
- }
- } else {
- // response payload should not contain ven_id or registration_id
- nantumRegistration = {};
- }
- return nantumRegistrationToOadrRegistrationCreated(
- oadrQueryRegistration.requestId,
- nantumRegistration,
- );
- }
- async function cancelParty(
- oadrCancelPartyRegistration,
- clientCertificateCn,
- clientCertificateFingerprint,
- ) {
- logger.info(
- 'cancelParty',
- oadrCancelPartyRegistration,
- clientCertificateCn,
- clientCertificateFingerprint,
- );
- const requestVenId = oadrCancelPartyRegistration.venId;
- validateVenId(requestVenId, clientCertificateFingerprint, false);
- const venId = clientCertificateFingerprint;
- let nantumRegistration = await nantum.fetchRegistration(requestVenId);
- let cancelledRegistrationId;
- if (nantumRegistration) {
- if (nantumRegistration.common_name !== clientCertificateCn) {
- const error = new Error('Client certificate CN mismatch');
- error.responseCode = 452;
- throw error;
- }
- cancelledRegistrationId = nantumRegistration.registration_id;
- // clear all registration data
- nantumRegistration = {
- ven_id: requestVenId,
- common_name: clientCertificateCn,
- };
- await nantum.updateRegistration(nantumRegistration);
- }
- if (cancelledRegistrationId == null) {
- const error = new Error('No current registration for VenID');
- error.responseCode = 452;
- throw error;
- }
- return {
- responseRequestId: oadrCancelPartyRegistration.requestId || '',
- responseCode: '200',
- responseDescription: 'OK',
- venId: venId,
- registrationId: cancelledRegistrationId,
- };
- }
- function nantumRegistrationToOadrRegistrationCreated(
- requestId,
- nantumRegistration,
- ) {
- return {
- responseRequestId: requestId || '',
- responseCode: '200',
- responseDescription: 'OK',
- registrationId: nantumRegistration.registration_id,
- venId: nantumRegistration.ven_id,
- vtnId: vtnId,
- pollFreqDuration: 'PT10S',
- };
- }
- function validateVenId(requestVenId, clientCertificateFingerprint, required) {
- if (requestVenId === clientCertificateFingerprint) {
- return;
- }
- if (!required && requestVenId == null) {
- return;
- }
- if (required && requestVenId == null) {
- const error = new Error('VenID is missing');
- error.responseCode = 452;
- throw error;
- }
- const error = new Error('VenID does not match certificate');
- error.responseCode = 452;
- throw error;
- }
- module.exports = {
- cancelParty,
- query,
- registerParty,
- };
|