mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-28 23:54:32 +00:00
Still not broken: Next commit decides the fate
This commit is contained in:
parent
cb2afda0f2
commit
dc45c50df4
17 changed files with 1028 additions and 608 deletions
|
|
@ -0,0 +1,87 @@
|
|||
//
|
||||
// YouTubeMusicAuthentication.swift
|
||||
// DynamicIsland
|
||||
//
|
||||
// Created by Alexander on 2025-09-14.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
// MARK: - Authentication Manager
|
||||
actor YouTubeMusicAuthManager {
|
||||
private var accessToken: String?
|
||||
private var authenticationTask: Task<String, Error>?
|
||||
private let httpClient: YouTubeMusicHTTPClient
|
||||
|
||||
init(httpClient: YouTubeMusicHTTPClient) {
|
||||
self.httpClient = httpClient
|
||||
}
|
||||
|
||||
var currentToken: String? {
|
||||
accessToken
|
||||
}
|
||||
|
||||
func authenticate() async throws -> String {
|
||||
// Return existing token if valid
|
||||
if let token = accessToken {
|
||||
return token
|
||||
}
|
||||
|
||||
// Wait for ongoing authentication if in progress
|
||||
if let task = authenticationTask {
|
||||
return try await task.value
|
||||
}
|
||||
|
||||
// Start new authentication
|
||||
let task = Task<String, Error> {
|
||||
do {
|
||||
let token = try await httpClient.authenticate()
|
||||
await setToken(token)
|
||||
return token
|
||||
} catch {
|
||||
await clearAuthenticationTask()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
authenticationTask = task
|
||||
return try await task.value
|
||||
}
|
||||
|
||||
func invalidateToken() async {
|
||||
accessToken = nil
|
||||
authenticationTask?.cancel()
|
||||
authenticationTask = nil
|
||||
}
|
||||
|
||||
private func setToken(_ token: String) async {
|
||||
accessToken = token
|
||||
authenticationTask = nil
|
||||
}
|
||||
|
||||
private func clearAuthenticationTask() async {
|
||||
authenticationTask = nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Authentication State
|
||||
enum AuthenticationState: Sendable {
|
||||
case unauthenticated
|
||||
case authenticating
|
||||
case authenticated(String)
|
||||
case failed(Error)
|
||||
|
||||
var isAuthenticated: Bool {
|
||||
if case .authenticated = self {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var token: String? {
|
||||
if case .authenticated(let token) = self {
|
||||
return token
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue