MCPcopy Create free account
hub / github.com/WarmUpTill/SceneSwitcher / IsValid

Method IsValid

plugins/twitch/token.cpp:352–409  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

350}
351
352bool TwitchToken::IsValid(bool forceUpdate) const
353{
354 static httplib::Client cli("https://id.twitch.tv");
355 httplib::Headers headers{{"Authorization", "OAuth " + _token}};
356
357 std::scoped_lock<std::mutex> lock(_cacheMutex);
358
359 auto currentTime = std::chrono::system_clock::now();
360 auto diff = currentTime - _lastValidityCheckTime;
361 const bool cacheIsTooOld = diff >= std::chrono::hours(1);
362
363 const auto checkToken = [&]() -> bool {
364 const auto response =
365 cli.Get("/oauth2/validate", httplib::Params{}, headers);
366 _lastValidityCheckTime = std::chrono::system_clock::now();
367 _lastValidityCheckValue = _token;
368
369 if (!response || response->status != 200) {
370 blog(LOG_INFO, "Twitch token %s is not valid!",
371 _name.c_str());
372 _lastValidityCheckResult = false;
373 return false;
374 }
375
376 OBSDataAutoRelease replyData =
377 obs_data_create_from_json(response->body.c_str());
378 const char *id = obs_data_get_string(replyData, "user_id");
379 if (!id) {
380 blog(LOG_INFO,
381 "Twitch token %s does validity check did not report user_id! Assume invalid!",
382 _name.c_str());
383 _lastValidityCheckResult = false;
384 return false;
385 }
386
387 if (_userID && _userID != id) {
388 blog(LOG_INFO,
389 "Twitch token %s does not match expected user (got %s, expected %s)!",
390 _name.c_str(), id, _userID->c_str());
391 _lastValidityCheckResult = false;
392 return false;
393 }
394
395 _lastValidityCheckResult = true;
396 return true;
397 };
398
399 const bool tokenChanged = _lastValidityCheckValue != _token;
400 if (tokenChanged) {
401 return checkToken();
402 }
403
404 if (!forceUpdate && !cacheIsTooOld) {
405 return _lastValidityCheckResult;
406 }
407
408 return checkToken();
409}

Callers 4

getCellLabelsFunction · 0.45
getInvalidTokensFunction · 0.45
TokenIsValidFunction · 0.45
CheckIfTokenValidMethod · 0.45

Calls 3

nowClass · 0.85
GetMethod · 0.80
c_strMethod · 0.45

Tested by

no test coverage detected