'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 ven = await nantum.getVenRegistration(requestVenId); if (ven) { if (ven.client_certificate_common_name !== clientCertificateCn) { const error = new Error('Client certificate CN mismatch'); error.responseCode = 452; throw error; } } else { const registrationId = v4().replace(/-/g, ''); ven = { client_certificate_common_name: clientCertificateCn, client_certificate_fingerprint: clientCertificateFingerprint, registration_id: registrationId, is_report_only: oadrCreatePartyRegistration.oadrReportOnly, profile_name: oadrCreatePartyRegistration.oadrProfileName, supports_xml_sig: oadrCreatePartyRegistration.oadrXmlSignature, transport_name: oadrCreatePartyRegistration.oadrTransportName, uses_http_pull: oadrCreatePartyRegistration.oadrHttpPullModel, dis: oadrCreatePartyRegistration.oadrVenName, }; await nantum.createVenRegistration(ven); } return venToOadrRegistrationCreated( oadrCreatePartyRegistration.requestId, ven, ); } 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 ven = await nantum.getVenRegistration(requestVenId); if (ven) { if (ven.client_certificate_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 ven = {}; } return venToOadrRegistrationCreated(oadrQueryRegistration.requestId, ven); } async function cancelParty( oadrCancelPartyRegistration, clientCertificateCn, clientCertificateFingerprint, ) { logger.info( 'cancelParty', oadrCancelPartyRegistration, clientCertificateCn, clientCertificateFingerprint, ); const requestVenId = oadrCancelPartyRegistration.venId; validateVenId(requestVenId, clientCertificateFingerprint, false); const venId = clientCertificateFingerprint; let ven = await nantum.getVenRegistration(requestVenId); let cancelledRegistrationId; if (ven) { if (ven.client_certificate_common_name !== clientCertificateCn) { const error = new Error('Client certificate CN mismatch'); error.responseCode = 452; throw error; } cancelledRegistrationId = ven.registration_id; if (cancelledRegistrationId == null) { const error = new Error('No current registration for VenID'); error.responseCode = 452; throw error; } // clear all registration data await nantum.deleteVenRegistration(ven._id); } return { responseRequestId: oadrCancelPartyRegistration.requestId || '', responseCode: '200', responseDescription: 'OK', venId: venId, registrationId: cancelledRegistrationId, }; } function venToOadrRegistrationCreated(requestId, ven) { return { responseRequestId: requestId || '', responseCode: '200', responseDescription: 'OK', registrationId: ven.registration_id, venId: ven.client_certificate_fingerprint, 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, };