Skip to content

Commit

Permalink
fix: return 400 instead of 404 on admin recovery
Browse files Browse the repository at this point in the history
Closes #1664
  • Loading branch information
aeneasr committed Feb 24, 2022
1 parent 7ceb1bc commit 27891c2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions selfservice/strategy/link/strategy_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ type selfServiceRecoveryLink struct {
//
// Responses:
// 200: selfServiceRecoveryLink
// 404: jsonError
// 400: jsonError
// 404: jsonError
// 500: jsonError
func (s *Strategy) createRecoveryLink(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var p adminCreateSelfServiceRecoveryLinkBody
Expand Down Expand Up @@ -159,10 +159,14 @@ func (s *Strategy) createRecoveryLink(w http.ResponseWriter, r *http.Request, _
}

id, err := s.d.IdentityPool().GetIdentity(r.Context(), p.IdentityID)
if err != nil {
if errors.Is(err, herodot.ErrNotFound) {
s.d.Writer().WriteError(w, r, errors.WithStack(herodot.ErrBadRequest.WithReasonf("The requested identity id does not exist.").WithWrap(err)))
return
} else if err != nil {
s.d.Writer().WriteError(w, r, err)
return
}

token := NewRecoveryToken(id.ID, expiresIn)
if err := s.d.RecoveryTokenPersister().CreateRecoveryToken(r.Context(), token); err != nil {
s.d.Writer().WriteError(w, r, err)
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/link/strategy_recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestAdminStrategy(t *testing.T) {
IdentityId: x.NewUUID().String(),
}).Execute()
require.IsType(t, err, new(kratos.GenericOpenAPIError), "%T", err)
assert.EqualError(t, err.(*kratos.GenericOpenAPIError), "404 Not Found")
assert.EqualError(t, err.(*kratos.GenericOpenAPIError), "400 Bad Request")
})

t.Run("description=should create a valid recovery link without email", func(t *testing.T) {
Expand Down

0 comments on commit 27891c2

Please sign in to comment.