Adjusted workflow.
This commit is contained in:
150
src/io/socket/rest/Twitch.cpp
Normal file
150
src/io/socket/rest/Twitch.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
#include "ehs/io/socket/rest/Twitch.h"
|
||||
#include "ehs/io/socket/DNS.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<Str_8>& scopes, const bool forceVerify)
|
||||
: client(AddrType::IPV4), 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(AddrType::IPV4);
|
||||
server.Bind(DNS::Resolve("localhost"), 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(
|
||||
"<!DOCTYPE html><html><head><title>LWE Response</title><link rel=\"icon\" type=\"image/png\" href=\"https://cdn3.iconfinder.com/data/icons/contour-animals-2/512/wolf-512.png\" /></head><body>Hostile Information Received</body></html>");
|
||||
|
||||
cbClient->SendRes(resp);
|
||||
cbClient->Release();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Response resp(200, "Event Horizon");
|
||||
resp.SetContentType(ContentType::TEXT_HTML);
|
||||
resp.SetBody(
|
||||
"<!DOCTYPE html><html><head><title>LWE Response</title><link rel=\"icon\" type=\"image/png\" href=\"https://cdn3.iconfinder.com/data/icons/contour-animals-2/512/wolf-512.png\" /></head><body>Authentication Successful</body></html>");
|
||||
|
||||
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("Error", 0, "Could not authorize with Twitch because the client id was invalid.");
|
||||
|
||||
return false;
|
||||
} else if (authRes.GetCode() == 403)
|
||||
{
|
||||
client.Release();
|
||||
|
||||
EHS_LOG_INT("Error", 1, "Could not authorize with Twitch because the secret was invalid.");
|
||||
|
||||
return false;
|
||||
} else if (authRes.GetCode() != 200)
|
||||
{
|
||||
client.Release();
|
||||
|
||||
EHS_LOG_INT("Error", 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user