00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "TextAuthenticationProtocol.h"
00021 #include "Stream.h"
00022 #include "AuthenticationManager.h"
00023
00024 namespace mdw
00025 {
00026
00027 TextAuthenticationProtocol::TextAuthenticationProtocol (StreamServer &server) : _server (server)
00028 {
00029 }
00030
00031
00032 TextAuthenticationProtocol::~TextAuthenticationProtocol()
00033 {
00034 }
00035
00036 void TextAuthenticationProtocol::visitRead (std::vector<char> &buffer, StreamInfo &info)
00037 {
00038 int streamId = info.getStreamId();
00039 StatusState &state = _statusMap[streamId];
00040 switch (state.state)
00041 {
00042 case WaitingForUsername_e:
00043 for (int i = 0; i < buffer.size(); ++i)
00044 {
00045 if (buffer[i] == '\r' || buffer[i] == '\n')
00046 {
00047 state.state = WaitingForPassword_e;
00048 Stream &stream = _server.outStream (streamId);
00049 stream << "Password : ";
00050 break;
00051 }
00052 state.username += buffer[i];
00053 }
00054 break;
00055 case WaitingForPassword_e:
00056 for (int i = 0; i < buffer.size(); ++i)
00057 {
00058 if (buffer[i] == '\r' || buffer[i] == '\n')
00059 {
00060 state.state = WaitingForPassword_e;
00061 AuthenticationManager &AM = AuthenticationManager::getInstance();
00062 Authentified *auth = AM.authentify (state.username, state.password, streamId);
00063
00064 if (auth == 0)
00065 {
00066 Stream &stream = _server.outStream (streamId);
00067 if (state.numberFailures < 3)
00068 {
00069 stream << "Wrong Username / Password" << endl;
00070 stream << "Username : ";
00071 state.numberFailures++;
00072 state.state = WaitingForUsername_e;
00073 state.username.clear();
00074 state.password.clear();
00075 }
00076 else
00077 {
00078 stream << "To many tries" << endl;
00079 close (streamId);
00080 }
00081 }
00082 else
00083 {
00084 state.state = Connected_e;
00085 state.auth = auth;
00086 }
00087 break;
00088 }
00089 state.password += buffer[i];
00090 }
00091 break;
00092 case Connected_e:
00093 _currentVisitor->visitRead (&*buffer.begin(), buffer.size(), info);
00094 break;
00095 }
00096 }
00097
00098 void TextAuthenticationProtocol::visitConnect (StreamInfo &info)
00099 {
00100 int streamId = info.getStreamId();
00101 Stream &stream = _server.outStream (streamId);
00102 stream << "*************************************" << endl;
00103 stream << "* Welcome on this server **" << endl;
00104 stream << "*************************************" << endl;
00105 stream << "You are connecting from " << info.getIp() << endl;
00106 stream << "Username : ";
00107 StatusState &state = _statusMap[streamId];
00108 state.username.clear();
00109 state.password.clear();
00110 state.state = WaitingForUsername_e;
00111 state.numberFailures = 0;
00112 state.auth = 0;
00113 }
00114 void TextAuthenticationProtocol::visitDisconnect (StreamInfo &info)
00115 {
00116 _currentVisitor->visitDisconnect (info);
00117 _statusMap.erase (info.getStreamId());
00118 }
00119
00120 void TextAuthenticationProtocol::accept (StreamServerVisitor &visitor, int timeOutSec, int timeOutMicro)
00121 {
00122 _server.accept (visitor, timeOutSec, timeOutMicro);
00123 }
00124 void TextAuthenticationProtocol::write (const char *buffer, int size, int streamId)
00125 {
00126 }
00127 Stream &TextAuthenticationProtocol::outStream (int streamId)
00128 {
00129 return _server.outStream (streamId);
00130 }
00131 void TextAuthenticationProtocol::close (int streamId)
00132 {
00133 _server.close (streamId);
00134 _statusMap.erase (streamId);
00135 }
00136
00137 }