Skip to content

Commit

Permalink
Add logout action and optimize logs (#730)
Browse files Browse the repository at this point in the history
* Add logout action on backend and handle redirect path

* Optimize the portal server logs format

* Update body logs handle logic
  • Loading branch information
hiveer authored Oct 23, 2024
1 parent 926fc87 commit 6978da8
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 21 deletions.
21 changes: 21 additions & 0 deletions internal/handlers/render/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (

type SessionHandler interface {
Login(ctx *gin.Context)
Logout(ctx *gin.Context)
SignUp(ctx *gin.Context)
Create(ctx *gin.Context)
}
Expand All @@ -47,6 +48,26 @@ func (i *SessionHandlerImpl) Login(ctx *gin.Context) {
ctx.Redirect(http.StatusFound, i.Config.LoginURL)
}

func (i *SessionHandlerImpl) Logout(ctx *gin.Context) {
cookies := ctx.Request.Cookies()
paramRedirectPath := ctx.Query("redirect_to")

// Loop through the cookies and remove them
for _, cookie := range cookies {
ctx.SetCookie(cookie.Name, "", -1, "/", "localhost", false, true)
ctx.SetCookie(cookie.Name, "", -1, "/", "opencsg.com", false, true)
ctx.SetCookie(cookie.Name, "", -1, "/", "stg.opencsg.com", false, true)
ctx.SetCookie(cookie.Name, "", -1, "/", ".opencsg-stg.com", false, true)
ctx.SetCookie(cookie.Name, "", -1, "/", ".opencsg.com", false, true)
}

if paramRedirectPath == "" {
ctx.Redirect(http.StatusFound, "/")
} else {
ctx.Redirect(http.StatusFound, paramRedirectPath)
}
}

func (i *SessionHandlerImpl) SignUp(ctx *gin.Context) {
ctx.Redirect(http.StatusFound, i.Config.SignupURL)
}
Expand Down
22 changes: 15 additions & 7 deletions internal/middleware/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"log/slog"
"regexp"
"time"

"github.com/gin-gonic/gin"
Expand All @@ -25,12 +26,19 @@ func Log() gin.HandlerFunc {
}

latency := time.Since(startTime).Milliseconds()
slog.InfoContext(ctx, "http request", slog.String("ip", ctx.ClientIP()),
slog.String("method", ctx.Request.Method),
slog.Int("latency(ms)", int(latency)),
slog.Int("status", ctx.Writer.Status()),
slog.String("current_user", username),
slog.String("url", ctx.Request.URL.RequestURI()),
)

assetsImagesReq := regexp.MustCompile(`^/(assets|images)(/.*)?`)

if assetsImagesReq.MatchString(ctx.Request.RequestURI) {
slog.Info("assets request", ctx.Request.RequestURI, int(latency))
} else {
slog.InfoContext(ctx, "Portal Request", slog.String("ip", ctx.ClientIP()),
slog.String("method", ctx.Request.Method),
slog.Int("latency(ms)", int(latency)),
slog.Int("status", ctx.Writer.Status()),
slog.String("current_user", username),
slog.String("url", ctx.Request.RequestURI),
)
}
}
}
3 changes: 2 additions & 1 deletion internal/routes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Initialize(svcCtx *svc.ServiceContext) (*gin.Engine, error) {

csghubServer, err := server.NewServer(svcCtx.Config)
if err != nil {
log.Fatalf("failed to create server: %w", err)
log.Fatalf("failed to create server: %v", err)
}

logFilePath := "./log/app.log"
Expand Down Expand Up @@ -67,6 +67,7 @@ func Initialize(svcCtx *svc.ServiceContext) (*gin.Engine, error) {
c.AbortWithStatus(http.StatusInternalServerError)
}))
g.Use(middleware.AuthMiddleware(csghubServer))
// This will track all request to portal go server
g.Use(middleware.Log())

frontendHandlers, err := frontendHandlers.NewHandlersRegistry(svcCtx)
Expand Down
1 change: 1 addition & 0 deletions internal/routes/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import (
func registerSessionsRoutes(engine *gin.Engine, handlersRegistry *HandlersRegistry) {
engine.GET("/signup", handlersRegistry.RenderHandler.SessionHandler.SignUp)
engine.GET("/login", handlersRegistry.RenderHandler.SessionHandler.Login)
engine.GET("/logout", handlersRegistry.RenderHandler.SessionHandler.Logout)
engine.GET("/server/callback", handlersRegistry.RenderHandler.SessionHandler.Create)
}
14 changes: 5 additions & 9 deletions pkg/server/backend/csghubserver/csghubserver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package csghubserver

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
Expand Down Expand Up @@ -52,22 +53,17 @@ func NewCsgHubServer(ctx context.Context, baseURL, apiKey string) (*CsgHubServer
}, nil
}

func (c *CsgHubServer) getParsedResponse(method, path string, header http.Header, body io.Reader, obj interface{}) (*http.Response, error) {
func (c *CsgHubServer) getParsedResponse(method, path string, header http.Header, body []byte, obj interface{}) (*http.Response, error) {
data, resp, err := c.getResponse(method, path, header, body)
if err != nil {
return resp, err
}
return resp, json.Unmarshal(data, obj)
}

func (c *CsgHubServer) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, *http.Response, error) {
bodyString := ""
if body != nil {
bodyData, _ := io.ReadAll(body)
bodyString = string(bodyData)
}
slog.Info("CsghubServer API Key Request", method, path, headersToString(header), bodyString)
resp, err := c.doRequest(method, path, header, body)
func (c *CsgHubServer) getResponse(method, path string, header http.Header, body []byte) ([]byte, *http.Response, error) {
slog.Info("Server Request", method, path, headersToString(header), string(body))
resp, err := c.doRequest(method, path, header, bytes.NewReader(body))
if err != nil {
return nil, resp, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/server/backend/csghubserver/jwt.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package csghubserver

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -33,7 +32,7 @@ func (c *CsgHubServer) CreateJWTToken(req types.CreateJWTReq) (*types.CreateJWTR
"POST",
fmt.Sprintf("/jwt/token?current_user_uuid=%s", req.UUID),
nil,
bytes.NewReader(body),
body,
checkResp,
)
return checkResp, resp, err
Expand Down
3 changes: 1 addition & 2 deletions pkg/server/backend/csghubserver/sensitive_check.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package csghubserver

import (
"bytes"
"encoding/json"
"net/http"

Expand All @@ -19,7 +18,7 @@ func (c *CsgHubServer) ImageSecureCheck(req types.ImageSensitiveCheckReq) (*type
"POST",
"/sensitive/image",
nil,
bytes.NewReader(body),
body,
checkResp,
)
return checkResp, resp, err
Expand Down

0 comments on commit 6978da8

Please sign in to comment.