@@ -10,9 +10,31 @@ import (
1010 api_helpers "ctf01d/internal/app/utils"
1111 "ctf01d/internal/app/view"
1212
13+ "github.com/google/uuid"
1314 openapi_types "github.com/oapi-codegen/runtime/types"
1415)
1516
17+ type SessionHandler struct {
18+ * Handlers
19+ SessionCache * SessionCache
20+ }
21+
22+ func (sc * SessionCache ) GetSession (sessionID string ) (openapi_types.UUID , bool ) {
23+ val , ok := sc .cache .Load (sessionID )
24+ if ! ok {
25+ return uuid .Nil , false
26+ }
27+ return val .(openapi_types.UUID ), true
28+ }
29+
30+ func (sc * SessionCache ) SetSession (sessionID string , userID uuid.UUID ) {
31+ sc .cache .Store (sessionID , userID )
32+ }
33+
34+ func (sc * SessionCache ) DeleteSession (sessionID string ) {
35+ sc .cache .Delete (sessionID )
36+ }
37+
1638func (h * Handlers ) PostApiV1AuthSignIn (w http.ResponseWriter , r * http.Request ) {
1739 var req server.PostApiV1AuthSignInJSONBody
1840 if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
@@ -72,13 +94,20 @@ func (h *Handlers) PostApiV1AuthSignOut(w http.ResponseWriter, r *http.Request)
7294 api_helpers .RespondWithJSON (w , http .StatusOK , map [string ]string {"data" : "User logout successful" })
7395}
7496
75- func (h * Handlers ) ValidateSession (w http.ResponseWriter , r * http.Request ) {
97+ func (h * SessionHandler ) ValidateSession (w http.ResponseWriter , r * http.Request ) {
7698 cookie , err := r .Cookie ("session_id" )
7799 if err != nil {
78100 slog .Warn (err .Error (), "handler" , "ValidateSession" )
79101 api_helpers .RespondWithJSON (w , http .StatusUnauthorized , map [string ]string {"error" : "No session found" })
80102 return
81103 }
104+
105+ if userId , ok := h .SessionCache .GetSession (cookie .Value ); ok {
106+ slog .Debug ("ValidateSession user.Id " + openapi_types .UUID (userId ).String ())
107+ h .respondWithUserDetails (w , r , userId )
108+ return
109+ }
110+
82111 slog .Debug ("cookie.Value, " + cookie .Value )
83112 repo := repository .NewSessionRepository (h .DB )
84113 var userId openapi_types.UUID
@@ -88,12 +117,17 @@ func (h *Handlers) ValidateSession(w http.ResponseWriter, r *http.Request) {
88117 api_helpers .RespondWithJSON (w , http .StatusUnauthorized , map [string ]string {"error" : "No user or session found" })
89118 return
90119 }
120+
121+ h .SessionCache .SetSession (cookie .Value , userId )
91122 slog .Debug ("ValidateSession user.Id " + openapi_types .UUID (userId ).String ())
123+ h .respondWithUserDetails (w , r , userId )
124+ }
92125
126+ func (h * SessionHandler ) respondWithUserDetails (w http.ResponseWriter , r * http.Request , userId openapi_types.UUID ) {
93127 userRepo := repository .NewUserRepository (h .DB )
94128 user , err := userRepo .GetById (r .Context (), userId )
95129 if err != nil {
96- slog .Warn (err .Error (), "handler" , "ValidateSession " )
130+ slog .Warn (err .Error (), "handler" , "respondWithUserDetails " )
97131 api_helpers .RespondWithJSON (w , http .StatusInternalServerError , map [string ]string {"error" : "Could not find user by user id" })
98132 return
99133 }
0 commit comments