objective-c - ConnectionKit 和 SFTP : How to authenticate CK2FileManager

标签 objective-c cocoa sftp connectionkit

对于我的 Mac OS X Cocoa 应用程序,我正在尝试

  • 连接到仅接受用户名/密码凭据的 SFTP 服务器
  • 获取远程目录的内容
  • 上传文件

发现它非常复杂。

在尝试了 ConnectionKit(几乎没有文档)、NMSSH(同时上传时经常崩溃)、rsync(服务器不支持)、sftp(如果编写脚本,则需要 key 身份验证,不适用于用户名/密码),我现在回到 ConnectionKit:https://github.com/karelia/ConnectionKit

但是,我正在努力应对身份验证挑战,因为我不知道如何处理委托(delegate)方法中的凭据。

  • 我下载并编译了 ConnectionKit(显然是版本 2)。
  • 我正在尝试使用 CK2FileManager 作为 Readme表示(这是正确的方法吗?或者我应该使用 libssh2_sftp-Cocoa-wrapper 来代替?…但是我之前在 NMSSH 中使用 libssh2 阻止方法时遇到了麻烦)
  • 我已成功设置连接网址并且
  • 我的代表的 -didReceiveAuthenticationChallenge 被调用

但这就是我挣扎的地方:我知道如何创建 NSURLCredential,但是,我不知道如何处理它 =>

- (void)fileManager:(CK2FileManager *)manager
 didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{
  NSURLCredential *credentials = [NSURLCredential 
    credentialWithUser:self.username
    password:[self getPassword]
    persistence:NSURLCredentialPersistenceForSession];

    // what to do now?
    // [manager useCredential:] doesn’t exist, nor is there a manager.connection?
    // ...
}

我已经阅读了标题,我搜索了此列表的文件,但所有答案似乎都已过时。 我还搜索了 Google、Bing 和 StackOverflow,发现了 2011 年使用 CKFTPConnection 的一个很有前景的示例,但它似乎不再包含在当前框架中。

非常感谢您指出正确的方向。

<小时/>

tl;博士

我不知道如何响应ConnectionKit的CK2FileManagerauthenticationChallenge: 请参阅代码示例中的注释

最佳答案

对于 CK2:

- (void)listDirectoryAtPath:(NSString *)path
{
    // path is here   @"download"
    NSURL *ftpServer = [NSURL URLWithString:@"sftp://companyname.topLevelDomain"];
    NSURL *directory = [CK2FileManager URLWithPath:path isDirectory:YES hostURL:ftpServer];

    CK2FileManager *fileManager = [[CK2FileManager alloc] init];
    fileManager.delegate = self;

    [fileManager contentsOfDirectoryAtURL:directory
               includingPropertiesForKeys:nil
                                  options:NSDirectoryEnumerationSkipsHiddenFiles
                        completionHandler:^(NSArray *contents, NSError *error) {
                            if (!error) {
                                NSLog(@"%@", contents);
                            } else {
                                NSLog(@"ERROR: %@", error.localizedDescription);
                            }
                        }];

}

并且您必须实现以下协议(protocol)

- (void)fileManager:(CK2FileManager *)manager operation:(CK2FileOperation *)operation
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(CK2AuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
    if (![challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodDefault]) {
        completionHandler(CK2AuthChallengePerformDefaultHandling, nil);
        return;
    }

    NSString * username = @"<username>";
    NSString * pathToPrivateSSHKey = @"</Users/myNameOnLocalMaschine/.ssh/id_rsa>"

    NSURLCredential *cred = [NSURLCredential ck2_credentialWithUser:username
                                                       publicKeyURL:nil
                                                      privateKeyURL:[NSURL fileURLWithPath:pathToPrivateSSHKey]
                                                           password:nil
                                                        persistence:NSURLCredentialPersistenceForSession];
    completionHandler(CK2AuthChallengeUseCredential, cred);

}

就是这样。

调用-listDirectoryAtPath:,然后您将在内容数组中的完成处理程序 block 中获取位于给定路径上的所有文件:)

关于objective-c - ConnectionKit 和 SFTP : How to authenticate CK2FileManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19999638/

相关文章:

html - 如何防止 WebView 缩小呈现的网页?

swift - NSTextField 上的自定义边框

python - 当 SFTP 对象存储在字典中时,为什么 Paramiko 会引发 EOFError()?

objective-c - NSString 在发布时崩溃

objective-c - 将 CCMenu 对齐到网格

ios - 在两个设备之间发送时数据会被弄乱

谷歌地图的 IOS 更改包标识符

cocoa-touch - 如何对 NSPredicate 进行排序

c - 性能 libssh2 sftp 上传多个文件

ubuntu - "Write Failed: Broken Pipe"尝试通过特定用户通过 ssh 登录时