| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 2024 Siemens AG |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "evolution-ews-config.h" |
| 7 |
|
| 8 |
#include <glib/gi18n-lib.h> |
| 9 |
|
| 10 |
#include "e-ms-oapxbc-util.h" |
| 11 |
|
| 12 |
#define MICROSOFT_ENTRA_ID_HOST "login.microsoftonline.com" |
| 13 |
|
| 14 |
SoupCookie * |
| 15 |
e_ms_oapxbc_util_get_prt_sso_cookie_sync (EOAuth2Service *service, |
| 16 |
ESource *source, |
| 17 |
const gchar *client_id, |
| 18 |
const gchar *sso_uri, |
| 19 |
const gchar *redirect_uri, |
| 20 |
const gchar *auth_user, |
| 21 |
GCancellable *cancellable, |
| 22 |
GError **error) |
| 23 |
{ |
| 24 |
EMsOapxbc *app; |
| 25 |
JsonObject *accounts = NULL, *account = NULL; |
| 26 |
JsonArray *scopes, *accounts_member; |
| 27 |
SoupCookie *prtsso_cookie = NULL; |
| 28 |
/* The broker workflow [ms-oapxbc] is only supported on Azure Entra ID */ |
| 29 |
const gchar *entra_sso_uri = "https://" MICROSOFT_ENTRA_ID_HOST; |
| 30 |
const gchar *authority = "https://" MICROSOFT_ENTRA_ID_HOST "/common"; |
| 31 |
const gchar *account_user = NULL; |
| 32 |
guint accounts_len = 0, ii = 0; |
| 33 |
GError *local_error = NULL; |
| 34 |
|
| 35 |
/* Check if the SSO URI is pointing to Azure Entra ID */ |
| 36 |
if (!sso_uri || g_ascii_strncasecmp (sso_uri, entra_sso_uri, strlen (entra_sso_uri)) != 0) { |
| 37 |
return NULL; |
| 38 |
} |
| 39 |
|
| 40 |
app = e_ms_oapxbc_new_sync (client_id, authority, cancellable, &local_error); |
| 41 |
if (!app) { |
| 42 |
if (local_error) { |
| 43 |
g_prefix_error (&local_error, "%s", _("Failed to create MS OAPXBC app: ")); |
| 44 |
g_propagate_error (error, local_error); |
| 45 |
} |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
accounts = e_ms_oapxbc_get_accounts_sync (app, cancellable, &local_error); |
| 49 |
if (!accounts) { |
| 50 |
if (local_error) { |
| 51 |
g_prefix_error (&local_error, "%s", _("Failed to get accounts: ")); |
| 52 |
g_propagate_error (error, local_error); |
| 53 |
} |
| 54 |
g_object_unref (app); |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
accounts_member = json_object_get_array_member (accounts, "accounts"); |
| 58 |
if (accounts_member) { |
| 59 |
accounts_len = json_array_get_length (accounts_member); |
| 60 |
} |
| 61 |
if (!accounts_member || accounts_len == 0) { |
| 62 |
json_object_unref (accounts); |
| 63 |
g_object_unref (app); |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
|
| 67 |
/* iterate accounts, find one with corresponding email */ |
| 68 |
for (ii = 0; ii < accounts_len; ii++) { |
| 69 |
account = json_array_get_object_element (accounts_member, ii); |
| 70 |
account_user = json_object_get_string_member (account, "username"); |
| 71 |
if (account_user && g_ascii_strcasecmp (account_user, auth_user) == 0) { |
| 72 |
break; |
| 73 |
} |
| 74 |
account = NULL; |
| 75 |
} |
| 76 |
if (!account) { |
| 77 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
| 78 |
_("No account found for user '%s'"), auth_user); |
| 79 |
json_object_unref (accounts); |
| 80 |
g_object_unref (app); |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
scopes = json_array_new (); |
| 85 |
json_array_add_string_element (scopes, "https://graph.microsoft.com/.default"); |
| 86 |
prtsso_cookie = e_ms_oapxbc_acquire_prt_sso_cookie_sync (app, account, sso_uri, scopes, |
| 87 |
redirect_uri, cancellable, &local_error); |
| 88 |
if (!prtsso_cookie && local_error) { |
| 89 |
g_propagate_error (error, local_error); |
| 90 |
} |
| 91 |
json_array_unref (scopes); |
| 92 |
json_object_unref (accounts); |
| 93 |
g_object_unref (app); |
| 94 |
|
| 95 |
return prtsso_cookie; |
| 96 |
} |