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