diff --git a/api/openapi.yaml b/api/openapi.yaml index 2fce11b..e38eaa8 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -12,7 +12,7 @@ servers: - url: 'http://localhost:4102' description: Local server paths: - /api/login: + /api/v1/auth/signin: post: tags: - Sessions @@ -60,7 +60,7 @@ paths: '500': description: Internal Server Error parameters: [] - /api/logout: + /api/v1/auth/signout: post: tags: - Sessions diff --git a/internal/app/handlers/interface.go b/internal/app/handlers/interface.go index b9ab26d..1417d77 100644 --- a/internal/app/handlers/interface.go +++ b/internal/app/handlers/interface.go @@ -34,12 +34,12 @@ func (siw *ServerInterfaceWrapper) UpdateGame(w http.ResponseWriter, r *http.Req siw.handlers.UpdateGame(w, r, id) } -func (siw *ServerInterfaceWrapper) PostApiLogin(w http.ResponseWriter, r *http.Request) { - siw.handlers.PostApiLogin(w, r) +func (siw *ServerInterfaceWrapper) PostApiV1AuthSignin(w http.ResponseWriter, r *http.Request) { + siw.handlers.PostApiV1AuthSignin(w, r) } -func (siw *ServerInterfaceWrapper) PostApiLogout(w http.ResponseWriter, r *http.Request) { - siw.handlers.PostApiLogout(w, r) +func (siw *ServerInterfaceWrapper) PostApiV1AuthSignout(w http.ResponseWriter, r *http.Request) { + siw.handlers.PostApiV1AuthSignout(w, r) } func (siw *ServerInterfaceWrapper) ListResults(w http.ResponseWriter, r *http.Request) { diff --git a/internal/app/handlers/sessions.go b/internal/app/handlers/sessions.go index 16971c5..f2a8375 100644 --- a/internal/app/handlers/sessions.go +++ b/internal/app/handlers/sessions.go @@ -10,8 +10,8 @@ import ( api_helpers "ctf01d/internal/app/utils" ) -func (h *Handlers) PostApiLogin(w http.ResponseWriter, r *http.Request) { - var req server.PostApiLoginJSONBody +func (h *Handlers) PostApiV1AuthSignin(w http.ResponseWriter, r *http.Request) { + var req server.PostApiV1AuthSigninJSONBody if err := json.NewDecoder(r.Body).Decode(&req); err != nil { slog.Warn(err.Error(), "handler", "LoginSessionHandler") http.Error(w, "Invalid request body", http.StatusBadRequest) @@ -44,7 +44,7 @@ func (h *Handlers) PostApiLogin(w http.ResponseWriter, r *http.Request) { api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "User logged in"}) } -func (h *Handlers) PostApiLogout(w http.ResponseWriter, r *http.Request) { +func (h *Handlers) PostApiV1AuthSignout(w http.ResponseWriter, r *http.Request) { cookie, err := r.Cookie("session_id") if err != nil { slog.Warn(err.Error(), "handler", "LogoutSessionHandler") diff --git a/internal/app/server/server.gen.go b/internal/app/server/server.gen.go index 8b66753..28964b6 100644 --- a/internal/app/server/server.gen.go +++ b/internal/app/server/server.gen.go @@ -219,27 +219,24 @@ type UserResponse struct { // UserResponseRole The role of the user (admin, player or guest) type UserResponseRole string -// PostApiLoginJSONBody defines parameters for PostApiLogin. -type PostApiLoginJSONBody struct { - Password *string `json:"password,omitempty"` - UserName *string `json:"user_name,omitempty"` -} - // GetApiUniversitiesParams defines parameters for GetApiUniversities. type GetApiUniversitiesParams struct { // Term Optional search term to filter universities by name. Term *string `form:"term,omitempty" json:"term,omitempty"` } +// PostApiV1AuthSigninJSONBody defines parameters for PostApiV1AuthSignin. +type PostApiV1AuthSigninJSONBody struct { + Password *string `json:"password,omitempty"` + UserName *string `json:"user_name,omitempty"` +} + // CreateGameJSONRequestBody defines body for CreateGame for application/json ContentType. type CreateGameJSONRequestBody = GameRequest // UpdateGameJSONRequestBody defines body for UpdateGame for application/json ContentType. type UpdateGameJSONRequestBody = GameRequest -// PostApiLoginJSONRequestBody defines body for PostApiLogin for application/json ContentType. -type PostApiLoginJSONRequestBody PostApiLoginJSONBody - // CreateResultJSONRequestBody defines body for CreateResult for application/json ContentType. type CreateResultJSONRequestBody = ResultRequest @@ -261,6 +258,9 @@ type CreateUserJSONRequestBody = UserRequest // UpdateUserJSONRequestBody defines body for UpdateUser for application/json ContentType. type UpdateUserJSONRequestBody = UserRequest +// PostApiV1AuthSigninJSONRequestBody defines body for PostApiV1AuthSignin for application/json ContentType. +type PostApiV1AuthSigninJSONRequestBody PostApiV1AuthSigninJSONBody + // ServerInterface represents all server handlers. type ServerInterface interface { // List all games @@ -278,12 +278,6 @@ type ServerInterface interface { // Update a game // (PUT /api/games/{id}) UpdateGame(w http.ResponseWriter, r *http.Request, id int) - // Login user - // (POST /api/login) - PostApiLogin(w http.ResponseWriter, r *http.Request) - // Logout user - // (POST /api/logout) - PostApiLogout(w http.ResponseWriter, r *http.Request) // List all results // (GET /api/results) ListResults(w http.ResponseWriter, r *http.Request) @@ -341,6 +335,12 @@ type ServerInterface interface { // Update a user // (PUT /api/users/{id}) UpdateUser(w http.ResponseWriter, r *http.Request, id int) + // Login user + // (POST /api/v1/auth/signin) + PostApiV1AuthSignin(w http.ResponseWriter, r *http.Request) + // Logout user + // (POST /api/v1/auth/signout) + PostApiV1AuthSignout(w http.ResponseWriter, r *http.Request) } // Unimplemented server implementation that returns http.StatusNotImplemented for each endpoint. @@ -377,18 +377,6 @@ func (_ Unimplemented) UpdateGame(w http.ResponseWriter, r *http.Request, id int w.WriteHeader(http.StatusNotImplemented) } -// Login user -// (POST /api/login) -func (_ Unimplemented) PostApiLogin(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusNotImplemented) -} - -// Logout user -// (POST /api/logout) -func (_ Unimplemented) PostApiLogout(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusNotImplemented) -} - // List all results // (GET /api/results) func (_ Unimplemented) ListResults(w http.ResponseWriter, r *http.Request) { @@ -503,6 +491,18 @@ func (_ Unimplemented) UpdateUser(w http.ResponseWriter, r *http.Request, id int w.WriteHeader(http.StatusNotImplemented) } +// Login user +// (POST /api/v1/auth/signin) +func (_ Unimplemented) PostApiV1AuthSignin(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotImplemented) +} + +// Logout user +// (POST /api/v1/auth/signout) +func (_ Unimplemented) PostApiV1AuthSignout(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotImplemented) +} + // ServerInterfaceWrapper converts contexts to parameters. type ServerInterfaceWrapper struct { Handler ServerInterface @@ -620,36 +620,6 @@ func (siw *ServerInterfaceWrapper) UpdateGame(w http.ResponseWriter, r *http.Req handler.ServeHTTP(w, r.WithContext(ctx)) } -// PostApiLogin operation middleware -func (siw *ServerInterfaceWrapper) PostApiLogin(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - - handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - siw.Handler.PostApiLogin(w, r) - })) - - for _, middleware := range siw.HandlerMiddlewares { - handler = middleware(handler) - } - - handler.ServeHTTP(w, r.WithContext(ctx)) -} - -// PostApiLogout operation middleware -func (siw *ServerInterfaceWrapper) PostApiLogout(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - - handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - siw.Handler.PostApiLogout(w, r) - })) - - for _, middleware := range siw.HandlerMiddlewares { - handler = middleware(handler) - } - - handler.ServeHTTP(w, r.WithContext(ctx)) -} - // ListResults operation middleware func (siw *ServerInterfaceWrapper) ListResults(w http.ResponseWriter, r *http.Request) { ctx := r.Context() @@ -1058,6 +1028,36 @@ func (siw *ServerInterfaceWrapper) UpdateUser(w http.ResponseWriter, r *http.Req handler.ServeHTTP(w, r.WithContext(ctx)) } +// PostApiV1AuthSignin operation middleware +func (siw *ServerInterfaceWrapper) PostApiV1AuthSignin(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + siw.Handler.PostApiV1AuthSignin(w, r) + })) + + for _, middleware := range siw.HandlerMiddlewares { + handler = middleware(handler) + } + + handler.ServeHTTP(w, r.WithContext(ctx)) +} + +// PostApiV1AuthSignout operation middleware +func (siw *ServerInterfaceWrapper) PostApiV1AuthSignout(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + siw.Handler.PostApiV1AuthSignout(w, r) + })) + + for _, middleware := range siw.HandlerMiddlewares { + handler = middleware(handler) + } + + handler.ServeHTTP(w, r.WithContext(ctx)) +} + type UnescapedCookieParamError struct { ParamName string Err error @@ -1186,12 +1186,6 @@ func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handl r.Group(func(r chi.Router) { r.Put(options.BaseURL+"/api/games/{id}", wrapper.UpdateGame) }) - r.Group(func(r chi.Router) { - r.Post(options.BaseURL+"/api/login", wrapper.PostApiLogin) - }) - r.Group(func(r chi.Router) { - r.Post(options.BaseURL+"/api/logout", wrapper.PostApiLogout) - }) r.Group(func(r chi.Router) { r.Get(options.BaseURL+"/api/results", wrapper.ListResults) }) @@ -1249,6 +1243,12 @@ func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handl r.Group(func(r chi.Router) { r.Put(options.BaseURL+"/api/users/{id}", wrapper.UpdateUser) }) + r.Group(func(r chi.Router) { + r.Post(options.BaseURL+"/api/v1/auth/signin", wrapper.PostApiV1AuthSignin) + }) + r.Group(func(r chi.Router) { + r.Post(options.BaseURL+"/api/v1/auth/signout", wrapper.PostApiV1AuthSignout) + }) return r }