Exemple #1
0
        public async Task <bool> DeleteWebAuthnKeyAsync(User user, int id)
        {
            var providers = user.GetTwoFactorProviders();

            if (providers == null)
            {
                return(false);
            }

            var keyName  = $"Key{id}";
            var provider = user.GetTwoFactorProvider(TwoFactorProviderType.WebAuthn);

            if (!provider?.MetaData?.ContainsKey(keyName) ?? true)
            {
                return(false);
            }

            if (provider.MetaData.Count < 2)
            {
                return(false);
            }

            // Delete U2F token is this is a migrated WebAuthn token.
            var entry = new TwoFactorProvider.WebAuthnData(provider.MetaData[keyName]);

            if (entry?.Migrated ?? false)
            {
                var u2fProvider = user.GetTwoFactorProvider(TwoFactorProviderType.U2f);
                if (u2fProvider?.MetaData?.ContainsKey(keyName) ?? false)
                {
                    u2fProvider.MetaData.Remove(keyName);
                    if (u2fProvider.MetaData.Count > 0)
                    {
                        providers[TwoFactorProviderType.U2f] = u2fProvider;
                    }
                    else
                    {
                        providers.Remove(TwoFactorProviderType.U2f);
                    }
                }
            }

            provider.MetaData.Remove(keyName);
            providers[TwoFactorProviderType.WebAuthn] = provider;
            user.SetTwoFactorProviders(providers);
            await UpdateTwoFactorProviderAsync(user, TwoFactorProviderType.WebAuthn);

            return(true);
        }
Exemple #2
0
        private List <Tuple <string, TwoFactorProvider.WebAuthnData> > LoadKeys(TwoFactorProvider provider)
        {
            var keys = new List <Tuple <string, TwoFactorProvider.WebAuthnData> >();

            if (!HasProperMetaData(provider))
            {
                return(keys);
            }

            // Support up to 5 keys
            for (var i = 1; i <= 5; i++)
            {
                var keyName = $"Key{i}";
                if (provider.MetaData.ContainsKey(keyName))
                {
                    var key = new TwoFactorProvider.WebAuthnData((dynamic)provider.MetaData[keyName]);

                    keys.Add(new Tuple <string, TwoFactorProvider.WebAuthnData>(keyName, key));
                }
            }

            return(keys);
        }
 public KeyModel(string id, TwoFactorProvider.WebAuthnData data)
 {
     Name     = data.Name;
     Id       = Convert.ToInt32(id.Replace("Key", string.Empty));
     Migrated = data.Migrated;
 }