stack/sdks/implementations/swift/Sources/StackAuth/Models/ApiKey.swift
Aman Ganapathy c8694c7ff5
[Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130)
### 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>
2026-01-28 02:17:27 +00:00

100 lines
3.2 KiB
Swift

import Foundation
/// Base API key properties
public struct ApiKeyBase: Sendable {
public let id: String
public let description: String
public let expiresAt: Date?
public let createdAt: Date
public let isValid: Bool
init(from json: [String: Any]) {
self.id = json["id"] as? String ?? ""
self.description = json["description"] as? String ?? ""
if let expiresMillis = json["expires_at_millis"] as? Int64 ?? json["expires_at"] as? Int64 {
self.expiresAt = Date(timeIntervalSince1970: Double(expiresMillis) / 1000.0)
} else {
self.expiresAt = nil
}
let createdMillis = json["created_at_millis"] as? Int64 ?? json["created_at"] as? Int64 ?? 0
self.createdAt = Date(timeIntervalSince1970: Double(createdMillis) / 1000.0)
self.isValid = json["is_valid"] as? Bool ?? true
}
}
/// User API key
public struct UserApiKey: Sendable {
public let base: ApiKeyBase
public let userId: String
public let teamId: String?
public var id: String { base.id }
public var description: String { base.description }
public var expiresAt: Date? { base.expiresAt }
public var createdAt: Date { base.createdAt }
public var isValid: Bool { base.isValid }
init(from json: [String: Any]) {
self.base = ApiKeyBase(from: json)
self.userId = json["user_id"] as? String ?? ""
self.teamId = json["team_id"] as? String
}
}
/// User API key with the key value (only returned on creation)
public struct UserApiKeyFirstView: Sendable {
public let base: UserApiKey
public let apiKey: String
public var id: String { base.id }
public var description: String { base.description }
public var expiresAt: Date? { base.expiresAt }
public var createdAt: Date { base.createdAt }
public var isValid: Bool { base.isValid }
public var userId: String { base.userId }
public var teamId: String? { base.teamId }
init(from json: [String: Any]) {
self.base = UserApiKey(from: json)
self.apiKey = json["api_key"] as? String ?? ""
}
}
/// Team API key
public struct TeamApiKey: Sendable {
public let base: ApiKeyBase
public let teamId: String
public var id: String { base.id }
public var description: String { base.description }
public var expiresAt: Date? { base.expiresAt }
public var createdAt: Date { base.createdAt }
public var isValid: Bool { base.isValid }
init(from json: [String: Any]) {
self.base = ApiKeyBase(from: json)
self.teamId = json["team_id"] as? String ?? ""
}
}
/// Team API key with the key value (only returned on creation)
public struct TeamApiKeyFirstView: Sendable {
public let base: TeamApiKey
public let apiKey: String
public var id: String { base.id }
public var description: String { base.description }
public var expiresAt: Date? { base.expiresAt }
public var createdAt: Date { base.createdAt }
public var isValid: Bool { base.isValid }
public var teamId: String { base.teamId }
init(from json: [String: Any]) {
self.base = TeamApiKey(from: json)
self.apiKey = json["api_key"] as? String ?? ""
}
}