Login complete (minus brokenness)
This commit is contained in:
parent
35858ef607
commit
9cdf2b5902
@ -7,6 +7,7 @@
|
||||
|
||||
#include "Config.h"
|
||||
#include "Client.h"
|
||||
#include "MD5.h"
|
||||
#include "Graphics.h"
|
||||
|
||||
#include "interface/Point.h"
|
||||
@ -41,29 +42,60 @@ Client::~Client()
|
||||
|
||||
LoginStatus Client::Login(string username, string password)
|
||||
{
|
||||
lastError = "";
|
||||
std::stringstream urlStream;
|
||||
std::stringstream hashStream;
|
||||
unsigned char cHashData[16];
|
||||
|
||||
//Build hash of username + password
|
||||
struct md5_context md5;
|
||||
md5_init(&md5);
|
||||
md5_update(&md5, (unsigned char *)username.c_str(), username.length());
|
||||
md5_update(&md5, (unsigned char *)"-", 1);
|
||||
md5_update(&md5, (unsigned char *)password.c_str(), password.length());
|
||||
md5_final(cHashData, &md5);
|
||||
//Make sure hash is Hex
|
||||
//char * cHashHex = (char *)malloc(33);
|
||||
for(int i = 0; i < 16; i++)
|
||||
{
|
||||
hashStream << hexChars[cHashData[i]>>4] << hexChars[cHashData[i]&15];
|
||||
//cHashHex[i*2] = hex[cHashData[i]>>4];
|
||||
//cHashHex[i*2+1] = hex[cHashData[i]&15];
|
||||
}
|
||||
//cHashHex[32] = 0;
|
||||
|
||||
char * data;
|
||||
int dataStatus, dataLength;
|
||||
data = http_auth_get("http://" SERVER "/Login.json", (char*)username.c_str(), (char*)password.c_str(), NULL, &dataStatus, &dataLength);
|
||||
char * postNames[] = { "Username", "Hash", NULL };
|
||||
char * postDatas[] = { (char*)username.c_str(), (char*)hashStream.str().c_str() };
|
||||
int postLengths[] = { username.length(), hashStream.str().length() };
|
||||
data = http_multipart_post("http://" SERVER "/Login.json", postNames, postDatas, postLengths, NULL, NULL, NULL, &dataStatus, &dataLength);
|
||||
//data = http_auth_get("http://" SERVER "/Login.json", (char*)username.c_str(), (char*)password.c_str(), NULL, &dataStatus, &dataLength);
|
||||
std::cout << data << std::endl;
|
||||
if(dataStatus == 200 && data)
|
||||
{
|
||||
std::istringstream dataStream(data);
|
||||
json::Object objDocument;
|
||||
json::Reader::Read(objDocument, dataStream);
|
||||
json::Number tempStatus = objDocument["Status"];
|
||||
try
|
||||
{
|
||||
std::istringstream dataStream(data);
|
||||
json::Object objDocument;
|
||||
json::Reader::Read(objDocument, dataStream);
|
||||
json::Number tempStatus = objDocument["Status"];
|
||||
|
||||
free(data);
|
||||
if(tempStatus.Value() == 1)
|
||||
{
|
||||
return LoginOkay;
|
||||
free(data);
|
||||
if(tempStatus.Value() == 1)
|
||||
{
|
||||
return LoginOkay;
|
||||
}
|
||||
else
|
||||
{
|
||||
json::String tempError = objDocument["Error"];
|
||||
lastError = tempError.Value();
|
||||
return LoginError;
|
||||
}
|
||||
}
|
||||
else if(tempStatus.Value() == 0)
|
||||
{
|
||||
return LoginPasswordInvalid;
|
||||
}
|
||||
else
|
||||
catch (json::Exception &e)
|
||||
{
|
||||
lastError = "Server responded with crap";
|
||||
return LoginError;
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
enum LoginStatus
|
||||
{
|
||||
LoginPasswordInvalid, LoginUsernameInvalid, LoginOkay, LoginBanned, LoginError
|
||||
LoginOkay, LoginError
|
||||
};
|
||||
|
||||
class Client: public Singleton<Client>
|
||||
|
@ -679,7 +679,6 @@ char *http_simple_get(char *uri, int *ret, int *len)
|
||||
}
|
||||
return http_async_req_stop(ctx, ret, len);
|
||||
}
|
||||
static char hex[] = "0123456789abcdef";
|
||||
void http_auth_headers(void *ctx, char *user, char *pass, char *session_id)
|
||||
{
|
||||
char *tmp;
|
||||
@ -702,8 +701,8 @@ void http_auth_headers(void *ctx, char *user, char *pass, char *session_id)
|
||||
tmp = (char *)malloc(33);
|
||||
for (i=0; i<16; i++)
|
||||
{
|
||||
tmp[i*2] = hex[hash[i]>>4];
|
||||
tmp[i*2+1] = hex[hash[i]&15];
|
||||
tmp[i*2] = hexChars[hash[i]>>4];
|
||||
tmp[i*2+1] = hexChars[hash[i]&15];
|
||||
}
|
||||
tmp[32] = 0;
|
||||
http_async_add_header(ctx, "X-Auth-Hash", tmp);
|
||||
@ -1032,8 +1031,8 @@ retry:
|
||||
tmp = (char *)malloc(33);
|
||||
for (i=0; i<16; i++)
|
||||
{
|
||||
tmp[i*2] = hex[hash[i]>>4];
|
||||
tmp[i*2+1] = hex[hash[i]&15];
|
||||
tmp[i*2] = hexChars[hash[i]>>4];
|
||||
tmp[i*2+1] = hexChars[hash[i]&15];
|
||||
}
|
||||
tmp[32] = 0;
|
||||
http_async_add_header(ctx, "X-Auth-Hash", tmp);
|
||||
|
@ -20,6 +20,8 @@
|
||||
#ifndef HTTP_H
|
||||
#define HTTP_H
|
||||
|
||||
static char hexChars[] = "0123456789abcdef";
|
||||
|
||||
void http_init(char *proxy);
|
||||
void http_done(void);
|
||||
|
||||
|
@ -24,17 +24,8 @@ void LoginModel::Login(string username, string password)
|
||||
statusText = "Logged in";
|
||||
loginStatus = true;
|
||||
break;
|
||||
case LoginPasswordInvalid:
|
||||
statusText = "Username or Password incorrect";
|
||||
break;
|
||||
case LoginUsernameInvalid:
|
||||
statusText = "Username incorrect";
|
||||
break;
|
||||
case LoginBanned:
|
||||
statusText = "Banned: " + Client::Ref().GetLastError();
|
||||
break;
|
||||
default:
|
||||
statusText = "Error";
|
||||
case LoginError:
|
||||
statusText = "Error: " + Client::Ref().GetLastError();
|
||||
break;
|
||||
}
|
||||
notifyStatusChanged();
|
||||
|
Reference in New Issue
Block a user