Skip to content

Commit e97402e

Browse files
Updated complete code with comments at places where required
1 parent c7a9f5b commit e97402e

File tree

12 files changed

+45
-3
lines changed

12 files changed

+45
-3
lines changed

config/app.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ type App struct {
1717

1818
/* normalization function */
1919
func (a *App) Normalize() error {
20+
21+
/* set default name to laclm */
2022
if a.Name == "" {
2123
a.Name = "laclm"
2224
}
2325

26+
/* set default version to v1.1 */
2427
if a.Version == "" {
2528
a.Name = "v1.1"
2629
}
@@ -30,10 +33,12 @@ func (a *App) Normalize() error {
3033
we want production to be true
3134
*/
3235

36+
/* set default session timeout to 24 hours */
3337
if a.SessionTimeout == 0 {
3438
a.SessionTimeout = 24
3539
}
3640

41+
/* check if base path is specified */
3742
if a.BasePath == "" {
3843
return errors.New(heredoc.Doc(`
3944
Base path is not specified in the configuration file.

config/authentication.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ func (a *Authentication) Normalize() error {
2525
return a.LDAPConfig.Normalize()
2626
}
2727

28+
/* ldap authentication normalization function */
2829
func (l *LDAPConfig) Normalize() error {
2930
/* TLS will be false by default */
3031

32+
/* check if address is specified */
3133
if l.Address == "" {
3234
return errors.New(heredoc.Doc(`
3335
LDAP address is not specified in the configuration file.
@@ -36,6 +38,7 @@ func (l *LDAPConfig) Normalize() error {
3638
`))
3739
}
3840

41+
/* check if admin DN is specified */
3942
if l.AdminDN == "" {
4043
return errors.New(heredoc.Doc(`
4144
LDAP admin DN is not specified in the configuration file.
@@ -44,6 +47,7 @@ func (l *LDAPConfig) Normalize() error {
4447
`))
4548
}
4649

50+
/* check if admin password is specified */
4751
if l.AdminPassword == "" {
4852
return errors.New(heredoc.Doc(`
4953
LDAP admin password is not specified in the configuration file.
@@ -52,6 +56,7 @@ func (l *LDAPConfig) Normalize() error {
5256
`))
5357
}
5458

59+
/* check if search base is specified */
5560
if l.SearchBase == "" {
5661
return errors.New(heredoc.Doc(`
5762
LDAP search base is not specified in the configuration file.

config/backend_security.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type BackendSecurity struct {
1414

1515
/* normalization function */
1616
func (b *BackendSecurity) Normalize() error {
17+
18+
/* check if JWT token secret is specified */
1719
if b.JWTTokenSecret == "" {
1820
return errors.New(heredoc.Doc(`
1921
JWT Token Security is not specified in the configuration file.
@@ -22,6 +24,7 @@ func (b *BackendSecurity) Normalize() error {
2224
`))
2325
}
2426

27+
/* set default JWT expiry to 24 hours */
2528
if b.JWTExpiry == 0 {
2629
b.JWTExpiry = 24
2730
}

config/database.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (d *Database) Normalize() error {
2424
return d.TransactionLogRedis.Normalize()
2525
}
2626

27+
/* transaction log redis normalization function */
2728
func (r *TransactionLogRedis) Normalize() error {
2829
if r.Address == "" {
2930
return errors.New(heredoc.Doc(`

config/filesystem.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@ func (f *FileSystemServers) Normalize() error {
2929
`))
3030
}
3131

32+
/* set default method to local */
3233
if f.Method == "" {
3334
f.Method = "local"
3435
}
3536

37+
/* check if method is remote */
3638
if f.Method == "remote" {
39+
/* check if remote is specified */
3740
if f.Remote == nil {
3841
return errors.New(heredoc.Doc(`
39-
42+
Remote file server not specified in the configuration file.
43+
44+
Please check the docs for more information:
4045
`))
4146
}
4247

48+
/* check if host is specified */
4349
if f.Remote.Host == "" {
4450
return errors.New(heredoc.Doc(`
4551
Address not provided for remote file server
@@ -48,6 +54,7 @@ func (f *FileSystemServers) Normalize() error {
4854
`))
4955
}
5056

57+
/* check if port is specified */
5158
if f.Remote.Port == 0 {
5259
return errors.New(heredoc.Doc(`
5360
Port not provided for remote file server

config/logging.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ type Logging struct {
1111

1212
/* normalization function */
1313
func (l *Logging) Normalize() error {
14+
15+
/* set default file to log/app.log */
1416
if l.File == "" {
1517
l.File = "log/app.log"
1618
}
1719

20+
/* set default max size to 100MB */
1821
if l.MaxSize == 0 {
1922
l.MaxSize = 100
2023
}
2124

25+
/* set default max backups to 3 */
2226
if l.MaxBackups == 0 {
2327
l.MaxBackups = 3
2428
}

config/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ type Server struct {
88

99
/* normalization function */
1010
func (s *Server) Normalize() error {
11+
12+
/* set default host to localhost */
1113
if s.Host == "" {
1214
s.Host = "localhost"
1315
}
1416

17+
/* set default port to 8080 */
1518
if s.Port == 0 {
1619
s.Port = 8080
1720
}

internal/auth/auth.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
func GenerateJWT(username string) (string, error) {
1515
expiryHours := config.BackendConfig.BackendSecurity.JWTExpiry
1616

17+
/* generate JWT token with claims */
1718
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
1819
"username": username,
1920
"exp": time.Now().Add(time.Hour * time.Duration(expiryHours)).Unix(),
@@ -24,17 +25,21 @@ func GenerateJWT(username string) (string, error) {
2425

2526
/* validate JWT token and return claims */
2627
func ValidateJWT(tokenString string) (jwt.MapClaims, error) {
28+
29+
/* parse the token */
2730
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
2831
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
2932
return nil, fmt.Errorf("unexpected signing method")
3033
}
3134
return []byte(config.BackendConfig.BackendSecurity.JWTTokenSecret), nil
3235
})
3336

37+
/* check if token is valid */
3438
if err != nil {
3539
return nil, fmt.Errorf("JWT parsing error: %w", err)
3640
}
3741

42+
/* check if token is valid */
3843
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
3944
return claims, nil
4045
}

internal/auth/handler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
1515

1616
/* POST Request only - specified in routes */
1717

18+
/* decode the request body */
1819
var user User
1920
err := json.NewDecoder(r.Body).Decode(&user)
2021
if err != nil {
2122
http.Error(w, "Invalid request body", http.StatusBadRequest)
2223
return
2324
}
2425

26+
/* check if username and password are specified */
2527
if user.Username == "" || user.Password == "" {
2628
http.Error(w, "Username and password are required", http.StatusBadRequest)
2729
return
@@ -32,6 +34,8 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
3234
user.Password,
3335
config.BackendConfig.Authentication.LDAPConfig.SearchBase,
3436
)
37+
38+
/* check if authentication is successful */
3539
if !authStatus {
3640
zap.L().Warn("User with invalid credentials attempted to log in")
3741
http.Error(w, "Invalid credentials", http.StatusUnauthorized)

internal/auth/ldap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func AuthenticateUser(username, password, searchbase string) bool {
2424
var err error
2525
ldapAddress := config.BackendConfig.Authentication.LDAPConfig.Address
2626

27+
/* check if TLS is enabled */
2728
if config.BackendConfig.Authentication.LDAPConfig.TLS {
2829
l, err = ldap.DialURL(ldapAddress, ldap.DialWithTLSConfig(&tls.Config{
2930

0 commit comments

Comments
 (0)