summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-25 00:57:39 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-25 00:57:39 (GMT)
commit9cdf2b5902b692bed2d27c28bd51a506d052bbfb (patch)
tree782b14846c45c432396ae224804d81be503ee5af /src
parent35858ef6073092f8447bbec8b18b768061cf8bd0 (diff)
downloadpowder-9cdf2b5902b692bed2d27c28bd51a506d052bbfb.zip
powder-9cdf2b5902b692bed2d27c28bd51a506d052bbfb.tar.gz
Login complete (minus brokenness)
Diffstat (limited to 'src')
-rw-r--r--src/client/Client.cpp60
-rw-r--r--src/client/Client.h2
-rw-r--r--src/client/HTTP.cpp9
-rw-r--r--src/client/HTTP.h2
-rw-r--r--src/login/LoginModel.cpp13
5 files changed, 55 insertions, 31 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp
index d0bfd97..0070e65 100644
--- a/src/client/Client.cpp
+++ b/src/client/Client.cpp
@@ -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"];
-
- free(data);
- if(tempStatus.Value() == 1)
- {
- return LoginOkay;
- }
- else if(tempStatus.Value() == 0)
+ try
{
- return LoginPasswordInvalid;
+ 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;
+ }
+ else
+ {
+ json::String tempError = objDocument["Error"];
+ lastError = tempError.Value();
+ return LoginError;
+ }
}
- else
+ catch (json::Exception &e)
{
+ lastError = "Server responded with crap";
return LoginError;
}
}
diff --git a/src/client/Client.h b/src/client/Client.h
index 644fb06..ef6a35e 100644
--- a/src/client/Client.h
+++ b/src/client/Client.h
@@ -12,7 +12,7 @@
enum LoginStatus
{
- LoginPasswordInvalid, LoginUsernameInvalid, LoginOkay, LoginBanned, LoginError
+ LoginOkay, LoginError
};
class Client: public Singleton<Client>
diff --git a/src/client/HTTP.cpp b/src/client/HTTP.cpp
index 7514515..a94ac73 100644
--- a/src/client/HTTP.cpp
+++ b/src/client/HTTP.cpp
@@ -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);
diff --git a/src/client/HTTP.h b/src/client/HTTP.h
index af0971c..51b9438 100644
--- a/src/client/HTTP.h
+++ b/src/client/HTTP.h
@@ -20,6 +20,8 @@
#ifndef HTTP_H
#define HTTP_H
+static char hexChars[] = "0123456789abcdef";
+
void http_init(char *proxy);
void http_done(void);
diff --git a/src/login/LoginModel.cpp b/src/login/LoginModel.cpp
index 7940cf0..185df68 100644
--- a/src/login/LoginModel.cpp
+++ b/src/login/LoginModel.cpp
@@ -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();