1
0

auth-base.cpp 1.7 KB

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