Skip to content

Commit

Permalink
Add GitHub Action for golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Giurgiu committed Oct 14, 2024
1 parent f9db77e commit a91d929
Show file tree
Hide file tree
Showing 4 changed files with 1,185 additions and 1,169 deletions.
28 changes: 28 additions & 0 deletions api/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
linters:
enable:
- errcheck
- govet
- staticcheck
- gofmt
- goimports

run:
timeout: 5m
tests: true

issues:
exclude-use-default: false
exclude:
- "Error return value of"
- "SA4006"

linters-settings:
errcheck:
exclude-functions:
- fmt.Println
- bytes.Buffer.WriteString
gofmt:
simplify: true
staticcheck:
checks:
- all
93 changes: 46 additions & 47 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,54 +232,53 @@ func (app *App) LoginUser(w http.ResponseWriter, r *http.Request) {
}
}


func (app *App) VerifySessionToken(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the session token from the cookie
cookie, err := r.Cookie("token")
if err != nil {
if err == http.ErrNoCookie {
// If the cookie is not set, return an unauthorized status
w.WriteHeader(http.StatusUnauthorized)
if encodeErr := json.NewEncoder(w).Encode(ErrorResponse{Message: "Unauthorized access"}); encodeErr != nil {
app.Logger.Printf("Error encoding JSON: %v", encodeErr)
}
return
}
// For any other type of error, return a bad request status
w.WriteHeader(http.StatusBadRequest)
if encodeErr := json.NewEncoder(w).Encode(ErrorResponse{Message: "Bad request"}); encodeErr != nil {
app.Logger.Printf("Error encoding JSON: %v", encodeErr)
}
return
}

// Retrieve the session token from the cookie
sessionToken := cookie.Value

// Get the session from the store
session, exists := sessionStore.Get(sessionToken)
if !exists {
// If the session token is not valid, return unauthorized
w.WriteHeader(http.StatusUnauthorized)
if encodeErr := json.NewEncoder(w).Encode(ErrorResponse{Message: "Invalid session token"}); encodeErr != nil {
app.Logger.Printf("Error encoding JSON: %v", encodeErr)
}
return
}

// Check if the session has expired
if session.ExpiresAt.Before(time.Now()) {
// If the session is expired, return unauthorized
w.WriteHeader(http.StatusUnauthorized)
if encodeErr := json.NewEncoder(w).Encode(ErrorResponse{Message: "Session expired"}); encodeErr != nil {
app.Logger.Printf("Error encoding JSON: %v", encodeErr)
}
return
}

next.ServeHTTP(w, r)
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the session token from the cookie
cookie, err := r.Cookie("token")
if err != nil {
if err == http.ErrNoCookie {
// If the cookie is not set, return an unauthorized status
w.WriteHeader(http.StatusUnauthorized)
if encodeErr := json.NewEncoder(w).Encode(ErrorResponse{Message: "Unauthorized access"}); encodeErr != nil {
app.Logger.Printf("Error encoding JSON: %v", encodeErr)
}
return
}
// For any other type of error, return a bad request status
w.WriteHeader(http.StatusBadRequest)
if encodeErr := json.NewEncoder(w).Encode(ErrorResponse{Message: "Bad request"}); encodeErr != nil {
app.Logger.Printf("Error encoding JSON: %v", encodeErr)
}
return
}

// Retrieve the session token from the cookie
sessionToken := cookie.Value

// Get the session from the store
session, exists := sessionStore.Get(sessionToken)
if !exists {
// If the session token is not valid, return unauthorized
w.WriteHeader(http.StatusUnauthorized)
if encodeErr := json.NewEncoder(w).Encode(ErrorResponse{Message: "Invalid session token"}); encodeErr != nil {
app.Logger.Printf("Error encoding JSON: %v", encodeErr)
}
return
}

// Check if the session has expired
if session.ExpiresAt.Before(time.Now()) {
// If the session is expired, return unauthorized
w.WriteHeader(http.StatusUnauthorized)
if encodeErr := json.NewEncoder(w).Encode(ErrorResponse{Message: "Session expired"}); encodeErr != nil {
app.Logger.Printf("Error encoding JSON: %v", encodeErr)
}
return
}

next.ServeHTTP(w, r)
})
}

func generateSessionToken() (string, error) {
Expand Down
Loading

0 comments on commit a91d929

Please sign in to comment.