-
Notifications
You must be signed in to change notification settings - Fork 13
Description
The databaseOverride parameter in AuthenticationHelper.setupAuthentication() uses String? with string comparisons instead of the type-safe MistKit.Database enum. This bypasses Swift's type system and allows invalid values.
Current Code
```swift
// AuthenticationHelper.swift:55-68
static func setupAuthentication(
apiToken: String,
webAuthToken: String?,
keyID: String?,
privateKey: String?,
privateKeyFile: String?,
databaseOverride: String? = nil //
) async throws -> AuthenticationResult {
// ...
if let override = databaseOverride, override == "private" { // String comparison
database = .private
}
// ...
}
```
Problems
- No compile-time safety: Typos like
"privat"or"Private"compile but fail at runtime - Magic strings: Database names hardcoded as strings
- Limited validation: No exhaustive checking of valid values
- Inconsistent: MistKit uses
Databaseenum everywhere else
Recommended Solution
```swift
static func setupAuthentication(
apiToken: String,
webAuthToken: String?,
keyID: String?,
privateKey: String?,
privateKeyFile: String?,
databaseOverride: MistKit.Database? = nil // ✅ Type-safe enum
) async throws -> AuthenticationResult {
// ...
if let override = databaseOverride {
database = override // Direct assignment, no string comparison
}
// ...
// Type-safe error throwing
if let override = databaseOverride, override == .private {
throw AuthenticationError.serverToServerRequiresPublicDatabase
}
}
```
Benefits
- Compile-time safety: Invalid values caught by compiler
- Autocomplete: IDE suggests
.publicand.private - Exhaustive: Compiler ensures all cases handled
- Consistent: Matches MistKit's type system
- Refactoring-safe: Renaming enum cases updates all usages
Migration Example
```swift
// Before (MistDemo.swift call site)
let result = try await AuthenticationHelper.setupAuthentication(
apiToken: apiToken,
webAuthToken: webAuthToken,
keyID: keyID,
privateKey: privateKey,
privateKeyFile: privateKeyFile,
databaseOverride: "private" // ❌ String
)
// After
let result = try await AuthenticationHelper.setupAuthentication(
apiToken: apiToken,
webAuthToken: webAuthToken,
keyID: keyID,
privateKey: privateKey,
privateKeyFile: privateKeyFile,
databaseOverride: .private // ✅ Enum
)
```
Files Affected
Examples/MistDemo/Sources/MistDemo/Utilities/AuthenticationHelper.swift:55-68, 89, 112- Call sites in
Examples/MistDemo/Sources/MistDemo/MistDemo.swift(if any) - Test files (update expectations)
Acceptance Criteria
-
databaseOverrideparameter type changed toMistKit.Database? - All string comparisons replaced with enum comparisons
- Call sites updated to use enum values
- Tests updated with type-safe values
- Compiler enforces valid database values
- No runtime validation needed for database parameter
Test Cases
```swift
@test func testDatabaseOverridePublic() async throws {
let result = try await AuthenticationHelper.setupAuthentication(
apiToken: "test-token",
webAuthToken: "test-web-token",
keyID: nil,
privateKey: nil,
privateKeyFile: nil,
databaseOverride: .public // ✅ Type-safe
)
#expect(result.database == .public)
}
@test func testServerToServerRejectsPrivateOverride() async throws {
await #expect(throws: AuthenticationError.serverToServerRequiresPublicDatabase) {
try await AuthenticationHelper.setupAuthentication(
apiToken: "test-token",
webAuthToken: nil,
keyID: "test-key-id",
privateKey: "test-key",
privateKeyFile: nil,
databaseOverride: .private // Should throw
)
}
}
```
References
- MistKit Database enum:
Sources/MistKit/Database.swift - Usage:
AuthenticationHelper.swift:59, 68, 89, 112