Auth.cpp 1.7 KB

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