-
Notifications
You must be signed in to change notification settings - Fork 2
Add AuthClient protocol and expose authClient on CrossmintSDK #97
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
22d83e3
Add AuthClient protocol and DefaultAuthClient implementation
tomas-martins-crossmint 0d270fc
Polish AuthClient: rename otp vars, return session from establishSess…
tomas-martins-crossmint 4da4d39
Extract OTPRequest, AuthSession, AuthUser to separate files
tomas-martins-crossmint 5aaa38a
Use .sheet(item:) in OTPSignInView, drop any from authClient, fix log…
tomas-martins-crossmint 30ab8e9
Call authClient.logout() from CrossmintSDK.logout() to clear pending …
tomas-martins-crossmint a078061
Add tests for normalizeEmail, establishSession, and DefaultAuthClient…
tomas-martins-crossmint File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // | ||
| // AuthClient.swift | ||
| // CrossmintSDK | ||
| // | ||
| // Created by Tomas Martins on 6/1/26. | ||
| // | ||
|
|
||
| public protocol AuthClient: Sendable { | ||
| func sendOTP(to email: String) async throws(AuthError) -> OTPRequest | ||
| func verifyOTP(code: String, requestId: String) async throws(AuthError) -> AuthSession | ||
| func logout() async | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // | ||
| // AuthSession.swift | ||
| // CrossmintSDK | ||
| // | ||
| // Created by Tomas Martins on 6/1/26. | ||
| // | ||
|
|
||
| public struct AuthSession: Sendable { | ||
| public let jwt: String | ||
| public let user: AuthUser | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // | ||
| // AuthUser.swift | ||
| // CrossmintSDK | ||
| // | ||
| // Created by Tomas Martins on 6/1/26. | ||
| // | ||
|
|
||
| public struct AuthUser: Sendable { | ||
| public let email: String | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // | ||
| // DefaultAuthClient.swift | ||
| // CrossmintSDK | ||
| // | ||
| // Created by Tomas Martins on 6/1/26. | ||
| // | ||
|
|
||
| import Utils | ||
|
|
||
| public actor DefaultAuthClient: AuthClient { | ||
| private let authService: AuthService | ||
| private let authManager: CrossmintAuthManager | ||
| private var pendingEmailsByRequestId: [String: String] = [:] | ||
|
|
||
| package init(authService: AuthService, authManager: CrossmintAuthManager) { | ||
| self.authService = authService | ||
| self.authManager = authManager | ||
| } | ||
|
|
||
| public func sendOTP(to email: String) async throws(AuthError) -> OTPRequest { | ||
| let normalizedEmail = normalizeEmail(email) | ||
| guard isValidEmail(normalizedEmail) else { | ||
| throw AuthError.generic("Invalid email address") | ||
| } | ||
| let response = try await authService.validateEmail(ValidateEmailRequest(email: normalizedEmail)) | ||
| pendingEmailsByRequestId[response.emailId] = normalizedEmail | ||
| return OTPRequest(requestId: response.emailId) | ||
| } | ||
|
|
||
| public func verifyOTP(code: String, requestId: String) async throws(AuthError) -> AuthSession { | ||
| guard let email = pendingEmailsByRequestId[requestId] else { | ||
| throw AuthError.generic("No pending OTP for the provided requestId") | ||
| } | ||
| let tokenResponse = try await authService.validateToken( | ||
| ValidateTokenRequest(email: email, token: code, emailID: requestId) | ||
| ) | ||
| let session = try await authManager.establishSession(oneTimeSecret: tokenResponse.oneTimeSecret) | ||
| pendingEmailsByRequestId.removeValue(forKey: requestId) | ||
| return AuthSession(jwt: session.jwt, user: AuthUser(email: session.email)) | ||
| } | ||
|
|
||
| public func logout() async { | ||
| _ = try? await authManager.logout() | ||
| pendingEmailsByRequestId.removeAll() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // | ||
| // OTPRequest.swift | ||
| // CrossmintSDK | ||
| // | ||
| // Created by Tomas Martins on 6/1/26. | ||
| // | ||
|
|
||
| public struct OTPRequest: Sendable, Identifiable { | ||
| public var id: String { requestId } | ||
| public let requestId: String | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,7 @@ import SwiftEmailValidator | |
| public func isValidEmail(_ email: String) -> Bool { | ||
| return EmailSyntaxValidator.correctlyFormatted(email) | ||
| } | ||
|
|
||
| public func normalizeEmail(_ email: String) -> String { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test? |
||
| email.lowercased().trimmingCharacters(in: .whitespacesAndNewlines) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,68 +1,31 @@ | ||
| import Foundation | ||
| import Testing | ||
| @testable import CrossmintAuth | ||
| import SecureStorage | ||
|
|
||
| @Suite("AuthManager", .tags(.unit)) | ||
| struct CrossmintAuthManagerTests { | ||
| private let authManager = CrossmintAuthManager( | ||
| authService: StubAuthService(), | ||
| secureStorage: StubSecureStorage() | ||
| authService: MockAuthService(), | ||
| secureStorage: MockSecureStorage() | ||
| ) | ||
|
|
||
| @Test("Throws invalidInput when OTP code is empty") | ||
| func throwsOnEmptyCode() async { | ||
| @Test func rejectsEmptyOtpCode() async { | ||
| await #expect(throws: AuthManagerError.invalidInput("OTP code cannot be empty")) { | ||
| _ = try await authManager.confirmEmailOtp(email: "user@example.com", code: "") | ||
| } | ||
| } | ||
|
|
||
| @Test("Throws invalidInput when OTP code is whitespace only") | ||
| func throwsOnWhitespaceCode() async { | ||
| @Test func rejectsWhitespaceOtpCode() async { | ||
| await #expect(throws: AuthManagerError.invalidInput("OTP code cannot be empty")) { | ||
| _ = try await authManager.confirmEmailOtp(email: "user@example.com", code: " ") | ||
| } | ||
| } | ||
|
|
||
| @Test("Does not throw when sending email OTP") | ||
| func doesNotThrowOnSendEmailOtp() async throws { | ||
| @Test func sendsOtpToValidEmail() async throws { | ||
| try await authManager.sendEmailOtp(email: "user@example.com") | ||
| } | ||
|
|
||
| @Test("Does not throw when OTP code is non-empty") | ||
| func doesNotThrowOnNonEmptyCode() async throws { | ||
| @Test func authenticatesWithValidOtpCode() async throws { | ||
| try await authManager.sendEmailOtp(email: "user@example.com") | ||
| _ = try await authManager.confirmEmailOtp(email: "user@example.com", code: "123456") | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Test Doubles | ||
|
|
||
| private struct StubAuthService: AuthService { | ||
| func validateEmail(_ request: ValidateEmailRequest) async throws(AuthError) -> ValidateEmailResponse { | ||
| ValidateEmailResponse(emailId: "test-email-id") | ||
| } | ||
|
|
||
| func validateToken(_ request: ValidateTokenRequest) async throws(AuthError) -> ValidateTokenResponse { | ||
| ValidateTokenResponse(callbackUrl: "", oneTimeSecret: "test-secret") | ||
| } | ||
|
|
||
| func refreshJWT(_ request: RefreshJWTRequest) async throws(AuthError) -> RefreshJWTResponse { | ||
| RefreshJWTResponse( | ||
| jwt: "test-jwt", | ||
| refresh: .init(secret: "test-secret", expiresAt: Date().addingTimeInterval(3600)), | ||
| user: .init(id: "test-id", email: "user@example.com") | ||
| ) | ||
| } | ||
|
|
||
| func logout(_ request: LogoutRequest) async throws(AuthError) {} | ||
| } | ||
|
|
||
| private struct StubSecureStorage: SecureStorage { | ||
| func getOneTimeSecret() async throws(SecureStorageError) -> String? { nil } | ||
| func storeOneTimeSecret(_ secret: String) async throws(SecureStorageError) {} | ||
| func getJWT() async throws(SecureStorageError) -> String? { nil } | ||
| func storeJWT(_ secret: String) async throws(SecureStorageError) {} | ||
| func getEmail() async throws(SecureStorageError) -> String? { nil } | ||
| func storeEmail(_ email: String) async throws(SecureStorageError) {} | ||
| func clear() {} | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test?