FreeRDP
Loading...
Searching...
No Matches
sso_mib_tokens.c
1/*
2 * SPDX-License-Identifier: Apache-2.0
3 * SPDX-FileCopyrightText: Copyright 2025 Siemens
4 */
5
6#include <sso-mib/sso-mib.h>
7#include <freerdp/crypto/crypto.h>
8#include <winpr/json.h>
9
10#include "sso_mib_tokens.h"
11
12#include <freerdp/log.h>
13#define TAG CLIENT_TAG("common.sso")
14
15enum sso_mib_state
16{
17 SSO_MIB_STATE_INIT = 0,
18 SSO_MIB_STATE_FAILED = 1,
19 SSO_MIB_STATE_SUCCESS = 2,
20};
21
22struct MIBClientWrapper
23{
24 MIBClientApp* app;
25 enum sso_mib_state state;
26 pGetCommonAccessToken GetCommonAccessToken;
27};
28
29static BOOL sso_mib_get_avd_access_token(rdpClientContext* client_context, char** token)
30{
31 WINPR_ASSERT(client_context);
32 WINPR_ASSERT(client_context->mibClientWrapper);
33 WINPR_ASSERT(client_context->mibClientWrapper->app);
34 WINPR_ASSERT(token);
35
36 MIBAccount* account = NULL;
37 GSList* scopes = NULL;
38
39 BOOL rc = FALSE;
40 *token = NULL;
41
42 account = mib_client_app_get_account_by_upn(client_context->mibClientWrapper->app, NULL);
43 if (!account)
44 {
45 goto cleanup;
46 }
47
48 scopes = g_slist_append(scopes, g_strdup("https://www.wvd.microsoft.com/.default"));
49
50 MIBPrt* prt = mib_client_app_acquire_token_silent(client_context->mibClientWrapper->app,
51 account, scopes, NULL, NULL, NULL);
52 if (prt)
53 {
54 const char* access_token = mib_prt_get_access_token(prt);
55 if (access_token)
56 {
57 *token = strdup(access_token);
58 }
59 g_object_unref(prt);
60 }
61
62 rc = TRUE && *token != NULL;
63cleanup:
64 if (account)
65 g_object_unref(account);
66 g_slist_free_full(scopes, g_free);
67 return rc;
68}
69
70static BOOL sso_mib_get_rdsaad_access_token(rdpClientContext* client_context, const char* scope,
71 const char* req_cnf, char** token)
72{
73 WINPR_ASSERT(client_context);
74 WINPR_ASSERT(client_context->mibClientWrapper);
75 WINPR_ASSERT(client_context->mibClientWrapper->app);
76 WINPR_ASSERT(scope);
77 WINPR_ASSERT(token);
78 WINPR_ASSERT(req_cnf);
79
80 GSList* scopes = NULL;
81 WINPR_JSON* json = NULL;
82 MIBPopParams* params = NULL;
83
84 BOOL rc = FALSE;
85 *token = NULL;
86 BYTE* req_cnf_dec = NULL;
87 size_t req_cnf_dec_len = 0;
88
89 scopes = g_slist_append(scopes, g_strdup(scope));
90
91 // Parse the "kid" element from req_cnf
92 crypto_base64_decode(req_cnf, strlen(req_cnf) + 1, &req_cnf_dec, &req_cnf_dec_len);
93 if (!req_cnf_dec)
94 {
95 goto cleanup;
96 }
97
98 json = WINPR_JSON_Parse((const char*)req_cnf_dec);
99 if (!json)
100 {
101 goto cleanup;
102 }
103 WINPR_JSON* prop = WINPR_JSON_GetObjectItem(json, "kid");
104 if (!prop)
105 {
106 goto cleanup;
107 }
108 const char* kid = WINPR_JSON_GetStringValue(prop);
109 if (!kid)
110 {
111 goto cleanup;
112 }
113
114 params = mib_pop_params_new(MIB_AUTH_SCHEME_POP, MIB_REQUEST_METHOD_GET, "");
115 mib_pop_params_set_kid(params, kid);
116 MIBPrt* prt = mib_client_app_acquire_token_interactive(
117 client_context->mibClientWrapper->app, scopes, MIB_PROMPT_NONE, NULL, NULL, NULL, params);
118 if (prt)
119 {
120 *token = strdup(mib_prt_get_access_token(prt));
121 rc = TRUE;
122 g_object_unref(prt);
123 }
124
125cleanup:
126 if (params)
127 g_object_unref(params);
128 WINPR_JSON_Delete(json);
129 free(req_cnf_dec);
130 g_slist_free_full(scopes, g_free);
131 return rc;
132}
133
134static BOOL sso_mib_get_access_token(rdpContext* context, AccessTokenType tokenType, char** token,
135 size_t count, ...)
136{
137 BOOL rc = FALSE;
138 rdpClientContext* client_context = (rdpClientContext*)context;
139 WINPR_ASSERT(client_context);
140 WINPR_ASSERT(client_context->mibClientWrapper);
141
142 if (!client_context->mibClientWrapper->app)
143 {
144 const char* client_id =
145 freerdp_settings_get_string(context->settings, FreeRDP_GatewayAvdClientID);
146 client_context->mibClientWrapper->app =
147 mib_public_client_app_new(client_id, MIB_AUTHORITY_COMMON, NULL, NULL);
148 }
149
150 if (!client_context->mibClientWrapper->app)
151 return FALSE;
152
153 const char* scope = NULL;
154 const char* req_cnf = NULL;
155
156 va_list ap;
157 va_start(ap, count);
158
159 if (tokenType == ACCESS_TOKEN_TYPE_AAD)
160 {
161 scope = va_arg(ap, const char*);
162 req_cnf = va_arg(ap, const char*);
163 }
164
165 if ((client_context->mibClientWrapper->state == SSO_MIB_STATE_INIT) ||
166 (client_context->mibClientWrapper->state == SSO_MIB_STATE_SUCCESS))
167 {
168 switch (tokenType)
169 {
170 case ACCESS_TOKEN_TYPE_AVD:
171 {
172 rc = sso_mib_get_avd_access_token(client_context, token);
173 if (rc)
174 client_context->mibClientWrapper->state = SSO_MIB_STATE_SUCCESS;
175 else
176 {
177 WLog_WARN(TAG, "Getting AVD token from identity broker failed, falling back to "
178 "browser-based authentication.");
179 client_context->mibClientWrapper->state = SSO_MIB_STATE_FAILED;
180 }
181 }
182 break;
183 case ACCESS_TOKEN_TYPE_AAD:
184 {
185 // Setup scope without URL encoding for sso-mib
186 char* scope_copy = winpr_str_url_decode(scope, strlen(scope));
187 if (!scope_copy)
188 WLog_ERR(TAG, "Failed to decode scope");
189 else
190 {
191 rc =
192 sso_mib_get_rdsaad_access_token(client_context, scope_copy, req_cnf, token);
193 free(scope_copy);
194 if (rc)
195 client_context->mibClientWrapper->state = SSO_MIB_STATE_SUCCESS;
196 else
197 {
198 WLog_WARN(TAG,
199 "Getting RDS token from identity broker failed, falling back to "
200 "browser-based authentication.");
201 client_context->mibClientWrapper->state = SSO_MIB_STATE_FAILED;
202 }
203 }
204 }
205 break;
206 default:
207 break;
208 }
209 }
210 if (!rc && client_context->mibClientWrapper->GetCommonAccessToken)
211 rc = client_context->mibClientWrapper->GetCommonAccessToken(context, tokenType, token,
212 count, scope, req_cnf);
213 va_end(ap);
214
215 return rc;
216}
217
218MIBClientWrapper* sso_mib_new(rdpContext* context)
219{
220
221 MIBClientWrapper* mibClientWrapper = (MIBClientWrapper*)calloc(1, sizeof(MIBClientWrapper));
222 if (!mibClientWrapper)
223 return NULL;
224
225 mibClientWrapper->GetCommonAccessToken = freerdp_get_common_access_token(context);
226 if (!freerdp_set_common_access_token(context, sso_mib_get_access_token))
227 {
228 sso_mib_free(mibClientWrapper);
229 return NULL;
230 }
231 mibClientWrapper->state = SSO_MIB_STATE_INIT;
232 return mibClientWrapper;
233}
234
235void sso_mib_free(MIBClientWrapper* sso)
236{
237 if (!sso)
238 return;
239
240 if (sso->app)
241 g_object_unref(sso->app);
242
243 free(sso);
244}
WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
Definition json.c:184
WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition json.c:234
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition json.c:144
WINPR_API WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
Definition json.c:113
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.