From 048749b5868352065ca8d26775797f2921fe855c Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Sat, 22 Feb 2020 18:12:55 +0100
Subject: [PATCH] Add Fallback auth and fix parsing of unauthorized, if no
 error is returned

---
 include/mtx/user_interactive.hpp | 5 ++++-
 lib/structs/errors.cpp           | 2 +-
 lib/structs/user_interactive.cpp | 1 +
 tests/requests.cpp               | 4 ++++
 4 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/include/mtx/user_interactive.hpp b/include/mtx/user_interactive.hpp
index 9c113222a..0d52f4cc7 100644
--- a/include/mtx/user_interactive.hpp
+++ b/include/mtx/user_interactive.hpp
@@ -142,6 +142,8 @@ struct Terms
 {};
 struct Dummy
 {};
+struct Fallback
+{};
 }
 struct Auth
 {
@@ -156,7 +158,8 @@ struct Auth
                      auth::MSISDN,
                      auth::OAuth2,
                      auth::Terms,
-                     auth::Dummy>
+                     auth::Dummy,
+                     auth::Fallback>
           content;
 };
 void
diff --git a/lib/structs/errors.cpp b/lib/structs/errors.cpp
index 42940151a..5d9fa2c09 100644
--- a/lib/structs/errors.cpp
+++ b/lib/structs/errors.cpp
@@ -84,7 +84,7 @@ from_string(const std::string &code)
 void
 from_json(const nlohmann::json &obj, Error &error)
 {
-        error.errcode = from_string(obj.at("errcode").get<std::string>());
+        error.errcode = from_string(obj.value("errcode", ""));
         error.error   = obj.value("error", "");
 
         if (obj.contains("session"))
diff --git a/lib/structs/user_interactive.cpp b/lib/structs/user_interactive.cpp
index c63933c95..17df7770c 100644
--- a/lib/structs/user_interactive.cpp
+++ b/lib/structs/user_interactive.cpp
@@ -118,6 +118,7 @@ to_json(nlohmann::json &obj, const Auth &auth)
                      [&obj](const auth::OAuth2 &) { obj["type"] = auth_types::oauth2; },
                      [&obj](const auth::Terms &) { obj["type"] = auth_types::terms; },
                      [&obj](const auth::Dummy &) { obj["type"] = auth_types::dummy; },
+                     [&obj](const auth::Fallback &) {},
                    },
                    auth.content);
 }
diff --git a/tests/requests.cpp b/tests/requests.cpp
index 1b52c68da..ad4b181e6 100644
--- a/tests/requests.cpp
+++ b/tests/requests.cpp
@@ -223,6 +223,10 @@ TEST(Requests, UserInteractiveAuth)
   "type": "m.login.dummy",
   "session": "<session ID>"
 })"_json);
+        a.content = auth::Fallback{};
+        EXPECT_EQ(nlohmann::json(a), R"({
+  "session": "<session ID>"
+})"_json);
 
         a.content = auth::EmailIdentity{{
           {"<identity server session id>",
-- 
GitLab