public static bool FetchUserInfoFromDatabaseService_ByMethod( IBDatabaseServiceInterface _DatabaseService, IBMemoryServiceInterface _MemoryService, string _Method, out string _UserID, out string _UserEmail, out string _UserName, out BWebServiceResponse _FailureResponse, Action <string> _ErrorMessageAction = null) { _UserID = null; _UserEmail = null; _UserName = null; _FailureResponse = new BWebServiceResponse(); string ReturnedEntryAsString = null; if (!_DatabaseService.GetItem( AuthDBEntry.DBSERVICE_AUTHMETHODS_TABLE(), AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY, new BPrimitiveType(_Method), AuthDBEntry.Properties, out JObject ReturnedObject, _ErrorMessageAction)) { _FailureResponse = BWebResponse.InternalError("Database fetch operation has failed"); return(false); } if (ReturnedObject == null) { _ErrorMessageAction?.Invoke("FetchFromDatabaseService: Given credentials are invalid: " + _Method); _FailureResponse = BWebResponse.Unauthorized("Given credentials are invalid."); return(false); } try { ReturnedEntryAsString = ReturnedObject.ToString(); var ReturnedEntry = JsonConvert.DeserializeObject <AuthDBEntry>(ReturnedEntryAsString); _UserID = ReturnedEntry.UserID; _UserEmail = ReturnedEntry.UserEmail; _UserName = ReturnedEntry.UserName; } catch (Exception e) { _ErrorMessageAction?.Invoke("FetchFromDatabaseService: " + e.Message + ", Trace: " + e.StackTrace); _FailureResponse = BWebResponse.InternalError("Database fetch operation failed."); return(false); } _MemoryService.SetKeyValue(CommonData.MemoryQueryParameters, new Tuple <string, BPrimitiveType>[] { new Tuple <string, BPrimitiveType>(AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY + _Method, new BPrimitiveType(ReturnedEntryAsString)) }, _ErrorMessageAction); return(true); }
private BWebServiceResponse DeleteUser(HttpListenerContext _Context, out bool _bSetClearanceForApiKeys, out List <string> _ApiKeys, Action <string> _ErrorMessageAction) { _bSetClearanceForApiKeys = false; _ApiKeys = new List <string>(); var UserKey = new BPrimitiveType(RequestedUserID); if (!DatabaseService.GetItem( UserDBEntry.DBSERVICE_USERS_TABLE(), UserDBEntry.KEY_NAME_USER_ID, UserKey, UserDBEntry.Properties, out JObject UserObject, _ErrorMessageAction)) { return(BWebResponse.InternalError("Database fetch-user-info operation has failed.")); } if (UserObject == null) { return(BWebResponse.NotFound("User does not exist.")); } var UserData = JsonConvert.DeserializeObject <UserDBEntry>(UserObject.ToString()); if (UserData.AuthMethods != null && UserData.AuthMethods.Count > 0) { foreach (var AMethod in UserData.AuthMethods) { string OldField = null; BPrimitiveType AuthMethodKey = null; switch (AMethod.Method) { case AuthMethod.Methods.USER_EMAIL_PASSWORD_METHOD: { AuthMethodKey = new BPrimitiveType(AMethod.UserEmail + AMethod.PasswordMD5); OldField = AMethod.UserEmail; break; } case AuthMethod.Methods.USER_NAME_PASSWORD_METHOD: { AuthMethodKey = new BPrimitiveType(AMethod.UserName + AMethod.PasswordMD5); OldField = AMethod.UserName; break; } case AuthMethod.Methods.API_KEY_METHOD: { AuthMethodKey = new BPrimitiveType(AMethod.ApiKey); _bSetClearanceForApiKeys = true; _ApiKeys.Add(AMethod.ApiKey); if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_API_KEY + ":" + AMethod.ApiKey, _ErrorMessageAction)) { return(BWebResponse.InternalError("Atomic operation control has failed.")); } break; } } if (AuthMethodKey != null) { Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, AuthDBEntry.DBSERVICE_AUTHMETHODS_TABLE(), AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY, AuthMethodKey); MemoryService.DeleteKey(CommonData.MemoryQueryParameters, AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY + AuthMethodKey.AsString, _ErrorMessageAction); } } } MemoryService.DeleteKey(CommonData.MemoryQueryParameters, UserBaseAccessMEntry.M_KEY_NAME_USER_ID + RequestedUserID, _ErrorMessageAction); Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, UserDBEntry.DBSERVICE_USERS_TABLE(), UserDBEntry.KEY_NAME_USER_ID, UserKey); Controller_UserActions.Get().BroadcastUserAction(new Action_UserDeleted ( RequestedUserID, UserData.UserEmail, UserData.UserName, UserData.UserModels, UserData.UserSharedModels ), _ErrorMessageAction); if (UserData.UserEmail != null && UserData.UserEmail.Length > 0) { Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_USER_EMAIL, new BPrimitiveType(UserData.UserEmail)); } if (UserData.UserName != null && UserData.UserName.Length > 0) { Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_USER_NAME, new BPrimitiveType(UserData.UserName)); } foreach (var ApiKey in _ApiKeys) { Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_API_KEY, new BPrimitiveType(ApiKey)); } return(BWebResponse.StatusOK("User has been deleted.")); }
private BWebServiceResponse UpdateUserInfo_Internal( HttpListenerContext _Context, string _NewEmailChange, string _NewUserNameChange, JObject _UserObject, JObject _UpdateFieldsUserEntry, JObject _UpdateFieldsAuthEntry, Action <string> _ErrorMessageAction) { var UserKey = new BPrimitiveType(RequestedUserID); if (_UpdateFieldsUserEntry.Count > 0) { string OldEmail = null; string OldUserName = null; if (_NewEmailChange != null && _UserObject.ContainsKey(UserDBEntry.USER_EMAIL_PROPERTY)) { OldEmail = (string)_UserObject[UserDBEntry.USER_EMAIL_PROPERTY]; Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_USER_EMAIL, new BPrimitiveType(OldEmail)); Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget( _Context, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_USER_EMAIL, new BPrimitiveType(_NewEmailChange), new JObject() { [UserDBEntry.KEY_NAME_USER_ID] = RequestedUserID }); } if (_NewUserNameChange != null && _UserObject.ContainsKey(UserDBEntry.USER_NAME_PROPERTY)) { OldUserName = (string)_UserObject[UserDBEntry.USER_NAME_PROPERTY]; Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_USER_NAME, new BPrimitiveType(OldUserName)); Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget( _Context, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_USER_NAME, new BPrimitiveType(_NewUserNameChange), new JObject() { [UserDBEntry.KEY_NAME_USER_ID] = RequestedUserID }); } Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget( _Context, UserDBEntry.DBSERVICE_USERS_TABLE(), UserDBEntry.KEY_NAME_USER_ID, UserKey, _UpdateFieldsUserEntry); var NewEmail = _NewEmailChange ?? (string)_UserObject[UserDBEntry.USER_EMAIL_PROPERTY]; var NewUserName = _NewUserNameChange ?? (string)_UserObject[UserDBEntry.USER_NAME_PROPERTY]; Controller_UserActions.Get().BroadcastUserAction(new Action_UserUpdated( RequestedUserID, OldEmail ?? NewEmail, NewEmail, OldUserName ?? NewUserName, NewUserName, _UpdateFieldsUserEntry), _ErrorMessageAction); } if (_UpdateFieldsAuthEntry.Count > 0) { var UserData = JsonConvert.DeserializeObject <UserDBEntry>(_UserObject.ToString()); if (UserData.AuthMethods != null && UserData.AuthMethods.Count > 0) { foreach (var Method in UserData.AuthMethods) { string PasswordMD5 = null; string OldField = null; BPrimitiveType AuthMethodKey = null; switch (Method.Method) { case AuthMethod.Methods.USER_EMAIL_PASSWORD_METHOD: { PasswordMD5 = Method.PasswordMD5; OldField = Method.UserEmail; AuthMethodKey = new BPrimitiveType(Method.UserEmail + PasswordMD5); break; } case AuthMethod.Methods.USER_NAME_PASSWORD_METHOD: { PasswordMD5 = Method.PasswordMD5; OldField = Method.UserName; AuthMethodKey = new BPrimitiveType(Method.UserName + PasswordMD5); break; } case AuthMethod.Methods.API_KEY_METHOD: AuthMethodKey = new BPrimitiveType(Method.ApiKey); break; } if (AuthMethodKey != null) { bool bRecreateNeed = (_NewEmailChange != null && Method.Method == AuthMethod.Methods.USER_EMAIL_PASSWORD_METHOD) || (_NewUserNameChange != null && Method.Method == AuthMethod.Methods.USER_NAME_PASSWORD_METHOD); if (bRecreateNeed) { MemoryService.DeleteKey(CommonData.MemoryQueryParameters, AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY + AuthMethodKey.AsString, _ErrorMessageAction); //No in-memory recreation for security. if (!DatabaseService.DeleteItem( AuthDBEntry.DBSERVICE_AUTHMETHODS_TABLE(), AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY, AuthMethodKey, out JObject DeletedAuthMethodObject, EBReturnItemBehaviour.ReturnAllOld, _ErrorMessageAction)) { DatabaseService.GetItem( AuthDBEntry.DBSERVICE_AUTHMETHODS_TABLE(), AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY, AuthMethodKey, AuthDBEntry.Properties, out DeletedAuthMethodObject, _ErrorMessageAction); Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, AuthDBEntry.DBSERVICE_AUTHMETHODS_TABLE(), AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY, AuthMethodKey); } if (DeletedAuthMethodObject != null) { DeletedAuthMethodObject.Merge(_UpdateFieldsAuthEntry, new JsonMergeSettings() { MergeArrayHandling = MergeArrayHandling.Replace }); _UpdateFieldsAuthEntry = DeletedAuthMethodObject; } if (_NewEmailChange != null && Method.Method == AuthMethod.Methods.USER_EMAIL_PASSWORD_METHOD) { AuthMethodKey = new BPrimitiveType(_NewEmailChange + PasswordMD5); } else if (_NewUserNameChange != null && Method.Method == AuthMethod.Methods.USER_NAME_PASSWORD_METHOD) { AuthMethodKey = new BPrimitiveType(_NewUserNameChange + PasswordMD5); } Controller_DeliveryEnsurer.Get().DB_PutItem_FireAndForget( _Context, AuthDBEntry.DBSERVICE_AUTHMETHODS_TABLE(), AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY, AuthMethodKey, _UpdateFieldsAuthEntry); } else { Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget( _Context, AuthDBEntry.DBSERVICE_AUTHMETHODS_TABLE(), AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY, AuthMethodKey, _UpdateFieldsAuthEntry); } } } } } return(BWebResponse.StatusOK("User has been updated.")); }
//TODO: If SSO is enabled for user; when it loses access to the tenant, delete all rights but keep the models and user. private BWebServiceResponse DeleteAccessMethodForUser(HttpListenerContext _Context, out bool _bSetClearanceForApiKey, out string _ApiKey, Action <string> _ErrorMessageAction) { _bSetClearanceForApiKey = false; _ApiKey = null; var UserKey = new BPrimitiveType(RequestedUserID); if (!DatabaseService.GetItem( UserDBEntry.DBSERVICE_USERS_TABLE(), UserDBEntry.KEY_NAME_USER_ID, UserKey, UserDBEntry.Properties, out JObject UserObject, _ErrorMessageAction)) { return(BWebResponse.InternalError("Database fetch-user-info operation has failed.")); } if (UserObject == null) { return(BWebResponse.NotFound("User does not exist.")); } if (!UserObject.ContainsKey(UserDBEntry.AUTH_METHODS_PROPERTY)) { return(BWebResponse.NotFound("User does not have any auth method.")); } bool bFound = false; var AuthMethodsArray = (JArray)UserObject[UserDBEntry.AUTH_METHODS_PROPERTY]; for (var i = (AuthMethodsArray.Count - 1); i >= 0; i--) { var MethodObject = (JObject)AuthMethodsArray[i]; var Method = JsonConvert.DeserializeObject <AuthMethod>(MethodObject.ToString()); string AuthMethodKey = null; switch (Method.Method) { case AuthMethod.Methods.USER_EMAIL_PASSWORD_METHOD: { if (!bIsInternalCall && Method.UserEmail.EndsWith(Controller_SSOAccessToken.EMAIL_USER_NAME_POSTFIX)) { return(BWebResponse.BadRequest("This auth method cannot be deleted.")); } AuthMethodKey = Method.UserEmail + Method.PasswordMD5; break; } case AuthMethod.Methods.USER_NAME_PASSWORD_METHOD: { AuthMethodKey = Method.UserName + Method.PasswordMD5; break; } case AuthMethod.Methods.API_KEY_METHOD: { AuthMethodKey = Method.ApiKey; _bSetClearanceForApiKey = true; _ApiKey = Method.ApiKey; if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_API_KEY + ":" + Method.ApiKey, _ErrorMessageAction)) { return(BWebResponse.InternalError("Atomic operation control has failed.")); } break; } } if (AuthMethodKey == RequestedAuthMethodKey) { AuthMethodsArray.RemoveAt(i); bFound = true; } } if (!bFound) { return(BWebResponse.NotFound("Auth method does not exist.")); } //Update UserDBEntry Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget( _Context, UserDBEntry.DBSERVICE_USERS_TABLE(), UserDBEntry.KEY_NAME_USER_ID, UserKey, UserObject); //Delete AuthDBEntry Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, AuthDBEntry.DBSERVICE_AUTHMETHODS_TABLE(), AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY, new BPrimitiveType(RequestedAuthMethodKey)); if (_bSetClearanceForApiKey) { //Delete ApiKey from UniqueUserFields Controller_DeliveryEnsurer.Get().DB_DeleteItem_FireAndForget( _Context, UniqueUserFieldsDBEntry.DBSERVICE_UNIQUEUSERFIELDS_TABLE(), UniqueUserFieldsDBEntry.KEY_NAME_API_KEY, new BPrimitiveType(_ApiKey)); } //Delete from cache MemoryService.DeleteKey( CommonData.MemoryQueryParameters, AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY + RequestedAuthMethodKey, _ErrorMessageAction); return(BWebResponse.StatusOK("Access method has been deleted.")); }
private void Cleanup_AuthMethods(Action <string> _ErrorMessageAction = null) { if (!DatabaseService.ScanTable( AuthDBEntry.DBSERVICE_AUTHMETHODS_TABLE(), out List <JObject> AuthEntries, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke("Cleanup_AuthMethods: Table does not exist or ScanTable operation has failed."); return; } if (AuthEntries.Count == 0) { return; } foreach (var Current in AuthEntries) { var Casted = JsonConvert.DeserializeObject <AuthDBEntry>(Current.ToString()); var EntryKey = (string)Current[AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY]; if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UserDBEntry.DBSERVICE_USERS_TABLE(), Casted.UserID, _ErrorMessageAction)) { continue; } try { bool bDeleteEntry = false; if (!DatabaseService.GetItem( UserDBEntry.DBSERVICE_USERS_TABLE(), UserDBEntry.KEY_NAME_USER_ID, new BPrimitiveType(Casted.UserID), UserDBEntry.Properties, out JObject UserObject, _ErrorMessageAction)) { continue; } if (UserObject == null) { //User does not exist bDeleteEntry = true; } else { bool bFound = false; bool bSSOMethod = false; bool bSSORefreshTokenExpired = false; var User = JsonConvert.DeserializeObject <UserDBEntry>(UserObject.ToString()); for (var i = 0; i < User.AuthMethods.Count; i++) { var UserAuthMethod = User.AuthMethods[i]; var UserAuthMethodKey = ""; if (UserAuthMethod.Method == AuthMethod.Methods.USER_EMAIL_PASSWORD_METHOD) { if (UserAuthMethod.UserEmail == null || UserAuthMethod.PasswordMD5 == null || UserAuthMethod.UserEmail.Length == 0 || UserAuthMethod.PasswordMD5.Length == 0) { continue; } UserAuthMethodKey = UserAuthMethod.UserEmail + UserAuthMethod.PasswordMD5; if (UserAuthMethodKey == EntryKey) { bFound = true; //SSO Auth Method Expiry Check if (UserAuthMethod.UserEmail.EndsWith(Controller_SSOAccessToken.EMAIL_USER_NAME_POSTFIX)) { bSSOMethod = true; var QueryParameters = Controller_SSOAccessToken.MakeSSOQueryParameters(UserAuthMethod.PasswordMD5); if (Controller_SSOAccessToken.IsTokenExpiredOrInvalid(out Dictionary <string, BPrimitiveType> _Result, MemoryService, QueryParameters, _ErrorMessageAction) || _Result == null) { bSSORefreshTokenExpired = true; User.AuthMethods.RemoveAt(i); } } break; } } else { if (UserAuthMethod.Method == AuthMethod.Methods.USER_NAME_PASSWORD_METHOD) { if (UserAuthMethod.UserName == null || UserAuthMethod.PasswordMD5 == null || UserAuthMethod.UserName.Length == 0 || UserAuthMethod.PasswordMD5.Length == 0) { continue; } UserAuthMethodKey = UserAuthMethod.UserName + UserAuthMethod.PasswordMD5; } else if (UserAuthMethod.Method == AuthMethod.Methods.API_KEY_METHOD) { UserAuthMethodKey = UserAuthMethod.ApiKey; } if (UserAuthMethodKey == EntryKey) { bFound = true; break; } } } if (!bFound) { bDeleteEntry = true; } else if (bSSOMethod && bSSORefreshTokenExpired) { _ErrorMessageAction?.Invoke("Cleanup_AuthMethods: Expired sso auth method has been found. Deleting the entry."); bDeleteEntry = true; DatabaseService.UpdateItem( UserDBEntry.DBSERVICE_USERS_TABLE(), UserDBEntry.KEY_NAME_USER_ID, new BPrimitiveType(Casted.UserID), JObject.Parse(JsonConvert.SerializeObject(User)), out JObject _, EBReturnItemBehaviour.DoNotReturn, null, _ErrorMessageAction); } } if (bDeleteEntry) { DatabaseService.DeleteItem( AuthDBEntry.DBSERVICE_AUTHMETHODS_TABLE(), AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY, new BPrimitiveType(EntryKey), out JObject _, EBReturnItemBehaviour.DoNotReturn, _ErrorMessageAction); MemoryService.DeleteKey( CommonData.MemoryQueryParameters, AuthDBEntry.KEY_NAME_AUTH_DB_ENTRY + EntryKey, _ErrorMessageAction); } } finally { Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(InnerProcessor, UserDBEntry.DBSERVICE_USERS_TABLE(), Casted.UserID, _ErrorMessageAction); } } }