次の方法で共有


Microsoft Information Protection SDK - 認証デリゲートの実装 (C++)

MIP SDK は、認証チャレンジを処理し、トークンで応答するための認証デリゲートを実装します。 それ自体はトークンの取得を実装しません。 トークン取得プロセスは開発者が行い、 mip::AuthDelegate クラス (具体的には AcquireOAuth2Token メンバー関数) を拡張することによって実現されます。

AuthDelegateImpl のビルド

基底クラス mip::AuthDelegateを拡張するために、 sample::auth::AuthDelegateImplという新しいクラスを作成します。 このクラスは、 AcquireOAuth2Token 機能を実装し、認証パラメーターを受け取るコンストラクターを設定します。

auth_delegate_impl.h

この例では、既定のコンストラクターは、ユーザー名、パスワード、アプリケーションの アプリケーション ID のみを受け入れます。 これらは、プライベート変数 mUserNamemPassword、および mClientIdに格納されます。

ID プロバイダーやリソース URI などの情報は、少なくとも AuthDelegateImpl コンストラクターに実装する必要はありません。 この情報は、OAuth2Challenge オブジェクトのAcquireOAuth2Tokenの一部として渡されます。 代わりに、これらの詳細をAcquireTokenAcquireOAuth2Token呼び出しに渡します。

//auth_delegate_impl.h
#include <string.h>
#include "mip/common_types.h"

namespace sample {
namespace auth {
class AuthDelegateImpl final : public mip::AuthDelegate { //extend mip::AuthDelegate base class
public:
  AuthDelegateImpl() = delete;

//constructor accepts username, password, and mip::ApplicationInfo.
  AuthDelegateImpl::AuthDelegateImpl(
    const mip::ApplicationInfo& applicationInfo,
    std::string& username,
    const std::string& password)
    : mApplicationInfo(applicationInfo),
      mUserName(username),
      mPassword(password) {
  }

  bool AcquireOAuth2Token(const mip::Identity& identity, const OAuth2Challenge& challenge, OAuth2Token& token) override;

  private:
    std::string mUserName;
    std::string mPassword;
    std::string mClientId;
    mip::ApplicationInfo mApplicationInfo;
};
}
}

auth_delegate_impl.cpp

AcquireOAuth2Token は、OAuth2 プロバイダーの呼び出しが行われる場所です。 この例では、下に示すように、AcquireToken()への2つの呼び出しがあります。 実際には、呼び出しは 1 つだけ行われます。 これらの実装については、「次の手順」のセクションで説明します

//auth_delegate_impl.cpp
#include "auth_delegate_impl.h"
#include <stdexcept>
#include "auth.h" //contains the auth class used later for token acquisition

using std::runtime_error;
using std::string;

namespace sample {
namespace auth {

AuthDelegateImpl::AuthDelegateImpl(
    const string& userName,
    const string& password,
    const string& clientId)
    : mApplicationInfo(applicationInfo),
    mUserName(userName),
    mPassword(password) {
}

//Here we could simply add our token acquisition code to AcquireOAuth2Token
//Instead, that code is implemented in auth.h/cpp to demonstrate calling an external library
bool AuthDelegateImpl::AcquireOAuth2Token(
    const mip::Identity& /*identity*/, //This won't be used
    const OAuth2Challenge& challenge,
    const OAuth2Token& token) {

      //sample::auth::AcquireToken is the code where the token acquisition routine is implemented.
      //AcquireToken() returns a string that contains the OAuth2 token.

      //Simple example for getting hard coded token. Comment out if not used.
      string accessToken = sample::auth::AcquireToken();

      //Practical example for calling external OAuth2 library with provided authentication details.
      string accessToken = sample::auth::AcquireToken(mUserName, mPassword, mApplicationInfo.applicationId, challenge.GetAuthority(), challenge.GetResource());

      //set the passed in OAuth2Token value to the access token acquired by our provider
      token.SetAccessToken(accessToken);
      return true;
    }
}
}

次のステップ

認証の実装を完了するには、 AcquireToken() 関数の背後にあるコードをビルドする必要があります。 次の例では、トークンを取得するいくつかの方法について説明します。