Files
ShadowsocksX-NG/ShadowsocksX-NG2/Domain/CredentialStore.swift
T
Qiu Yuzhou 00fcd61a99 refactor(ng2): change bundle id to ShadowsocksX-NG2
Distinct id from Legacy (com.qiuyuzhou.ShadowsocksX-NG) lets NG2 own
~/Library/Application Support/ShadowsocksX-NG2/ directly; the v2/
subdir prefix is removed from all runtime paths.

- app/agent/tests targets + LaunchAgent plist renamed accordingly
- keychain service, queue labels, and Vendor signIdentifiers follow
  the new bundle id; embedded helpers re-sign at build time
- Legacy handoff/import constants keep pointing at the old id

Migration: dev data under ShadowsocksX-NG/v2/ is not carried over
(fresh catalog/activation); old LaunchAgent registration and old
keychain entries must be cleaned up manually.
2026-09-21 20:33:37 +08:00

79 lines
3.1 KiB
Swift

import Foundation
import Security
/// 凭据存储缝(spec #21 D5):密码、插件参数、非空远程 URL 等秘密值的权威持久
/// 副本在 Keychain,配置树只持凭据引用。实现须保证写入/读出一致。
protocol CredentialStoring {
/// 写入即覆盖既有值(同一引用始终对应最新秘密)。
func save(_ secret: String, for reference: CredentialReference) throws
/// 无对应条目返回 `nil`,不视为错误。
func secret(for reference: CredentialReference) throws -> String?
/// 删除不存在的条目不视为错误(幂等)。
func delete(_ reference: CredentialReference) throws
}
enum CredentialStoreError: Error, Equatable {
/// Keychain 返回预期之外的状态码。
case keychainStatus(OSStatus)
/// 存储的秘密值不是合法 UTF-8 文本。
case secretNotUTF8
}
/// 生产实现:登录钥匙串 Generic Password。`service` 隔离本应用命名空间,
/// `account` 即凭据引用;可访问性取「首次解锁后」,兼顾代理运行时读取。
struct KeychainCredentialStore: CredentialStoring {
let service: String
init(service: String = "com.qiuyuzhou.ShadowsocksX-NG2.credentials") {
self.service = service
}
func save(_ secret: String, for reference: CredentialReference) throws {
let data = Data(secret.utf8)
let query = baseQuery(for: reference)
let update: [String: Any] = [
kSecValueData as String: data,
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock,
]
var status = SecItemUpdate(query as CFDictionary, update as CFDictionary)
if status == errSecItemNotFound {
var addItem = query
addItem[kSecValueData as String] = data
addItem[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
status = SecItemAdd(addItem as CFDictionary, nil)
}
guard status == errSecSuccess else { throw CredentialStoreError.keychainStatus(status) }
}
func secret(for reference: CredentialReference) throws -> String? {
var query = baseQuery(for: reference)
query[kSecReturnData as String] = kCFBooleanTrue as Any
query[kSecMatchLimit as String] = kSecMatchLimitOne
var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
if status == errSecItemNotFound { return nil }
guard status == errSecSuccess, let data = result as? Data else {
throw CredentialStoreError.keychainStatus(status)
}
guard let secret = String(data: data, encoding: .utf8) else {
throw CredentialStoreError.secretNotUTF8
}
return secret
}
func delete(_ reference: CredentialReference) throws {
let status = SecItemDelete(baseQuery(for: reference) as CFDictionary)
guard status == errSecSuccess || status == errSecItemNotFound else {
throw CredentialStoreError.keychainStatus(status)
}
}
private func baseQuery(for reference: CredentialReference) -> [String: Any] {
[
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: reference.rawValue,
]
}
}