Skip to content

Commit

Permalink
aad sample
Browse files Browse the repository at this point in the history
  • Loading branch information
yao-msft committed Jun 22, 2023
1 parent b7b2f0e commit c4447c5
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/AppInstallerCLI/AppInstallerCLI.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,4 @@
<Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.props'))" />
<Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\packages\Microsoft.Windows.CppWinRT.2.0.210505.3\build\native\Microsoft.Windows.CppWinRT.targets'))" />
</Target>
</Project>
</Project>
2 changes: 2 additions & 0 deletions src/AppInstallerCLICore/AppInstallerCLICore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Argument.h" />
<ClInclude Include="AuthProto.h" />
<ClInclude Include="ChannelStreams.h" />
<ClInclude Include="Command.h" />
<ClInclude Include="Commands\COMCommand.h" />
Expand Down Expand Up @@ -404,6 +405,7 @@
<ClInclude Include="Workflows\WorkflowBase.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="AuthProto.cpp" />
<ClCompile Include="COMContext.cpp" />
<ClCompile Include="Commands\COMCommand.cpp" />
<ClCompile Include="Commands\ConfigureCommand.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions src/AppInstallerCLICore/AppInstallerCLICore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@
<ClInclude Include="ConfigurationSetProcessorFactoryRemoting.h">
<Filter>Workflows</Filter>
</ClInclude>
<ClInclude Include="AuthProto.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
Expand Down Expand Up @@ -403,6 +406,9 @@
<ClCompile Include="ConfigurationSetProcessorFactoryRemoting.cpp">
<Filter>Workflows</Filter>
</ClCompile>
<ClCompile Include="AuthProto.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="PropertySheet.props" />
Expand Down
42 changes: 42 additions & 0 deletions src/AppInstallerCLICore/AuthProto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "pch.h"
#include <wil/result_macros.h>
#include <inspectable.h>
#include <wrl/wrappers/corewrappers.h>

Check failure on line 4 in src/AppInstallerCLICore/AuthProto.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`corewrappers` is not a recognized word. (unrecognized-spelling)
#include <windows.ui.core.h>
#include <wil/result_macros.h>

using namespace ::Windows::Foundation;
using namespace ::Microsoft::WRL;
using namespace ABI::Windows::Foundation;

void TryAuthProto()
{
::Microsoft::WRL::ComPtr<ABI::Windows::Security::Authentication::Web::Core::IWebAuthenticationCoreManagerStatics> m_authManagerStatics;
::Microsoft::WRL::ComPtr<ABI::Windows::Security::Authentication::Web::Core::IWebTokenRequestFactory> m_tokenFactory;
::Microsoft::WRL::ComPtr<ABI::Windows::Security::Credentials::IWebAccountProvider> 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<IWebAuthenticationCoreManagerInterop> managerInterop;
::Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncOperation<ABI::Windows::Security::Authentication::Web::Core::WebTokenRequestResult*>> webTokenRequestResultOperation;

hr = m_authManagerStatics.As(&managerInterop);

ComPtr<IAsyncOperation<ABI::Windows::Security::Credentials::WebAccountProvider*>> findAccountProviderAsyncOperation;

hr = m_authManagerStatics->FindAccountProviderWithAuthorityAsync(
::Microsoft::WRL::Wrappers::HStringReference(L"https://login.microsoft.com").Get(),
::Microsoft::WRL::Wrappers::HStringReference(L"organizations").Get(),
&findAccountProviderAsyncOperation);

//findAccountProviderAsyncOperation
}
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/AuthProto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void TryAuthProto();
125 changes: 111 additions & 14 deletions src/AppInstallerCLICore/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@
#include "Commands/InstallCommand.h"
#include "COMContext.h"
#include <AppInstallerFileLogger.h>
#include <wil/result_macros.h>
#include <inspectable.h>
#include <wrl/wrappers/corewrappers.h>
#include "AuthProto.h"

#ifndef AICLI_DISABLE_TEST_HOOKS
#include <winget/Debugging.h>
#endif
#include <windows.ui.core.h>

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;

Expand Down Expand Up @@ -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

Check failure on line 58 in src/AppInstallerCLICore/Core.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`wparam` is not a recognized word. (unrecognized-spelling)

Check failure on line 58 in src/AppInstallerCLICore/Core.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`lparam` is not a recognized word. (unrecognized-spelling)

Check failure on line 58 in src/AppInstallerCLICore/Core.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`WPARAM` is not a recognized word. (unrecognized-spelling)

Check failure on line 58 in src/AppInstallerCLICore/Core.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`LRESULT` is not a recognized word. (unrecognized-spelling)

Check failure on line 58 in src/AppInstallerCLICore/Core.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`Wnd` is not a recognized word. (unrecognized-spelling)

