Microsoft Information Protection SDK — implementowanie pełnomocnika uwierzytelniania (C++)

Zestaw SDK protokołu MIP implementuje pełnomocnika uwierzytelniania, który ma reagować na wyzwania związane z uwierzytelnianiem i odpowiadać przy użyciu tokenu. Nie obejmuje ona samego pozyskiwania tokenów. Proces pozyskiwania tokenu należy do dewelopera i jest zrealizowany przez rozszerzenie klasy, a mip::AuthDelegate zwłaszcza AcquireOAuth2Token funkcji członka.

Building AuthDelegateImpl

Aby rozszerzyć zajęcia mip::AuthDelegate podstawowe, tworzymy nowe zajęcia o nazwie sample::auth::AuthDelegateImpl . Ta klasa implementuje funkcję i konfiguruje konstruktora w celu AcquireOAuth2Token wykonania parametrów uwierzytelniania.

auth_delegate_impl.h

W tym przykładzie konstruktor domyślny akceptuje tylko nazwę użytkownika, hasło i identyfikator aplikacji. Będą one przechowywane w zmiennych prywatnych mUserName , mPassword i mClientId .

Należy pamiętać, że informacje, takie jak dostawca tożsamości lub URI zasobu, nie są konieczne do wdrożenia, przynajmniej nie w AuthDelegateImpl konstruktorze. Te informacje są przekazywane jako część AcquireOAuth2TokenOAuth2Challenge obiektu. Zamiast tego przekażemy te dane do AcquireToken rozmowy w AcquireOAuth2Token .

//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 w którym zostanie na wykonane połączenie z dostawcą OAuth2. W poniższym przykładzie są dwa połączenia z AcquireToken() . W praktyce zostanie wykonana tylko jedna połączenie. Te implementacje zostaną opisane w sekcjach poszczególnych kroków

//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;
    }
}
}

Następne kroki

Aby ukończyć implementację uwierzytelniania, konieczne jest skompilowanie kodu, który jest pod nią AcquireToken() funkcją. W poniższych przykładach omówiono kilka sposobów na uzyskanie tokenu.