Skip to content
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

[PM-13239] Add isPreAuth to ClientService.generator() calls #1045

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .xcode-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
15.4

4 changes: 2 additions & 2 deletions BitwardenShared/Core/Auth/Services/AuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
26 changes: 13 additions & 13 deletions BitwardenShared/Core/Platform/Services/ClientService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -68,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)
}

Expand All @@ -97,8 +94,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 = false) async throws -> ClientGeneratorsProtocol {
try await generators(for: nil, isPreAuth: isPreAuth)
}

/// Returns a `ClientPlatformService` for client platform tasks.
Expand Down Expand Up @@ -203,8 +203,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: "-",
Expand All @@ -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 {
Expand Down
Loading