]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
auth: mech-oauth2 - Look for openid configuration URL if missing
authorAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 15 May 2023 08:51:19 +0000 (11:51 +0300)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Tue, 29 Aug 2023 07:08:45 +0000 (07:08 +0000)
src/auth/mech-oauth2.c
src/auth/passdb-oauth2.c
src/auth/passdb.h

index 1b145674b3b2568a154f05978735e07d376cf66c..c51d16f73f81a8ee2a9d65b9e5c73d3c0491ff02 100644 (file)
@@ -14,6 +14,29 @@ struct oauth2_auth_request {
        bool failed;
 };
 
+static bool oauth2_find_oidc_url(struct auth_request *req, const char **url_r)
+{
+       struct auth_passdb *db = req->passdb;
+       if (req->openid_config_url != NULL) {
+               *url_r = req->openid_config_url;
+               return TRUE;
+       }
+
+       /* keep looking until you get a value */
+       for (; db != NULL; db = db->next) {
+               if (strcmp(db->passdb->iface.name, "oauth2") == 0) {
+                       const char *url =
+                               passdb_oauth2_get_oidc_url(req->passdb->passdb);
+                       if (url == NULL || *url == '\0')
+                               continue;
+                       *url_r = url;
+                       return TRUE;
+               }
+       }
+
+       return FALSE;
+}
+
 /* RFC5801 based unescaping */
 static bool oauth2_unescape_username(const char *in, const char **username_r)
 {
@@ -44,6 +67,7 @@ static void oauth2_verify_callback(enum passdb_result result,
 {
        struct oauth2_auth_request *oauth2_req =
                        (struct oauth2_auth_request*)request;
+       const char *oidc_url;
 
        i_assert(result == PASSDB_RESULT_OK || error_fields != NULL);
        switch (result) {
@@ -76,11 +100,11 @@ static void oauth2_verify_callback(enum passdb_result result,
                   This **must** be removed from here and db-oauth2 once the
                   validation result et al is handled here.
                */
-               if (request->openid_config_url != NULL) {
+               if (oauth2_find_oidc_url(request, &oidc_url)) {
                        if (str_len(error) > 0)
                                str_append_c(error, ',');
                        str_printfa(error, "\"openid-configuration\":\"");
-                       json_append_escaped(error, request->openid_config_url);
+                       json_append_escaped(error, oidc_url);
                        str_append_c(error, '"');
                }
                str_append_c(error, '}');
index 000d1ec6e8cbb97879dc521776be1e10b209c426..e52a73f573ef7e35a13498184ac44a9588317a22 100644 (file)
@@ -67,6 +67,16 @@ static void oauth2_deinit(struct passdb_module *passdb)
        db_oauth2_unref(&module->db);
 }
 
+/* FIXME: Remove when oauth2 mech is fixed */
+const char *passdb_oauth2_get_oidc_url(struct passdb_module *passdb)
+{
+       struct oauth2_passdb_module *module =
+               container_of(passdb, struct oauth2_passdb_module, module);
+       if (module->db != NULL)
+               return db_oauth2_get_openid_configuration_url(module->db);
+       return NULL;
+}
+
 struct passdb_module_interface passdb_oauth2 = {
        "oauth2",
 
index b405aa7e3f45da63cf3510c24c84fefaa5af71fc..f9b33ea81ffb9acc28ce643354006a7b681e5727 100644 (file)
@@ -116,6 +116,8 @@ void passdbs_generate_md5(unsigned char md5[STATIC_ARRAY MD5_RESULTLEN]);
 void passdbs_init(void);
 void passdbs_deinit(void);
 
+const char *passdb_oauth2_get_oidc_url(struct passdb_module *passdb);
+
 #include "auth-request.h"
 
 #endif