unified code verification service
This commit is contained in:
45
api/server/internal/mutil/verification/verificatoin.go
Normal file
45
api/server/internal/mutil/verification/verificatoin.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package mutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/tech/sendico/pkg/api/http/response"
|
||||
"github.com/tech/sendico/pkg/db/verification"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func MapTokenErrorToResponse(logger mlogger.Logger, service mservice.Type, err error) http.HandlerFunc {
|
||||
if errors.Is(err, verification.ErrTokenNotFound) {
|
||||
logger.Debug("Verification token not found during consume", zap.Error(err))
|
||||
return response.NotFound(logger, service, "No account found associated with given verifcation token")
|
||||
}
|
||||
if errors.Is(err, verification.ErrTokenExpired) {
|
||||
logger.Debug("Verification token expired during consume", zap.Error(err))
|
||||
return response.Gone(logger, service, "token_expired", "verification token has expired")
|
||||
}
|
||||
if errors.Is(err, verification.ErrTokenAlreadyUsed) {
|
||||
logger.Debug("Verification token already used during consume", zap.Error(err))
|
||||
return response.DataConflict(logger, service, "verification token has already been used")
|
||||
}
|
||||
if errors.Is(err, verification.ErrTokenAttemptsExceeded) {
|
||||
logger.Debug("Verification token attempts exceeded", zap.Error(err))
|
||||
return response.Forbidden(logger, service, "code_attempts_exceeded", "verification token has already been used")
|
||||
}
|
||||
if errors.Is(err, verification.ErrCooldownActive) {
|
||||
logger.Debug("Cooldown is still active", zap.Error(err))
|
||||
return response.TooManyRequests(logger, service, "verification token can't be generated yet, cooldown is still active")
|
||||
}
|
||||
if errors.Is(err, verification.ErrIdempotencyConflict) {
|
||||
logger.Debug("Verification idempotency key conflict", zap.Error(err))
|
||||
return response.DataConflict(logger, service, "verification request was already processed")
|
||||
}
|
||||
if err != nil {
|
||||
logger.Warn("Unexpected error during token verification", zap.Error(err))
|
||||
return response.Auto(logger, service, err)
|
||||
}
|
||||
logger.Debug("No token verification error found")
|
||||
return response.Success(logger)
|
||||
}
|
||||
Reference in New Issue
Block a user