Esempio n. 1
0
        public void RegisterResponse_Equals()
        {
            RegisterResponse registerResponse     = new RegisterResponse(TestConts.REGISTRATION_RESPONSE_DATA_BASE64, TestConts.CLIENT_DATA_REGISTER_BASE64);
            RegisterResponse sameRegisterResponse = RegisterResponse.FromJson <RegisterResponse>(JsonData);

            Assert.IsTrue(sameRegisterResponse.Equals(registerResponse));
        }
        public bool CompleteRegistration(string userName, string deviceResponse)
        {
            if (string.IsNullOrWhiteSpace(deviceResponse))
            {
                return(false);
            }

            var user = _userRepository.FindUser(userName);

            if (user == null ||
                user.AuthenticationRequest == null ||
                user.AuthenticationRequest.Count == 0)
            {
                return(false);
            }


            RegisterResponse registerResponse = RegisterResponse.FromJson <RegisterResponse>(deviceResponse);

            // When the user is registration they should only ever have one auth request.
            AuthenticationRequest authenticationRequest = user.AuthenticationRequest.First();

            StartedRegistration startedRegistration = new StartedRegistration(authenticationRequest.Challenge, authenticationRequest.AppId);
            DeviceRegistration  registration        = U2F.FinishRegistration(startedRegistration, registerResponse);

            _userRepository.RemoveUsersAuthenticationRequests(userName);
            _userRepository.AddDeviceRegistration(userName, registration.AttestationCert, registration.Counter, registration.KeyHandle, registration.PublicKey);

            return(true);
        }
Esempio n. 3
0
        public void RegisterResponse_FromJson()
        {
            RegisterResponse registerResponse = RegisterResponse.FromJson <RegisterResponse>(JsonData);

            Assert.IsNotNull(registerResponse);
            Assert.IsNotNull(registerResponse.GetClientData());
            Assert.IsNotNull(registerResponse.ToJson());
            Assert.IsTrue(registerResponse.GetHashCode() != 0);
            Assert.AreEqual(JsonData, registerResponse.ToJson());
            Assert.AreEqual(TestConts.REGISTRATION_RESPONSE_DATA_BASE64, registerResponse.RegistrationData);
            Assert.AreEqual(TestConts.CLIENT_DATA_REGISTER_BASE64, registerResponse.ClientData);
        }
Esempio n. 4
0
        public async Task <bool> CompleteRegistration(string userId, string deviceResponse, string name)
        {
            if (string.IsNullOrWhiteSpace(deviceResponse))
            {
                return(false);
            }

            if (!UserAuthenticationRequests.ContainsKey(userId) || !UserAuthenticationRequests[userId].Any())
            {
                return(false);
            }

            var registerResponse = RegisterResponse.FromJson <RegisterResponse>(deviceResponse);

            //There is only 1 request when registering device
            var authenticationRequest = UserAuthenticationRequests[userId].First();

            var startedRegistration =
                new StartedRegistration(authenticationRequest.Challenge, authenticationRequest.AppId);
            var registration = FinishRegistrationCore(startedRegistration, registerResponse);

            UserAuthenticationRequests.AddOrReplace(userId, new List <U2FDeviceAuthenticationRequest>());
            using (var context = _contextFactory.CreateContext())
            {
                var duplicate = context.U2FDevices.Any(device =>
                                                       device.ApplicationUserId == userId &&
                                                       device.KeyHandle.Equals(registration.KeyHandle) &&
                                                       device.PublicKey.Equals(registration.PublicKey));

                if (duplicate)
                {
                    throw new U2fException("The U2F Device has already been registered with this user");
                }

                await context.U2FDevices.AddAsync(new U2FDevice()
                {
                    Id = Guid.NewGuid().ToString(),
                    AttestationCert   = registration.AttestationCert,
                    Counter           = Convert.ToInt32(registration.Counter),
                    Name              = name,
                    KeyHandle         = registration.KeyHandle,
                    PublicKey         = registration.PublicKey,
                    ApplicationUserId = userId
                });

                await context.SaveChangesAsync();
            }

            return(true);
        }
Esempio n. 5
0
        public async Task <bool> CompleteRegistration(string userName, string deviceResponse)
        {
            if (string.IsNullOrWhiteSpace(deviceResponse))
            {
                return(false);
            }

            User user = await FindUserByUsername(userName);

            if (user?.AuthenticationRequest == null || user.AuthenticationRequest.Count == 0)
            {
                return(false);
            }


            RegisterResponse registerResponse = RegisterResponse.FromJson <RegisterResponse>(deviceResponse);

            // TODO When the user is registration they should only ever have one auth request.????
            AuthenticationRequest authenticationRequest = user.AuthenticationRequest.First();

            StartedRegistration startedRegistration = new StartedRegistration(authenticationRequest.Challenge, authenticationRequest.AppId);
            DeviceRegistration  registration        = Core.Crypto.U2F.FinishRegistration(startedRegistration, registerResponse);

            user.AuthenticationRequest.Clear();
            user.UpdatedOn = DateTime.Now;
            user.DeviceRegistrations.Add(new Device
            {
                AttestationCert = registration.AttestationCert,
                Counter         = Convert.ToInt32(registration.Counter),
                CreatedOn       = DateTime.Now,
                UpdatedOn       = DateTime.Now,
                KeyHandle       = registration.KeyHandle,
                PublicKey       = registration.PublicKey
            });
            int result = await _dataContext.SaveChangesAsync();

            if (result > 0)
            {
                await _signInManager.SignInAsync(user, new AuthenticationProperties(), "U2F");
            }

            return(result > 0);
        }
Esempio n. 6
0
        public bool CompleteRegistration(string userName, string deviceResponse)
        {
            if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(deviceResponse))
            {
                return(false);
            }

            RegisterResponse registerResponse = RegisterResponse.FromJson(deviceResponse);

            var user = _userRepository.FindUser(userName);

            if (user == null || user.AuthenticationRequest == null)
            {
                return(false);
            }

            StartedRegistration startedRegistration = new StartedRegistration(user.AuthenticationRequest.Challenge, user.AuthenticationRequest.AppId);
            DeviceRegistration  registration        = U2F.FinishRegistration(startedRegistration, registerResponse);

            _userRepository.RemoveUsersAuthenticationRequest(userName);
            _userRepository.AddDeviceRegistration(userName, registration.AttestationCert, registration.Counter, registration.KeyHandle, registration.PublicKey);

            return(true);
        }