Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
De MIP SDK implementeert een authenticatiegedelegeerde voor het afhandelen van authenticatie-uitdagingen en het reageren met een token. Het implementeert zelf geen tokenverwerving. Het tokenverwervingsproces is aan de ontwikkelaar en wordt uitgevoerd door de mip::AuthDelegate klasse uit te breiden, met name de AcquireOAuth2Token lidfunctie.
AuthDelegateImpl bouwen
Als u de basisklasse mip::AuthDelegatewilt uitbreiden, maken we een nieuwe klasse met de naam sample::auth::AuthDelegateImpl. Deze klasse implementeert de AcquireOAuth2Token functionaliteit en stelt de constructor in om onze verificatieparameters in te voeren.
auth_delegate_impl.h
In dit voorbeeld accepteert de standaardconstructor alleen gebruikersnaam, wachtwoord en de toepassings-id van de toepassing. Deze worden opgeslagen in de privévariabelen mUserName, mPassworden mClientId.
Het is belangrijk om te weten dat gegevens zoals de id-provider of resource-URI niet nodig zijn om te implementeren, in ieder geval niet in de AuthDelegateImpl constructor. Deze informatie wordt doorgegeven als onderdeel van AcquireOAuth2Token in het OAuth2Challenge-object. In plaats daarvan geven we die details door aan de AcquireToken-oproep in 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 is waar de aanroep naar de OAuth2-provider wordt uitgevoerd. In het onderstaande voorbeeld zijn er twee aanroepen naar AcquireToken(). In de praktijk zou er slechts één oproep worden gedaan. Deze implementaties worden behandeld in de secties onder Volgende stappen
//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;
}
}
}
Volgende stappen
Als u de verificatie-implementatie wilt voltooien, moet u de code achter de AcquireToken() functie bouwen. In de onderstaande voorbeelden worden enkele manieren besproken om het token te verkrijgen.