如何在iOS应用程序中完全注销Facebook SDK身份验证

4

使用FBSDKCoreKit 4.22.1

我在一款公开应用程序中,为多个用户提供服务。一个人可以走到iPad旁边,用他们的Facebook帐户登录:

enter image description here

成功登录后,他们可以做自己的工作并使用应用程序,然后过一段时间退出:

enter image description here

注销成功后,下一个用户来使用公共iPad并点击Facebook登录按钮,但他们会看到:enter image description here

FBSDKLoginManager或其他Facebook SDK库已经记住了先前用户的Facebook登录的某些元素。

我想完全清除有关上一个用户Facebook凭据的所有信息

成功注销后,将调用此FBSDKLoginButtonDelegate方法,并我尝试以下方法以完全删除Facebook帐户信息,但没有成功:

func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
    print("\(#function) in \(#file.components(separatedBy: "/").last ?? "")")
    print("Todo, must completely remove Facebook Auth token info for current user logging out")
    FBSDKAccessToken.setCurrent(nil)
    FBSDKLoginManager().logOut()
    FBSDKProfile.setCurrent(nil)
}

使用 FBSDKCoreKit 4.22.1,Safari 浏览器用来打开 Facebook 认证重定向 URL,该 JavaScript 使用本地存储:enter image description here 有趣的是,阻止所有 Cookie(苹果暗示这也会阻止网站数据(localStorage)),并不会阻止 localStorage,Facebook 仍然可以创建网站数据存储:enter image description here 苹果在此处 (链接) 中表示:
“更改接受的 cookies 和网站数据:选择一个“Cookies 和网站数据”选项:”
- “始终阻止:Safari 不允许任何网站、第三方或广告商在 Mac 上存储 cookie 和其他数据。这可能会防止某些网站正常工作。”
这就是我预期 localStorage 和 cookie 都会被阻止,但 Facebook 仍然可以创建 localStorage 条目。

该 SDK 可能使用 WebView 用于登录表单。你需要做的是删除该 WebView 使用的 Cookie 存储。 - uliwitness
@uliwitness 很好的想法,我本以为你是对的,也许你还是对的...我刚刚在模拟器上清除了Safari的历史记录和网站数据,但仍然遇到了同样的问题... - Brian Ogden
@uliwitness 这是网站数据,信息存储的地方,并不是cookie,我想它基本上是本地存储,无法停止这种存储。 - Brian Ogden
本地存储可以从JavaScript中访问。因此,您可以找到一个JavaScript来删除给定域的本地存储,并让您的网站运行它。 - uliwitness
请查看我在此处的答案 https://stackoverflow.com/a/51039251/5093900 也许可以帮到你。 - phantom_2
3个回答

3

我曾经遇到过类似的问题,最终我使用了一个修改过的SDK版本,但这样做实际上是非常危险的,因为其他开发人员可能不知道它被修改了。因此,你需要留下一条说明。

问题:在Safari中退出登录后,你仍然处于登录状态,但只有当你使用本地登录或系统登录时才会出现这种情况,并且没有办法从应用程序内部退出Safari。这非常烦人(你也无法在应用程序内部清除Safari的cookie或数据)。

解决方案:

如果你查看SDK的文档,它会显示:

// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.




typedef NS_ENUM(NSUInteger, FBSDKLoginBehavior)
{
  /*!
   @abstract This is the default behavior, and indicates logging in through the native
   Facebook app may be used. The SDK may still use Safari instead.
   */
  FBSDKLoginBehaviorNative = 0,
  /*!
   @abstract Attempts log in through the Safari or SFSafariViewController, if available.
   */
  FBSDKLoginBehaviorBrowser,
  /*!
   @abstract Attempts log in through the Facebook account currently signed in through
   the device Settings.
   @note If the account is not available to the app (either not configured by user or
   as determined by the SDK) this behavior falls back to \c FBSDKLoginBehaviorNative.
   */
  FBSDKLoginBehaviorSystemAccount,
  /*!
   @abstract Attemps log in through a modal \c UIWebView pop up

   @note This behavior is only available to certain types of apps. Please check the Facebook
   Platform Policy to verify your app meets the restrictions.
   */
  FBSDKLoginBehaviorWeb,
};

