Skip to content

Commit

Permalink
Make generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
yspreen committed Apr 26, 2021
1 parent a2deb95 commit d0806cc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
2 changes: 2 additions & 0 deletions DGCAVerifier/Models/LocalData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ struct LocalData: Codable {
static func set(resumeToken: String) {
sharedInstance.resumeToken = resumeToken
}

static let storage = SecureStorage<LocalData>()
}
42 changes: 18 additions & 24 deletions DGCAVerifier/Services/SecureStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,28 @@ struct SecureDB: Codable {
let signature: Data
}

struct SecureStorage {
static let documents: URL! = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
static let path: URL! = URL(string: documents.absoluteString + "secure.db")
struct SecureStorage<T: Codable> {
let documents: URL! = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
var path: URL! { URL(string: documents.absoluteString + "secure.db") }

/**
Loads encrypted db and overrides it with an empty one if that fails.
*/
public static func loadOverride(completion: ((Bool) -> Void)? = nil) {
load { success in
if success {
completion?(true)
public func loadOverride(fallback: T, completion: ((T?) -> Void)? = nil) {
load { result in
if result != nil {
completion?(result)
return
}
save { _ in
save(fallback) { _ in
load(completion: completion)
}
}
}

public static func load(completion: ((Bool) -> Void)? = nil) {
public func load(completion: ((T?) -> Void)? = nil) {
if !FileManager.default.fileExists(atPath: path.path) {
save()
if FileManager.default.fileExists(atPath: path.path) {
load(completion: completion)
} else {
completion?(false)
}
completion?(nil)
return
}

Expand All @@ -68,26 +63,25 @@ struct SecureStorage {
let key = Enclave.symmetricKey,
Enclave.verify(data: data, signature: signature, with: key).0
else {
completion?(false)
completion?(nil)
return
}
Enclave.decrypt(data: data, with: key) { decrypted, err in
guard
let decrypted = decrypted,
err == nil,
let data = try? JSONDecoder().decode(LocalData.self, from: decrypted)
let data = try? JSONDecoder().decode(T.self, from: decrypted)
else {
completion?(false)
completion?(nil)
return
}
LocalData.sharedInstance = data
completion?(true)
completion?(data)
}
}

public static func save(completion: ((Bool) -> Void)? = nil) {
public func save(_ instance: T, completion: ((Bool) -> Void)? = nil) {
guard
let data = try? JSONEncoder().encode(LocalData.sharedInstance),
let data = try? JSONEncoder().encode(instance),
let key = Enclave.symmetricKey,
let encrypted = Enclave.encrypt(data: data, with: key).0
else {
Expand All @@ -106,7 +100,7 @@ struct SecureStorage {
}
}

static func write(data: Data, signature: Data) -> Bool {
func write(data: Data, signature: Data) -> Bool {
guard
let rawData = try? JSONEncoder().encode(SecureDB(data: data, signature: signature)),
let _ = try? rawData.write(to: path)
Expand All @@ -116,7 +110,7 @@ struct SecureStorage {
return true
}

static func read() -> (Data, Data)? {
func read() -> (Data, Data)? {
guard
let rawData = try? Data(contentsOf: path),
let result = try? JSONDecoder().decode(SecureDB.self, from: rawData)
Expand Down
5 changes: 3 additions & 2 deletions DGCAVerifier/ViewControllers/Home.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ class HomeVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

SecureStorage.loadOverride { [weak self] success in
guard success else {
LocalData.storage.loadOverride(fallback: LocalData.sharedInstance) { [weak self] success in
guard let result = success else {
return
}
LocalData.sharedInstance = result
DispatchQueue.main.async {
self?.performSegue(withIdentifier: "scanner", sender: self)
}
Expand Down

0 comments on commit d0806cc

Please sign in to comment.