Exemplo n.º 1
0
 public void AddFido2StoredCredential(Fido2StoredCredential fido2StoredCredential)
 {
     _applicationDbContext.fido2StoredCredentials.Add(fido2StoredCredential);
     _applicationDbContext.SaveChanges();
 }
        public async Task <JsonResult> MakeCredential([FromBody] AuthenticatorAttestationRawResponse authenticatorAttestationRawResponse)
        {
            try
            {
                var user = await _userManager.GetUserAsync(User);

                if (user == null)
                {
                    throw new Exception("Unable to retrieve user.");
                }

                var jsonOptions = await _distributedCache.GetStringAsync(UniqueId);

                if (string.IsNullOrEmpty(jsonOptions))
                {
                    throw new Exception("Cant get Credential options from cache.");
                }
                var options = CredentialCreateOptions.FromJson(jsonOptions);

                IsCredentialIdUniqueToUserAsyncDelegate isCredentialIdUniqueToUserAsyncDelegate = async(IsCredentialIdUniqueToUserParams isCredentialIdUniqueToUserParams) =>
                {
                    var fido2Users = await _fido2Service.GetFido2UsersByCredentialIdAsync(isCredentialIdUniqueToUserParams.CredentialId);

                    if (fido2Users.Count > 0)
                    {
                        return(false);
                    }
                    return(true);
                };
                var result = await _fido2.MakeNewCredentialAsync(authenticatorAttestationRawResponse, options, isCredentialIdUniqueToUserAsyncDelegate);

                if (result.Status != "ok")
                {
                    throw new Exception("Unable to create credential.");
                }


                var newFido2StoredCredential = new Fido2StoredCredential {
                };
                newFido2StoredCredential.UserName         = options.User.Name;
                newFido2StoredCredential.UserId           = options.User.Id;
                newFido2StoredCredential.PublicKey        = result.Result.PublicKey;
                newFido2StoredCredential.UserHandle       = result.Result.User.Id;
                newFido2StoredCredential.SignatureCounter = result.Result.Counter;
                newFido2StoredCredential.CredType         = result.Result.CredType;
                newFido2StoredCredential.RegDate          = DateTime.Now;
                newFido2StoredCredential.AaGuid           = Guid.NewGuid();
                newFido2StoredCredential.Descriptor       = new PublicKeyCredentialDescriptor(result.Result.CredentialId);

                _fido2Service.AddFido2StoredCredential(newFido2StoredCredential);

                return(Json(result));
            }
            catch (Exception exception)
            {
                return(Json(new CredentialCreateOptions()
                {
                    Status = "error", ErrorMessage = CommonFunctions.FormatException(exception)
                }));
            }
        }