-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #209 from czechboy0/hd/keychain
[WIP] Storing credentials in Keychain
- Loading branch information
Showing
34 changed files
with
775 additions
and
227 deletions.
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
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,96 @@ | ||
// | ||
// SecurePersistence.swift | ||
// Buildasaur | ||
// | ||
// Created by Honza Dvorsky on 1/22/16. | ||
// Copyright © 2016 Honza Dvorsky. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import KeychainAccess | ||
import XcodeServerSDK | ||
import SwiftSafe | ||
|
||
final class SecurePersistence { | ||
|
||
#if TESTING | ||
typealias Keychain = NSMutableDictionary | ||
#endif | ||
|
||
static let Prefix = "com.honzadvorsky.buildasaur" | ||
|
||
private let keychain: Keychain | ||
private let safe: Safe | ||
|
||
private init(keychain: Keychain, safe: Safe = EREW()) { | ||
self.keychain = keychain | ||
self.safe = safe | ||
} | ||
|
||
static func xcodeServerPasswordKeychain() -> SecurePersistence { | ||
return self.keychain("\(Prefix).xcs.password") | ||
} | ||
|
||
static func sourceServerTokenKeychain() -> SecurePersistence { | ||
return self.keychain("\(Prefix).source_server.oauth_tokens") | ||
} | ||
|
||
static func sourceServerPassphraseKeychain() -> SecurePersistence { | ||
return self.keychain("\(Prefix).source_server.passphrase") | ||
} | ||
|
||
static private func keychain(service: String) -> SecurePersistence { | ||
#if TESTING | ||
let keychain = NSMutableDictionary() | ||
#else | ||
let keychain = Keychain(service: service) | ||
#endif | ||
return self.init(keychain: keychain) | ||
} | ||
|
||
func read(key: String) -> String? { | ||
var val: String? | ||
self.safe.read { | ||
#if TESTING | ||
val = self.keychain[key] as? String | ||
#else | ||
val = self.keychain[key] | ||
#endif | ||
} | ||
return val | ||
} | ||
|
||
func writeIfNeeded(key: String, value: String?) { | ||
self.safe.write { | ||
self.updateIfNeeded(key, value: value) | ||
} | ||
} | ||
|
||
private func updateIfNeeded(key: String, value: String?) { | ||
#if TESTING | ||
let existing = self.keychain[key] as? String | ||
#else | ||
let existing = self.keychain[key] | ||
#endif | ||
if existing != value { | ||
self.keychain[key] = value | ||
} | ||
} | ||
} | ||
|
||
public protocol KeychainSaveable { | ||
func keychainKey() -> String | ||
} | ||
|
||
extension XcodeServerConfig: KeychainSaveable { | ||
public func keychainKey() -> String { | ||
return "\(self.host):\(self.user ?? "")" | ||
} | ||
} | ||
|
||
extension ProjectConfig: KeychainSaveable { | ||
public func keychainKey() -> String { | ||
return self.id | ||
} | ||
} | ||
|
Oops, something went wrong.