public static async ETTask <SignInResult> SignIn(Func <Session> realmSessionFunc, Func <string, Session> gateSessionFunc, C2R_Authentication c2R_SignIn) { Log.Info("連接Realm"); Session realmSession = realmSessionFunc?.Invoke(); R2C_Authentication r2CLogin = (R2C_Authentication)await realmSession.Call(c2R_SignIn); if (r2CLogin.Error != ErrorCode.ERR_Success) { if (r2CLogin.Error == ErrorCode.ERR_AccountDoesntExist) { Log.Info("登錄失敗-帳號不存在"); } else if (r2CLogin.Error == ErrorCode.ERR_PasswordIncorrect) { Log.Info("登錄失敗-密碼不正確"); } return(new SignInResult { Message = r2CLogin.Message, Error = r2CLogin.Error }); } else { return(await OnSignInSuccessAsync(realmSessionFunc, gateSessionFunc, r2CLogin)); } }
private async static ETTask RealmToGate(Session session, User user, R2C_Authentication response, bool isRefreshToken) { // 隨機分配GateServer StartConfig config = Game.Scene.GetComponent <RealmGateAddressComponent>().GetAddress(); // Log.Debug($"gate address: {MongoHelper.ToJson(config)}"); IPEndPoint innerAddress = config.GetComponent <InnerConfig>().IPEndPoint; Session gateSession = Game.Scene.GetComponent <NetInnerComponent>().Get(innerAddress); //Game.Scene.GetComponent<PingComponent>().RemoveSession(session.Id); // 向Gate請求一個Key,Client可以拿這個Key連接Gate G2R_GetLoginKey g2RGetLoginKey = (G2R_GetLoginKey)await gateSession.Call(new R2G_GetLoginKey() { Uid = user.Id }); string outerAddress = config.GetComponent <OuterConfig>().Address2; // 創造權杖 if (isRefreshToken) { SignInCryptographyHelper.Token tok = new SignInCryptographyHelper.Token { uid = user.Id, lastCreateTokenAt = user.lastCreateTokenAt, salt = user.salt, }; string token = SignInCryptographyHelper.EncodeToken(tok); response.Token = token; } PlayerRideTotalInfo playerRideTotalInfo = await UserDataHelper.QueryUserRideAllRecord(user); response.Error = ErrorCode.ERR_Success; response.Address = outerAddress; response.Key = g2RGetLoginKey.Key; response.Data = new PlayerBaseInfo { Uid = user.Id, Name = user.name, Sex = user.gender, Location = user.location, Height = user.height, Weight = user.weight, Birthday = user.birthday, CreateAt = user.createAt, // 校時用 LastOnlineAt = DateTimeOffset.Now.ToUnixTimeMilliseconds(), CharSetting = user.playerCharSetting, TotalInfo = playerRideTotalInfo, Language = user.language, }; response.LinkTypes.Clear(); response.LinkTypes.AddRange(await GetAllLinkType(user.Id)); }
private async static ETTask SignInByUid(Session session, User user, R2C_Authentication response, string firebaseDeviceToken, bool isRefreshToken, string signInMethod) { BsonDocument log = new BsonDocument(); // 更新user登入資訊 user.lastOnlineAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); log["lastOnlineAt"] = user.lastOnlineAt; // 最後登入時間 log["signInMethod"] = signInMethod; // 登入方式 if (user.firebaseDeviceToken != firebaseDeviceToken) { user.firebaseDeviceToken = firebaseDeviceToken; log["firebaseDeviceToken"] = user.firebaseDeviceToken; // 最後更新的FirebaseToken } if (isRefreshToken) { user.lastCreateTokenAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); log["lastCreateTokenAt"] = user.lastCreateTokenAt; // 最後更新Token的時間 } log["ip"] = session.RemoteAddress.ToString(); // 登入位址 await UserDataHelper.UpsertUser(user, DBLog.LogType.SignUserIn, log); // 從Realm轉登到Gate並傳給Client用戶資料 await RealmToGate(session, user, response, isRefreshToken); }
private static async ETTask <SignInResult> OnSignInSuccessAsync(Func <Session> realmSessionFunc, Func <string, Session> gateSessionFunc, R2C_Authentication r2CLogin) { Log.Info("連接Gate"); Session gateSession = gateSessionFunc?.Invoke(r2CLogin.Address); G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await gateSession.Call(new C2G_LoginGate() { Key = r2CLogin.Key }); return(new SignInResult { Message = g2CLoginGate.Message, Error = g2CLoginGate.Error, playerBaseInfo = r2CLogin.Data, Token = r2CLogin.Token }); }
/// <summary> /// 使用AppleID登入 /// </summary> /// <param name="session"></param> /// <param name="info"></param> /// <param name="response"></param> /// <returns></returns> public async static ETTask AuthenticationByAppleId(Session session, AuthenticationInfo info, R2C_Authentication response) { string appleId = CryptographyHelper.AESDecrypt(info.Secret); ThirdPartyInfo appleInfo = new ThirdPartyInfo { id = appleId, }; long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); ThirdPartyUser thirdPartyUser = await UserDataHelper.FindOneThirdPartyUser(appleInfo.id, UserDataHelper.tagAppleId); User user = null; if (thirdPartyUser == null) { // 用AppleId註冊帳號 user = ComponentFactory.CreateWithId <User>(IdGenerater.GenerateId()); string salt = CryptographyHelper.GenerateRandomId(); string password = CryptographyHelper.GenerateRandomId(16); string hashPassword = CryptographyHelper.MD5Encoding(password, salt); user.salt = salt; user.hashPassword = hashPassword; user.email = appleInfo.email; user.name = user.Id.ToString(); user.gender = appleInfo.genderCode; user.location = appleInfo.locationCode; user.birthday = appleInfo.birthdayCode; user.createAt = now; user.playerCharSetting = new PlayerCharSetting(); user.playerCharSetting.CharacterId = 1L; user.playerRideTotalInfo = new RideTotalInfo(); user.language = info.Language; user.identity = (int)User.Identity.Player; user.userBagCapacity = EquipmentDataHelper.GetDefaultUserBag(); await UserDataHelper.SinUserUp(user); // 註冊第三方-AppleId thirdPartyUser = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId()); thirdPartyUser.uid = user.Id; thirdPartyUser.party = UserDataHelper.tagAppleId; thirdPartyUser.userId = appleInfo.id; thirdPartyUser.gender = appleInfo.gender; thirdPartyUser.location = appleInfo.location; thirdPartyUser.email = appleInfo.email; thirdPartyUser.name = appleInfo.name; thirdPartyUser.birthday = appleInfo.birthday; thirdPartyUser.createAt = now; await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser); // 註冊第三方-Guest thirdPartyUser = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId()); thirdPartyUser.uid = user.Id; thirdPartyUser.party = UserDataHelper.tagGuest; thirdPartyUser.userId = info.DeviceId; thirdPartyUser.name = ""; thirdPartyUser.gender = ""; thirdPartyUser.location = ""; thirdPartyUser.email = ""; thirdPartyUser.birthday = ""; thirdPartyUser.createAt = now; await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser); } else { user = await UserDataHelper.FindOneUser(thirdPartyUser.uid); } await SignInByUid(session, user, response, info.FirebaseDeviceToken, true, UserDataHelper.tagAppleId); }
public async static ETTask AuthenticationByFaceBook(Session session, AuthenticationInfo info, R2C_Authentication response) { string fbToken = info.Secret; bool isValidToken = await FacebookHelper.ValidateFacebookToken(fbToken); if (!isValidToken) { response.Error = ErrorCode.ERR_FBSignInFailed; return; } ThirdPartyInfo fbInfo = await FacebookHelper.GetFacebookUserInfo(fbToken); if (fbInfo == null) { response.Error = ErrorCode.ERR_FBSignInFailed; return; } long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); ThirdPartyUser thirdPartyUser = await UserDataHelper.FindOneThirdPartyUser(fbInfo.id, UserDataHelper.tagFB); User user = null; if (thirdPartyUser == null) { // 用FB註冊帳號 user = ComponentFactory.CreateWithId <User>(IdGenerater.GenerateId()); string salt = CryptographyHelper.GenerateRandomId(); string password = CryptographyHelper.GenerateRandomId(16); string hashPassword = CryptographyHelper.MD5Encoding(password, salt); user.salt = salt; user.hashPassword = hashPassword; user.email = fbInfo.email; user.name = fbInfo.name; user.gender = fbInfo.genderCode; user.location = fbInfo.locationCode; user.birthday = fbInfo.birthdayCode; user.createAt = now; user.playerCharSetting = new PlayerCharSetting(); user.playerCharSetting.CharacterId = 1L; user.playerRideTotalInfo = new RideTotalInfo(); user.language = info.Language; user.identity = (int)User.Identity.Player; user.userBagCapacity = EquipmentDataHelper.GetDefaultUserBag(); await UserDataHelper.SinUserUp(user); //註冊第三方-FB thirdPartyUser = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId()); thirdPartyUser.uid = user.Id; thirdPartyUser.party = UserDataHelper.tagFB; thirdPartyUser.userId = fbInfo.id; thirdPartyUser.gender = fbInfo.gender; thirdPartyUser.location = fbInfo.location; thirdPartyUser.email = fbInfo.email; thirdPartyUser.name = fbInfo.name; thirdPartyUser.birthday = fbInfo.birthday; thirdPartyUser.createAt = now; await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser); //註冊第三方-Guest thirdPartyUser = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId()); thirdPartyUser.uid = user.Id; thirdPartyUser.party = UserDataHelper.tagGuest; thirdPartyUser.userId = info.DeviceId; thirdPartyUser.name = ""; thirdPartyUser.gender = ""; thirdPartyUser.location = ""; thirdPartyUser.email = ""; thirdPartyUser.birthday = ""; thirdPartyUser.createAt = now; await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser); } else { user = await UserDataHelper.FindOneUser(thirdPartyUser.uid); } await SignInByUid(session, user, response, info.FirebaseDeviceToken, true, UserDataHelper.tagFB); }
public async static ETTask AuthenticationByGuest(Session session, AuthenticationInfo info, R2C_Authentication response) { string deviceUniqueIdentifier = string.Empty; try { deviceUniqueIdentifier = CryptographyHelper.AESDecrypt(info.Secret); } catch (Exception) { response.Error = ErrorCode.ERR_InvalidDeviceUniqueIdentifier; return; } if (string.IsNullOrEmpty(deviceUniqueIdentifier)) { response.Error = ErrorCode.ERR_DeviceUniqueIdentifierIsNull; return; } long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); ThirdPartyUser thirdPartyUser = await UserDataHelper.FindOneThirdPartyUser(deviceUniqueIdentifier, UserDataHelper.tagGuest); User user = null; if (thirdPartyUser == null) { user = ComponentFactory.CreateWithId <User>(IdGenerater.GenerateId()); string salt = CryptographyHelper.GenerateRandomId(); string password = CryptographyHelper.GenerateRandomId(16); string hashPassword = CryptographyHelper.MD5Encoding(password, salt); user.salt = salt; user.hashPassword = hashPassword; user.createAt = now; user.name = $"{user.Id}"; user.email = ""; user.playerCharSetting = new PlayerCharSetting(); user.playerCharSetting.CharacterId = 1L; user.playerRideTotalInfo = new RideTotalInfo(); user.language = info.Language; user.identity = (int)User.Identity.Player; user.userBagCapacity = EquipmentDataHelper.GetDefaultUserBag(); await UserDataHelper.SinUserUp(user); //註冊第三方-Guest thirdPartyUser = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId()); thirdPartyUser.uid = user.Id; thirdPartyUser.party = UserDataHelper.tagGuest; thirdPartyUser.userId = deviceUniqueIdentifier; thirdPartyUser.name = ""; thirdPartyUser.gender = ""; thirdPartyUser.location = ""; thirdPartyUser.email = ""; thirdPartyUser.birthday = ""; thirdPartyUser.createAt = now; await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser); } else { user = await UserDataHelper.FindOneUser(thirdPartyUser.uid); } await SignInByUid(session, user, response, info.FirebaseDeviceToken, true, UserDataHelper.tagGuest); }
public async static ETTask AuthenticationByToken(Session session, AuthenticationInfo info, R2C_Authentication response) { SignInCryptographyHelper.Token tok = null; try { tok = SignInCryptographyHelper.DecodeToken(info.Secret); if (tok == null) { response.Error = ErrorCode.ERR_InvalidToken; return; } } catch (Exception e) { response.Error = ErrorCode.ERR_InvalidToken; return; } User user = await UserDataHelper.FindOneUser(tok.uid); if (user != null) { if (user.salt != tok.salt || user.lastCreateTokenAt != tok.lastCreateTokenAt) { response.Error = ErrorCode.ERR_InvalidToken; } else { await SignInByUid(session, user, response, info.FirebaseDeviceToken, false, UserDataHelper.tagToken); } } else { response.Error = ErrorCode.ERR_AccountDoesntExist; } }