Skip to content

Commit

Permalink
ensure only one key is deleted if multiple with same name
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerNoblett committed Jul 24, 2023
1 parent d29ce9d commit 9bdb251
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions backend/authschemes/webauthn/webauthn.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,17 @@ func (a WebAuthn) deleteCredential(userID int64, credentialName string, bridge a
return backend.WebauthnLoginError(err, "Unable to parse webauthn credentials")
}

results := helpers.Filter(creds, func(cred AShirtWebauthnCredential) bool {
return cred.CredentialName != credentialName
})
encodedCreds, err := json.Marshal(results)
var filteredCreds []AShirtWebauthnCredential
for i, v := range creds {
if v.CredentialName != credentialName {
filteredCreds = append(filteredCreds, creds[i])
} else {
filteredCreds = append(filteredCreds, creds[i+1:]...)
break
}
}

encodedCreds, err := json.Marshal(filteredCreds)
if err != nil {
return backend.WrapError("Unable to delete credential", err)
}
Expand Down
2 changes: 1 addition & 1 deletion backend/helpers/functional.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Map[T any, U any](slice []T, mapFn func(T) U) []U {
return result
}

// Find is a generic function that searches through a list searching for all items that match
// Filter is a generic function that searches through a list searching for all items that match
// the given predicate. This returns the elements that matched the predicate in the order they were
// encountered.
func Filter[T any](slice []T, predicate func(T) bool) []T {
Expand Down

0 comments on commit 9bdb251

Please sign in to comment.