@@ -34,12 +34,15 @@ import Foundation
3434///
3535/// ## OAuth Authentication
3636///
37- /// For OAuth-based authentication, use the `.oauth` case with an authentication manager :
37+ /// For OAuth-based authentication (requires macOS 14+, iOS 17+), use the `.oauth(manager:)` factory method :
3838///
3939/// ```swift
40- /// let authManager = HuggingFaceAuthenticationManager(
40+ /// let authManager = try HuggingFaceAuthenticationManager(
4141/// clientID: "your-client-id",
42- /// redirectURL: URL(string: "myapp://oauth")!
42+ /// redirectURL: URL(string: "myapp://oauth")!,
43+ /// scope: .basic,
44+ /// keychainService: "com.example.app",
45+ /// keychainAccount: "huggingface"
4346/// )
4447/// let client = HubClient(tokenProvider: .oauth(manager: authManager))
4548/// ```
@@ -117,13 +120,13 @@ public indirect enum TokenProvider: Sendable {
117120 /// the same token detection logic as the Hugging Face CLI.
118121 case environment
119122
120- /// An OAuth token provider that uses HuggingFaceAuthenticationManager .
123+ /// An OAuth token provider that retrieves tokens asynchronously .
121124 ///
122- /// Use this case for OAuth-based authentication flows. The authentication
123- /// manager handles the complete OAuth flow including token refresh .
125+ /// Use this case for OAuth-based authentication flows. Create instances using
126+ /// the `TokenProvider.oauth(manager:)` factory method when using `HuggingFaceAuthenticationManager` .
124127 ///
125- /// - Parameter manager: The OAuth authentication manager that handles token retrieval and refresh .
126- case oauth( manager : HuggingFaceAuthenticationManager )
128+ /// - Parameter getToken: A closure that retrieves a valid OAuth token .
129+ case oauth( getToken : @ Sendable ( ) async throws -> String )
127130
128131 /// A composite token provider that tries multiple providers in order.
129132 ///
@@ -185,7 +188,7 @@ public indirect enum TokenProvider: Sendable {
185188 case . environment:
186189 return try getTokenFromEnvironment ( )
187190
188- case . oauth( let manager ) :
191+ case . oauth:
189192 fatalError (
190193 " OAuth token provider requires async context. Use getToken() in an async context or switch to a synchronous provider. "
191194 )
@@ -209,6 +212,39 @@ public indirect enum TokenProvider: Sendable {
209212 }
210213}
211214
215+ // MARK: - OAuth Factory
216+
217+ #if canImport(AuthenticationServices)
218+ import Observation
219+
220+ extension TokenProvider {
221+ /// Creates an OAuth token provider using HuggingFaceAuthenticationManager.
222+ ///
223+ /// Use this factory method for OAuth-based authentication flows. The authentication
224+ /// manager handles the complete OAuth flow including token refresh.
225+ ///
226+ /// ```swift
227+ /// let authManager = try HuggingFaceAuthenticationManager(
228+ /// clientID: "your-client-id",
229+ /// redirectURL: URL(string: "myapp://oauth")!,
230+ /// scope: .basic,
231+ /// keychainService: "com.example.app",
232+ /// keychainAccount: "huggingface"
233+ /// )
234+ /// let client = HubClient(tokenProvider: .oauth(manager: authManager))
235+ /// ```
236+ ///
237+ /// - Parameter manager: The OAuth authentication manager that handles token retrieval and refresh.
238+ /// - Returns: A token provider that retrieves tokens from the authentication manager.
239+ @available ( macOS 14 . 0 , macCatalyst 17 . 0 , iOS 17 . 0 , watchOS 10 . 0 , tvOS 17 . 0 , * )
240+ public static func oauth( manager: HuggingFaceAuthenticationManager ) -> TokenProvider {
241+ return . oauth( getToken: { @MainActor in
242+ try await manager. getValidToken ( )
243+ } )
244+ }
245+ }
246+ #endif
247+
212248// MARK: - ExpressibleByStringLiteral & ExpressibleByStringInterpolation
213249
214250extension TokenProvider : ExpressibleByStringLiteral , ExpressibleByStringInterpolation {
0 commit comments