Skip to content

Commit fe3501f

Browse files
committed
Misc keychain changes
1 parent bbf7866 commit fe3501f

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

Sources/MatrixClient/API/Auth/Login.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ extension MatrixLoginFlowType: ExpressibleByStringLiteral {
125125
}
126126

127127
public struct MatrixLoginFlow {
128+
public init(type: MatrixLoginFlowType, identiyProviders: [MatrixLoginFlow.IdentityProvider]? = nil, extraInfo: [String : AnyCodable] = [:]) {
129+
self.type = type
130+
self.identiyProviders = identiyProviders
131+
self.extraInfo = extraInfo
132+
}
133+
128134
public var type: MatrixLoginFlowType
129135

130136
public var identiyProviders: [IdentityProvider]?

Sources/MatrixClient/MatrixClient/MatrixClient+Auth.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,27 @@ public extension MatrixClient {
115115
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
116116
func login(
117117
token: Bool = false,
118-
username: String,
118+
username: String? = nil,
119119
password: String,
120120
displayName: String? = nil,
121121
deviceId: String? = nil
122122
) async throws -> MatrixLogin {
123123
let flow: MatrixLoginFlowType
124+
let identifier: MatrixLoginUserIdentifier?
124125
if token {
125126
flow = .token
127+
identifier = nil
126128
} else {
127129
flow = .password
130+
guard let username else {
131+
throw MatrixCommonErrorCode.missingParam
132+
}
133+
identifier = .user(id: username)
128134
}
135+
129136
var request = MatrixLoginRequest(
130137
type: flow.rawValue,
131-
identifier: MatrixLoginUserIdentifier.user(id: username),
138+
identifier: identifier,
132139
deviceId: deviceId,
133140
initialDeviceDisplayName: displayName
134141
)

Sources/MatrixCore/MatrixStore.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import OSLog
1414
public protocol MatrixStore {
1515
// MARK: - Account Info
1616

17+
static var extraKeychainArguments: [String: Any] { get }
18+
1719
/// Type for Account Informations.
1820
associatedtype AccountInfo: MatrixStoreAccountInfo
1921
//associatedtype RoomState: MatrixStoreRoomState

Sources/MatrixCore/MatrixStoreAccountInfo.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public extension MatrixStoreAccountInfo {
3838
var dict = dict
3939
dict[kSecClass as String] = kSecClassGenericPassword
4040
dict[kSecAttrAccount as String] = mxID.FQMXID
41-
//dict[kSecUseDataProtectionKeychain as String] = true
41+
dict[kSecUseDataProtectionKeychain as String] = true
4242
if dict[kSecAttrLabel as String] == nil {
4343
dict[kSecAttrLabel as String] = MXkSecAttrLabel
4444
}
@@ -73,6 +73,10 @@ public extension MatrixStoreAccountInfo {
7373

7474
/// Save the ``accessToken`` to keychain, using the accountID data as identifier.
7575
func saveToKeychain(extraKeychainArguments: [String: Any] = [:]) throws {
76+
do {
77+
try self.deleteFromKeychain(extraKeychainArguments: extraKeychainArguments)
78+
}
79+
7680
guard let accessToken = self.accessToken?.data(using: .utf8)
7781
else {
7882
throw MatrixErrorCode.NotFound
@@ -133,8 +137,15 @@ public extension MatrixStore {
133137
func getAccounts() async throws -> [MatrixCore<Self>] {
134138
let accounts = try await getAccountInfos()
135139

136-
return accounts.map { MatrixCore(store: self, account: $0) }
137-
// Init MatrixCore.client somehow?
140+
var cores: [MatrixCore<Self>] = []
141+
142+
for account in accounts {
143+
var account = account
144+
try account.loadAccessToken(extraKeychainArguments: Self.extraKeychainArguments)
145+
cores.append(MatrixCore(store: self, account: account))
146+
}
147+
148+
return cores
138149
}
139150

140151
/// Create ``MatrixCore`` instance for the given MatrixUser ID with data from the store.

0 commit comments

Comments
 (0)