Skip to content

Commit

Permalink
feat: double scan
Browse files Browse the repository at this point in the history
Co-authored-by: Ludovico Girolimini <[email protected]>
Co-authored-by: eapuzzo <[email protected]>
Co-authored-by: Light2288 <[email protected]>
  • Loading branch information
4 people authored Mar 2, 2022
1 parent c074dd2 commit 53a1b35
Show file tree
Hide file tree
Showing 59 changed files with 3,072 additions and 1,577 deletions.
10 changes: 5 additions & 5 deletions DGCAVerifier/BusinessRules/Internal/VaccineValidityCheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct VaccineValidityCheck {
guard let validityEnd = preconditions.vaccineDate.add(end, ofType: .day)?.startOfDay else { return .notValid }

guard let currentDate = Date.startOfDay else { return .notValid }
// J&J booster is immediately valid
let fromDate = preconditions.isJJBooster ? preconditions.vaccineDate : validityStart

Expand Down Expand Up @@ -209,7 +209,7 @@ struct VaccineValidityCheck {
}

if preconditions.isCurrentDoseIncomplete {
return self.getValue(for: Constants.vaccineIncompleteStartDays, type: preconditions.medicalProduct)?.intValue
return self.getValue(for: Constants.vaccineIncompleteStartDays, type: preconditions.medicalProduct)?.intValue
}

if preconditions.isJJ {
Expand Down Expand Up @@ -264,9 +264,9 @@ struct VaccineValidityCheck {
return self.getValue(for: Constants.vaccineBoosterEndDays_IT)?.intValue
}

if preconditions.isCurrentDoseIncomplete {
return self.getValue(for: Constants.vaccineIncompleteEndDays, type: preconditions.medicalProduct)?.intValue
}
if preconditions.isCurrentDoseIncomplete {
return self.getValue(for: Constants.vaccineIncompleteEndDays, type: preconditions.medicalProduct)?.intValue
}

return self.getValue(for: Constants.vaccineSchoolEndDays)?.intValue
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ struct BoosterValidatorFactory: DCGValidatorFactory {
struct SchoolValidatorFactory: DCGValidatorFactory {

func getValidator(hcert: HCert) -> DGCValidator? {
let isIT = hcert.countryCode == Constants.ItalyCountryCode
let isIT = hcert.countryCode == Constants.ItalyCountryCode
switch hcert.extendedType {
case .unknown:
return UnknownValidator()
case .vaccine:
return isIT ? VaccineSchoolValidator() : VaccineSchoolValidatorNotItaly()
return isIT ? VaccineSchoolValidator() : VaccineSchoolValidatorNotItaly()
case .recovery:
return RecoverySchoolValidator()
case .test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class DGCValidatorBuilder {
var checkHCert: Bool = true
var checkBlackList: Bool = true
var checkRevocationList: Bool = true
var checkTestOnly: Bool = false
var mode: ScanMode?


func checkHCert(_ check: Bool) -> Self {
self.checkHCert = check
return self
Expand All @@ -49,26 +49,36 @@ class DGCValidatorBuilder {
return self
}

func checkTestOnly(_ check: Bool) -> Self {
self.checkTestOnly = check
return self
}

func scanMode(_ mode: ScanMode) -> Self {
self.mode = mode
return self
}

func build(hCert: HCert) -> DGCValidator? {
var validators: [DGCValidator] = []
if checkHCert {

if self.checkHCert {
validators.append(HCertValidator())
}

if checkBlackList {
if self.checkBlackList {
validators.append(BlackListValidator())
}

if (checkRevocationList) {
if self.checkRevocationList {
validators.append(RevocationValidator())
}

if self.checkTestOnly {
validators.append(TestOnlyValidator())
}

if let scanMode = mode {
if let scanMode = mode, !self.checkTestOnly {
let factory = ValidatorProducer.getProducer(scanMode: scanMode)
if let validator = factory?.getValidator(hcert: hCert) {
validators.append(validator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,41 @@ import Foundation
import SwiftDGC

struct RecoveryInfo {
private var hcert: HCert!
public var isCBIS: Bool {
guard self.hcert.rcountryCode?.uppercased() == Constants.ItalyCountryCode.uppercased() else { return false }
guard let signedCerficate = self.hcert.signedCerficate else { return false }
let extendedKeyUsage = signedCerficate.extendedKeyUsage
let validKeysUsages = extendedKeyUsage.filter{ $0 == Constants.OID_RECOVERY || $0 == Constants.OID_RECOVERY_ALT }
return !validKeysUsages.isEmpty
}
public var isIT: Bool {
return self.hcert.countryCode == Constants.ItalyCountryCode
}
var patientOver50: Bool {
guard let age = self.hcert.age else { return false }
return age >= 50
}
public static func from(hcert: HCert) -> RecoveryInfo {
return RecoveryInfo(hcert: hcert)
}
private var hcert: HCert!
public var isCBIS: Bool {
guard self.hcert.rcountryCode?.uppercased() == Constants.ItalyCountryCode.uppercased() else { return false }
guard let signedCerficate = self.hcert.signedCerficate else { return false }
let extendedKeyUsage = signedCerficate.extendedKeyUsage
let validKeysUsages = extendedKeyUsage.filter{ $0 == Constants.OID_RECOVERY || $0 == Constants.OID_RECOVERY_ALT }
return !validKeysUsages.isEmpty
}
public var isIT: Bool {
return self.hcert.countryCode == Constants.ItalyCountryCode
}
var patientOver50: Bool {
guard let age = self.hcert.age else { return false }
return age >= 50
}
public static func from(hcert: HCert) -> RecoveryInfo {
return RecoveryInfo(hcert: hcert)
}
}

class RecoveryBaseValidator: DGCValidator {

typealias Validator = RecoveryBaseValidator
fileprivate var recoveryInfo: RecoveryInfo!
fileprivate var recoveryInfo: RecoveryInfo!

func validate(hcert: HCert) -> Status {
self.recoveryInfo = RecoveryInfo.from(hcert: hcert)
self.recoveryInfo = RecoveryInfo.from(hcert: hcert)
guard let validityFrom = hcert.recoveryDateFrom?.toRecoveryDate else { return .notValid }
guard let validityUntil = hcert.recoveryDateUntil?.toRecoveryDate else { return .notValid }

Expand Down Expand Up @@ -118,29 +118,29 @@ class RecoveryBaseValidator: DGCValidator {
}

class RecoveryReinforcedValidator: RecoveryBaseValidator {
override func getStartDays(from hcert: HCert) -> Int? {
let startDaysConfig: String
if isSpecialRecovery(hcert: hcert) {
startDaysConfig = Constants.recoverySpecialStartDays
}
else {
startDaysConfig = Constants.recoveryStartDays_IT
}
return getValue(for: startDaysConfig)?.intValue
}
override func getEndDays(from hcert: HCert) -> Int? {
let endDaysConfig: String
if isSpecialRecovery(hcert: hcert) {
endDaysConfig = Constants.recoverySpecialEndDays
}
else {
endDaysConfig = Constants.recoveryEndDays_IT
}
return getValue(for: endDaysConfig)?.intValue
}
override func getStartDays(from hcert: HCert) -> Int? {
let startDaysConfig: String
if isSpecialRecovery(hcert: hcert) {
startDaysConfig = Constants.recoverySpecialStartDays
}
else {
startDaysConfig = Constants.recoveryStartDays_IT
}
return getValue(for: startDaysConfig)?.intValue
}
override func getEndDays(from hcert: HCert) -> Int? {
let endDaysConfig: String
if isSpecialRecovery(hcert: hcert) {
endDaysConfig = Constants.recoverySpecialEndDays
}
else {
endDaysConfig = Constants.recoveryEndDays_IT
}
return getValue(for: endDaysConfig)?.intValue
}
}

class RecoveryBoosterValidator: RecoveryReinforcedValidator {
Expand All @@ -161,36 +161,36 @@ class RecoveryBoosterValidator: RecoveryReinforcedValidator {

return validate(currentDate, from: validityStart, to: validityEnd)
}
func validate(_ current: Date, from validityStart: Date, to validityEnd: Date) -> Status {
switch current {
case ..<validityStart:
return .notValidYet
case validityStart...validityEnd:
return self.recoveryInfo.isCBIS ? .valid : .verificationIsNeeded
default:
return .expired
}
}
func validate(_ current: Date, from validityStart: Date, to validityEnd: Date) -> Status {
switch current {
case ..<validityStart:
return .notValidYet
case validityStart...validityEnd:
return self.recoveryInfo.isCBIS ? .valid : .verificationIsNeeded
default:
return .expired
}
}
}

class RecoverySchoolValidator: RecoveryBaseValidator {
override func validate(hcert: HCert) -> Status {
guard let validityFrom = hcert.recoveryDateFirstPositive?.toRecoveryDate else { return .notValid }
guard let recoveryStartDays = getStartDays(from: hcert) else { return .notValid }
guard let recoveryEndDays = getEndDays(from: hcert) else { return .notValid }
guard let validityStart = validityFrom.add(recoveryStartDays, ofType: .day) else { return .notValid }
guard let validityEnd = validityFrom.add(recoveryEndDays, ofType: .day) else { return .notValid }
guard let currentDate = Date.startOfDay else { return .notValid }
return self.validate(currentDate, from: validityStart, to: validityEnd)
}
override func validate(hcert: HCert) -> Status {
guard let validityFrom = hcert.recoveryDateFirstPositive?.toRecoveryDate else { return .notValid }
guard let recoveryStartDays = getStartDays(from: hcert) else { return .notValid }
guard let recoveryEndDays = getEndDays(from: hcert) else { return .notValid }
guard let validityStart = validityFrom.add(recoveryStartDays, ofType: .day) else { return .notValid }
guard let validityEnd = validityFrom.add(recoveryEndDays, ofType: .day) else { return .notValid }
guard let currentDate = Date.startOfDay else { return .notValid }
return self.validate(currentDate, from: validityStart, to: validityEnd)
}

override func getEndDays(from hcert: HCert) -> Int? {
var endDaysConfig = Constants.recoverySchoolEndDays
Expand All @@ -215,15 +215,15 @@ class RecoverySchoolValidator: RecoveryBaseValidator {
}

class RecoveryWorkValidator: RecoveryBaseValidator {
override func getStartDays(from hcert: HCert) -> Int? {
return super.getStartDays(from: hcert)
}
override func getEndDays(from hcert: HCert) -> Int? {
override func getStartDays(from hcert: HCert) -> Int? {
return super.getStartDays(from: hcert)
}
override func getEndDays(from hcert: HCert) -> Int? {
return super.getEndDays(from: hcert)
}
}
}

class RecoveryItalyEntryValidator: RecoveryBaseValidator {
Expand Down
Loading

0 comments on commit 53a1b35

Please sign in to comment.