Skip to content

Commit

Permalink
Bugfixes/issue 16 (eu-digital-green-certificates#18)
Browse files Browse the repository at this point in the history
* fixed issue eu-digital-green-certificates#16
Add Region to external parameters/filter criterias

* added logic to filer by region

Co-authored-by: Alexandr Chernyy <[email protected]>
  • Loading branch information
alexchornyi and pingus-nikalex authored Jul 1, 2021
1 parent 76244f8 commit 14bcdce
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
35 changes: 29 additions & 6 deletions Sources/CertLogic/CertLogic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions Sources/CertLogic/ExternalParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,14 +31,16 @@ 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
self.exp = exp
self.iat = iat
self.certificationType = certificationType
self.issueCountryCode = issueCountryCode
self.region = region
}

required public init(from decoder: Decoder) throws {
Expand All @@ -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)
}
}
21 changes: 19 additions & 2 deletions Sources/CertLogic/Rule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
}

}
Expand Down

0 comments on commit 14bcdce

Please sign in to comment.