Skip to content

Commit 376457e

Browse files
Formated the code with gofmt
1 parent ace8207 commit 376457e

File tree

15 files changed

+101
-98
lines changed

15 files changed

+101
-98
lines changed

api/middleware/middleware.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func LoggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
1515

1616
/* logging recieved request at the instant of receiving */
1717
zap.L().Info("Recieved request",
18-
zap.String("Method", r.Method),
18+
zap.String("Method", r.Method),
1919
zap.String("Path", r.URL.Path),
2020
)
2121

@@ -36,11 +36,11 @@ func AuthenticationMiddleware(next http.HandlerFunc) http.HandlerFunc {
3636
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3737

3838
/* authenticate the request through JWT */
39-
username, err := auth.ExtractUsernameFromRequest(r)
39+
username, err := auth.ExtractUsernameFromRequest(r)
4040
if err != nil {
4141
zap.L().Error("Error during authentication",
4242
zap.Error(err),
43-
)
43+
)
4444
return
4545
}
4646

config/app.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package config
22

33
/* app parameters */
44
type App struct {
5-
Name string `yaml:"name,omitempty"`
6-
Version string `yaml:"version,omitempty"`
7-
DebugMode bool `yaml:"debug_mode,omitempty"`
5+
Name string `yaml:"name,omitempty"`
6+
Version string `yaml:"version,omitempty"`
7+
DebugMode bool `yaml:"debug_mode,omitempty"`
88
}
99

1010
/* normalization function */
@@ -17,10 +17,10 @@ func (a *App) Normalize() error {
1717
a.Name = "v1.1"
1818
}
1919

20-
/*
21-
if debug_mode is not provided, it's false
20+
/*
21+
if debug_mode is not provided, it's false
2222
we want production to be true
2323
*/
2424

25-
return nil
25+
return nil
2626
}

config/authentication.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88

99
/* authentication parameters */
1010
type Authentication struct {
11-
LDAPConfig LDAPConfig `yaml:"ldap,omitempty"`
11+
LDAPConfig LDAPConfig `yaml:"ldap,omitempty"`
1212
}
1313

1414
/* ldap authentication parameters */
1515
type LDAPConfig struct {
16-
TLS bool `yaml:"tls,omitempty"`
17-
Address string `yaml:"address,omitempty"`
18-
AdminDN string `yaml:"admin_dn,omitempty"`
19-
AdminPassword string `yaml:"admin_password,omitempty"`
20-
SearchBase string `yaml:"search_base,omitempty"`
16+
TLS bool `yaml:"tls,omitempty"`
17+
Address string `yaml:"address,omitempty"`
18+
AdminDN string `yaml:"admin_dn,omitempty"`
19+
AdminPassword string `yaml:"admin_password,omitempty"`
20+
SearchBase string `yaml:"search_base,omitempty"`
2121
}
2222

2323
/* normalization function */
@@ -59,6 +59,6 @@ func (l *LDAPConfig) Normalize() error {
5959
Please check the docs for more information:
6060
`))
6161
}
62-
62+
6363
return nil
6464
}

config/backend_security.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
/* backend security configs */
1010
type BackendSecurity struct {
11-
JWTTokenSecret string `yaml:"jwt_secret_token,omitempty"`
12-
JWTExpiry int `yaml:"jwt_expiry,omitempty"`
11+
JWTTokenSecret string `yaml:"jwt_secret_token,omitempty"`
12+
JWTExpiry int `yaml:"jwt_expiry,omitempty"`
1313
}
1414

1515
/* normalization function */
@@ -19,7 +19,7 @@ func (b *BackendSecurity) Normalize() error {
1919
JWT Token Security is not specified in the configuration file.
2020
2121
Please check the docs for more information:
22-
`))
22+
`))
2323
}
2424

2525
if b.JWTExpiry == 0 {

config/database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ func (r *TransactionLogRedis) Normalize() error {
3232
Please check the docs for more information:
3333
`))
3434
}
35-
35+
3636
/* password can be empty */
3737
if r.Password == "" {
3838
/* just warn users to use password protected redis */
39-
fmt.Printf("Prefer using password for redis for security purposes\n\n")
39+
fmt.Printf("Prefer using password for redis for security purposes\n\n")
4040
}
4141

