More work on gsasl authentication.
This commit is contained in:
@@ -18,16 +18,21 @@
|
||||
***************************************************************************/
|
||||
|
||||
#include <net/sessiondata.h>
|
||||
#include <gsasl.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
SessionData::SessionData(boost::shared_ptr<boost::asio::ip::tcp::socket> sock, SessionId id, SessionDataCallback &cb)
|
||||
: m_socket(sock), m_id(id), m_gameId(0), m_state(SessionData::Init), m_readyFlag(false),
|
||||
m_wantsLobbyMsg(true), m_activityTimeoutNoticeSent(false), m_callback(cb)
|
||||
m_wantsLobbyMsg(true), m_activityTimeoutNoticeSent(false), m_callback(cb),
|
||||
m_authSession(NULL), m_curAuthStep(0)
|
||||
{
|
||||
}
|
||||
|
||||
SessionData::~SessionData()
|
||||
{
|
||||
m_callback.SignalSessionTerminated(m_id);
|
||||
InternalClearAuthSession();
|
||||
}
|
||||
|
||||
SessionId
|
||||
@@ -71,6 +76,64 @@ SessionData::GetAsioSocket()
|
||||
return m_socket;
|
||||
}
|
||||
|
||||
bool
|
||||
SessionData::CreateAuthSession(Gsasl *context, bool server, const string &userName, const string &password)
|
||||
{
|
||||
bool retVal = false;
|
||||
boost::mutex::scoped_lock lock(m_dataMutex);
|
||||
InternalClearAuthSession();
|
||||
int errorCode;
|
||||
if (server)
|
||||
errorCode = gsasl_server_start(context, "SCRAM-SHA-1", &m_authSession);
|
||||
else
|
||||
errorCode = gsasl_client_start(context, "SCRAM-SHA-1", &m_authSession);
|
||||
if (errorCode == GSASL_OK)
|
||||
{
|
||||
gsasl_property_set(m_authSession, GSASL_AUTHID, userName.c_str());
|
||||
gsasl_property_set(m_authSession, GSASL_PASSWORD, password.c_str());
|
||||
retVal = true;
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
bool
|
||||
SessionData::AuthStep(int stepNum, const std::string &inData, std::string &outData)
|
||||
{
|
||||
bool retVal = false;
|
||||
boost::mutex::scoped_lock lock(m_dataMutex);
|
||||
if (m_authSession && stepNum == m_curAuthStep + 1)
|
||||
{
|
||||
m_curAuthStep = stepNum;
|
||||
char *tmpOut;
|
||||
size_t tmpOutSize;
|
||||
int errorCode = gsasl_step(m_authSession, inData.c_str(), inData.length(), &tmpOut, &tmpOutSize);
|
||||
if (errorCode == GSASL_NEEDS_MORE)
|
||||
{
|
||||
outData = string(tmpOut, tmpOutSize);
|
||||
retVal = true;
|
||||
}
|
||||
else if (errorCode == GSASL_OK && stepNum != 1)
|
||||
{
|
||||
outData = string(tmpOut, tmpOutSize);
|
||||
retVal = true;
|
||||
InternalClearAuthSession();
|
||||
}
|
||||
gsasl_free(tmpOut);
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
void
|
||||
SessionData::InternalClearAuthSession()
|
||||
{
|
||||
if (m_authSession)
|
||||
{
|
||||
gsasl_finish(m_authSession);
|
||||
m_authSession = NULL;
|
||||
m_curAuthStep = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SessionData::SetReadyFlag()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user