| 890 | } |
| 891 | |
| 892 | void TokenGrabberThread::run() |
| 893 | { |
| 894 | // Reset |
| 895 | _server.stop(); |
| 896 | _server.~Server(); |
| 897 | new (&_server) httplib::Server(); |
| 898 | if (_serverThread.joinable()) { |
| 899 | _serverThread.join(); |
| 900 | } |
| 901 | _stopWaiting = false; |
| 902 | |
| 903 | // Generate URI to request token |
| 904 | auto state = generateStateString(); |
| 905 | QString getTokenURI = |
| 906 | ("https://id.twitch.tv/oauth2/authorize" |
| 907 | "?response_type=token" |
| 908 | "&client_id=" + |
| 909 | QString(GetClientID()) + |
| 910 | "&redirect_uri=http://localhost:%1" |
| 911 | "/auth" |
| 912 | "&scope=" + |
| 913 | _scope + "&state=" + QString::fromStdString(state)) |
| 914 | .arg(QString::number(tokenGrabberPort)); |
| 915 | |
| 916 | // Setup server receiving token string |
| 917 | auto html = getHtml(getTokenURI); |
| 918 | _server.Get("/auth", [html, state](const httplib::Request &req, |
| 919 | httplib::Response &res) { |
| 920 | // Check for errors |
| 921 | if (req.has_param("error")) { |
| 922 | auto recvState = req.get_param_value("state"); |
| 923 | if (recvState != state) { |
| 924 | blog(LOG_WARNING, |
| 925 | "state string does not match in error handling?! " |
| 926 | "Got \"%s\" - expected \"%s\"\n" |
| 927 | "ignoring error ...", |
| 928 | recvState.c_str(), state.c_str()); |
| 929 | return; |
| 930 | } |
| 931 | auto errorStr = |
| 932 | req.get_param_value("error_description"); |
| 933 | res.set_content(getAuthErrorString(errorStr.c_str()), |
| 934 | "text/plain"); |
| 935 | return; |
| 936 | } |
| 937 | |
| 938 | // Parse fragments and redirect to /token with |
| 939 | // corresponding parameters. |
| 940 | res.set_content(html, "text/html"); |
| 941 | }); |
| 942 | _server.Get("/token", [&](const httplib::Request &req, |
| 943 | httplib::Response &res) { |
| 944 | // Check if valid request and grab the token string |
| 945 | std::lock_guard<std::mutex> lk(_mutex); |
| 946 | auto recvState = req.get_param_value("state"); |
| 947 | if (recvState != state) { |
| 948 | blog(LOG_WARNING, |
| 949 | "state string does not match! " |
no test coverage detected