4242
/* r.DB default value can be 0 */

config/filesystem.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ func (f *FileSystemServers) Normalize() error {
2626
Remote server file path not specified in the configuration file.
2727
2828
Please check the docs for more information:
29-
`))
29+
`))
3030
}
3131

3232
if f.Method == "" {
3333
f.Method = "local"
3434
}
35-
35+
3636
if f.Method == "remote" {
3737
if f.Remote == nil {
3838
return errors.New(heredoc.Doc(`
3939
4040
`))
4141
}
42-
42+
4343
if f.Remote.Host == "" {
4444
return errors.New(heredoc.Doc(`
4545
Address not provided for remote file server
4646
4747
Please check the docs for more information:
4848
`))
49-
}
49+
}
5050

5151
if f.Remote.Port == 0 {
5252
return errors.New(heredoc.Doc(`

config/loader.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"os"
66

7-
"gopkg.in/yaml.v3"
87
"github.com/davecgh/go-spew/spew"
8+
"gopkg.in/yaml.v3"
99
)
1010

1111
/*
@@ -18,32 +18,32 @@ import (
1818
func LoadConfig(path string) error {
1919

2020
/* read the yaml config file */
21-
data, err := os.ReadFile(path)
22-
if err != nil {
23-
return fmt.Errorf("config loading error %w",
21+
data, err := os.ReadFile(path)
22+
if err != nil {
23+
return fmt.Errorf("config loading error %w",
2424
err,
2525
)
2626

27-
}
27+
}
2828

2929
/* expand all environment variables in the yaml config */
3030
expanded := os.ExpandEnv(string(data))
3131

3232
/* unmarshal the yaml file to defined struct */
33-
err = yaml.Unmarshal([]byte(expanded), &BackendConfig)
34-
if err != nil {
35-
return fmt.Errorf("config loading error %w",
33+
err = yaml.Unmarshal([]byte(expanded), &BackendConfig)
34+
if err != nil {
35+
return fmt.Errorf("config loading error %w",
3636
err,
3737
)
38-
}
38+
}
3939

4040
/* write the config file in console if in debug mode */
4141
if BackendConfig.AppInfo.DebugMode {
4242
fmt.Println("Contents of Config File (debug mode ON)")
4343
spew.Dump(BackendConfig)
4444
fmt.Println()
4545
}
46-
46+
4747
/* normalize the complete backend config before proceeding */
4848
return BackendConfig.Normalize()
4949
}

config/logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (l *Logging) Normalize() error {
2222
if l.MaxBackups == 0 {
2323
l.MaxBackups = 3
2424
}
25-
25+
2626
/* let compression remain false by default */
2727

2828
return nil

internal/auth/auth.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package auth
1+
package auth
22

33
import (
44
"fmt"
5-
"time"
65
"net/http"
76
"strings"
7+
"time"
88

9-
"github.com/golang-jwt/jwt/v5"
109
"github.com/PythonHacker24/linux-acl-management-backend/config"
10+
"github.com/golang-jwt/jwt/v5"
1111
)
1212

1313
/* generating jwt token for user identification with specified configs */
@@ -51,34 +51,34 @@ func ValidateJWT(tokenString string) (jwt.MapClaims, error) {
5151
func GetUsernameFromJWT(tokenString string) (string, error) {
5252

5353
/* get claims from JWT Token */
54-
claims, err := ValidateJWT(tokenString)
55-
if err != nil {
54+
claims, err := ValidateJWT(tokenString)
55+
if err != nil {
5656
return "", fmt.Errorf("JWT validation error: %w", err)
57-
}
57+
}
5858

5959
/* extract username from JWT Token */
60-
username, ok := claims["username"].(string)
61-
if !ok {
62-
return "", fmt.Errorf("username not found in token")
63-
}
60+
username, ok := claims["username"].(string)
61+
if !ok {
62+
return "", fmt.Errorf("username not found in token")
63+
}
6464

65-
return username, nil
65+
return username, nil
6666
}
6767

6868
/* extract username from http request (wrapper around GetUsernameFromJWT for http requests) */
6969
func ExtractUsernameFromRequest(r *http.Request) (string, error) {
7070

7171
/* extract authentication hearder from http request */
72-
authHeader := r.Header.Get("Authorization")
73-
if authHeader == "" {
74-
return "", fmt.Errorf("missing Authorization header")
75-
}
76-
72+
authHeader := r.Header.Get("Authorization")
73+
if authHeader == "" {
74+
return "", fmt.Errorf("missing Authorization header")
75+
}
76+
7777
/* parse the token from the header */
78-
tokenParts := strings.Split(authHeader, " ")
79-
if len(tokenParts) != 2 || tokenParts[0] != "Bearer" {
80-
return "", fmt.Errorf("invalid Authorization header format")
81-
}
78+
tokenParts := strings.Split(authHeader, " ")
79+
if len(tokenParts) != 2 || tokenParts[0] != "Bearer" {
80+
return "", fmt.Errorf("invalid Authorization header format")
81+
}
8282

83-
return GetUsernameFromJWT(tokenParts[1])
83+
return GetUsernameFromJWT(tokenParts[1])
8484
}

internal/auth/handler.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,53 @@ import (
1212

1313
/* Handles user login and creates a session */
1414
func LoginHandler(w http.ResponseWriter, r *http.Request) {
15-
15+
1616
/* POST Request only - specified in routes */
1717

18-
var user User
19-
err := json.NewDecoder(r.Body).Decode(&user)
20-
if err != nil {
21-
http.Error(w, "Invalid request body", http.StatusBadRequest)
22-
return
23-
}
18+
var user User
19+
err := json.NewDecoder(r.Body).Decode(&user)
20+
if err != nil {
21+
http.Error(w, "Invalid request body", http.StatusBadRequest)
22+
return
23+
}
2424

25-
if user.Username == "" || user.Password == "" {
26-
http.Error(w, "Username and password are required", http.StatusBadRequest)
27-
return
28-
}
25+
if user.Username == "" || user.Password == "" {
26+
http.Error(w, "Username and password are required", http.StatusBadRequest)
27+
return
28+
}
2929

3030
/* authenticate the user */
31-
authStatus := AuthenticateUser(user.Username,
32-
user.Password,
31+
authStatus := AuthenticateUser(user.Username,
32+
user.Password,
3333
config.BackendConfig.Authentication.LDAPConfig.SearchBase,
3434
)
35-
if !authStatus {
35+
if !authStatus {
3636
zap.L().Warn("User with invalid credentials attempted to log in")
37-
http.Error(w, "Invalid credentials", http.StatusUnauthorized)
38-
return
39-
}
37+
http.Error(w, "Invalid credentials", http.StatusUnauthorized)
38+
return
39+
}
4040

4141
/* after building session manager */
42-
session.CreateSession(user.Username)
42+
session.CreateSession(user.Username)
4343

4444
/* generate JWT for user interaction */
45-
token, err := GenerateJWT(user.Username)
46-
if err != nil {
47-
zap.L().Error("Error generating token",
45+
token, err := GenerateJWT(user.Username)
46+
if err != nil {
47+
zap.L().Error("Error generating token",
4848
zap.Error(err),
4949
)
50-
http.Error(w, "Error generating token", http.StatusInternalServerError)
51-
return
52-
}
50+
http.Error(w, "Error generating token", http.StatusInternalServerError)
51+
return
52+
}
5353

5454
/* create auth successful response */
55-
response := map[string]string{"token": token}
56-
w.Header().Set("Content-Type", "application/json")
57-
if err := json.NewEncoder(w).Encode(response); err != nil {
58-
zap.L().Error("Failed to encode response",
55+
response := map[string]string{"token": token}
56+
w.Header().Set("Content-Type", "application/json")
57+
if err := json.NewEncoder(w).Encode(response); err != nil {
58+
zap.L().Error("Failed to encode response",
5959
zap.Error(err),
6060
)
61-
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
62-
return
63-
}
61+
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
62+
return
63+
}
6464
}

0 commit comments

Comments
 (0)