diff --git a/Sources/CertLogic/CertLogic.swift b/Sources/CertLogic/CertLogic.swift index 0fb71fa..d85d620 100644 --- a/Sources/CertLogic/CertLogic.swift +++ b/Sources/CertLogic/CertLogic.swift @@ -59,11 +59,11 @@ final public class CertLogicEngine { if results as! Bool { result.append(ValidationResult(rule: rule, result: .passed, validationErrors: nil)) } else { - result.append(ValidationResult(rule: rule, result: .open, validationErrors: nil)) + result.append(ValidationResult(rule: rule, result: .fail, validationErrors: nil)) } } } catch { - result.append(ValidationResult(rule: rule, result: .fail, validationErrors: [error])) + result.append(ValidationResult(rule: rule, result: .open, validationErrors: [error])) } } } @@ -111,12 +111,24 @@ final public class CertLogicEngine { // Get List of Rules for Country by Code private func getListOfRulesFor(external: ExternalParameter, issuerCountryCode: String) -> [Rule] { var returnedRulesItems: [Rule] = [] - let generalRulesWithAcceptence = rules.filter { rule in + var generalRulesWithAcceptence = rules.filter { rule in return rule.countryCode.lowercased() == external.countryCode.lowercased() && rule.ruleType == .acceptence && rule.certificateFullType == .general && external.validationClock >= rule.validFromDate && external.validationClock <= rule.validToDate } - let generalRulesWithInvalidation = rules.filter { rule in + if let region = external.region { + generalRulesWithAcceptence = generalRulesWithAcceptence.filter { rule in + rule.region?.lowercased() == region.lowercased() + } + } + + var generalRulesWithInvalidation = rules.filter { rule in return rule.countryCode.lowercased() == issuerCountryCode.lowercased() && rule.ruleType == .invalidation && rule.certificateFullType == .general && external.validationClock >= rule.validFromDate && external.validationClock <= rule.validToDate } + if let region = external.region { + generalRulesWithInvalidation = generalRulesWithInvalidation.filter { rule in + rule.region?.lowercased() == region.lowercased() + } + } + //General Rule with Acceptence type and max Version number if generalRulesWithAcceptence.count > 0 { if let maxRules = generalRulesWithAcceptence.max(by: { (ruleOne, ruleTwo) -> Bool in @@ -133,12 +145,23 @@ final public class CertLogicEngine { returnedRulesItems.append( maxRules) } } - let certTypeRulesWithAcceptence = rules.filter { rule in + var certTypeRulesWithAcceptence = rules.filter { rule in return rule.countryCode.lowercased() == external.countryCode.lowercased() && rule.ruleType == .acceptence && rule.certificateFullType == external.certificationType && external.validationClock >= rule.validFromDate && external.validationClock <= rule.validToDate } - let certTypeRulesWithInvalidation = rules.filter { rule in + if let region = external.region { + certTypeRulesWithAcceptence = certTypeRulesWithAcceptence.filter { rule in + rule.region?.lowercased() == region.lowercased() + } + } + + var certTypeRulesWithInvalidation = rules.filter { rule in return rule.countryCode.lowercased() == issuerCountryCode.lowercased() && rule.ruleType == .invalidation && rule.certificateFullType == external.certificationType && external.validationClock >= rule.validFromDate && external.validationClock <= rule.validToDate } + if let region = external.region { + certTypeRulesWithInvalidation = certTypeRulesWithInvalidation.filter { rule in + rule.region?.lowercased() == region.lowercased() + } + } //Rule with CertificationType with Acceptence type and max Version number if certTypeRulesWithAcceptence.count > 0 { diff --git a/Sources/CertLogic/ExternalParameter.swift b/Sources/CertLogic/ExternalParameter.swift index 04f7de4..d1988a0 100644 --- a/Sources/CertLogic/ExternalParameter.swift +++ b/Sources/CertLogic/ExternalParameter.swift @@ -19,9 +19,10 @@ public class ExternalParameter: Codable { public var iat: Date public var certificationType: CertificateType = .general public var kid: String? + public var region: String? enum CodingKeys: String, CodingKey { - case validationClock, valueSets, countryCode, exp, iat, issueCountryCode + case validationClock, valueSets, countryCode, exp, iat, issueCountryCode, kid, region } public init(validationClock: Date, @@ -30,7 +31,8 @@ public class ExternalParameter: Codable { exp: Date, iat: Date, certificationType: CertificateType, - issueCountryCode: String) { + issueCountryCode: String, + region: String? = nil) { self.validationClock = validationClock self.valueSets = valueSets self.countryCode = countryCode @@ -38,6 +40,7 @@ public class ExternalParameter: Codable { self.iat = iat self.certificationType = certificationType self.issueCountryCode = issueCountryCode + self.region = region } required public init(from decoder: Decoder) throws { @@ -48,5 +51,7 @@ public class ExternalParameter: Codable { exp = try container.decode(Date.self, forKey: .exp) iat = try container.decode(Date.self, forKey: .iat) issueCountryCode = try container.decode(String.self, forKey: .issueCountryCode) + kid = try? container.decode(String.self, forKey: .kid) + region = try? container.decode(String.self, forKey: .region) } } diff --git a/Sources/CertLogic/Rule.swift b/Sources/CertLogic/Rule.swift index 358cbbf..2bc9c10 100644 --- a/Sources/CertLogic/Rule.swift +++ b/Sources/CertLogic/Rule.swift @@ -113,7 +113,20 @@ public class Rule: Codable { } enum CodingKeys: String, CodingKey { - case identifier = "Identifier", type = "Type", version = "Version", schemaVersion = "SchemaVersion", engine = "Engine", engineVersion = "EngineVersion", certificateType = "CertificateType", description = "Description", validFrom = "ValidFrom", validTo = "ValidTo", affectedString = "AffectedFields", countryCode = "Country", logic = "Logic" + case identifier = "Identifier", + type = "Type", + version = "Version", + schemaVersion = "SchemaVersion", + engine = "Engine", + engineVersion = "EngineVersion", + certificateType = "CertificateType", + description = "Description", + validFrom = "ValidFrom", + validTo = "ValidTo", + affectedString = "AffectedFields", + countryCode = "Country", + logic = "Logic", + region } // Set Hash of JSON string @@ -134,7 +147,8 @@ public class Rule: Codable { validTo: String, affectedString: [String], logic: JSON, - countryCode: String) { + countryCode: String, + region: String? = nil) { self.identifier = identifier self.type = type self.version = version @@ -148,6 +162,7 @@ public class Rule: Codable { self.affectedString = affectedString self.logic = logic self.countryCode = countryCode + self.region = region } // Init Rule from JSON Data @@ -166,6 +181,7 @@ public class Rule: Codable { affectedString = try container.decode([String].self, forKey: .affectedString) logic = try container.decode(JSON.self, forKey: .logic) countryCode = try container.decode(String.self, forKey: .countryCode) + region = try? container.decode(String.self, forKey: .region) } public func encode(to encoder: Encoder) throws { @@ -183,6 +199,7 @@ public class Rule: Codable { try container.encode(affectedString, forKey: .affectedString) try container.encode(logic, forKey: .logic) try container.encode(countryCode, forKey: .countryCode) + try container.encode(region, forKey: .region) } }