Windows 8.1 Universal アプリケーションおよび Windows 10 UWP アプリケーションでのチャレンジ・ハンドラーの実装

improve this page | report issue

概説

保護リソースへのアクセスを試みると、クライアントにはサーバー (セキュリティー検査) から、クライアントが対処する必要がある 1 つ以上のチャレンジを含んだリストが返信されます。
このリストは、JSON オブジェクトとして届けられ、セキュリティー検査名とともにオプションで追加データの JSON がリストされています。

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

その後、クライアントは、セキュリティー検査ごとにチャレンジ・ハンドラーを登録する必要があります。
チャレンジ・ハンドラーによって、そのセキュリティー検査特定のクライアント・サイドの動作が定義されます。

チャレンジ・ハンドラーの作成

チャレンジ・ハンドラーは、MobileFirst Server によって送信されるチャレンジを処理するクラスです。例えば、ログイン画面を表示したり、資格情報を収集したり、それらを元のセキュリティー検査に送信したりします。

この例の場合、セキュリティー検査は PinCodeAttempts であり、これは CredentialsValidationSecurityCheck の実装で定義したものです。 このセキュリティー検査によって送信されるチャレンジには、ログインを試行できる残りの回数 (remainingAttempts) と、オプションで errorMsg が含まれます。

Worklight.SecurityCheckChallengeHandler を継承する C# クラスを作成します。

public class PinCodeChallengeHandler : Worklight.SecurityCheckChallengeHandler
{
}

チャレンジの処理

SecurityCheckChallengeHandler クラスが求める最小要件は、コンストラクターおよび HandleChallenge メソッドを実装することです。このメソッドは、ユーザーに資格情報の提出を求める責任があります。 HandleChallenge メソッドは、Object としてチャレンジを受け取ります。

コンストラクター・メソッドを追加します。

public PinCodeChallengeHandler(String securityCheck) {
    this.securityCheck = securityCheck;
}

この HandleChallenge の例では、PIN コードの入力をユーザーに要求するアラートが出されます。

public override void HandleChallenge(Object challenge)
{
    try
    {
      JObject challengeJSON = (JObject)challenge;

      if (challengeJSON.GetValue("errorMsg") != null)
      {
          if (challengeJSON.GetValue("errorMsg").Type == JTokenType.Null)
              errorMsg = "This data requires a PIN Code.\n";
      }

      await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
           async () =>
           {
               _this.HintText.Text = "";
               _this.LoginGrid.Visibility = Visibility.Visible;
               if (errorMsg != "")
               {
                   _this.HintText.Text = errorMsg + "Remaining attempts: " + challengeJSON.GetValue("remainingAttempts");
               }
               else
               {
                   _this.HintText.Text = challengeJSON.GetValue("errorMsg") + "\n" + "Remaining attempts: " + challengeJSON.GetValue("remainingAttempts");
               }

               _this.GetBalance.IsEnabled = false;
           });
    } catch (Exception e)
    {
        Debug.WriteLine(e.StackTrace);
    }
}

showChallenge の実装がサンプル・アプリケーションに組み込まれています。

資格情報が正しくない場合、フレームワークによって再度 HandleChallenge が呼び出されます。

チャレンジ応答の送信

UI から資格情報が収集された後は、SecurityCheckChallengeHandlerShouldSubmitChallengeAnswer() メソッドと GetChallengeAnswer() メソッドを使用して、セキュリティー検査に応答を返信します。 ShouldSubmitChallengeAnswer() は、セキュリティー検査にチャレンジ応答を返信する必要があるかどうかを示すブール値を返します。 この例の場合、PinCodeAttempts は、提供された PIN コードを含んでいる pin というプロパティーを必要とします。

public override bool ShouldSubmitChallengeAnswer()
{
  JObject pinJSON = new JObject();
  pinJSON.Add("pin", pinCodeTxt.Text);
  this.challengeAnswer = pinJSON;
  return this.shouldsubmitchallenge;
}

public override JObject GetChallengeAnswer()
{
  return this.challengeAnswer;
}

チャレンジのキャンセル

UI で「キャンセル」ボタンがクリックされたときなど、フレームワークに対して、このチャレンジを完全に破棄するように指示する必要が生じる場合があります。

これを実現するには、ShouldCancel メソッドをオーバーライドします。

public override bool ShouldCancel()
{
  return shouldsubmitcancel;
}

チャレンジ・ハンドラーの登録

チャレンジ・ハンドラーが正しいチャレンジを listen するためには、フレームワークに対して、チャレンジ・ハンドラーと特定のセキュリティー検査名を関連付けるように指示する必要があります。

そのためには、以下のようにセキュリティー検査を指定してチャレンジ・ハンドラーを初期設定します。

PinCodeChallengeHandler pinCodeChallengeHandler = new PinCodeChallengeHandler("PinCodeAttempts");

次に、チャレンジ・ハンドラー・インスタンスを登録する必要があります。

IWorklightClient client = WorklightClient.createInstance();
client.RegisterChallengeHandler(pinCodeChallengeHandler);

サンプル・アプリケーション

PinCodeWin8 および PinCodeWin10 のサンプルは、ResourceRequest を使用して銀行の残高を照会する C# アプリケーションです。
このメソッドは、PIN コードと、最大 3 回までの試行によって保護されています。

ここをクリック して SecurityCheckAdapters Maven プロジェクトをダウンロードします。
ここをクリック して Windows 8 プロジェクトをダウンロードします。
ここをクリック して Windows 10 UWP プロジェクトをダウンロードします。

サンプルの使用法

サンプルの 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 March 15, 2018