Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): fixed error handeling in authentication middleware #31

Merged
merged 1 commit into from
Jan 5, 2024

Conversation

dvjsharma
Copy link
Contributor

Description

This PR solves the nil pointer dereference issue(internal server error) highlighted in #28

Changes

In the authentication middleware, the err variable is used inconsistently. Please refer to the snippet below-

LicenseDb/pkg/auth/auth.go

Lines 194 to 207 in aa42310

decodedAuth, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(authHeader, "Basic "))
if err != nil {
er := models.LicenseError{
Status: http.StatusUnauthorized,
Message: "Please check your credentials and try again",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusUnauthorized, er)
c.Abort()
return
}

Here the err variable is appropriately used to capture potential errors during the base64 decoding process, but it is incorrectly used further in the code.

LicenseDb/pkg/auth/auth.go

Lines 219 to 247 in aa42310

result := db.DB.Where(models.User{Username: username}).First(&user)
if result.Error != nil {
er := models.LicenseError{
Status: http.StatusUnauthorized,
Message: "User name not found",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusUnauthorized, er)
c.Abort()
return
}
// Check if the password matches
if *user.Userpassword != password {
er := models.LicenseError{
Status: http.StatusUnauthorized,
Message: "Incorrect password",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusUnauthorized, er)
c.Abort()
return
}

At line 224, err.Error() references to decoding errors but it should be referencing to the errors occurring during GORM db querying process happening at line 219. Hence it is replaced by result.Error.Error() to deliever GORM specific errors.

Furthermore, line 239 is also modified to give 'Password does not match' error rather than err.Error().

Fixes #28
CC @GMishx

Copy link
Member

@GMishx GMishx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes looks good.

Copy link
Member

@GMishx GMishx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested, working as expected.

@GMishx GMishx merged commit 55dbde0 into fossology:main Jan 5, 2024
4 checks passed
@dvjsharma dvjsharma deleted the fix/issue-28 branch January 5, 2024 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Post request in License giving internal server error
2 participants