首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConnectionKit和SFTP:如何认证CK2FileManager

ConnectionKit和SFTP:如何认证CK2FileManager
EN

Stack Overflow用户
提问于 2013-11-15 11:06:04
回答 1查看 594关注 0票数 0

对于我的应用程序,我正在尝试

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

发现这件事令人惊讶地复杂。

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

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

  • 我下载并编译了ConnectionKit (显然是第2版)。
  • 正如CK2FileManager所指出的那样,我试图使用自述 (这完全是正确的方法吗?)或者我应该使用sftp-Cocoa包装器来代替?…不过,我以前在NMSSH中遇到了libssh2阻塞方法方面的问题)
  • 我正在成功地设置我的连接URL
  • 我的代表的-didReceiveAuthenticationChallenge被称为

但这正是我所奋斗的地方:我知道如何创建一个NSURLCredential,但是,我不知道如何使用它-- =>

代码语言:javascript
复制
- (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?
    // ...
}

我已经读了标题,我搜索了这个列表的档案,但是所有的答案似乎都过时了。我还搜索了谷歌、必应和StackOverflow,并找到了一个从2011年开始使用CKFTPConnection的很有希望的例子,这个例子似乎不再包含在当前的框架中。

非常感谢任何指向正确方向的指针。

tl;dr

我不知道如何响应ConnectionKit的CK2FileManager authenticationChallenge:参见代码示例中的注释

EN

回答 1

Stack Overflow用户

发布于 2014-01-18 00:36:26

对于CK2:

代码语言:javascript
复制
- (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);
                            }
                        }];

}

而不是您必须实现以下协议

代码语言:javascript
复制
- (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:,然后在内容数组中的完成处理程序块中获得位于给定路径上的所有文件:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19999638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档