async void OnLogin(PlayerPostLoginEventArgs e) { try { Contributor con = await Contributors.GetAsync(e.Player.User.ID); if (con != null) { // Start listening to events con.Listen(this, e.Player); // Store the contributor object e.Player.SetData(Contributor.DataKey, con); await con.UpdateNotifications(); } } catch { // Catch any exception that is thrown here to prevent pointless error messages } }
public async void Authenticate(CommandArgs args) { if (!args.Player.IsLoggedIn || args.Player.User == null) { args.Player.SendErrorMessage("You must be logged in to do that!"); return; } Color c = Color.LightGreen; if (args.Player.ContainsData(Contributor.DataKey)) { // Players can only authenticate their user account to one contributor forum account args.Player.SendMessage($"{Tag} You already authenticated this user account.", c); return; } if (args.Parameters.Count == 0) { args.Player.SendMessage($"{Tag} Usage: {spe}auth <code> OR {spe}auth <username> <password>", c); //args.Player.SendMessage($"You can get your code in your profile page at {TShock.Utils.ColorTag(CTRS.Config.AuthCodeGetURL, Color.White)}", c); args.Player.SendMessage($"{Tag} If using the credentials method, please enter data for an existing forum account.", c); args.Player.SendMessage($"{Tag} Authentication via code is currently disabled.", c); } else if (args.Parameters.Count == 1) { #region Code Authentication // Temporary lockdown until a proper interface is made (I highly doubt this will ever get done) args.Player.SendMessage($"{Tag} Authentication via code is currently disabled. Please use the forum credentials method.", c); return; //string authCode = args.Parameters[0]; //WebClient client = new WebClient(); //var sb = new StringBuilder(); //sb.Append(CTRS.Config.AuthCodeHandlerURL); //sb.Append("?code=").Append(authCode); //sb.Append("&user="******"An error occurred while trying to contact the xenforo database. Wait a few minutes and try again.", c); //} //else if (code == ReturnCode.NotFound) //{ // args.Player.SendMessage("The auth code you entered was invalid. Make sure you've entered it correctly (NOTE: codes are case-sensitive).", c); //} //else //{ // contributor.XenforoID = Int32.Parse(result[1]); // if (await CTRS.Contributors.UpdateAsync(contributor, ContributorUpdates.XenforoID)) // args.Player.SendMessage($"You have binded this account to {TShock.Utils.ColorTag("xenforo:" + contributor.XenforoID.Value.ToString(), Color.White)}.", c); // else // args.Player.SendMessage("Something went wrong with the database... contact an admin and try again later.", c); //} #endregion } else { string username = args.Parameters[0]; string password = args.Parameters[1]; AuthResult response; try { response = await _main.CredentialHelper.Authenticate(args.Player.User, new Credentials(username, password)); } catch (Exception ex) { // Catching the exception should hopefully prevent unknown outcomes from crashing the server args.Player.SendErrorMessage("Something went wrong... contact an admin and try again later."); TShock.Log.ConsoleError( $"An error occurred while trying to authenticate player '{args.Player.Name}'. Exception message: {ex.Message}."); TShock.Log.Error(ex.ToString()); return; } switch (response.Code) { case LMReturnCode.Success: // Store the contributor object to finish the authentication process Contributor contributor = response.Contributor; bool success = await _main.XenforoUsers.SetTShockID(contributor.XenforoId.Value, args.Player.User.ID); #region DEBUG #if DEBUG TShock.Log.ConsoleInfo($"CTRS-AUTH: Set TShockID for Contributor {contributor.UserID.Value}? {success}"); #endif #endregion if (success) { // Start listening to events contributor.Listen(_main, args.Player); // Store the contributor object args.Player.SetData(Contributor.DataKey, contributor); args.Player.SendSuccessMessage($"{Tag} You are now authenticated for the forum account '{username}'."); await contributor.UpdateNotifications(); } else { goto case LMReturnCode.DatabaseError; } break; case LMReturnCode.EmptyParameter: case LMReturnCode.InsuficientParameters: args.Player.SendErrorMessage($"Invalid syntax! Proper syntax: {spe}auth <username> <password>"); break; case LMReturnCode.UserNotFound: args.Player.SendErrorMessage($"The user '{username}' was not found. Make sure to create a forum account beforehand."); break; case LMReturnCode.IncorrectData: args.Player.SendErrorMessage($"Invalid username or password!"); break; case LMReturnCode.UnloadedCredentials: // Should never happen here break; case LMReturnCode.DatabaseError: args.Player.SendMessage($"{Tag} Something went wrong with the database... contact an admin and try again later.", c); break; case LMReturnCode.UserNotAContributor: args.Player.SendMessage($"{Tag} Currently, only contributors are able to connect their forum accounts to their game accounts.", c); break; case LMReturnCode.AccountLimitReached: args.Player.SendErrorMessage($"Account limit reached. Contact an admin to revoke authentication on a previous account."); break; } } }