Return 401 for Login Errors (#2312)

Signed-off-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
This commit is contained in:
Daniel Valdivia
2022-09-15 13:33:53 -07:00
committed by GitHub
parent 41671b4f25
commit 4ac6ecb558
3 changed files with 40 additions and 29 deletions

View File

@@ -81,6 +81,12 @@ func ErrorWithContext(ctx context.Context, err ...interface{}) *models.Error {
var exists bool
if len(err) > 0 {
if err1, exists = err[0].(error); exists {
var lastError error
if len(err) > 1 {
if err2, lastExists := err[1].(error); lastExists {
lastError = err2
}
}
if err1.Error() == ErrForbidden.Error() {
errorCode = 403
}
@@ -95,6 +101,11 @@ func ErrorWithContext(ctx context.Context, err ...interface{}) *models.Error {
errorCode = 401
errorMessage = ErrInvalidLogin.Error()
}
// If the last error is ErrInvalidLogin, this is a login failure
if errors.Is(lastError, ErrInvalidLogin) {
errorCode = 401
errorMessage = err1.Error()
}
if strings.Contains(err1.Error(), ErrLoginNotAllowed.Error()) {
errorCode = 400
errorMessage = ErrLoginNotAllowed.Error()

View File

@@ -96,11 +96,11 @@ func TestError(t *testing.T) {
}
tests = append(tests,
testError{
name: "passing multiple errors",
name: "passing multiple errors but ErrInvalidLogin is last",
args: args{
err: []interface{}{ErrDefault, ErrInvalidLogin},
},
want: &models.Error{Code: int32(500), Message: swag.String(ErrDefault.Error()), DetailedMessage: swag.String(ErrDefault.Error())},
want: &models.Error{Code: int32(401), Message: swag.String(ErrDefault.Error()), DetailedMessage: swag.String(ErrDefault.Error())},
})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {