Skip to content

Commit 277bf58

Browse files
authored
fix: return store error from verifyOTP when OTP is missing in store. (#52)
When OTP record's missing in the store, the UI would end up showing this error message on action `check`: "Internal error" "The provider for this OTP was not found." Now verifyOTP returns a store-level not-found error which is mapped to the webview "Session expired" message. Added test cases for the same.
1 parent 3cfcf35 commit 277bf58

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

cmd/otpgateway/handlers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,11 @@ func verifyOTP(namespace, id, otp string, deleteOnVerify bool, app *App) (models
534534
// Check the OTP.
535535
out, err := app.store.Check(namespace, id, store.CounterAttempts)
536536
if err != nil {
537-
if err != store.ErrNotExist {
538-
app.lo.Error("error checking OTP", "error", err)
539-
return out, err
537+
if err == store.ErrNotExist {
538+
return out, store.ErrNotExist
540539
}
541-
return out, errors.New("error checking OTP.")
540+
app.lo.Error("error checking OTP", "error", err)
541+
return out, errors.New("error checking OTP")
542542
}
543543

544544
errMsg := ""

cmd/otpgateway/handlers_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ func TestCheckOTP(t *testing.T) {
235235
// Check it again. Should be deleted.
236236
r = testRequest(t, http.MethodPost, "/api/otp/"+dummyOTPID, cp, &data)
237237
assert.NotEqual(t, http.StatusOK, r.StatusCode, "OTP didn't get deleted on verification")
238+
239+
// Check non-existent OTP, should not return 200.
240+
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")
242+
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")
238246
}
239247

240248
func TestCheckOTPAttempts(t *testing.T) {

0 commit comments

Comments
 (0)