Skip to content

chore: sign user out if token is expired #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func applicationDidFinishLaunching(_: Notification) {
menuBar = .init(menuBarExtra: FluidMenuBarExtra(title: "Coder Desktop", image: "MenuBarIcon") {
VPNMenu<CoderVPNService>().frame(width: 256)
.environmentObject(self.vpn)
.environmentObject(self.state)
})
menuBar = .init(menuBarExtra: FluidMenuBarExtra(
title: "Coder Desktop",
image: "MenuBarIcon",
onAppear: {
// If the VPN is enabled, it's likely the token isn't expired
guard case .disabled = self.vpn.state, self.state.hasSession else { return }
Task { @MainActor in
await self.state.handleTokenExpiry()
}
}, content: {
VPNMenu<CoderVPNService>().frame(width: 256)
.environmentObject(self.vpn)
.environmentObject(self.state)
}
))
// Subscribe to system VPN updates
NotificationCenter.default.addObserver(
self,
Expand Down
23 changes: 23 additions & 0 deletions Coder-Desktop/Coder-Desktop/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import CoderSDK
import Foundation
import KeychainAccess
import NetworkExtension
import os
import SwiftUI

@MainActor
class AppState: ObservableObject {
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "AppState")
let appId = Bundle.main.bundleIdentifier!

// Stored in UserDefaults
Expand Down Expand Up @@ -95,6 +97,9 @@ class AppState: ObservableObject {
)
if hasSession {
_sessionToken = Published(initialValue: keychainGet(for: Keys.sessionToken))
if sessionToken == nil || sessionToken!.isEmpty == true {
clearSession()
}
}
}

Expand All @@ -105,6 +110,24 @@ class AppState: ObservableObject {
reconfigure()
}

public func handleTokenExpiry() async {
if hasSession {
let client = Client(url: baseAccessURL!, token: sessionToken!)
do {
_ = try await client.user("me")
} catch let ClientError.api(apiErr) {
// Expired token
if apiErr.statusCode == 401 {
clearSession()
}
} catch {
// Some other failure, we'll show an error if they try and do something
logger.error("failed to check token validity: \(error)")
return
}
}
}

public func clearSession() {
hasSession = false
sessionToken = nil
Expand Down
8 changes: 4 additions & 4 deletions Coder-Desktop/CoderSDK/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ public struct Client {
}

public struct APIError: Decodable, Sendable {
let response: Response
let statusCode: Int
let method: String
let url: URL
public let response: Response
public let statusCode: Int
public let method: String
public let url: URL

var description: String {
var components = ["\(method) \(url.absoluteString)\nUnexpected status code \(statusCode):\n\(response.message)"]
Expand Down
6 changes: 4 additions & 2 deletions Coder-Desktop/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ packages:
url: https://github.com/SimplyDanny/SwiftLintPlugins
from: 0.57.1
FluidMenuBarExtra:
# Forked so we can dynamically update the menu bar icon.
# Forked to:
# - Dynamically update the menu bar icon
# - Set onAppear/disappear handlers.
# The upstream repo has a purposefully limited API
url: https://github.com/coder/fluid-menu-bar-extra
revision: 020be37
revision: 96a861a
KeychainAccess:
url: https://github.com/kishikawakatsumi/KeychainAccess
branch: e0c7eebc5a4465a3c4680764f26b7a61f567cdaf
Expand Down
Loading