Skip to content

Commit

Permalink
Fix: Refactor authentication logic and remove redundant flag
Browse files Browse the repository at this point in the history
Removed unused HaveAccountField flag and updated conditional checks. Improved the handling and verification flow of user authentication to streamline functions and ensure code clarity. Simplified caching logic and added comments for better understanding of the process stages.

Signed-off-by: Christian Roessner <c@roessner.co>
  • Loading branch information
Christian Roessner committed Nov 14, 2024
1 parent c7f2bc8 commit 279ca40
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions server/core/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ type AuthState struct {
// StartTime represents the starting time of a client request.
StartTime time.Time

// HaveAccountField is a flag that is set if a user account field was found in a Database.
HaveAccountField bool

// NoAuth is a flag that is set if the request mode does not require authentication.
NoAuth bool

Expand Down Expand Up @@ -592,7 +589,7 @@ func setCommonHeaders(ctx *gin.Context, a *AuthState) {
ctx.Header("Auth-Status", "OK")
ctx.Header("X-Nauthilus-Session", *a.GUID)

if a.Service != global.ServBasicAuth && a.HaveAccountField {
if a.Service != global.ServBasicAuth {
if account, found := a.getAccountOk(); found {
ctx.Header("Auth-User", account)
}
Expand Down Expand Up @@ -1555,6 +1552,10 @@ func (a *AuthState) appendBackend(passDBs []*PassDBMap, backendType global.Backe
func (a *AuthState) postVerificationProcesses(ctx *gin.Context, useCache bool, backendPos map[global.Backend]int, passDBs []*PassDBMap) global.AuthResult {
var accountName string

/*
* 1. Verify password
*/

passDBResult, err := a.verifyPassword(passDBs)
if err != nil {
var detailedError *errors.DetailedError
Expand All @@ -1578,7 +1579,15 @@ func (a *AuthState) postVerificationProcesses(ctx *gin.Context, useCache bool, b
return global.AuthResultTempFail
}

if a.UserFound && !a.NoAuth {
/*
* 2. Does the user exist on the backend?
*/

if a.UserFound {
if passDBResult.AccountField != nil {
a.AccountField = passDBResult.AccountField
}

accountName, err = a.updateUserAccountInRedis()
if err != nil {
level.Error(log.Logger).Log(global.LogKeyGUID, a.GUID, global.LogKeyMsg, err.Error())
Expand All @@ -1591,8 +1600,9 @@ func (a *AuthState) postVerificationProcesses(ctx *gin.Context, useCache bool, b
}
}

if useCache && !a.NoAuth {
if useCache {
// Make sure the cache backend is in front of the used backend.
// If this is a userdb-request, the authentication state is forced to "true" (see verifyPassword()-moethod)
if passDBResult.Authenticated {
if accountName != "" {
if backendPos[global.BackendCache] < backendPos[a.UsedPassDBBackend] {
Expand Down Expand Up @@ -1651,10 +1661,21 @@ func (a *AuthState) postVerificationProcesses(ctx *gin.Context, useCache bool, b
a.saveFailedPasswordCounterInRedis()
}

a.getAllPasswordHistories()
// Only passdb requests need reloading
if !a.NoAuth {
a.getAllPasswordHistories()
}
}

if !passDBResult.Authenticated {
/*
* 3. Check the authentication state
*/

if passDBResult.Authenticated {
if !(a.haveMonitoringFlag(global.MonInMemory) || a.isMasterUser()) {
localcache.LocalCache.Set(a.generateLocalChacheKey(), a, config.EnvConfig.LocalCacheAuthTTL)
}
} else {
a.updateBruteForceBucketsCounter()

authResult := a.filterLua(passDBResult, ctx)
Expand All @@ -1664,19 +1685,9 @@ func (a *AuthState) postVerificationProcesses(ctx *gin.Context, useCache bool, b
return authResult
}

// Set new username
if passDBResult.UserFound {
if passDBResult.AccountField != nil {
a.AccountField = passDBResult.AccountField
a.HaveAccountField = true
}
}

if passDBResult.Authenticated {
if !(a.haveMonitoringFlag(global.MonInMemory) || a.isMasterUser()) {
localcache.LocalCache.Set(a.generateLocalChacheKey(), a, config.EnvConfig.LocalCacheAuthTTL)
}
}
/*
* 4. User was fine so far, do remaining Lua tasks
*/

authResult := global.AuthResultOK

Expand Down

0 comments on commit 279ca40

Please sign in to comment.