MantisBase v0.4.9
Loading...
Searching...
No Matches
auth_methods.h
Go to the documentation of this file.
1
5
6#ifndef MANTISBASE_AUTH_METHODS_H
7#define MANTISBASE_AUTH_METHODS_H
8
9#include <nlohmann/json.hpp>
11#include <mantisbase/export.h>
12
13namespace mb {
22 bool password = true;
23 bool otp = false;
24 bool oauth = false;
26
27 [[nodiscard]] nlohmann::json toJSON() const {
28 return {
29 {"password", password},
30 {"otp", otp},
31 {"oauth", oauth},
32 {"requireEmailVerification", requireEmailVerification}
33 };
34 }
35
37 [[nodiscard]] static MbAuthMethods fromJSON(const nlohmann::json &auth) {
38 MbAuthMethods methods;
39 if (!auth.is_object())
40 return methods;
41 auto take = [&](const char *key, bool &dest) {
42 if (auth.contains(key) && auth[key].is_boolean())
43 dest = auth[key].get<bool>();
44 };
45 take("password", methods.password);
46 take("otp", methods.otp);
47 take("oauth", methods.oauth);
48 take("requireEmailVerification", methods.requireEmailVerification);
49 return methods;
50 }
51
57 void mergeStrict(const nlohmann::json &auth) {
58 if (!auth.is_object())
59 throw MbException(400, "Expected `auth` to be an object.");
60 for (const auto &[key, value]: auth.items()) {
61 if (key != "password" && key != "otp" && key != "oauth" && key != "requireEmailVerification")
62 throw MbException(400, "Unknown auth setting `" + key + "`.");
63 if (!value.is_boolean())
64 throw MbException(400, "Expected `auth." + key + "` to be a boolean.");
65 if (key == "password")
66 password = value.get<bool>();
67 else if (key == "otp")
68 otp = value.get<bool>();
69 else if (key == "oauth")
70 oauth = value.get<bool>();
71 else
72 requireEmailVerification = value.get<bool>();
73 }
74 }
75 };
76} // mb
77
78#endif // MANTISBASE_AUTH_METHODS_H
Application exception carrying an HTTP status code and messages.
Definition exceptions.h:23
Custom exception class for MantisBase errors.
DLL/shared-library symbol visibility macros for MantisBase.
#define MANTISBASE_API
Definition export.h:31
Definition api_keys.h:35
Definition auth_methods.h:21
nlohmann::json toJSON() const
Definition auth_methods.h:27
bool otp
Definition auth_methods.h:23
bool password
Definition auth_methods.h:22
static MbAuthMethods fromJSON(const nlohmann::json &auth)
Definition auth_methods.h:37
bool oauth
Definition auth_methods.h:24
void mergeStrict(const nlohmann::json &auth)
Definition auth_methods.h:57
bool requireEmailVerification
Definition auth_methods.h:25