2

适用于 iOS 的 box.com SDK 有一个名为的对象,该对象sharedSDK包含另一个名为OAuth2Session. OAuth2Session有一个名为 的属性isAuthorized。在每次应用程序启动时,此属性设置为NO。即使我保留refreshToken系统钥匙串内部,并在启动时分配它,如下所示:

//...applicationDidFinisLaunching...
NSString *token = [controllerObject fetchFromKeychainForKey:@"com.box.token"];
[BoxSDK sharedSDK].OAuth2Session.refreshToken = token;

if ([BoxSDK sharedSDK].OAuth2Session.isAuthorized) {
    //Not until signing in
    NSLog(@"Authorized.)";
} else {
    NSLog(@"Not Authorized.");
}

我应该做些什么来检查身份验证状态?Dropbox SDK 有一种方法来确定会话是否已链接,并通过启动持续存在。

4

1 回答 1

3

我是 iOS SDK 的作者。该isAuthorized方法只是对当前 OAuth2 令牌是否有效的最佳猜测。从文档中:

将 accessTokenExpiration 与当前时间进行比较以确定访问令牌是否有效。这不能保证访问令牌有效,因为它可能已被撤销或已刷新。

因为accessTokenExpiration没有被 Box iOS SDK 存储在任何地方,所以这个字段在初始化后将是 nil,即使加载了刷新令牌。

Box iOS SDK 的立场是 Box API 是状态的真实来源,并且不会尝试执行可以由服务器更可靠地处理的客户端检查。

重新加载 OAuth2 会话的推荐方法是像您所做的那样从钥匙串设置刷新令牌,然后发出“心跳”API 调用以触发自动刷新,或者如果刷新令牌无效则失败。

可以在Box iOS SDK 示例应用程序中找到一个示例

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(boxAPIAuthenticationDidSucceed:)
                                                 name:BoxOAuth2SessionDidBecomeAuthenticatedNotification
                                               object:[BoxSDK sharedSDK].OAuth2Session];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(boxAPIAuthenticationDidFail:)
                                                 name:BoxOAuth2SessionDidReceiveAuthenticationErrorNotification
                                               object:[BoxSDK sharedSDK].OAuth2Session];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(boxAPIInitiateLogin:)
                                                 name:BoxOAuth2SessionDidReceiveRefreshErrorNotification
                                               object:[BoxSDK sharedSDK].OAuth2Session];

    // attempt to heartbeat. This will succeed if we successfully refresh
    // on failure, the BoxOAuth2SessionDidReceiveRefreshErrorNotification notification will be triggered
    [self boxAPIHeartbeat];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)boxAPIHeartbeat
{
    [[BoxSDK sharedSDK].foldersManager folderInfoWithID:BoxAPIFolderIDRoot requestBuilder:nil success:nil failure:nil];
}

#pragma mark - Handle OAuth2 session notifications
- (void)boxAPIAuthenticationDidSucceed:(NSNotification *)notification
{
    NSLog(@"Received OAuth2 successfully authenticated notification");
    BoxOAuth2Session *session = (BoxOAuth2Session *) [notification object];
    NSLog(@"Access token  (%@) expires at %@", session.accessToken, session.accessTokenExpiration);
    NSLog(@"Refresh token (%@)", session.refreshToken);

    [self dismissViewControllerAnimated:YES completion:nil];

    BOXAssert(self.viewControllers.count == 1, @"There should only be one folder in the hierarchy when authentication succeeds");
    BoxFolderViewController *rootVC = (BoxFolderViewController *)self.topViewController;
    [rootVC fetchFolderItemsWithFolderID:BoxAPIFolderIDRoot name:@"All Files"];
}

- (void)boxAPIAuthenticationDidFail:(NSNotification *)notification
{
    NSLog(@"Received OAuth2 failed authenticated notification");
    NSString *oauth2Error = [[notification userInfo] valueForKey:BoxOAuth2AuthenticationErrorKey];
    NSLog(@"Authentication error  (%@)", oauth2Error);

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)boxAPIInitiateLogin:(NSNotification *)notification
{
    NSLog(@"Refresh failed. User is logged out. Initiate login flow");

    dispatch_sync(dispatch_get_main_queue(), ^{
        [self popToRootViewControllerAnimated:YES];

        NSURL *authorizationURL = [BoxSDK sharedSDK].OAuth2Session.authorizeURL;
        NSString *redirectURI = [BoxSDK sharedSDK].OAuth2Session.redirectURIString;
        BoxAuthorizationViewController *authorizationViewController = [[BoxAuthorizationViewController alloc] initWithAuthorizationURL:authorizationURL redirectURI:redirectURI];
        BoxAuthorizationNavigationController *loginNavigation = [[BoxAuthorizationNavigationController alloc] initWithRootViewController:authorizationViewController];
        authorizationViewController.delegate = loginNavigation;
        loginNavigation.modalPresentationStyle = UIModalPresentationFormSheet;

        [self presentViewController:loginNavigation animated:YES completion:nil];
    });

}

此视图控制器注册 OAuth2 通知,这些通知在成功刷新或注销时触发。在为这些回调注册的选择器中,您可以在应用中加载视图控制器或加载 BoxAuthorizationViewController 以登录用户。

于 2013-11-13T01:07:43.883 回答