1
0

auth-base.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "auth-base.hpp"
  2. #include "window-basic-main.hpp"
  3. #include <vector>
  4. #include <map>
  5. struct AuthInfo {
  6. Auth::Def def;
  7. Auth::create_cb create;
  8. };
  9. static std::vector<AuthInfo> authDefs;
  10. void Auth::RegisterAuth(const Def &d, create_cb create)
  11. {
  12. AuthInfo info = {d, create};
  13. authDefs.push_back(info);
  14. }
  15. std::shared_ptr<Auth> Auth::Create(const std::string &service)
  16. {
  17. for (auto &a : authDefs) {
  18. if (service.find(a.def.service) != std::string::npos) {
  19. return a.create();
  20. }
  21. }
  22. return nullptr;
  23. }
  24. Auth::Type Auth::AuthType(const std::string &service)
  25. {
  26. for (auto &a : authDefs) {
  27. if (service.find(a.def.service) != std::string::npos) {
  28. return a.def.type;
  29. }
  30. }
  31. return Type::None;
  32. }
  33. void Auth::Load()
  34. {
  35. OBSBasic *main = OBSBasic::Get();
  36. const char *typeStr = config_get_string(main->Config(), "Auth", "Type");
  37. if (!typeStr)
  38. typeStr = "";
  39. main->auth = Create(typeStr);
  40. if (main->auth) {
  41. if (main->auth->LoadInternal()) {
  42. main->auth->LoadUI();
  43. }
  44. }
  45. }
  46. void Auth::Save()
  47. {
  48. OBSBasic *main = OBSBasic::Get();
  49. Auth *auth = main->auth.get();
  50. if (!auth) {
  51. if (config_has_user_value(main->Config(), "Auth", "Type")) {
  52. config_remove_value(main->Config(), "Auth", "Type");
  53. config_save_safe(main->Config(), "tmp", nullptr);
  54. }
  55. return;
  56. }
  57. config_set_string(main->Config(), "Auth", "Type", auth->service());
  58. auth->SaveInternal();
  59. config_save_safe(main->Config(), "tmp", nullptr);
  60. }