Skip to content

Commit 7295c28

Browse files
author
Christian Roessner
committed
Fix grammar and comments in auth.go
Corrected grammar and clarified comments within the `AuthState` struct and associated methods in the `auth.go` file. Added initialization of `accountName` variable for feature handling to ensure proper flow and readability. Signed-off-by: Christian Roessner <[email protected]>
1 parent 993170b commit 7295c28

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

server/core/auth.go

+32-21
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,30 @@ type JSONRequest struct {
130130
AuthLoginAttempt uint `json:"auth_login_attempt"`
131131
}
132132

133-
// AuthState represents a struct that holds information related to authentication process.
133+
// AuthState represents a struct that holds information related to an authentication process.
134134
type AuthState struct {
135135
// StartTime represents the starting time of a client request.
136136
StartTime time.Time
137137

138-
// HaveAccountField is a flag that is set, if a user account field was found in a Database.
138+
// HaveAccountField is a flag that is set if a user account field was found in a Database.
139139
HaveAccountField bool
140140

141-
// NoAuth is a flag that is set, if the request mode does not require authentication.
141+
// NoAuth is a flag that is set if the request mode does not require authentication.
142142
NoAuth bool
143143

144-
// ListAccounts is a flag that is set, if Nauthilus is requested to send a full list of available user accounts.
144+
// ListAccounts is a flag that is set if Nauthilus is requested to send a full list of available user accounts.
145145
ListAccounts bool
146146

147-
// UserFound is a flag that is set, if a password Database found the user.
147+
// UserFound is a flag that is set if a password Database found the user.
148148
UserFound bool
149149

150-
// PasswordsAccountSeen is a counter that is increased whenever a new failed password was detected for the current account.
150+
// PasswordsAccountSeen is a counter increased whenever a new failed password was detected for the current account.
151151
PasswordsAccountSeen uint
152152

153-
// PasswordsTotalSeen is a counter that is increased whenever a new failed password was detected.
153+
// PasswordsTotalSeen is a counter increased whenever a new failed password was detected.
154154
PasswordsTotalSeen uint
155155

156-
// LoginAttempts is a counter that is incremented for each failed login request
156+
// LoginAttempts is a counter incremented for each failed login request
157157
LoginAttempts uint
158158

159159
// StatusCodeOk is the HTTP status code that is set by setStatusCodes.
@@ -165,21 +165,21 @@ type AuthState struct {
165165
// StatusCodeFail is the HTTP status code that is set by setStatusCodes.
166166
StatusCodeFail int
167167

168-
// GUID is a global unique identifier that is inherited in all functions and methods that deal with the
169-
// authentication process. It is needed to track log lines belonging to one request.
168+
// GUID is a global unique identifier inherited in all functions and methods that deal with the
169+
// authentication process. It is necessary to track log lines belonging to one request.
170170
GUID *string
171171

172172
// Method is set by the "Auth-Method" HTTP request header (Nginx protocol). It is typically something like "plain"
173173
// or "login".
174174
Method *string
175175

176-
// AccountField is the name of either a SQL field name or an LDAP attribute that was used to retrieve a user account.
176+
// AccountField is the name of either an SQL field name or an LDAP attribute that was used to retrieve a user account.
177177
AccountField *string
178178

179-
// Username is the value that was taken from the HTTP header "Auth-User" (Nginx protocol).
179+
// Username is the value taken from the HTTP header "Auth-User" (Nginx protocol).
180180
Username string
181181

182-
// Password is the value that was taken from the HTTP header "Auth-Pass" (Nginx protocol).
182+
// Password is the value taken from the HTTP header "Auth-Pass" (Nginx protocol).
183183
Password string
184184

185185
// ClientIP is the IP of a client that is to be authenticated. The value is set by the HTTP request header
@@ -243,7 +243,7 @@ type AuthState struct {
243243
// FeatureName is the name of a feature that has triggered a reject.
244244
FeatureName string
245245

246-
// TOTPSecret is used to store a TOTP secret in a SQL Database.
246+
// TOTPSecret is used to store a TOTP secret in an SQL Database.
247247
TOTPSecret *string
248248

249249
// TOTPSecretField is the SQL field or LDAP attribute that resolves the TOTP secret for two-factor authentication.
@@ -267,7 +267,7 @@ type AuthState struct {
267267
BruteForceCounter map[string]uint
268268

269269
// SourcePassDBBackend is a marker for the Database that is responsible for a specific user. It is set by the
270-
// password Database and stored in Redis to track the authentication flow accross databases (including proxy).
270+
// password Database and stored in Redis to track the authentication flow across databases (including proxy).
271271
SourcePassDBBackend global.Backend
272272

273273
// UsedPassDBBackend is set by the password Database that answered the current authentication request.
@@ -280,7 +280,7 @@ type AuthState struct {
280280
UsedBackendPort int
281281

282282
// Attributes is a result container for SQL and LDAP queries. Databases store their result by using a field or
283-
// attribute name as key and the corresponding result as value.
283+
// attribute name as a key and the corresponding result as a value.
284284
Attributes backend.DatabaseResult
285285

286286
// Protocol is set by the HTTP request header "Auth-Protocol" (Nginx protocol).
@@ -322,7 +322,7 @@ type PassDBResult struct {
322322
// DisplayNameField is the display name of a user
323323
DisplayNameField *string
324324

325-
// Backend is set by the Database backend which has found the user.
325+
// Backend is set by the Database backend, which has found the user.
326326
Backend global.Backend
327327

328328
// Attributes is the result catalog returned by the underlying password Database.
@@ -336,7 +336,7 @@ type (
336336

337337
// PassDBMap is a struct type that represents a mapping between a backend type and a PassDBOption function.
338338
// It is used in the verifyPassword method of the AuthState struct to perform password verification against multiple databases.
339-
// The backend field represents the type of database backend (global.Backend) and the fn field represents the PassDBOption function.
339+
// The backend field represents the type of database backend (global.Backend), and the fn field represents the PassDBOption function.
340340
// The PassDBOption function takes an AuthState pointer as input and returns a PassDBResult pointer and an error.
341341
// The PassDBResult pointer contains the result of the password verification process.
342342
// This struct is used to store the database mappings in an array and loop through them in the verifyPassword method.
@@ -586,7 +586,8 @@ func (a *AuthState) authOK(ctx *gin.Context) {
586586

587587
// setCommonHeaders sets common headers for the given gin.Context and AuthState.
588588
// It sets the "Auth-Status" header to "OK" and the "X-Nauthilus-Session" header to the GUID of the AuthState.
589-
// If the AuthState's Service is not global.ServBasicAuth and the HaveAccountField flag is true, it retrieves the account from the AuthState and sets the "Auth-User" header
589+
// If the AuthState's Service is not global.ServBasicAuth, and the HaveAccountField flag is true,
590+
// it retrieves the account from the AuthState and sets the "Auth-User" header
590591
func setCommonHeaders(ctx *gin.Context, a *AuthState) {
591592
ctx.Header("Auth-Status", "OK")
592593
ctx.Header("X-Nauthilus-Session", *a.GUID)
@@ -691,7 +692,7 @@ func handleAttributeValue(ctx *gin.Context, name string, value []any) {
691692

692693
// formatValues takes an array of values and formats them into strings.
693694
// It creates an empty slice of strings called stringValues.
694-
// It then iterates over each value in the values array and appends the formatted string representation of that value to stringValues using fmt.Sprintf("%v", values[index]).
695+
// It then iterates over each value in the "values" array and appends the formatted string representation of that value to stringValues using fmt.Sprintf("%v", values[index]).
695696
// After iterating over all the values, it returns stringValues.
696697
// Example usage:
697698
// values := []any{"one", "two", "three"}
@@ -1156,6 +1157,13 @@ func (a *AuthState) refreshUserAccount() (accountName string) {
11561157

11571158
// handleFeatures iterates through the list of enabled features and returns true, if a feature returned positive.
11581159
func (a *AuthState) handleFeatures(ctx *gin.Context) (authResult global.AuthResult) {
1160+
var accountName string
1161+
1162+
// If brute-force is enabled, the account should have been refreshed by calling the checkBruteForce() method.
1163+
if !config.LoadableConfig.HasFeature(global.FeatureBruteForce) {
1164+
accountName = a.refreshUserAccount()
1165+
}
1166+
11591167
// Helper function that sends an action request and waits for it to be finished. Features may change the Lua context.
11601168
// Lua post actions may make use of these changes.
11611169
doAction := func(luaAction global.LuaAction, luaActionName string) {
@@ -1170,7 +1178,10 @@ func (a *AuthState) handleFeatures(ctx *gin.Context) (authResult global.AuthResu
11701178
}
11711179

11721180
finished := make(chan action.Done)
1173-
accountName := a.getAccount()
1181+
1182+
if accountName == "" {
1183+
accountName = a.getAccount()
1184+
}
11741185

11751186
action.RequestChan <- &action.Action{
11761187
LuaAction: luaAction,

0 commit comments

Comments
 (0)