public async Task RememberDeviceFor2fa(RememberDeviceParameters p, IScenePeerClient peer, CancellationToken ct)
        {
            if (string.IsNullOrWhiteSpace(p.UserId))
            {
                throw new ClientException($"authentication.RememberDeviceFor2fa.missingUserId");
            }
            if (string.IsNullOrWhiteSpace(p.UserDeviceId))
            {
                throw new ClientException($"authentication.RememberDeviceFor2fa.UserDeviceId");
            }

            var user = await _users.GetUser(p.UserId);


            Dictionary <string, string> RememberedDeviceIds = new Dictionary <string, string>();

            //Dictionary<string, string> RememberedDeviceIds = JsonConvert.DeserializeObject<Dictionary<string, string>>(user.UserData["RememberedDevices"].ToString());
            if (user.UserData["RememberedDevices"] != null)
            {
                RememberedDeviceIds = JsonConvert.DeserializeObject <Dictionary <string, string> >(user.UserData["RememberedDevices"].ToString());
            }

            // remove expired devices from dictionary
            foreach (KeyValuePair <string, string> entry in RememberedDeviceIds)
            {
                DateTime RememberedDate = DateTime.ParseExact(entry.Value, "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture);
                if (DateTime.UtcNow - RememberedDate > TimeSpan.FromDays(30))
                {
                    RememberedDeviceIds.Remove(entry.Key);
                }
            }

            if (RememberedDeviceIds.ContainsKey(p.UserDeviceId))
            {
                throw new ClientException($"authentication.RememberDeviceFor2fa device already remembered. how was this device not recognized? this should not happen");
            }
            RememberedDeviceIds.Add(p.UserDeviceId, DateTime.UtcNow.ToString("MM-dd-yyyy"));
            user.UserData["RememberedDevices"] = JsonConvert.SerializeObject(RememberedDeviceIds);

            await _users.UpdateUserData(user.Id, user.UserData);
        }
Пример #2
0
 public async Task RememberDeviceForTwoFactor(RememberDeviceParameters parameters)
 {
     await _auth.RememberDeviceFor2fa(parameters, this.Request.RemotePeer, this.Request.CancellationToken);
 }