Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
MIP SDK memang menerapkan delegasi autentikasi untuk menangani tantangan autentikasi dan merespons dengan token. Ini tidak mengimplementasikan akuisisi token. Proses akuisisi token terserah pengembang dan dicapai dengan memperluas mip::AuthDelegate kelas, khususnya AcquireOAuth2Token fungsi anggota.
Membangun AuthDelegateImpl
Untuk memperluas kelas mip::AuthDelegatedasar, kami membuat kelas baru yang disebut sample::auth::AuthDelegateImpl. Kelas ini mengimplementasikan AcquireOAuth2Token fungsionalitas, dan menyiapkan konstruktor untuk mengambil parameter autentikasi kami.
auth_delegate_impl.h
Untuk contoh ini, konstruktor default hanya menerima nama pengguna, kata sandi, dan ID aplikasi aplikasi. Ini akan disimpan dalam variabel mUserNameprivat , , mPassworddan mClientId.
Penting untuk dicatat bahwa informasi seperti penyedia identitas atau URI sumber daya tidak perlu diterapkan, setidaknya tidak di AuthDelegateImpl konstruktor. Informasi tersebut diteruskan sebagai bagian AcquireOAuth2Token dalam OAuth2Challenge objek. Sebagai gantinya, kita akan meneruskan detail tersebut ke panggilan AcquireToken di 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 adalah tempat panggilan ke penyedia OAuth2 akan dilakukan. Dalam contoh, di bawah ini ada dua panggilan ke AcquireToken(). Dalam praktiknya, hanya satu panggilan yang akan dilakukan. Implementasi ini akan tercakup dalam bagian yang disediakan di bawah Langkah Berikutnya
//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;
}
}
}
Langkah Selanjutnya
Untuk menyelesaikan implementasi autentikasi, perlu untuk membangun kode di belakang AcquireToken() fungsi. Contoh di bawah ini membahas beberapa cara untuk memperoleh token.