-
-
Notifications
You must be signed in to change notification settings - Fork 117
feat: add SSH TOTP/two-factor authentication support #315
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
377fbee
feat: add SSH TOTP/two-factor authentication support (#312)
datlechin 72cf1a9
Merge branch 'main' into worktree-ssh-totp-support
datlechin a9e56a8
fix: address code review issues in SSH TOTP implementation
datlechin 3267132
fix: address all PR review comments
datlechin 831a078
fix: address latest PR review comments
datlechin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| libssh2_universal.a |
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // | ||
| // AgentAuthenticator.swift | ||
| // TablePro | ||
| // | ||
|
|
||
| import Foundation | ||
| import os | ||
|
|
||
| import CLibSSH2 | ||
|
|
||
| internal struct AgentAuthenticator: SSHAuthenticator { | ||
| private static let logger = Logger(subsystem: "com.TablePro", category: "AgentAuthenticator") | ||
|
|
||
| /// Protects setenv/unsetenv of SSH_AUTH_SOCK across concurrent tunnel setups | ||
| private static let agentSocketLock = NSLock() | ||
|
|
||
| let socketPath: String? | ||
|
|
||
| func authenticate(session: OpaquePointer, username: String) throws { | ||
| // Save original SSH_AUTH_SOCK so we can restore it | ||
| let originalSocketPath = ProcessInfo.processInfo.environment["SSH_AUTH_SOCK"] | ||
| let needsSocketOverride = socketPath != nil | ||
|
|
||
| if let overridePath = socketPath, needsSocketOverride { | ||
| Self.agentSocketLock.lock() | ||
| Self.logger.debug("Using custom SSH agent socket: \(overridePath, privacy: .private)") | ||
| setenv("SSH_AUTH_SOCK", overridePath, 1) | ||
| } | ||
datlechin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| defer { | ||
| if needsSocketOverride { | ||
| // Restore original SSH_AUTH_SOCK | ||
| if let originalSocketPath { | ||
| setenv("SSH_AUTH_SOCK", originalSocketPath, 1) | ||
| } else { | ||
| unsetenv("SSH_AUTH_SOCK") | ||
| } | ||
| Self.agentSocketLock.unlock() | ||
| } | ||
| } | ||
|
|
||
| guard let agent = libssh2_agent_init(session) else { | ||
| throw SSHTunnelError.tunnelCreationFailed("Failed to initialize SSH agent") | ||
| } | ||
|
|
||
| defer { | ||
| libssh2_agent_disconnect(agent) | ||
| libssh2_agent_free(agent) | ||
| } | ||
|
|
||
| var rc = libssh2_agent_connect(agent) | ||
| guard rc == 0 else { | ||
| Self.logger.error("Failed to connect to SSH agent (rc=\(rc))") | ||
| throw SSHTunnelError.tunnelCreationFailed("Failed to connect to SSH agent") | ||
| } | ||
|
|
||
| rc = libssh2_agent_list_identities(agent) | ||
| guard rc == 0 else { | ||
| Self.logger.error("Failed to list SSH agent identities (rc=\(rc))") | ||
| throw SSHTunnelError.tunnelCreationFailed("Failed to list SSH agent identities") | ||
| } | ||
|
|
||
| // Iterate through available identities and try each | ||
| var previousIdentity: UnsafeMutablePointer<libssh2_agent_publickey>? | ||
| var currentIdentity: UnsafeMutablePointer<libssh2_agent_publickey>? | ||
|
|
||
| while true { | ||
| rc = libssh2_agent_get_identity(agent, ¤tIdentity, previousIdentity) | ||
|
|
||
| if rc == 1 { | ||
| // End of identity list, none worked | ||
| break | ||
| } | ||
| if rc < 0 { | ||
| Self.logger.error("Failed to get SSH agent identity (rc=\(rc))") | ||
| throw SSHTunnelError.tunnelCreationFailed("Failed to get SSH agent identity") | ||
| } | ||
|
|
||
| guard let identity = currentIdentity else { | ||
| break | ||
| } | ||
|
|
||
| let authRc = libssh2_agent_userauth(agent, username, identity) | ||
| if authRc == 0 { | ||
| Self.logger.info("SSH agent authentication succeeded") | ||
| return | ||
| } | ||
|
|
||
| previousIdentity = identity | ||
| } | ||
|
|
||
| Self.logger.error("SSH agent authentication failed: no identity accepted") | ||
| throw SSHTunnelError.authenticationFailed | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // | ||
| // CompositeAuthenticator.swift | ||
| // TablePro | ||
| // | ||
|
|
||
| import Foundation | ||
| import os | ||
|
|
||
| import CLibSSH2 | ||
|
|
||
| /// Authenticator that tries multiple auth methods in sequence. | ||
| /// Used for servers requiring e.g. password + keyboard-interactive (TOTP). | ||
| internal struct CompositeAuthenticator: SSHAuthenticator { | ||
| private static let logger = Logger(subsystem: "com.TablePro", category: "CompositeAuthenticator") | ||
|
|
||
| let authenticators: [any SSHAuthenticator] | ||
|
|
||
| func authenticate(session: OpaquePointer, username: String) throws { | ||
| var lastError: Error? | ||
| for (index, authenticator) in authenticators.enumerated() { | ||
| Self.logger.debug("Trying authenticator \(index + 1)/\(authenticators.count)") | ||
| do { | ||
| try authenticator.authenticate(session: session, username: username) | ||
| } catch { | ||
| Self.logger.debug("Authenticator \(index + 1) failed: \(error)") | ||
| lastError = error | ||
| } | ||
|
|
||
| if libssh2_userauth_authenticated(session) != 0 { | ||
| Self.logger.info("Authentication succeeded after \(index + 1) step(s)") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| if libssh2_userauth_authenticated(session) == 0 { | ||
| throw lastError ?? SSHTunnelError.authenticationFailed | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.