Skip to content

Commit

Permalink
Fix DELETE request for non-existent public key (#19443) (#19444)
Browse files Browse the repository at this point in the history
- Backport #19443
  - Add a return for the first "block" of errors, which fixes the double error messages.
  - Add a return for `externallyManaged`.
  - Resolves #19398

Co-authored-by: 6543 <6543@obermui.de>
  • Loading branch information
Gusted and 6543 authored Apr 20, 2022
1 parent 5863f7e commit 0c7bf68
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions routers/api/v1/user/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,21 @@ func DeletePublicKey(ctx *context.APIContext) {
id := ctx.ParamsInt64(":id")
externallyManaged, err := asymkey_model.PublicKeyIsExternallyManaged(id)
if err != nil {
ctx.Error(http.StatusInternalServerError, "PublicKeyIsExternallyManaged", err)
if asymkey_model.IsErrKeyNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "PublicKeyIsExternallyManaged", err)
}
return
}

if externallyManaged {
ctx.Error(http.StatusForbidden, "", "SSH Key is externally managed for this user")
return
}

if err := asymkey_service.DeletePublicKey(ctx.User, id); err != nil {
if asymkey_model.IsErrKeyNotExist(err) {
ctx.NotFound()
} else if asymkey_model.IsErrKeyAccessDenied(err) {
if asymkey_model.IsErrKeyAccessDenied(err) {
ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
} else {
ctx.Error(http.StatusInternalServerError, "DeletePublicKey", err)
Expand Down

0 comments on commit 0c7bf68

Please sign in to comment.