在 iOS 应用程序中实施验证问题处理程序

improve this page | report issue


概述

在尝试访问受保护资源时,服务器(安全性检查)会向客户机发回一个列表,其中包含一个或多个验证问题,供客户机处理。
该列表会作为 JSON 对象接收,列出安全性检查名称以及其他数据的可选 JSON

{
  "challenges": {
    "SomeSecurityCheck1":null,
    "SomeSecurityCheck2":{
      "some property": "some value"
    }
  }
}

然后,客户机必须为每项安全性检查注册验证问题处理程序
验证问题处理程序可定义特定于安全性检查的客户机端行为。

创建验证问题处理程序

验证问题处理程序是可处理 MobileFirst Server 发送的验证问题的类,如显示登录屏幕、收集凭证和将其提交回安全性检查。

在此示例中,安全性检查为 PinCodeAttempts,在实现 CredentialsValidationSecurityCheck 中定义。 此安全性检查发送的验证问题包含剩余登录尝试次数 (remainingAttempts) 以及可选 errorMsg

创建可扩展 SecurityCheckChallengeHandler 的 Swift 类:

open class PinCodeChallengeHandler : SecurityCheckChallengeHandlerSwift {

}

处理验证问题

SecurityCheckChallengeHandler 协议的最低要求是实现 handleChallenge 方法,这会提示用户提供凭证。 handleChallenge 方法会接收作为 Dictionary 的验证问题 JSON

在此示例中,警报会提示用户输入 PIN 码:

override open func handleChallenge(challengeResponse: [AnyHashable: Any]!) {
  var errorMsg : String
  if challengeResponse!["errorMsg"] is NSNull {
      errorMsg = "This data requires a PIN code."
    }
    else{
      errorMsg = challengeResponse!["errorMsg"] as! String
  }
  let remainingAttempts = challengeResponse!["remainingAttempts"] as! Int + 2;

  showPopup(errorMsg,remainingAttempts: remainingAttempts);
}

样本应用程序中包含 showPopup 的实现。

如果凭证不正确,那么预计框架会再次调用 handleChallenge

提交验证问题的答案

在从 UI 收集凭证之后,使用 WLChallengeHandlersubmitChallengeAnswer(answer: [NSObject : AnyObject]!) 方法将答案发送回安全性检查。 在此示例中,PinCodeAttempts 预期有一个名为 pin 且包含提交的 PIN 码的属性:

self.submitChallengeAnswer(["pin": pinTextField.text!])

取消验证问题

在某些情况下(如单击 UI 中的取消按钮),您想要通知框架完全丢弃此验证问题。

要实现此目标,请调用:

self.cancel()

处理故障

某些场景可能会触发故障(如达到最大尝试次数)。 要处理这些场景,请实现 SecurityCheckChallengeHandlerhandleFailure 方法。 作为参数传递的 Dictionary 的结构很大程度上取决于故障性质。

override open func handleFailure(failureResponse: [AnyHashable: Any]!) {
    if let errorMsg = failureResponse["failure"] as? String {
        showError(errorMsg)
    }
    else{
        showError("Unknown error")
    }
}

样本应用程序中包含 showError 的实现。

处理成功

(可选)还可通过实现 SecurityCheckChallengeHandler 的 handleSuccess(successResponse: [AnyHashable: Any]!) 方法,选择在框架关闭验证问题处理程序流之前执行某些操作。同样,成功的 Dictionary 的内容和结构取决于安全性检查发送的内容。

注册验证问题处理程序

为了使验证问题处理程序侦听正确的验证问题,您必须通知框架将验证问题处理程序与特定的安全性检查名称相关联。

为此,请使用安全性检查初始化验证问题处理程序,如下所述:

var someChallengeHandler = SomeChallengeHandler(securityCheck: "securityCheckName”);

然后,您必须注册验证问题处理程序实例:

WLClientSwift.sharedInstance().registerChallengeHandler(challengeHandler: someChallengeHandler);

在此示例中,位于一行上:

WLClientSwift.sharedInstance().registerChallengeHandler(challengeHandler: PinCodeChallengeHandler(securityCheck: securityCheck));

注:在整个应用程序生命周期内,注册验证问题处理程序应当只进行一次。 建议使用 iOS AppDelegate 类来执行此操作。

样本应用程序

样本 PinCodeSwift 是一款使用 WLResourceRequest 获取银行存款余额的 iOS Swift 应用程序。
该方法通过 PIN 码受到保护,最多有 3 次尝试机会。

单击以下载 SecurityAdapters Maven 项目。
单击以下载 iOS Swift 本机项目。

样本用法

请遵循样本的 README.md 文件获取指示信息。

样本应用程序

Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on June 01, 2020