From e2de0f1831b8fdc68d338bf39ed77e51ab37d84e Mon Sep 17 00:00:00 2001 From: Katherine Bertelsen Date: Mon, 14 Oct 2024 15:00:14 -0500 Subject: [PATCH 1/4] Add isPreAuth to clientservice.generators --- .../Core/Auth/Services/AuthService.swift | 4 ++-- .../Core/Platform/Services/ClientService.swift | 18 ++++++++++++------ .../Repositories/GeneratorRepository.swift | 8 ++++---- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/BitwardenShared/Core/Auth/Services/AuthService.swift b/BitwardenShared/Core/Auth/Services/AuthService.swift index e9ad44faa..9cf34d9cc 100644 --- a/BitwardenShared/Core/Auth/Services/AuthService.swift +++ b/BitwardenShared/Core/Auth/Services/AuthService.swift @@ -407,11 +407,11 @@ class DefaultAuthService: AuthService { // swiftlint:disable:this type_body_leng minNumber: 1, minSpecial: 0 ) - codeVerifier = try await clientService.generators().password(settings: passwordSettings) + codeVerifier = try await clientService.generators(isPreAuth: true).password(settings: passwordSettings) let codeChallenge = Data(codeVerifier.utf8) .generatedHashBase64Encoded(using: SHA256.self) .urlEncoded() - let state = try await clientService.generators().password(settings: passwordSettings) + let state = try await clientService.generators(isPreAuth: true).password(settings: passwordSettings) let queryItems = [ URLQueryItem(name: "client_id", value: Constants.clientType), diff --git a/BitwardenShared/Core/Platform/Services/ClientService.swift b/BitwardenShared/Core/Platform/Services/ClientService.swift index 5be4b42d5..de06d1dc6 100644 --- a/BitwardenShared/Core/Platform/Services/ClientService.swift +++ b/BitwardenShared/Core/Platform/Services/ClientService.swift @@ -32,10 +32,13 @@ protocol ClientService { /// Returns a `ClientGeneratorsProtocol` for generator data tasks. /// - /// - Parameter userId: The user ID mapped to the client instance. + /// - Parameters: + /// - userId: The user ID mapped to the client instance. + /// - isPreAuth: Whether the client is being used for a user prior to authentication (when + /// the user's ID doesn't yet exist). /// - Returns: A `ClientGeneratorsProtocol` for generator data tasks. /// - func generators(for userId: String?) async throws -> ClientGeneratorsProtocol + func generators(for userId: String?, isPreAuth: Bool) async throws -> ClientGeneratorsProtocol /// Returns a `ClientPlatformService` for client platform tasks. /// @@ -97,8 +100,11 @@ extension ClientService { /// Returns a `ClientGeneratorsProtocol` for generator data tasks. /// - func generators() async throws -> ClientGeneratorsProtocol { - try await generators(for: nil) + /// - Parameter isPreAuth: Whether the client is being used for a user prior to authentication + /// (when the user's ID doesn't yet exist). This primarily will happen in SSO flows. + /// + func generators(isPreAuth: Bool) async throws -> ClientGeneratorsProtocol { + try await generators(for: nil, isPreAuth: isPreAuth) } /// Returns a `ClientPlatformService` for client platform tasks. @@ -203,8 +209,8 @@ actor DefaultClientService: ClientService { try await client(for: userId).exporters() } - func generators(for userId: String?) async throws -> ClientGeneratorsProtocol { - try await client(for: userId).generators() + func generators(for userId: String?, isPreAuth: Bool = false) async throws -> ClientGeneratorsProtocol { + try await client(for: userId, isPreAuth: isPreAuth).generators() } func platform(for userId: String?) async throws -> ClientPlatformService { diff --git a/BitwardenShared/Core/Tools/Repositories/GeneratorRepository.swift b/BitwardenShared/Core/Tools/Repositories/GeneratorRepository.swift index 96c18a974..871883ae4 100644 --- a/BitwardenShared/Core/Tools/Repositories/GeneratorRepository.swift +++ b/BitwardenShared/Core/Tools/Repositories/GeneratorRepository.swift @@ -171,7 +171,7 @@ extension DefaultGeneratorRepository: GeneratorRepository { // MARK: Generator func generateMasterPassword() async throws -> String { - try await clientService.generators().passphrase( + try await clientService.generators(isPreAuth: false).passphrase( settings: PassphraseGeneratorRequest( numWords: 3, wordSeparator: "-", @@ -182,15 +182,15 @@ extension DefaultGeneratorRepository: GeneratorRepository { } func generatePassphrase(settings: PassphraseGeneratorRequest) async throws -> String { - try await clientService.generators().passphrase(settings: settings) + try await clientService.generators(isPreAuth: false).passphrase(settings: settings) } func generatePassword(settings: PasswordGeneratorRequest) async throws -> String { - try await clientService.generators().password(settings: settings) + try await clientService.generators(isPreAuth: false).password(settings: settings) } func generateUsername(settings: UsernameGeneratorRequest) async throws -> String { - try await clientService.generators().username(settings: settings) + try await clientService.generators(isPreAuth: false).username(settings: settings) } func getPasswordGenerationOptions() async throws -> PasswordGenerationOptions { From e624d31c64074d79e884fc8b23e54f2757971a2e Mon Sep 17 00:00:00 2001 From: Katherine Bertelsen Date: Mon, 14 Oct 2024 15:12:09 -0500 Subject: [PATCH 2/4] Fix tests --- .../Core/Platform/Services/ClientServiceTests.swift | 2 +- .../Platform/Services/TestHelpers/MockClientService.swift | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/BitwardenShared/Core/Platform/Services/ClientServiceTests.swift b/BitwardenShared/Core/Platform/Services/ClientServiceTests.swift index c37b4f217..c37d0eccf 100644 --- a/BitwardenShared/Core/Platform/Services/ClientServiceTests.swift +++ b/BitwardenShared/Core/Platform/Services/ClientServiceTests.swift @@ -352,7 +352,7 @@ final class ClientServiceTests: BitwardenTestCase { // swiftlint:disable:this ty func test_generators() async throws { stateService.activeAccount = .fixture(profile: .fixture(userId: "1")) - let generators = try await subject.generators() + let generators = try await subject.generators(isPreAuth: false) XCTAssertIdentical(generators, clientBuilder.clients.first?.clientGenerators) let user2Generators = try await subject.generators(for: "2") diff --git a/BitwardenShared/Core/Platform/Services/TestHelpers/MockClientService.swift b/BitwardenShared/Core/Platform/Services/TestHelpers/MockClientService.swift index 332594307..79d6978ff 100644 --- a/BitwardenShared/Core/Platform/Services/TestHelpers/MockClientService.swift +++ b/BitwardenShared/Core/Platform/Services/TestHelpers/MockClientService.swift @@ -9,6 +9,8 @@ class MockClientService: ClientService { var mockCrypto: MockClientCrypto var mockExporters: MockClientExporters var mockGenerators: MockClientGenerators + var mockGeneratorsIsPreAuth = false + var mockGeneratorsUserId: String? var mockPlatform: MockClientPlatformService var mockSends: MockClientSends var mockVault: MockClientVaultService @@ -46,8 +48,10 @@ class MockClientService: ClientService { mockExporters } - func generators(for userId: String?) -> ClientGeneratorsProtocol { - mockGenerators + func generators(for userId: String?, isPreAuth: Bool) -> ClientGeneratorsProtocol { + mockGeneratorsIsPreAuth = isPreAuth + mockGeneratorsUserId = userId + return mockGenerators } func platform(for userId: String?) -> ClientPlatformService { From 8a3c38619519eb0a87940bd08a85c2cc5aa09a96 Mon Sep 17 00:00:00 2001 From: Katherine Bertelsen Date: Tue, 15 Oct 2024 10:48:41 -0500 Subject: [PATCH 3/4] Create default arguments in client service --- .../Core/Platform/Services/ClientService.swift | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/BitwardenShared/Core/Platform/Services/ClientService.swift b/BitwardenShared/Core/Platform/Services/ClientService.swift index de06d1dc6..faa882b72 100644 --- a/BitwardenShared/Core/Platform/Services/ClientService.swift +++ b/BitwardenShared/Core/Platform/Services/ClientService.swift @@ -71,18 +71,12 @@ protocol ClientService { // MARK: Extension extension ClientService { - /// Returns a `ClientAuthProtocol` for auth data tasks. - /// - func auth() async throws -> ClientAuthProtocol { - try await auth(for: nil, isPreAuth: false) - } - /// Returns a `ClientAuthProtocol` for auth data tasks. /// /// - Parameter isPreAuth: Whether the client is being used for a user prior to authentication /// (when the user's ID doesn't yet exist). /// - func auth(isPreAuth: Bool) async throws -> ClientAuthProtocol { + func auth(isPreAuth: Bool = false) async throws -> ClientAuthProtocol { try await auth(for: nil, isPreAuth: isPreAuth) } @@ -103,7 +97,7 @@ extension ClientService { /// - Parameter isPreAuth: Whether the client is being used for a user prior to authentication /// (when the user's ID doesn't yet exist). This primarily will happen in SSO flows. /// - func generators(isPreAuth: Bool) async throws -> ClientGeneratorsProtocol { + func generators(isPreAuth: Bool = false) async throws -> ClientGeneratorsProtocol { try await generators(for: nil, isPreAuth: isPreAuth) } From 66ee00959667b6178a61aeca51f1b15402c186dd Mon Sep 17 00:00:00 2001 From: Katherine Bertelsen Date: Wed, 16 Oct 2024 09:42:28 -0500 Subject: [PATCH 4/4] Add Xcode version file --- .xcode-version | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .xcode-version diff --git a/.xcode-version b/.xcode-version new file mode 100644 index 000000000..6446dcacf --- /dev/null +++ b/.xcode-version @@ -0,0 +1,2 @@ +15.4 +