auth-base.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. bool Auth::External(const std::string &service)
  34. {
  35. for (auto &a : authDefs) {
  36. if (service.find(a.def.service) != std::string::npos) {
  37. return a.def.externalOAuth;
  38. }
  39. }
  40. return false;
  41. }
  42. void Auth::Load()
  43. {
  44. OBSBasic *main = OBSBasic::Get();
  45. const char *typeStr = config_get_string(main->Config(), "Auth", "Type");
  46. if (!typeStr)
  47. typeStr = "";
  48. main->auth = Create(typeStr);
  49. if (main->auth) {
  50. if (main->auth->LoadInternal()) {
  51. main->auth->LoadUI();
  52. }
  53. }
  54. }
  55. void Auth::Save()
  56. {
  57. OBSBasic *main = OBSBasic::Get();
  58. Auth *auth = main->auth.get();
  59. if (!auth) {
  60. if (config_has_user_value(main->Config(), "Auth", "Type")) {
  61. config_remove_value(main->Config(), "Auth", "Type");
  62. config_save_safe(main->Config(), "tmp", nullptr);
  63. }
  64. return;
  65. }
  66. config_set_string(main->Config(), "Auth", "Type", auth->service());
  67. auth->SaveInternal();
  68. config_save_safe(main->Config(), "tmp", nullptr);
  69. }