auth-base.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "moc_auth-base.cpp"
  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. main->SetBroadcastFlowEnabled(main->auth->broadcastFlow());
  53. }
  54. } else {
  55. main->SetBroadcastFlowEnabled(false);
  56. }
  57. }
  58. void Auth::Save()
  59. {
  60. OBSBasic *main = OBSBasic::Get();
  61. Auth *auth = main->auth.get();
  62. if (!auth) {
  63. if (config_has_user_value(main->Config(), "Auth", "Type")) {
  64. config_remove_value(main->Config(), "Auth", "Type");
  65. config_save_safe(main->Config(), "tmp", nullptr);
  66. }
  67. return;
  68. }
  69. config_set_string(main->Config(), "Auth", "Type", auth->service());
  70. auth->SaveInternal();
  71. config_save_safe(main->Config(), "tmp", nullptr);
  72. }