Skip to content

Commit 46de072

Browse files
authored
fix: return apt 410 Gone status code and error message when OTP no longer exists in the store. (#53)
1 parent 277bf58 commit 46de072

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

cmd/otpgateway/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func handleVerifyOTP(w http.ResponseWriter, r *http.Request) {
318318
if err != nil {
319319
code := http.StatusBadRequest
320320
if err == store.ErrNotExist {
321-
sendErrorResponse(w, err.Error(), code, nil)
321+
sendErrorResponse(w, err.Error(), http.StatusGone, nil)
322322
return
323323
}
324324

cmd/otpgateway/handlers_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,12 @@ func TestCheckOTP(t *testing.T) {
238238

239239
// Check non-existent OTP, should not return 200.
240240
r = testRequest(t, http.MethodPost, "/api/otp/abc123", cp, &data)
241-
assert.NotEqual(t, http.StatusOK, r.StatusCode, "non-existent OTP didn't return 400")
241+
assert.NotEqual(t, http.StatusOK, r.StatusCode, "non-existent OTP didn't return 200")
242242

243-
// Check non-existent OTP, should return store.ErrNotExist error message.
244-
_ = testRequest(t, http.MethodPost, "/api/otp/abc123", cp, &out)
245-
assert.Equal(t, "the OTP does not exist", out.Message, "non-existent OTP passed")
243+
// Check non-existent OTP, should return 410.
244+
r = testRequest(t, http.MethodPost, "/api/otp/abc123", cp, &out)
245+
assert.Equal(t, http.StatusGone, r.StatusCode, "non-existent OTP returns 410")
246+
assert.Equal(t, "OTP has expired or doesn't exist", out.Message, "non-existent OTP passed")
246247
}
247248

248249
func TestCheckOTPAttempts(t *testing.T) {
@@ -322,8 +323,9 @@ func TestDeleteOnOTPCheck(t *testing.T) {
322323
assert.Equal(t, http.StatusOK, r.StatusCode, "verification pending")
323324

324325
// Reattempt status check
325-
r = testRequest(t, http.MethodDelete, "/api/otp/"+dummyOTPID+"/status", nil, &data)
326-
assert.Equal(t, http.StatusBadRequest, r.StatusCode, "otp not found")
326+
r = testRequest(t, http.MethodDelete, "/api/otp/"+dummyOTPID+"/status", nil, &out)
327+
assert.Equal(t, http.StatusGone, r.StatusCode, "otp not found returns 410")
328+
assert.Equal(t, "OTP has expired or doesn't exist", out.Message, "deleted OTP passed")
327329
}
328330

329331
func testRequest(t *testing.T, method, path string, p url.Values, out interface{}) *http.Response {

internal/store/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// ErrNotExist is thrown when an OTP (requested by namespace / ID)
1010
// does not exist.
11-
var ErrNotExist = errors.New("the OTP does not exist")
11+
var ErrNotExist = errors.New("OTP has expired or doesn't exist")
1212

1313
const (
1414
CounterAttempts = "attempts"

0 commit comments

Comments
 (0)