@@ -8,7 +8,7 @@ import code.consumer.Consumers
88import code .model .Consumer
99import code .util .Helper .MdcLoggable
1010import com .openbankproject .commons .ExecutionContext .Implicits .global
11- import com .openbankproject .commons .model .{RegulatedEntityTrait , User }
11+ import com .openbankproject .commons .model .{RegulatedEntityAttributeSimple , RegulatedEntityTrait , User }
1212import net .liftweb .common .{Box , Empty , Failure , Full }
1313import net .liftweb .http .provider .HTTPParam
1414import net .liftweb .util .Helpers
@@ -111,29 +111,40 @@ object BerlinGroupSigning extends MdcLoggable {
111111 certificate
112112 }
113113
114- def getTppByCertificate (certificate : X509Certificate , callContext : Option [CallContext ]): Future [List [RegulatedEntityTrait ]] = {
115- // Use the regular expression to find the value of CN
116- val extractedCN = cnPattern.findFirstMatchIn(certificate.getIssuerDN.getName) match {
117- case Some (m) => m.group(1 ) // Extract the value of CN
118- case None => " CN not found"
119- }
120- val issuerCommonName = extractedCN // Certificate.caCert
114+ def getRegulatedEntityByCertificate (certificate : X509Certificate , callContext : Option [CallContext ]): Future [List [RegulatedEntityTrait ]] = {
115+ val issuerCN = cnPattern.findFirstMatchIn(certificate.getIssuerDN.getName)
116+ .map(_.group(1 ).trim)
117+ .getOrElse(" CN not found" )
118+
121119 val serialNumber = certificate.getSerialNumber.toString
122- val regulatedEntities : Future [List [RegulatedEntityTrait ]] = for {
120+
121+ for {
123122 (entities, _) <- getRegulatedEntitiesNewStyle(callContext)
124123 } yield {
125- logger.debug(" Regulated Entities: " + entities)
126124 entities.filter { entity =>
127- val hasSerialNumber = entity.attributes.exists(_.exists(a =>
128- a.name == " CERTIFICATE_SERIAL_NUMBER" && a.value == serialNumber
129- ))
130- val hasCaName = entity.attributes.exists(_.exists(a =>
131- a.name == " CERTIFICATE_CA_NAME" && a.value == issuerCommonName
132- ))
133- hasSerialNumber && hasCaName
125+ val attrs = entity.attributes.getOrElse(Nil )
126+
127+ // Extract serial number and CA name from attributes
128+ val serialOpt = attrs.collectFirst { case a if a.name.equalsIgnoreCase(" CERTIFICATE_SERIAL_NUMBER" ) => a.value.trim }
129+ val caNameOpt = attrs.collectFirst { case a if a.name.equalsIgnoreCase(" CERTIFICATE_CA_NAME" ) => a.value.trim }
130+
131+ val serialMatches = serialOpt.contains(serialNumber)
132+ val caNameMatches = caNameOpt.exists(_.equalsIgnoreCase(issuerCN))
133+
134+ val isMatch = serialMatches && caNameMatches
135+
136+ // Log everything for debugging
137+ val serialLog = serialOpt.getOrElse(" N/A" )
138+ val caNameLog = caNameOpt.getOrElse(" N/A" )
139+ val allAttrsLog = attrs.map(a => s " ${a.name}=' ${a.value}' " ).mkString(" , " )
140+
141+ if (isMatch)
142+ logger.debug(s " [MATCH] Entity ' ${entity.entityName}' (Code: ${entity.entityCode}) matches CN=' $issuerCN', Serial=' $serialNumber' " +
143+ s " (Attributes found: Serial=' $serialLog', CA Name=' $caNameLog', All Attributes: [ $allAttrsLog]) " )
144+
145+ isMatch
134146 }
135147 }
136- regulatedEntities
137148 }
138149
139150
@@ -280,7 +291,7 @@ object BerlinGroupSigning extends MdcLoggable {
280291 }
281292
282293 for {
283- entities <- getTppByCertificate (certificate, forwardResult._2) // Find TPP via certificate
294+ entities <- getRegulatedEntityByCertificate (certificate, forwardResult._2) // Find Regulated Entity via certificate
284295 } yield {
285296 // Certificate can be changed but this value is permanent per Regulated entity
286297 val idno = entities.map(_.entityCode).headOption.getOrElse(" " )
0 commit comments