-
-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Keychain-backed settings #536
Draft
paulhdk
wants to merge
9
commits into
maxgoedjen:main
Choose a base branch
from
paulhdk:settings-rfc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2a58323
Add SettingsStore
paulhdk 70cfdf8
Use keychain instead of UserDefaults in JustUpdatedChecker.swift
paulhdk 4602296
Add ability disable the SSH comment via settings
paulhdk a5a3f1c
Make SettingsStore functions non-static, re-implement them as
paulhdk 5cbc4e5
Add rawValues for CommentStyle enum
paulhdk e29dd20
Use SettingsStore for querying the comment style in SecretDetailView
paulhdk 52f3fea
Revert "Use keychain instead of UserDefaults in JustUpdatedChecker.sw…
paulhdk 748fc4e
Remove copyright info
paulhdk 77bf72e
Use "enum Constants" in SettingsStore
paulhdk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
import Foundation | ||
|
||
class SettingsStore: ObservableObject { | ||
enum Constants { | ||
static let service = "com.maxgoedjen.Secretive" | ||
} | ||
} | ||
|
||
extension SettingsStore { | ||
subscript(key: String) -> String? { | ||
set(value) { | ||
guard let valueData = value?.data(using: String.Encoding.utf8)! else { | ||
return | ||
} | ||
|
||
if let keyVal = self[key] { | ||
if keyVal == value { | ||
return | ||
} | ||
|
||
let updateQuery: [String: Any] = [kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrServer as String: Constants.service] | ||
let attributes: [String: Any] = [kSecAttrAccount as String: key, | ||
kSecValueData as String: valueData] | ||
// FIXME: Make this non-blocking as described here: https://developer.apple.com/documentation/security/1393617-secitemupdate | ||
let status = SecItemUpdate(updateQuery as CFDictionary, attributes as CFDictionary) | ||
guard status == errSecSuccess else { | ||
print("Couldn't update item in keychain. " + status.description) | ||
return | ||
} | ||
} else { | ||
let addquery: [String: Any] = [kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrAccount as String: key, | ||
kSecAttrServer as String: Constants.service, | ||
kSecValueData as String: valueData] | ||
// FIXME: Make this non-blocking as described here: https://developer.apple.com/documentation/security/1401659-secitemadd | ||
let status = SecItemAdd(addquery as CFDictionary, nil) | ||
guard status == errSecSuccess else { | ||
print("Couldn't add item to keychain. " + status.description) | ||
return | ||
} | ||
} | ||
} | ||
|
||
get { | ||
let getquery: [String: Any] = [kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrAccount as String: key, | ||
kSecAttrServer as String: Constants.service, | ||
kSecMatchLimit as String: kSecMatchLimitOne, | ||
kSecReturnData as String: true] | ||
var item: CFTypeRef? | ||
let status = SecItemCopyMatching(getquery as CFDictionary, &item) | ||
|
||
return status == errSecSuccess ? String(decoding: item as! Data, as: UTF8.self) : nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
import SwiftUI | ||
|
||
enum CommentStyle: String, CaseIterable, Identifiable { | ||
case keyAndHost = "keyAndHost" | ||
case none = "none" | ||
|
||
var id: Self { self } | ||
} | ||
|
||
struct GeneralSettingsView: View { | ||
@AppStorage("com.maxgoedjen.Secretive.commentStyle") var selectedCommentStyle: CommentStyle = .keyAndHost | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as secretdetailview, need to make sure this makes it to secure store not just defaults. |
||
|
||
var body: some View { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UI looks nice 👏 |
||
VStack(alignment: .leading) { | ||
Section(footer: Text("SSH public keys can be extended with an arbitrary comment string without changing the meaning of the key.") | ||
.font(.caption) | ||
.fontWeight(.light)) { | ||
Picker("SSH Public Key Comment", selection: $selectedCommentStyle) { | ||
Text("Default").tag(CommentStyle.keyAndHost) | ||
Text("None").tag(CommentStyle.none) | ||
} | ||
.pickerStyle(DefaultPickerStyle()) | ||
} | ||
} | ||
.padding(20) | ||
.frame(width: 350, height: 100) | ||
.navigationTitle("Settings") | ||
} | ||
} | ||
|
||
|
||
struct SettingsView: View { | ||
private enum Tabs: Hashable { | ||
case general | ||
} | ||
var body: some View { | ||
TabView { | ||
GeneralSettingsView() | ||
.tabItem { | ||
Label("General", systemImage: "gear") | ||
} | ||
.tag(Tabs.general) | ||
} | ||
.padding(20) | ||
.frame(width: 500, height: 200) | ||
} | ||
} | ||
|
||
#Preview { | ||
SettingsView() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this method gets more complicated, it MIGHT make sense to move it into
OpenSSHKeyWriter
– just instead of taking acomment: String
parameter, we define that enum insideOpenSSHKeyWriter
and pass an enum valuecommentStyle: CommentStyle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dashedKeyName
anddashedHostName
are both defined inSecretDetailView.swift
. That means we would also have to move those intoOpenSSHKeyWriter.swift
too, yes?EDIT: forgot to tag you, @maxgoedjen 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maxgoedjen just giving this a polite bump in case it got lost 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah they're just there since I think that's the only place that used them til now - feel free to move them somewhere more appropriate