Check failure on line 58 in src/AppInstallerCLICore/Core.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`LPARAM` is not a recognized word. (unrecognized-spelling)
{
WINRT_ASSERT(window);



return DefWindowProc(window, message, wparam, lparam);
}

int CoreMain(int, wchar_t const**)
{
init_apartment();

Expand All @@ -74,6 +90,8 @@ namespace AppInstaller::CLI
// Set output to UTF8
ConsoleOutputCPRestore utf8CP(CP_UTF8);



Logging::Telemetry().SetCaller("winget-cli");
Logging::Telemetry().LogStartup();

Expand All @@ -82,20 +100,102 @@ 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<PCWSTR>(CoreMain),
&resourceModule);
THROW_LAST_ERROR_IF_NULL(resourceModule);

WNDCLASS wc = { };

Check failure on line 115 in src/AppInstallerCLICore/Core.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`WNDCLASS` is not a recognized word. (unrecognized-spelling)

wc.lpfnWndProc = WndProc;

Check failure on line 117 in src/AppInstallerCLICore/Core.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`lpfn` is not a recognized word. (unrecognized-spelling)
wc.hInstance = resourceModule;
wc.lpszClassName = CLASS_NAME;

Check failure on line 119 in src/AppInstallerCLICore/Core.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

`lpsz` is not a recognized word. (unrecognized-spelling)

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<WebTokenRequestResult> requestOperation;

constexpr winrt::guid iidAsyncRequestResult{ winrt::guid_of<IAsyncOperation<WebTokenRequestResult>>() };

auto managerFactory = winrt::get_activation_factory<WebAuthenticationCoreManager>();
winrt::com_ptr<IWebAuthenticationCoreManagerInterop> managerInterop{ managerFactory.as<IWebAuthenticationCoreManagerInterop>() };

auto hr = managerInterop->RequestTokenForWindowAsync(hwnd, req.as<::IInspectable>().get(), iidAsyncRequestResult, reinterpret_cast<void**>(&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<std::string> utf8Args;
for (int i = 1; i < argc; ++i)
{
utf8Args.emplace_back(Utility::ConvertToUTF8(argv[i]));
}
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) };
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions src/AppInstallerCLICore/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include <winrt/Windows.ApplicationModel.Store.Preview.InstallControl.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Security.Cryptography.Certificates.h>
#include <winrt/Windows.Security.Authentication.Web.Core.h>
#include <winrt/Windows.UI.ApplicationSettings.h>

#pragma warning( push )
#pragma warning ( disable : 6001 6285 6340 6388 )
Expand All @@ -51,3 +53,4 @@

#include <wrl/client.h>
#include <AppxPackaging.h>
#include <Webauthenticationcoremanagerinterop.h>

1 comment on commit c4447c5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log for details.

Unrecognized words (12)
corewrappers
hwnd
lparam
lpfn
lpsz
LRESULT
OVERLAPPEDWINDOW
USEDEFAULT
Webauthenticationcoremanagerinterop
Wnd
WNDCLASS
wparam
Previously acknowledged words that are now absent dsc remoting :arrow_right:
To accept ✔️ these unrecognized words as correct and remove the previously acknowledged and now absent words, run the following commands

... in a clone of the git@github.com:yao-msft/winget-cli.git repository
on the aadproto branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.21/apply.pl' |
perl - 'https://github.com/yao-msft/winget-cli/actions/runs/5341738971/attempts/1'
Available 📚 dictionaries could cover words not in the 📘 dictionary

This includes both expected items (479) from .github/actions/spelling/expect.txt and unrecognized words (12)

Dictionary Entries Covers
cspell:cpp/src/cpp.txt 30216 30
cspell:win32/src/win32.txt 53509 25
cspell:python/src/python/python-lib.txt 3873 7
cspell:php/php.txt 2597 6
cspell:java/java.txt 7642 5
cspell:python/src/python/python.txt 453 3
cspell:python/src/common/extra.txt 741 3
cspell:django/django.txt 859 3
cspell:typescript/typescript.txt 1211 2
cspell:npm/npm.txt 288 2

Consider adding them using (in .github/workflows/spelling3.yml):

      with:
        extra_dictionaries:
          cspell:cpp/src/cpp.txt
          cspell:win32/src/win32.txt
          cspell:python/src/python/python-lib.txt
          cspell:php/php.txt
          cspell:java/java.txt
          cspell:python/src/python/python.txt
          cspell:python/src/common/extra.txt
          cspell:django/django.txt
          cspell:typescript/typescript.txt
          cspell:npm/npm.txt

To stop checking additional dictionaries, add:

      with:
        check_extra_dictionaries: ''
If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.