-
Notifications
You must be signed in to change notification settings - Fork 2
feat: use the deployment's hostname suffix in the UI #133
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
Changes from 5 commits
6462177
f141a74
5dea75f
e04f61a
cbd8ce4
a354ebe
75f17ca
ba2d732
26d2eb8
b6218fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,10 @@ class AppState: ObservableObject { | |
} | ||
} | ||
|
||
@Published private(set) var hostnameSuffix: String = defaultHostnameSuffix | ||
|
||
static let defaultHostnameSuffix: String = "coder" | ||
|
||
// Stored in Keychain | ||
@Published private(set) var sessionToken: String? { | ||
didSet { | ||
|
@@ -33,6 +37,8 @@ class AppState: ObservableObject { | |
} | ||
} | ||
|
||
private var client: Client? | ||
|
||
@Published var useLiteralHeaders: Bool = UserDefaults.standard.bool(forKey: Keys.useLiteralHeaders) { | ||
didSet { | ||
reconfigure() | ||
|
@@ -80,7 +86,7 @@ class AppState: ObservableObject { | |
private let keychain: Keychain | ||
private let persistent: Bool | ||
|
||
let onChange: ((NETunnelProviderProtocol?) -> Void)? | ||
private let onChange: ((NETunnelProviderProtocol?) -> Void)? | ||
|
||
// reconfigure must be called when any property used to configure the VPN changes | ||
public func reconfigure() { | ||
|
@@ -107,21 +113,35 @@ class AppState: ObservableObject { | |
if sessionToken == nil || sessionToken!.isEmpty == true { | ||
clearSession() | ||
} | ||
client = Client( | ||
url: baseAccessURL!, | ||
token: sessionToken!, | ||
headers: useLiteralHeaders ? literalHeaders.map { $0.toSDKHeader() } : [] | ||
) | ||
Task { | ||
await handleTokenExpiry() | ||
await refreshDeploymentConfig() | ||
} | ||
} | ||
} | ||
|
||
public func login(baseAccessURL: URL, sessionToken: String) { | ||
hasSession = true | ||
self.baseAccessURL = baseAccessURL | ||
self.sessionToken = sessionToken | ||
client = Client( | ||
url: baseAccessURL, | ||
token: sessionToken, | ||
headers: useLiteralHeaders ? literalHeaders.map { $0.toSDKHeader() } : [] | ||
) | ||
Task { await refreshDeploymentConfig() } | ||
reconfigure() | ||
} | ||
|
||
public func handleTokenExpiry() async { | ||
if hasSession { | ||
let client = Client(url: baseAccessURL!, token: sessionToken!) | ||
do { | ||
_ = try await client.user("me") | ||
_ = try await client!.user("me") | ||
} catch let SDKError.api(apiErr) { | ||
// Expired token | ||
if apiErr.statusCode == 401 { | ||
|
@@ -135,9 +155,24 @@ class AppState: ObservableObject { | |
} | ||
} | ||
|
||
public func refreshDeploymentConfig() async { | ||
if hasSession { | ||
do { | ||
let config = try await client!.sshConfiguration() | ||
hostnameSuffix = config.hostname_suffix ?? Self.defaultHostnameSuffix | ||
} catch { | ||
// If fetching the config fails, there's likely a bigger issue. | ||
// We'll show an error in the UI if they try and do something | ||
ethanndickson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.error("failed to refresh deployment config: \(error)") | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we wanted to show that something went wrong to the user here, the UX would be awkward. I don't think we should create an alert or anything, and I don't think we should reuse the Connect failure error box. I'm not sure how the user would get into a state where everything else was functional, except the deployment config couldn't be fetched, so I'm happy with this. |
||
} | ||
} | ||
} | ||
|
||
public func clearSession() { | ||
hasSession = false | ||
sessionToken = nil | ||
client = nil | ||
reconfigure() | ||
} | ||
|
||
|
@@ -159,6 +194,7 @@ class AppState: ObservableObject { | |
static let hasSession = "hasSession" | ||
static let baseAccessURL = "baseAccessURL" | ||
static let sessionToken = "sessionToken" | ||
static let hostnameSuffix = "hostnameSuffix" | ||
ethanndickson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
static let useLiteralHeaders = "UseLiteralHeaders" | ||
static let literalHeaders = "LiteralHeaders" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drive-by fix: we need to check for token expiry on app launch. We were previously only checking when the menu bar window was opened, and Connect was disabled. We need to account for when Connect is configured to start when the app is launched.