6#include <sso-mib/sso-mib.h>
7#include <freerdp/crypto/crypto.h>
10#include <sso-mib/sso-mib.h>
11#include "sso_mib_tokens.h"
13#include <freerdp/log.h>
14#define TAG CLIENT_TAG("common.sso")
18 SSO_MIB_STATE_INIT = 0,
19 SSO_MIB_STATE_FAILED = 1,
20 SSO_MIB_STATE_SUCCESS = 2,
23struct MIBClientWrapper
26 enum sso_mib_state state;
27 pGetCommonAccessToken GetCommonAccessToken;
30static BOOL sso_mib_get_avd_access_token(rdpClientContext* client_context,
char** token)
32 WINPR_ASSERT(client_context);
33 WINPR_ASSERT(client_context->mibClientWrapper);
34 WINPR_ASSERT(client_context->mibClientWrapper->app);
37 MIBAccount* account = NULL;
38 GSList* scopes = NULL;
43 account = mib_client_app_get_account_by_upn(client_context->mibClientWrapper->app, NULL);
49 scopes = g_slist_append(scopes, g_strdup(
"https://www.wvd.microsoft.com/.default"));
51 MIBPrt* prt = mib_client_app_acquire_token_silent(client_context->mibClientWrapper->app,
52 account, scopes, NULL, NULL, NULL);
55 const char* access_token = mib_prt_get_access_token(prt);
58 *token = strdup(access_token);
63 rc = TRUE && *token != NULL;
66 g_object_unref(account);
67 g_slist_free_full(scopes, g_free);
71static BOOL sso_mib_get_rdsaad_access_token(rdpClientContext* client_context,
const char* scope,
72 const char* req_cnf,
char** token)
74 WINPR_ASSERT(client_context);
75 WINPR_ASSERT(client_context->mibClientWrapper);
76 WINPR_ASSERT(client_context->mibClientWrapper->app);
79 WINPR_ASSERT(req_cnf);
81 GSList* scopes = NULL;
82 WINPR_JSON* json = NULL;
83 MIBPopParams* params = NULL;
87 BYTE* req_cnf_dec = NULL;
88 size_t req_cnf_dec_len = 0;
90 scopes = g_slist_append(scopes, g_strdup(scope));
93 crypto_base64_decode(req_cnf, strlen(req_cnf) + 1, &req_cnf_dec, &req_cnf_dec_len);
115 params = mib_pop_params_new(MIB_AUTH_SCHEME_POP, MIB_REQUEST_METHOD_GET,
"");
116 mib_pop_params_set_kid(params, kid);
117 MIBPrt* prt = mib_client_app_acquire_token_interactive(
118 client_context->mibClientWrapper->app, scopes, MIB_PROMPT_NONE, NULL, NULL, NULL, params);
121 *token = strdup(mib_prt_get_access_token(prt));
128 g_object_unref(params);
131 g_slist_free_full(scopes, g_free);
135static BOOL sso_mib_get_access_token(rdpContext* context, AccessTokenType tokenType,
char** token,
139 rdpClientContext* client_context = (rdpClientContext*)context;
140 WINPR_ASSERT(client_context);
141 WINPR_ASSERT(client_context->mibClientWrapper);
143 if (!client_context->mibClientWrapper->app)
145 const char* client_id =
147 client_context->mibClientWrapper->app =
148 mib_public_client_app_new(client_id, MIB_AUTHORITY_COMMON, NULL, NULL);
151 if (!client_context->mibClientWrapper->app)
152 return ERROR_INTERNAL_ERROR;
154 const char* scope = NULL;
155 const char* req_cnf = NULL;
160 if (tokenType == ACCESS_TOKEN_TYPE_AAD)
162 scope = va_arg(ap,
const char*);
163 req_cnf = va_arg(ap,
const char*);
166 if ((client_context->mibClientWrapper->state == SSO_MIB_STATE_INIT) ||
167 (client_context->mibClientWrapper->state == SSO_MIB_STATE_SUCCESS))
171 case ACCESS_TOKEN_TYPE_AVD:
173 rc = sso_mib_get_avd_access_token(client_context, token);
175 client_context->mibClientWrapper->state = SSO_MIB_STATE_SUCCESS;
178 WLog_WARN(TAG,
"Getting AVD token from identity broker failed, falling back to "
179 "browser-based authentication.");
180 client_context->mibClientWrapper->state = SSO_MIB_STATE_FAILED;
184 case ACCESS_TOKEN_TYPE_AAD:
187 char* scope_copy = winpr_str_url_decode(scope, strlen(scope));
189 WLog_ERR(TAG,
"Failed to decode scope");
193 sso_mib_get_rdsaad_access_token(client_context, scope_copy, req_cnf, token);
196 client_context->mibClientWrapper->state = SSO_MIB_STATE_SUCCESS;
200 "Getting RDS token from identity broker failed, falling back to "
201 "browser-based authentication.");
202 client_context->mibClientWrapper->state = SSO_MIB_STATE_FAILED;
211 if (!rc && client_context->mibClientWrapper->GetCommonAccessToken)
212 rc = client_context->mibClientWrapper->GetCommonAccessToken(context, tokenType, token,
213 count, scope, req_cnf);
219MIBClientWrapper* sso_mib_new(rdpContext* context)
222 MIBClientWrapper* mibClientWrapper = (MIBClientWrapper*)calloc(1,
sizeof(MIBClientWrapper));
223 if (!mibClientWrapper)
226 mibClientWrapper->GetCommonAccessToken = freerdp_get_common_access_token(context);
227 if (!freerdp_set_common_access_token(context, sso_mib_get_access_token))
229 sso_mib_free(mibClientWrapper);
232 mibClientWrapper->state = SSO_MIB_STATE_INIT;
233 return mibClientWrapper;
236void sso_mib_free(MIBClientWrapper* sso)
242 g_object_unref(sso->app);
WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
WINPR_API WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.