From c4447c5b51f3f945ea35b13597c580b66a181676 Mon Sep 17 00:00:00 2001 From: Yao Sun Date: Wed, 21 Jun 2023 21:51:24 -0700 Subject: [PATCH] aad sample --- src/AppInstallerCLI/AppInstallerCLI.vcxproj | 2 +- .../AppInstallerCLICore.vcxproj | 2 + .../AppInstallerCLICore.vcxproj.filters | 6 + src/AppInstallerCLICore/AuthProto.cpp | 42 ++++++ src/AppInstallerCLICore/AuthProto.h | 1 + src/AppInstallerCLICore/Core.cpp | 125 ++++++++++++++++-- src/AppInstallerCLICore/pch.h | 3 + 7 files changed, 166 insertions(+), 15 deletions(-) create mode 100644 src/AppInstallerCLICore/AuthProto.cpp create mode 100644 src/AppInstallerCLICore/AuthProto.h diff --git a/src/AppInstallerCLI/AppInstallerCLI.vcxproj b/src/AppInstallerCLI/AppInstallerCLI.vcxproj index 9d23801443..3ce537f7e7 100644 --- a/src/AppInstallerCLI/AppInstallerCLI.vcxproj +++ b/src/AppInstallerCLI/AppInstallerCLI.vcxproj @@ -295,4 +295,4 @@ - + \ No newline at end of file diff --git a/src/AppInstallerCLICore/AppInstallerCLICore.vcxproj b/src/AppInstallerCLICore/AppInstallerCLICore.vcxproj index 04ebb3e393..c8c7065be8 100644 --- a/src/AppInstallerCLICore/AppInstallerCLICore.vcxproj +++ b/src/AppInstallerCLICore/AppInstallerCLICore.vcxproj @@ -337,6 +337,7 @@ + @@ -404,6 +405,7 @@ + diff --git a/src/AppInstallerCLICore/AppInstallerCLICore.vcxproj.filters b/src/AppInstallerCLICore/AppInstallerCLICore.vcxproj.filters index dd08ae6951..72fc851078 100644 --- a/src/AppInstallerCLICore/AppInstallerCLICore.vcxproj.filters +++ b/src/AppInstallerCLICore/AppInstallerCLICore.vcxproj.filters @@ -218,6 +218,9 @@ Workflows + + Header Files + @@ -403,6 +406,9 @@ Workflows + + Source Files + diff --git a/src/AppInstallerCLICore/AuthProto.cpp b/src/AppInstallerCLICore/AuthProto.cpp new file mode 100644 index 0000000000..a9c7168b0b --- /dev/null +++ b/src/AppInstallerCLICore/AuthProto.cpp @@ -0,0 +1,42 @@ +#include "pch.h" +#include +#include +#include +#include +#include + +using namespace ::Windows::Foundation; +using namespace ::Microsoft::WRL; +using namespace ABI::Windows::Foundation; + +void TryAuthProto() +{ + ::Microsoft::WRL::ComPtr m_authManagerStatics; + ::Microsoft::WRL::ComPtr m_tokenFactory; + ::Microsoft::WRL::ComPtr m_liveProvider; + + HRESULT hr = ::Windows::Foundation::GetActivationFactory( + ::Microsoft::WRL::Wrappers::HStringReference(RuntimeClass_Windows_Security_Authentication_Web_Core_WebAuthenticationCoreManager).Get(), + &m_authManagerStatics); + + if (SUCCEEDED(hr)) + { + hr = ::Windows::Foundation::GetActivationFactory( + ::Microsoft::WRL::Wrappers::HStringReference(RuntimeClass_Windows_Security_Authentication_Web_Core_WebTokenRequest).Get(), + &m_tokenFactory); + } + + ::Microsoft::WRL::ComPtr managerInterop; + ::Microsoft::WRL::ComPtr> webTokenRequestResultOperation; + + hr = m_authManagerStatics.As(&managerInterop); + + ComPtr> findAccountProviderAsyncOperation; + + hr = m_authManagerStatics->FindAccountProviderWithAuthorityAsync( + ::Microsoft::WRL::Wrappers::HStringReference(L"https://login.microsoft.com").Get(), + ::Microsoft::WRL::Wrappers::HStringReference(L"organizations").Get(), + &findAccountProviderAsyncOperation); + + //findAccountProviderAsyncOperation +} \ No newline at end of file diff --git a/src/AppInstallerCLICore/AuthProto.h b/src/AppInstallerCLICore/AuthProto.h new file mode 100644 index 0000000000..3268b1c720 --- /dev/null +++ b/src/AppInstallerCLICore/AuthProto.h @@ -0,0 +1 @@ +void TryAuthProto(); \ No newline at end of file diff --git a/src/AppInstallerCLICore/Core.cpp b/src/AppInstallerCLICore/Core.cpp index 03818c81f8..afe7aceb7e 100644 --- a/src/AppInstallerCLICore/Core.cpp +++ b/src/AppInstallerCLICore/Core.cpp @@ -9,13 +9,20 @@ #include "Commands/InstallCommand.h" #include "COMContext.h" #include +#include +#include +#include +#include "AuthProto.h" #ifndef AICLI_DISABLE_TEST_HOOKS #include #endif +#include using namespace winrt; using namespace winrt::Windows::Foundation; +using namespace winrt::Windows::Security::Authentication::Web::Core; +using namespace winrt::Windows::Security::Credentials; using namespace AppInstaller::CLI; using namespace AppInstaller::Utility::literals; @@ -48,7 +55,16 @@ namespace AppInstaller::CLI }; } - int CoreMain(int argc, wchar_t const** argv) try + static LRESULT __stdcall WndProc(HWND const window, UINT const message, WPARAM const wparam, LPARAM const lparam) noexcept + { + WINRT_ASSERT(window); + + + + return DefWindowProc(window, message, wparam, lparam); + } + + int CoreMain(int, wchar_t const**) { init_apartment(); @@ -74,6 +90,8 @@ namespace AppInstaller::CLI // Set output to UTF8 ConsoleOutputCPRestore utf8CP(CP_UTF8); + + Logging::Telemetry().SetCaller("winget-cli"); Logging::Telemetry().LogStartup(); @@ -82,7 +100,89 @@ namespace AppInstaller::CLI context << Workflow::ReportExecutionStage(Workflow::ExecutionStage::ParseArgs); - // Convert incoming wide char args to UTF8 + + // std::thread waitThread([&] + // { + const wchar_t CLASS_NAME[] = L"Sample Window Class"; + + HMODULE resourceModule = nullptr; + GetModuleHandleExW( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(CoreMain), + &resourceModule); + THROW_LAST_ERROR_IF_NULL(resourceModule); + + WNDCLASS wc = { }; + + wc.lpfnWndProc = WndProc; + wc.hInstance = resourceModule; + wc.lpszClassName = CLASS_NAME; + + RegisterClass(&wc); + + HWND hwnd = CreateWindowEx( + 0, // Optional window styles. + CLASS_NAME, // Window class + L"Learn to Program Windows", // Window text + WS_OVERLAPPEDWINDOW, // Window style + + // Size and position + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + + NULL, // Parent window + NULL, // Menu + resourceModule, // Instance handle + NULL // Additional application data + ); + + if (hwnd == NULL) + { + return 0; + } + + auto provider = WebAuthenticationCoreManager::FindAccountProviderAsync(L"https://login.microsoft.com", L"organizations").get(); + + WebTokenRequest req(provider, L"", L"edb7e0dc-a3bf-4b99-a0aa-6cad61ed1b5e"); + + req.Properties().Insert(L"resource", L"https://graph.microsoft.com"); + + winrt::Windows::Foundation::IAsyncOperation requestOperation; + + constexpr winrt::guid iidAsyncRequestResult{ winrt::guid_of>() }; + + auto managerFactory = winrt::get_activation_factory(); + winrt::com_ptr managerInterop{ managerFactory.as() }; + + auto hr = managerInterop->RequestTokenForWindowAsync(hwnd, req.as<::IInspectable>().get(), iidAsyncRequestResult, reinterpret_cast(&requestOperation)); + + if (SUCCEEDED(hr)) + { + auto result = requestOperation.get(); + if (result.ResponseStatus() != WebTokenRequestStatus::Success) + { + std::cout << result.ResponseError().ErrorCode() << std::endl; + std::wcout << result.ResponseError().ErrorMessage().c_str(); + return -1; + } + else + { + std::wcout << result.ResponseData().GetAt(0).Token().c_str(); + } + } + + // TryAuthProto(); + + + // RuntimeClass_Windows_UI_Core_CoreWindow + +// return 0; + // }); + + // waitThread.join(); + + return 0; + + /*// Convert incoming wide char args to UTF8 std::vector utf8Args; for (int i = 1; i < argc; ++i) { @@ -90,12 +190,12 @@ namespace AppInstaller::CLI } AICLI_LOG(CLI, Info, << "WinGet invoked with arguments:" << [&]() { - std::stringstream strstr; - for (const auto& arg : utf8Args) - { - strstr << " '" << arg << '\''; - } - return strstr.str(); + std::stringstream strstr; + for (const auto& arg : utf8Args) + { + strstr << " '" << arg << '\''; + } + return strstr.str(); }()); Invocation invocation{ std::move(utf8Args) }; @@ -142,12 +242,9 @@ namespace AppInstaller::CLI } return Execute(context, command); - } - // End of the line exceptions that are not ever expected. - // Telemetry cannot be reliable beyond this point, so don't let these happen. - catch (...) - { - return APPINSTALLER_CLI_ERROR_INTERNAL_ERROR; + + // End of the line exceptions that are not ever expected. + // Telemetry cannot be reliable beyond this point, so don't let these happen.*/ } void ServerInitialize() diff --git a/src/AppInstallerCLICore/pch.h b/src/AppInstallerCLICore/pch.h index 50a37a2cc0..122a875c18 100644 --- a/src/AppInstallerCLICore/pch.h +++ b/src/AppInstallerCLICore/pch.h @@ -40,6 +40,8 @@ #include #include #include +#include +#include #pragma warning( push ) #pragma warning ( disable : 6001 6285 6340 6388 ) @@ -51,3 +53,4 @@ #include #include +#include