FreeRDP
sdl_webview.cpp
1 
20 #include <string>
21 #include <sstream>
22 #include <cstdlib>
23 #include <winpr/string.h>
24 #include <freerdp/log.h>
25 
26 #include "sdl_webview.hpp"
27 #include "webview_impl.hpp"
28 
29 #define TAG CLIENT_TAG("SDL.webview")
30 
31 static BOOL sdl_webview_get_rdsaad_access_token(freerdp* instance, const char* scope,
32  const char* req_cnf, char** token)
33 {
34  WINPR_ASSERT(instance);
35  WINPR_ASSERT(scope);
36  WINPR_ASSERT(req_cnf);
37  WINPR_ASSERT(token);
38 
39  WINPR_UNUSED(instance);
40 
41  std::string client_id = "5177bc73-fd99-4c77-a90c-76844c9b6999";
42  std::string redirect_uri =
43  "ms-appx-web%3a%2f%2fMicrosoft.AAD.BrokerPlugin%2f5177bc73-fd99-4c77-a90c-76844c9b6999";
44 
45  *token = nullptr;
46 
47  auto url =
48  "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=" + client_id +
49  "&response_type=code&scope=" + scope + "&redirect_uri=" + redirect_uri;
50 
51  const std::string title = "FreeRDP WebView - AAD access token";
52  std::string code;
53  auto rc = webview_impl_run(title, url, code);
54  if (!rc || code.empty())
55  return FALSE;
56 
57  auto token_request = "grant_type=authorization_code&code=" + code + "&client_id=" + client_id +
58  "&scope=" + scope + "&redirect_uri=" + redirect_uri +
59  "&req_cnf=" + req_cnf;
60  return client_common_get_access_token(instance, token_request.c_str(), token);
61 }
62 
63 static BOOL sdl_webview_get_avd_access_token(freerdp* instance, char** token)
64 {
65  WINPR_ASSERT(token);
66 
67  std::string client_id = "a85cf173-4192-42f8-81fa-777a763e6e2c";
68  std::string redirect_uri =
69  "ms-appx-web%3a%2f%2fMicrosoft.AAD.BrokerPlugin%2fa85cf173-4192-42f8-81fa-777a763e6e2c";
70  std::string scope = "https%3A%2F%2Fwww.wvd.microsoft.com%2F.default";
71 
72  *token = nullptr;
73 
74  auto url =
75  "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=" + client_id +
76  "&response_type=code&scope=" + scope + "&redirect_uri=" + redirect_uri;
77  const std::string title = "FreeRDP WebView - AVD access token";
78  std::string code;
79  auto rc = webview_impl_run(title, url, code);
80  if (!rc || code.empty())
81  return FALSE;
82 
83  auto token_request = "grant_type=authorization_code&code=" + code + "&client_id=" + client_id +
84  "&scope=" + scope + "&redirect_uri=" + redirect_uri;
85  return client_common_get_access_token(instance, token_request.c_str(), token);
86 }
87 
88 BOOL sdl_webview_get_access_token(freerdp* instance, AccessTokenType tokenType, char** token,
89  size_t count, ...)
90 {
91  WINPR_ASSERT(instance);
92  WINPR_ASSERT(token);
93  switch (tokenType)
94  {
95  case ACCESS_TOKEN_TYPE_AAD:
96  {
97  if (count < 2)
98  {
99  WLog_ERR(TAG,
100  "ACCESS_TOKEN_TYPE_AAD expected 2 additional arguments, but got %" PRIuz
101  ", aborting",
102  count);
103  return FALSE;
104  }
105  else if (count > 2)
106  WLog_WARN(TAG,
107  "ACCESS_TOKEN_TYPE_AAD expected 2 additional arguments, but got %" PRIuz
108  ", ignoring",
109  count);
110  va_list ap = {};
111  va_start(ap, count);
112  const char* scope = va_arg(ap, const char*);
113  const char* req_cnf = va_arg(ap, const char*);
114  const BOOL rc = sdl_webview_get_rdsaad_access_token(instance, scope, req_cnf, token);
115  va_end(ap);
116  return rc;
117  }
118  case ACCESS_TOKEN_TYPE_AVD:
119  if (count != 0)
120  WLog_WARN(TAG,
121  "ACCESS_TOKEN_TYPE_AVD expected 0 additional arguments, but got %" PRIuz
122  ", ignoring",
123  count);
124  return sdl_webview_get_avd_access_token(instance, token);
125  default:
126  WLog_ERR(TAG, "Unexpected value for AccessTokenType [%" PRIuz "], aborting", tokenType);
127  return FALSE;
128  }
129 }