#include "ehs/io/socket/rest/Twitch.h" #include "ehs/io/socket/DNS_LNX.h" #include "ehs/system/System.h" #include "ehs/URI.h" namespace ehs { Twitch::~Twitch() { client.Release(); } Twitch::Twitch() : forceVerify(false) { } Twitch::Twitch(const Str_8& clientId, const Str_8& secret, const Str_8& redURI, const Array& scopes, const bool forceVerify) : client(IP::V4), clientId(clientId), secret(secret), redURI(redURI), scopes(scopes), forceVerify(forceVerify) { } bool Twitch::Authorize() { Str_8 scopesFinal; for (UInt_64 i = 0; i < scopes.Size(); ++i) { scopesFinal += scopes[i]; if (i < scopes.Size() - 1) scopesFinal += "%20"; } Str_8 rURI = URI::Encode(redURI); Str_8 uri = "https://id.twitch.tv/oauth2/authorize?client_id=" + clientId + "&redirect_uri=" + rURI + "&response_type=code&force_verify=" + (forceVerify ? "true" : "false") + "&scope=" + scopesFinal; TCP server(IP::V4); server.Bind("", 65535); server.Listen(); System::OpenURI(uri); TCP* cbClient = server.Accept(); Request cbReq = cbClient->RecvReq(); if (cbReq.GetResource() != "/callback") { Response resp(423, "Event Horizon"); resp.SetContentType(ContentType::TEXT_HTML); resp.SetBody( "LWE ResponseHostile Information Received"); cbClient->SendRes(resp); cbClient->Release(); return false; } Response resp(200, "Event Horizon"); resp.SetContentType(ContentType::TEXT_HTML); resp.SetBody( "LWE ResponseAuthentication Successful"); cbClient->SendRes(resp); cbClient->Release(); server.Release(); client.Initialize(); client.Connect("id.twitch.tv", SSL::HTTPS_Port); Request authReq(Verb::POST, "/oauth2/token"); authReq.SetContentType(ContentType::APP_FORMURLENCODED); authReq.AddToBody("client_id", clientId); authReq.AddToBody("client_secret", secret); authReq.AddToBody("code", cbReq.GetQuery("code")); authReq.AddToBody("grant_type", "authorization_code"); authReq.AddToBody("redirect_uri", redURI); client.SendReq(authReq); Response authRes = client.RecvRes(); if (authRes.GetCode() == 400) { client.Release(); EHS_LOG_INT(LogType::ERR, 0, "Could not authorize with Twitch because the client id was invalid."); return false; } else if (authRes.GetCode() == 403) { client.Release(); EHS_LOG_INT(LogType::ERR, 1, "Could not authorize with Twitch because the secret was invalid."); return false; } else if (authRes.GetCode() != 200) { client.Release(); EHS_LOG_INT(LogType::ERR, 2, "Could not authorize with Twitch."); return false; } Json authResJson = authRes.GetJson(); JsonObj* value = (JsonObj*) authResJson.GetValue(); if (!value) return false; JsonVar* var = value->GetVar("access_token"); if (!var) return false; token = ((JsonStr*) var->GetValue())->value; return true; } Str_8 Twitch::GetClientId() const { return clientId; } Str_8 Twitch::GetSecret() const { return secret; } Str_8 Twitch::GetRedURI() const { return redURI; } bool Twitch::IsVerificationForced() const { return forceVerify; } Str_8 Twitch::GetToken() const { return token; } }