mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-16 21:08:38 +08:00
### Summary of Changes Previously, on the Swift SDK, the `signInWithOAuth` function wasn't working. In this PR, we fix it by having the `getOAuthUrl` function to actually redirect correctly. Note that to do so, we updated the `validRedirectUrl` check on the backend to accept app native redirects (from our new trusted url scheme). Another thing to note is that we added functionality to the `TokenStore` abstraction to conditionally refresh the access token that the user is trying to fetch if it is expired/close to expiring if possible. `getOAuthUrl` will attempt to get a valid access token, and thus will rely on our algorithm documented in `utilities.md`. The specs serve as the source of truth. We go further and implement Apple Native sign in. To do so, we have it hit a new route on the backend and verify the `jwtToken` retrieved by the sdk against an Apple-provided set of `jwks`. We use jose to do so, in line with the rest of the codebase. We take this opportunity to refactor the oauth provider route owing to the amount of duplicated logic. Additionally, to enable the apple sign in, users will have to update the Apple authentication method modal on the dashboard and add accepted bundle ids. These are identifiers for projects, and we will check the `JWT` on the backend to make sure the audience is set to an accepted bundleId. We also update the Apple modal to be more informative. ### Using the new Features To use the Apple native sign in, users will have to 1) sign up with an apple developer account, 2) set up their bundleids for their projects by connecting them to the apple developer account, 3) update the Stack-Auth Authentication Methods dashboard apple modal with the relevant fields. Then, trying to sign in with apple with our Swift SDK will use the apple native sign in. ### UI Changes Renamed the fields in the apple modal. Added a new field for bundle ids. See below. https://github.com/user-attachments/assets/0e760c0e-3198-4818-ac7f-4900d7a125bb Co-authored-by: Konstantin Wohlwend <n2d4xc@gmail.com>
82 lines
3.2 KiB
Swift
82 lines
3.2 KiB
Swift
import Foundation
|
|
|
|
/// Base user properties visible to clients
|
|
/// Note: [String: Any] is not Sendable but we accept this for JSON data
|
|
public struct User: @unchecked Sendable {
|
|
public let id: String
|
|
public let displayName: String?
|
|
public let primaryEmail: String?
|
|
public let primaryEmailVerified: Bool
|
|
public let profileImageUrl: String?
|
|
public let signedUpAt: Date
|
|
public let clientMetadata: [String: Any]
|
|
public let clientReadOnlyMetadata: [String: Any]
|
|
public let hasPassword: Bool
|
|
public let emailAuthEnabled: Bool
|
|
public let otpAuthEnabled: Bool
|
|
public let passkeyAuthEnabled: Bool
|
|
public let isMultiFactorRequired: Bool
|
|
public let isAnonymous: Bool
|
|
public let isRestricted: Bool
|
|
public let restrictedReason: RestrictedReason?
|
|
public let oauthProviders: [OAuthProviderInfo]
|
|
|
|
public struct RestrictedReason: Sendable {
|
|
public let type: String // "anonymous" | "email_not_verified"
|
|
}
|
|
|
|
public struct OAuthProviderInfo: Sendable {
|
|
public let id: String
|
|
}
|
|
}
|
|
|
|
// Make User Sendable by using a wrapper for the metadata
|
|
extension User {
|
|
init(from json: [String: Any]) {
|
|
self.id = json["id"] as? String ?? ""
|
|
self.displayName = json["display_name"] as? String
|
|
self.primaryEmail = json["primary_email"] as? String
|
|
self.primaryEmailVerified = json["primary_email_verified"] as? Bool ?? false
|
|
self.profileImageUrl = json["profile_image_url"] as? String
|
|
|
|
let millis = json["signed_up_at_millis"] as? Int64 ?? 0
|
|
self.signedUpAt = Date(timeIntervalSince1970: Double(millis) / 1000.0)
|
|
|
|
// Note: These are not truly Sendable but we accept the risk for JSON data
|
|
self.clientMetadata = json["client_metadata"] as? [String: Any] ?? [:]
|
|
self.clientReadOnlyMetadata = json["client_read_only_metadata"] as? [String: Any] ?? [:]
|
|
|
|
self.hasPassword = json["has_password"] as? Bool ?? false
|
|
self.emailAuthEnabled = json["auth_with_email"] as? Bool ?? false
|
|
self.otpAuthEnabled = json["otp_auth_enabled"] as? Bool ?? false
|
|
self.passkeyAuthEnabled = json["passkey_auth_enabled"] as? Bool ?? false
|
|
self.isMultiFactorRequired = json["requires_totp_mfa"] as? Bool ?? false
|
|
self.isAnonymous = json["is_anonymous"] as? Bool ?? false
|
|
self.isRestricted = json["is_restricted"] as? Bool ?? false
|
|
|
|
if let reason = json["restricted_reason"] as? [String: Any],
|
|
let type = reason["type"] as? String {
|
|
self.restrictedReason = RestrictedReason(type: type)
|
|
} else {
|
|
self.restrictedReason = nil
|
|
}
|
|
|
|
if let providers = json["oauth_providers"] as? [[String: Any]] {
|
|
self.oauthProviders = providers.map { OAuthProviderInfo(id: $0["id"] as? String ?? "") }
|
|
} else {
|
|
self.oauthProviders = []
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Partial user info extracted from JWT token
|
|
public struct TokenPartialUser: Sendable {
|
|
public let id: String
|
|
public let displayName: String?
|
|
public let primaryEmail: String?
|
|
public let primaryEmailVerified: Bool
|
|
public let isAnonymous: Bool
|
|
public let isRestricted: Bool
|
|
public let restrictedReason: User.RestrictedReason?
|
|
}
|