예제 #1
0
        public ActionResult Post([FromBody] StateResponseDto stateResponseChange, [FromHeader(Name = "Device-ID")] Guid deviceId, [FromHeader(Name = "Recovery-Code")] Guid recoveryCode)
        {
            stateResponseChange.Guid          = stateResponseChange.Guid;
            stateResponseChange.VersionNumber = 1;

            byte[] tokenData = new byte[256];
            Rng.GetBytes(tokenData);

            string secretToken = Convert.ToBase64String(tokenData);

            var newDevice  = new AccountDevice(deviceId, secretToken, recoveryCode);
            var newAccount = new Account(Guid.NewGuid(), new List <AccountDevice>(new[] { newDevice }), stateResponseChange);

            ApplicationStateAccess.RegisterNewAccount(newDevice, newAccount, recoveryCode);

            var registrationResponseDto = new RegistrationResponseDto
            {
                DeviceId     = deviceId,
                RecoveryCode = recoveryCode,
                SecretToken  = secretToken,
                InitialState = stateResponseChange
            };

            Console.WriteLine($"Register new device. DeviceId:{deviceId} RecoveryCode:{recoveryCode} SecretToken:{secretToken}");
            return(new JsonResult(registrationResponseDto));
        }
 public static void AddSyncStateToAccount(Account account, StateResponseDto dto)
 {
     lock (dbLock)
     {
         var dbState         = GetDbState();
         var existingAccount = dbState.Accounts.First(x => x.Id == account.Id);
         existingAccount.SyncStates.Add(dto);
         WriteDbState(dbState);
     }
 }
예제 #3
0
        public ActionResult PostState([FromBody] StateResponseDto stateResponseChange, [FromHeader(Name = "Device-ID")] Guid deviceId, [FromHeader(Name = "Authorization")] string token)
        {
            var newKey = new AccountDevice(deviceId, token, Guid.Empty);

            if (!ApplicationStateAccess.TryGetAccount(newKey, out var account))
            {
                return(new UnauthorizedResult());
            }

            if (account.SyncStates.Last().VersionNumber + 1 != stateResponseChange.VersionNumber)
            {
                return(BadRequest(
                           $"The Version Number of '{stateResponseChange.VersionNumber}' is not expected. Expected:{account.SyncStates.Last().VersionNumber + 1}"));
            }
            ApplicationStateAccess.AddSyncStateToAccount(account, stateResponseChange);
            return(new JsonResult(account.SyncStates.Last()));
        }
예제 #4
0
 public Account(Guid id, List <AccountDevice> devices, StateResponseDto initialState)
 {
     Id         = id;
     Devices    = devices;
     SyncStates = new List <StateResponseDto>(new[] { initialState });
 }