Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Reset CoreData if on old model (needs a better migration policy)
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanLipnik committed Aug 24, 2021
1 parent 23c2544 commit 1dab1f7
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 4 deletions.
Binary file not shown.
24 changes: 24 additions & 0 deletions Shared/OpenSesame.xcdatamodeld/Shared 1.1.xcdatamodel/contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="19197" systemVersion="21A5304g" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithCloudKit="YES" userDefinedModelVersionIdentifier="">
<entity name="Account" representedClassName="Account" syncable="YES" codeGenerationType="class">
<attribute name="dateAdded" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="domain" attributeType="String" defaultValueString=""/>
<attribute name="isPinned" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="lastModified" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="notes" optional="YES" attributeType="String"/>
<attribute name="otpAuth" optional="YES" attributeType="String"/>
<attribute name="password" optional="YES" attributeType="Binary" defaultValueString="" allowsCloudEncryption="YES"/>
<attribute name="passwordLength" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="url" attributeType="String" defaultValueString=""/>
<attribute name="username" attributeType="String" defaultValueString=""/>
<relationship name="vault" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Vault" inverseName="accounts" inverseEntity="Vault"/>
</entity>
<entity name="Vault" representedClassName="Vault" syncable="YES" codeGenerationType="class">
<attribute name="name" attributeType="String" defaultValueString=""/>
<relationship name="accounts" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Account" inverseName="vault" inverseEntity="Account"/>
</entity>
<elements>
<element name="Account" positionX="-54" positionY="9" width="128" height="194"/>
<element name="Vault" positionX="-63" positionY="-18" width="128" height="59"/>
</elements>
</model>
4 changes: 1 addition & 3 deletions Shared/OpenSesame.xcdatamodeld/Shared.xcdatamodel/contents
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
<entity name="Account" representedClassName="Account" syncable="YES" codeGenerationType="class">
<attribute name="dateAdded" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="domain" attributeType="String" defaultValueString=""/>
<attribute name="encryptionTag" attributeType="String" defaultValueString=""/>
<attribute name="isPinned" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="lastModified" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="nonce" optional="YES" attributeType="String"/>
<attribute name="notes" optional="YES" attributeType="String"/>
<attribute name="otpAuth" optional="YES" attributeType="String"/>
<attribute name="password" optional="YES" attributeType="Binary" defaultValueString="" allowsCloudEncryption="YES"/>
Expand All @@ -20,7 +18,7 @@
<relationship name="accounts" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Account" inverseName="vault" inverseEntity="Account"/>
</entity>
<elements>
<element name="Account" positionX="-54" positionY="9" width="128" height="224"/>
<element name="Account" positionX="-54" positionY="9" width="128" height="194"/>
<element name="Vault" positionX="-63" positionY="-18" width="128" height="59"/>
</elements>
</model>
138 changes: 138 additions & 0 deletions Shared/OpenSesameMapping.xcmappingmodel/xcmapping.xml

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Shared/Persistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation
import CoreData
import KeychainAccess

struct PersistenceController {
static let shared = PersistenceController()
Expand Down Expand Up @@ -52,6 +53,16 @@ struct PersistenceController {

print("CoreData location", PersistenceController.storeURL.path)
}

if let coreDataVersion = UserDefaults(suiteName: "group.OpenSesame.ethanlipnik")?.float(forKey: "coreDataVersion"), coreDataVersion < 1.1 {
try? FileManager.default.removeItem(at: PersistenceController.storeURL)
try? Keychain(service: "com.ethanlipnik.OpenSesame", accessGroup: "B6QG723P8Z.OpenSesame")
.synchronizable(true)
.remove("encryptionTest")

UserDefaults(suiteName: "group.OpenSesame.ethanlipnik")?.set(1.1, forKey: "coreDataVersion")
}

let viewContext = container.viewContext
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
Expand Down
6 changes: 5 additions & 1 deletion Shared/Views/ContentView/ContentView+List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ extension ContentView {
Section("Pinned") {
ForEach(pinnedAccounts) { account in
NavigationLink {
VaultView(vault: account.vault!, selectedAccount: account)
if let vault = account.vault {
VaultView(vault: vault, selectedAccount: account)
} else {
Text("Failed to get vault for pinned account")
}
} label: {
VStack(alignment: .leading) {
Text(account.domain!.capitalizingFirstLetter())
Expand Down
2 changes: 2 additions & 0 deletions Shared/Views/LockView/LockView+CreatePassword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extension LockView {
.font(.title.bold())
GroupBox {
TextField("Enter a new master password", text: $password, onCommit: {
guard !password.isEmpty else { return }
completionAction(password)
})
.font(.system(.body, design: .monospaced))
Expand All @@ -28,6 +29,7 @@ extension LockView {
#endif
}
Button("Continue") {
guard !password.isEmpty else { return }
completionAction(password)
}
}
Expand Down

0 comments on commit 1dab1f7

Please sign in to comment.