macos-ai-menubar-usage/AIMenubarUsage/Services/ClaudeAuthService.swift
Felipe M. 00c4e33171
Initial commit: AI Menubar Usage — macOS menu bar app for Cursor and Claude.
Includes provider integrations, settings, NSStatusItem UI, tests, Makefile, and README with screenshot.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-27 20:41:40 +02:00

49 lines
1.3 KiB
Swift

import Foundation
enum ClaudeAuthService {
static func parseOAuthJSON(_ json: [String: Any], source: ClaudeCredentialSource = .credentialsFile) throws -> ClaudeOAuthCredentials {
let oauth = json["claudeAiOauth"] as? [String: Any]
guard let oauth,
let accessToken = oauth["accessToken"] as? String,
!accessToken.isEmpty
else {
throw ClaudeAuthError.tokenNotFound
}
var expiresAt: Date?
if let expiresMs = oauth["expiresAt"] as? Double {
expiresAt = Date(timeIntervalSince1970: expiresMs / 1000)
}
let subscriptionType = oauth["subscriptionType"] as? String
return ClaudeOAuthCredentials(
accessToken: accessToken,
expiresAt: expiresAt,
subscriptionType: subscriptionType,
source: source
)
}
}
enum ClaudeCredentialSource: String, Sendable {
case credentialsFile
case manual
}
enum ClaudeAuthError: Error, LocalizedError {
case tokenNotFound
var errorDescription: String? {
switch self {
case .tokenNotFound:
"No OAuth token found."
}
}
}
struct ClaudeOAuthCredentials: Sendable {
let accessToken: String
let expiresAt: Date?
let subscriptionType: String?
let source: ClaudeCredentialSource
}