如果您使用原生应用程序,但无法正常工作,它将回退到Safari。如果您使用系统登录,但无法正常工作,则会回退到Native,然后回退到Safari。
然后有FBSDKLoginBehaviorWeb,它使用模态Web视图/弹出窗口!因此,如果您不绝对必须使用Native或System Login,则建议您选择此选项,因为它不会回退到Safari。
否则: 这就是我所做的更改,以便在后台从未使用Safari:
FBLoginSDKManager.m:
- (void)logInWithBehavior:(FBSDKLoginBehavior)loginBehavior
{
  NSDictionary *loginParams = [self logInParametersWithPermissions:_requestedPermissions];

  void(^completion)(BOOL, NSString *, NSError *) = ^void(BOOL didPerformLogIn, NSString *authMethod, NSError *error) {
    if (didPerformLogIn) {
      [_logger startAuthMethod:authMethod];
      _performingLogIn = YES;
    } else {
      if (!error) {
        error = [NSError errorWithDomain:FBSDKLoginErrorDomain code:FBSDKLoginUnknownErrorCode userInfo:nil];
      }
      [self invokeHandler:nil error:error];
    }
  };

  switch (loginBehavior) {
    case FBSDKLoginBehaviorNative: {
      if ([FBSDKInternalUtility isFacebookAppInstalled]) {
        [FBSDKServerConfigurationManager loadServerConfigurationWithCompletionBlock:^(FBSDKServerConfiguration *serverConfiguration, NSError *loadError) {
          BOOL useNativeDialog = [serverConfiguration useNativeDialogForDialogName:FBSDKDialogConfigurationNameLogin];
          if (useNativeDialog && loadError == nil) {
            [self performNativeLogInWithParameters:loginParams handler:^(BOOL openedURL, NSError *openedURLError) {
              if (openedURLError) {
                [FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorDeveloperErrors
                                   formatString:@"FBSDKLoginBehaviorNative failed : %@\nTrying FBSDKLoginBehaviorBrowser", openedURLError];
              }
              if (openedURL) {
                completion(YES, FBSDKLoginManagerLoggerAuthMethod_Native, openedURLError);
              } else {
                [self logInWithBehavior:FBSDKLoginBehaviorWeb];  //-- CHANGED BY BRANDON T.
              }
            }];
          } else {
            [self logInWithBehavior:FBSDKLoginBehaviorWeb];  //-- CHANGED BY BRANDON T.
          }
        }];
        break;
      }
        // intentional fall through.  -- CHANGED BY BRANDON T.
        [self logInWithBehavior:FBSDKLoginBehaviorWeb];  //-- CHANGED BY BRANDON T.
        break;
    }
    case FBSDKLoginBehaviorBrowser: {
      [self performBrowserLogInWithParameters:loginParams handler:^(BOOL openedURL,
                                                                    NSString *authMethod,
                                                                    NSError *openedURLError) {
        if (openedURL) {
          completion(YES, authMethod, openedURLError);
        } else {
          completion(NO, authMethod, openedURLError);
        }
      }];
      break;
    }
    case FBSDKLoginBehaviorSystemAccount: {
      [FBSDKServerConfigurationManager loadServerConfigurationWithCompletionBlock:^(FBSDKServerConfiguration *serverConfiguration, NSError *loadError) {
        if (serverConfiguration.isSystemAuthenticationEnabled && loadError == nil) {
          [self beginSystemLogIn];
        } else {
          [self logInWithBehavior:FBSDKLoginBehaviorNative];
        }
      }];
      completion(YES, FBSDKLoginManagerLoggerAuthMethod_System, nil);
      break;
    }
    case FBSDKLoginBehaviorWeb:
      [self performWebLogInWithParameters:loginParams handler:^(BOOL openedURL, NSError *openedURLError) {
        completion(openedURL, FBSDKLoginManagerLoggerAuthMethod_Webview, openedURLError);
      }];
      break;
  }
}

这样,所有本机登录或系统登录都将回溯到应用内的modal-UIWebView。然后,您可以在注销时清除cookies,就没有问题了。注销后删除NSHTTPCookieStorage.sharedHTTPCookieStorage().cookiesNSURLCache.sharedURLCache().removeAllCachedResponses()
显然最安全的选择是永远不使用系统或本机登录,而始终使用:FBSDKLoginBehaviorWeb……

谢谢@Brandon,真是个麻烦,不过这也是软件开发生涯中的家常便饭哈哈,我现在正在尝试实施你的解决方案。 - Brian Ogden

1

但是网页弹出窗口的弹出方式很古怪。我最接近的解决方案是显示提示,要求用户在Safari上注销,然后调用UIApplication.shared.open(url: URL(string: "https://facebook.com")!)将他们带到那里。这会打破应用程序的流程,但会完全注销他们(如果他们按照指示在Safari上注销):(。我想知道Facebook SDK是如何存储凭据的,以便Safari知道该特定用户已登录。有人知道他们是如何做到的吗?如果我知道了,也许我就可以用自己隐藏的WKWebView自动化它。 - Xavier L.

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接