Login complete (minus brokenness)

This commit is contained in:
Simon Robertshaw
2012-01-25 00:57:39 +00:00
parent 35858ef607
commit 9cdf2b5902
5 changed files with 55 additions and 31 deletions

View File

@@ -7,6 +7,7 @@
#include "Config.h" #include "Config.h"
#include "Client.h" #include "Client.h"
#include "MD5.h"
#include "Graphics.h" #include "Graphics.h"
#include "interface/Point.h" #include "interface/Point.h"
@@ -41,29 +42,60 @@ Client::~Client()
LoginStatus Client::Login(string username, string password) LoginStatus Client::Login(string username, string password)
{ {
lastError = "";
std::stringstream urlStream; 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; char * data;
int dataStatus, dataLength; 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; std::cout << data << std::endl;
if(dataStatus == 200 && data) if(dataStatus == 200 && data)
{ {
std::istringstream dataStream(data); try
json::Object objDocument; {
json::Reader::Read(objDocument, dataStream); std::istringstream dataStream(data);
json::Number tempStatus = objDocument["Status"]; json::Object objDocument;
json::Reader::Read(objDocument, dataStream);
json::Number tempStatus = objDocument["Status"];
free(data); free(data);
if(tempStatus.Value() == 1) if(tempStatus.Value() == 1)
{ {
return LoginOkay; return LoginOkay;
}
else
{
json::String tempError = objDocument["Error"];
lastError = tempError.Value();
return LoginError;
}
} }
else if(tempStatus.Value() == 0) catch (json::Exception &e)
{
return LoginPasswordInvalid;
}
else
{ {
lastError = "Server responded with crap";
return LoginError; return LoginError;
} }
} }

View File

@@ -12,7 +12,7 @@
enum LoginStatus enum LoginStatus
{ {
LoginPasswordInvalid, LoginUsernameInvalid, LoginOkay, LoginBanned, LoginError LoginOkay, LoginError
}; };
class Client: public Singleton<Client> class Client: public Singleton<Client>

View File

@@ -679,7 +679,6 @@ char *http_simple_get(char *uri, int *ret, int *len)
} }
return http_async_req_stop(ctx, ret, 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) void http_auth_headers(void *ctx, char *user, char *pass, char *session_id)
{ {
char *tmp; char *tmp;
@@ -702,8 +701,8 @@ void http_auth_headers(void *ctx, char *user, char *pass, char *session_id)
tmp = (char *)malloc(33); tmp = (char *)malloc(33);
for (i=0; i<16; i++) for (i=0; i<16; i++)
{ {
tmp[i*2] = hex[hash[i]>>4]; tmp[i*2] = hexChars[hash[i]>>4];
tmp[i*2+1] = hex[hash[i]&15]; tmp[i*2+1] = hexChars[hash[i]&15];
} }
tmp[32] = 0; tmp[32] = 0;
http_async_add_header(ctx, "X-Auth-Hash", tmp); http_async_add_header(ctx, "X-Auth-Hash", tmp);
@@ -1032,8 +1031,8 @@ retry:
tmp = (char *)malloc(33); tmp = (char *)malloc(33);
for (i=0; i<16; i++) for (i=0; i<16; i++)
{ {
tmp[i*2] = hex[hash[i]>>4]; tmp[i*2] = hexChars[hash[i]>>4];
tmp[i*2+1] = hex[hash[i]&15]; tmp[i*2+1] = hexChars[hash[i]&15];
} }
tmp[32] = 0; tmp[32] = 0;
http_async_add_header(ctx, "X-Auth-Hash", tmp); http_async_add_header(ctx, "X-Auth-Hash", tmp);

View File

@@ -20,6 +20,8 @@
#ifndef HTTP_H #ifndef HTTP_H
#define HTTP_H #define HTTP_H
static char hexChars[] = "0123456789abcdef";
void http_init(char *proxy); void http_init(char *proxy);
void http_done(void); void http_done(void);

View File

@@ -24,17 +24,8 @@ void LoginModel::Login(string username, string password)
statusText = "Logged in"; statusText = "Logged in";
loginStatus = true; loginStatus = true;
break; break;
case LoginPasswordInvalid: case LoginError:
statusText = "Username or Password incorrect"; statusText = "Error: " + Client::Ref().GetLastError();
break;
case LoginUsernameInvalid:
statusText = "Username incorrect";
break;
case LoginBanned:
statusText = "Banned: " + Client::Ref().GetLastError();
break;
default:
statusText = "Error";
break; break;
} }
notifyStatusChanged(); notifyStatusChanged();