-
Notifications
You must be signed in to change notification settings - Fork 17
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
WIP: Remove locksmith dependency #37
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d25057e
refactor locksmith code to CelySecureStorage
f08a723
replace locksmith with own implementation
8791a02
add swift format comment
608a66b
Replace LocksmithError with CelyKeychainStatus
4c158fd
Revert "Replace LocksmithError with CelyKeychainStatus"
92cb3b5
Refactor keychain code to separate internal class CelyKeychain
9857e95
update ownership on project
61facb7
address PR comments
3051b3b
Merge branch 'master' into remove-locksmith-dependency
initFabian File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,78 @@ | ||
// | ||
// CelyKeychain.swift | ||
// Cely-iOS | ||
// | ||
// Created by Fabian Buentello on 8/7/19. | ||
// Copyright © 2019 ChaiOne. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
internal struct CelyKeychain { | ||
|
||
// query to identify Cely Credentials | ||
private let baseQuery: [String: Any] = [ | ||
kSecClass as String: kSecClassInternetPassword, | ||
kSecAttrLabel as String: "cely.secure.store.key", | ||
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked | ||
] | ||
|
||
private var searchQuery: [String: Any] { | ||
var queryCopy = baseQuery | ||
queryCopy[kSecMatchLimit as String] = kSecMatchLimitOne | ||
queryCopy[kSecReturnAttributes as String] = true | ||
return queryCopy | ||
} | ||
|
||
func clearKeychain() -> StorageResult { | ||
let status = SecItemDelete(baseQuery as CFDictionary) | ||
let errorStatus = LocksmithError(fromStatusCode: Int(status)) | ||
guard errorStatus == .noError | ||
else { return StorageResult.fail(errorStatus) } | ||
return .success | ||
} | ||
|
||
func getCredentials() throws -> [String: Any] { | ||
var item: CFTypeRef? | ||
let status = SecItemCopyMatching(searchQuery as CFDictionary, &item) | ||
guard status != errSecItemNotFound else { throw LocksmithError.notFound } | ||
guard status == errSecSuccess else { throw LocksmithError.undefined } | ||
|
||
do { | ||
guard let existingItem = item as? [String : Any], | ||
let secureData = existingItem[kSecValueData as String] as? Data, | ||
let loadedDictionary = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(secureData) as? [String : Any] | ||
else { throw LocksmithError.decode } | ||
return loadedDictionary | ||
} | ||
} | ||
|
||
func set(_ secrets: [String : Any]) -> StorageResult { | ||
var queryCopy = baseQuery | ||
let storeData = NSKeyedArchiver.archivedData(withRootObject: secrets) | ||
queryCopy[kSecValueData as String] = storeData | ||
|
||
// try adding first | ||
let status: OSStatus = SecItemAdd(queryCopy as CFDictionary, nil) | ||
let err = LocksmithError(fromStatusCode: Int(status)) | ||
if err == .noError { | ||
return .success | ||
} else if err == .duplicate { | ||
// already exists, should update instead | ||
return update(secrets: secrets) | ||
} | ||
|
||
return .fail(err) | ||
} | ||
|
||
private func update(secrets: [String : Any]) -> StorageResult { | ||
let secretData = NSKeyedArchiver.archivedData(withRootObject: secrets) | ||
let updateDictionary = [kSecValueData as String: secretData] | ||
let status: OSStatus = SecItemUpdate(baseQuery as CFDictionary, updateDictionary as CFDictionary) | ||
let err = LocksmithError(fromStatusCode: Int(status)) | ||
if err == .noError { | ||
return .success | ||
} | ||
return .fail(err) | ||
} | ||
} |
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,54 @@ | ||
// | ||
// CelySecureStorage.swift | ||
// Cely-iOS | ||
// | ||
// Created by Fabian Buentello on 7/27/19. | ||
// Copyright © 2019 Fabian Buentello. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
internal class CelySecureStorage { | ||
var store: [String : Any] = [:] | ||
private let _celyKeychain = CelyKeychain() | ||
|
||
init() { | ||
do { | ||
let credentials = try _celyKeychain.getCredentials() | ||
store = credentials | ||
} catch let error as LocksmithError { | ||
print("Failed to retrieve store from keychain") | ||
print(error) | ||
} catch { | ||
print("Failed to retrieve store from keychain") | ||
print(error) | ||
} | ||
} | ||
|
||
func clearStorage() { | ||
let status = _celyKeychain.clearKeychain() | ||
if status != .success { | ||
print("Failed to clear keychain, error: \(status)") | ||
} else { | ||
store = [:] | ||
} | ||
} | ||
|
||
func set(_ value: Any, forKey key: String) -> StorageResult { | ||
var storeCopy = store | ||
storeCopy[key] = value | ||
let result = _celyKeychain.set(storeCopy) | ||
if result == .success { | ||
store[key] = value | ||
} | ||
return result | ||
} | ||
|
||
func get(_ key: String) -> Any? { | ||
if let value = store[key] { | ||
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. This is functionally equivalent to |
||
return value | ||
} | ||
|
||
return 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
Oops, something went wrong.
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.
I believe you can update your © for the project. 😛