00001 #ifndef _ots_Utilities_WebUsers_h_
00002 #define _ots_Utilities_WebUsers_h_
00003
00004 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00005 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00006 #include "otsdaq-core/SOAPUtilities/SOAPMessenger.h"
00007
00008 #include <string>
00009 #include <vector>
00010 #include <iostream>
00011
00012 #define WEB_LOGIN_DB_PATH std::string(getenv("SERVICE_DATA_PATH")) + "/LoginData/"
00013 #define WEB_LOGIN_CERTDATA_PATH std::string(getenv("CERT_DATA_PATH"))
00014 #define HASHES_DB_PATH "HashesData/"
00015 #define USERS_DB_PATH "UsersData/"
00016 #define USERS_LOGIN_HISTORY_PATH USERS_DB_PATH + "UserLoginHistoryData/"
00017 #define USERS_PREFERENCES_PATH USERS_DB_PATH + "UserPreferencesData/"
00018 #define TOOLTIP_DB_PATH USERS_DB_PATH + "/TooltipData/"
00019
00020
00021
00022
00023
00024 namespace ots
00025 {
00026
00027 class HttpXmlDocument;
00028
00029 class WebUsers
00030 {
00031 public:
00032 WebUsers();
00033
00034 enum {
00035 SESSION_ID_LENGTH = 512,
00036 COOKIE_CODE_LENGTH = 512,
00037 NOT_FOUND_IN_DATABASE = uint64_t(-1),
00038 USERNAME_LENGTH = 4,
00039 DISPLAY_NAME_LENGTH = 4,
00040 };
00041
00042 enum {
00043 DB_SAVE_OPEN_AND_CLOSE,
00044 DB_SAVE_OPEN,
00045 DB_SAVE_CLOSE
00046 };
00047
00048 enum {
00049 DB_USERS,
00050 DB_HASHES
00051 };
00052
00053 enum {
00054 MOD_TYPE_UPDATE,
00055 MOD_TYPE_ADD,
00056 MOD_TYPE_DELETE
00057 };
00058
00059 static const std::string DEFAULT_ADMIN_USERNAME;
00060 static const std::string DEFAULT_ADMIN_DISPLAY_NAME;
00061 static const std::string DEFAULT_ADMIN_EMAIL;
00062 static const std::string DEFAULT_ITERATOR_USERNAME;
00063
00064 static const std::string REQ_NO_LOGIN_RESPONSE;
00065 static const std::string REQ_NO_PERMISSION_RESPONSE;
00066 static const std::string REQ_USER_LOCKOUT_RESPONSE;
00067
00068 static const std::string SECURITY_TYPE_NONE;
00069 static const std::string SECURITY_TYPE_DIGEST_ACCESS;
00070
00071 bool createNewAccount (std::string username, std::string displayName, std::string email);
00072 void cleanupExpiredEntries (std::vector<std::string> *loggedOutUsernames = 0);
00073 std::string createNewLoginSession (std::string uuid, std::string ip = "0");
00074
00075 uint64_t attemptActiveSession(std::string uuid, std::string &jumbledUser, std::string jumbledPw, std::string &newAccountCode);
00076 uint64_t attemptActiveSessionWithCert(std::string uuid, std::string &jumbledEmail, std::string &cookieCode, std::string& username);
00077 uint64_t isCookieCodeActiveForLogin (std::string uuid, std::string &cookieCode,std::string &username);
00078 bool cookieCodeIsActiveForRequest (std::string &cookieCode, uint8_t *userPermissions = 0, uint64_t *uid = 0, std::string ip = "0", bool refresh = true, std::string *userWithLock = 0);
00079 uint64_t cookieCodeLogout (std::string cookieCode, bool logoutOtherUserSessions, uint64_t *uid = 0, std::string ip = "0");
00080
00081 std::string getUsersDisplayName (uint64_t uid);
00082 std::string getUsersUsername (uint64_t uid);
00083 uint64_t getActiveSessionCountForUser (uint64_t uid);
00084 uint8_t getPermissionsForUser (uint64_t uid);
00085 void insertSettingsForUser (uint64_t uid, HttpXmlDocument *xmldoc,bool includeAccounts=false);
00086 std::string getGenericPreference (uint64_t uid, const std::string &preferenceName, HttpXmlDocument *xmldoc = 0) const;
00087
00088 void changeSettingsForUser (uint64_t uid, const std::string &bgcolor, const std::string &dbcolor, const std::string &wincolor, const std::string &layout, const std::string &syslayout);
00089 void setGenericPreference (uint64_t uid, const std::string &preferenceName, const std::string &preferenceValue);
00090 static void tooltipCheckForUsername (const std::string& username, HttpXmlDocument *xmldoc, const std::string &srcFile, const std::string &srcFunc, const std::string &srcId);
00091 static void tooltipSetNeverShowForUsername (const std::string& username, HttpXmlDocument *xmldoc, const std::string &srcFile, const std::string &srcFunc, const std::string &srcId, bool doNeverShow, bool temporarySilence);
00092
00093 void modifyAccountSettings (uint64_t uid_master, uint8_t cmd_type, std::string username, std::string displayname, std::string email, std::string permissions);
00094 bool setUserWithLock (uint64_t uid_master, bool lock, std::string username);
00095 std::string getUserWithLock () { return usersUsernameWithLock_; }
00096
00097 std::string getActiveUsersString ();
00098
00099 bool getUserInfoForCookie (std::string &cookieCode, std::string *userName, std::string *displayName = 0, uint64_t *activeSessionIndex = 0);
00100
00101 bool isUsernameActive (std::string username) const;
00102 bool isUserIdActive (uint64_t uid) const;
00103 uint64_t getAdminUserID ();
00104 std::string getSecurity ();
00105
00106 static void deleteUserData ();
00107 static void resetAllUserTooltips (const std::string &userNeedle = "*");
00108
00109 static void NACDisplayThread (std::string nac, std::string user);
00110
00111 void saveActiveSessions ();
00112 void loadActiveSessions ();
00113
00114 private:
00115 void loadSecuritySelection ();
00116 void loadUserWithLock ();
00117 unsigned int hexByteStrToInt (const char *h);
00118 void intToHexStr (uint8_t i, char *h);
00119 std::string sha512 (std::string user, std::string password, std::string &salt);
00120 std::string dejumble (std::string jumbledUser, std::string sessionId);
00121 std::string createNewActiveSession (uint64_t uid,std::string ip = "0", uint64_t asIndex = 0);
00122 bool addToHashesDatabase (std::string hash);
00123 std::string genCookieCode ();
00124 std::string refreshCookieCode (unsigned int i, bool enableRefresh = true);
00125 void removeActiveSessionEntry (unsigned int i);
00126 void removeLoginSessionEntry (unsigned int i);
00127 bool deleteAccount (std::string username, std::string displayName);
00128
00129 void saveToDatabase (FILE * fp, std::string field, std::string value, uint8_t type = DB_SAVE_OPEN_AND_CLOSE, bool addNewLine = true);
00130 bool saveDatabaseToFile (uint8_t db);
00131 bool loadDatabases ();
00132
00133 uint64_t searchUsersDatabaseForUsername (std::string username) const;
00134 uint64_t searchUsersDatabaseForUserEmail (std::string useremail) const;
00135 uint64_t searchUsersDatabaseForUserId (uint64_t uid) const;
00136 uint64_t searchLoginSessionDatabaseForUUID (std::string uuid) const;
00137 uint64_t searchHashesDatabaseForHash (std::string hash);
00138 uint64_t searchActiveSessionDatabaseForCookie (std::string cookieCode) const;
00139
00140 static std::string getTooltipFilename (const std::string& username, const std::string &srcFile, const std::string &srcFunc, const std::string &srcId);
00141
00142 std::unordered_map<std::string, std::string> certFingerprints_;
00143 std::string getUserEmailFromFingerprint(std::string fingerprint);
00144
00145 std::vector<std::string> UsersDatabaseEntryFields,HashesDatabaseEntryFields;
00146 bool CareAboutCookieCodes_;
00147 std::string securityType_;
00148
00149
00150
00151
00152
00153
00154
00155
00156 std::vector<std::string> LoginSessionIdVector, LoginSessionUUIDVector, LoginSessionIpVector;
00157 std::vector<time_t> LoginSessionStartTimeVector;
00158 std::vector<uint8_t> LoginSessionAttemptsVector;
00159 enum {
00160 LOGIN_SESSION_EXPIRATION_TIME = 5*60,
00161 LOGIN_SESSION_ATTEMPTS_MAX = 5,
00162 };
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 std::vector<std::string> ActiveSessionCookieCodeVector, ActiveSessionIpVector;
00174 std::vector<uint64_t> ActiveSessionUserIdVector, ActiveSessionIndex;
00175 std::vector<time_t> ActiveSessionStartTimeVector;
00176 enum {
00177 ACTIVE_SESSION_EXPIRATION_TIME = 120*60,
00178 ACTIVE_SESSION_COOKIE_OVERLAP_TIME = 10*60,
00179 ACTIVE_SESSION_STALE_COOKIE_LIMIT = 10,
00180 };
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 std::vector<std::string> UsersUsernameVector, UsersUserEmailVector, UsersDisplayNameVector, UsersSaltVector, UsersLastModifierUsernameVector;
00195 std::vector<uint8_t> UsersPermissionsVector;
00196 std::vector<uint64_t> UsersUserIdVector;
00197 std::vector<time_t> UsersLastLoginAttemptVector, UsersAccountCreatedTimeVector, UsersLastModifiedTimeVector;
00198 std::vector<uint8_t> UsersLoginFailureCountVector;
00199 uint64_t usersNextUserId_;
00200 enum {
00201 USERS_LOGIN_HISTORY_SIZE = 20,
00202 USERS_GLOBAL_HISTORY_SIZE = 1000,
00203 USERS_MAX_LOGIN_FAILURES = 20,
00204 };
00205 std::string usersUsernameWithLock_;
00206
00207 std::vector<std::string> UsersLoggedOutUsernames_;
00208
00209
00210
00211 std::vector<std::string> HashesVector;
00212 std::vector<time_t> HashesAccessTimeVector;
00213 };
00214
00215 }
00216
00217 #endif