Notatka
Dostęp do tej strony wymaga autoryzacji. Może spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
W tym przykładzie pokazano, jak wywołać skrypt Python zewnętrznego w celu uzyskania tokenu OAuth 2.0. Implementacja delegata uwierzytelniania wymaga prawidłowego tokenu dostępu OAuth 2.0.
Wymagania wstępne
Aby uruchomić próbkę:
- Zainstaluj środowisko Python w wersji 3.10 lub nowszej.
- Zaimplementuj plik utils.h/cpp w projekcie.
- Dodaj
auth.pydo projektu w tym samym katalogu co pliki binarne w czasie kompilacji. - Ukończ konfigurowanie i konfigurację zestawu SDK usługi Microsoft Information Protection (MIP). Między innymi rejestrujesz aplikację kliencką w swojej dzierżawie Microsoft Entra. Microsoft Entra ID udostępnia identyfikator aplikacji, znany również jako identyfikator klienta dla logiki pozyskiwania tokenu.
Ten kod nie jest przeznaczony do użytku produkcyjnego. Użyj go tylko do programowania i do zrozumienia pojęć dotyczących uwierzytelniania. Przykład jest międzyplatformowy.
sample::auth::AcquireToken()
W prostym przykładzie uwierzytelniania pokazano prostą AcquireToken() funkcję, która nie przyjmuje parametrów i zwraca wartość tokenu zakodowanego w kodzie. Ten przykład przeciąża element AcquireToken(), aby akceptował parametry uwierzytelniania i wywoływał zewnętrzny skrypt w języku Python w celu zwrócenia tokenu.
auth.h
W pliku auth.h AcquireToken() jest przeciążony. Przeciążona funkcja i zaktualizowane parametry są następujące:
//auth.h
#include <string>
namespace sample {
namespace auth {
std::string AcquireToken(
const std::string& userName, //A string value containing the user's UPN.
const std::string& password, //The user's password in plaintext
const std::string& clientId, //The Azure AD client ID (also known as Application ID) of your application.
const std::string& resource, //The resource URL for which an OAuth2 token is required. Provided by challenge object.
const std::string& authority); //The authentication authority endpoint. Provided by challenge object.
}
}
Dane wejściowe użytkownika lub Twoja aplikacja dostarczają trzy pierwsze parametry. Zestaw SDK udostępnia dwa ostatnie parametry delegatowi uwierzytelniania.
auth.cpp
Plik auth.cpp dodaje przeciążonej definicji funkcji, a następnie definiuje kod, który wywołuje skrypt Python. Funkcja akceptuje wszystkie podane parametry i przekazuje je do skryptu Python. Skrypt jest uruchamiany i zwraca token w formacie ciągu.
#include "auth.h"
#include "utils.h"
#include <fstream>
#include <functional>
#include <memory>
#include <string>
using std::string;
using std::runtime_error;
namespace sample {
namespace auth {
//This function implements token acquisition in the application by calling an external Python script.
//The Python script requires username, password, clientId, resource, and authority.
//Username, Password, and ClientId are provided by the user/developer
//Resource and Authority are provided as part of the OAuth2Challenge object that is passed in by the SDK to the AuthDelegate.
string AcquireToken(
const string& userName,
const string& password,
const string& clientId,
const string& resource,
const string& authority) {
string cmd = "python";
if (sample::FileExists("auth.py"))
cmd += " auth.py -u ";
else
throw runtime_error("Unable to find auth script.");
cmd += userName;
cmd += " -p ";
cmd += password;
cmd += " -a ";
cmd += authority;
cmd += " -r ";
cmd += resource;
cmd += " -c ";
// Replace <application-id> with the Application ID provided during your Azure AD application registration.
cmd += (!clientId.empty() ? clientId : "<application-id>");
string result = sample::Execute(cmd.c_str());
if (result.empty())
throw runtime_error("Failed to acquire token. Ensure Python is installed correctly.");
return result;
}
}
}
Skrypt języka Python
Ten skrypt uzyskuje tokeny uwierzytelniania bezpośrednio przy użyciu Microsoft Authentication Library (MSAL) dla Python. Ten kod jest dołączany tylko do uzyskiwania tokenów uwierzytelniania do użycia przez przykładowe aplikacje i nie jest przeznaczony do użycia w środowisku produkcyjnym. Skrypt działa tylko w środowiskach, które obsługują uwierzytelnianie za pomocą nazwy użytkownika i hasła. Skrypt nie obsługuje uwierzytelniania wieloskładnikowego ani uwierzytelniania opartego na certyfikatach.
Uwaga / Notatka
Przed uruchomieniem tego przykładu zainstaluj bibliotekę MSAL dla Python, uruchamiając jedno z następujących poleceń:
pip install msal
pip3 install msal
import getopt
import sys
import json
import re
from msal import PublicClientApplication
def printUsage():
print('auth.py -u <username> -p <password> -a <authority> -r <resource> -c <clientId>')
def main(argv):
try:
options, args = getopt.getopt(argv, 'hu:p:a:r:c:')
except getopt.GetoptError:
printUsage()
sys.exit(-1)
username = ''
password = ''
authority = ''
resource = ''
clientId = ''
for option, arg in options:
if option == '-h':
printUsage()
sys.exit()
elif option == '-u':
username = arg
elif option == '-p':
password = arg
elif option == '-a':
authority = arg
elif option == '-r':
resource = arg
elif option == '-c':
clientId = arg
if username == '' or password == '' or authority == '' or resource == '' or clientId == '':
printUsage()
sys.exit(-1)
# ONLY FOR DEMO PURPOSES AND MSAL FOR PYTHON
# This shouldn't be required when using proper auth flows in production.
if authority.find('common') > 1:
authority = authority.split('/common')[0] + "/organizations"
app = PublicClientApplication(client_id=clientId, authority=authority)
result = None
if resource.endswith('/'):
resource += ".default"
else:
resource += "/.default"
# *DO NOT* use username/password authentication in production system.
# Instead, consider auth code flow and using a browser to fetch the token.
result = app.acquire_token_by_username_password(username=username, password=password, scopes=[resource])
print(result['access_token'])
if __name__ == '__main__':
main(sys.argv[1:])
Aktualizowanie tokenu AcquireOAuth2Token
Na koniec zaktualizuj funkcję AcquireOAuth2Token w AuthDelegateImpl aby wywołać funkcję AcquireToken przeciążoną. Przeczytaj challenge.GetResource() i challenge.GetAuthority() , aby uzyskać adresy URL zasobów i urzędu. SDK przekazuje element OAuth2Challenge delegatowi uwierzytelniania podczas dodawania silnika. To zachowanie tego SDK nie wymaga od dewelopera żadnej dodatkowej pracy.
bool AuthDelegateImpl::AcquireOAuth2Token(
const mip::Identity& /*identity*/,
const OAuth2Challenge& challenge,
OAuth2Token& token) {
//call our AcquireToken function, passing in username, password, clientId, and getting the resource/authority from the OAuth2Challenge object
string accessToken = sample::auth::AcquireToken(mUserName, mPassword, mClientId, challenge.GetResource(), challenge.GetAuthority());
token.SetAccessToken(accessToken);
return true;
}
Gdy SDK dodaje element engine, wywołuje funkcję AcquireOAuth2Token. Funkcja przekazuje wyzwanie, uruchamia skrypt Python, odbiera token i przekazuje go usłudze.