From 2aade5d2fe6d694ca232ac676c9b857c86713136 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 18 Mar 2024 19:59:14 +0100 Subject: [PATCH 1/8] Docs example --- backend/docs/docs.go | 24 +++++++++++++++++++ .../handlers/handlers_user_related.go | 7 +++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/backend/docs/docs.go b/backend/docs/docs.go index 8026f58..75908b4 100644 --- a/backend/docs/docs.go +++ b/backend/docs/docs.go @@ -32,6 +32,17 @@ const docTemplate = `{ "User" ], "summary": "Register a new user", + "parameters": [ + { + "description": "User to register", + "name": "{string}", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/types.NewUser" + } + } + ], "responses": { "200": { "description": "User added", @@ -55,6 +66,19 @@ const docTemplate = `{ } } }, + "definitions": { + "types.NewUser": { + "type": "object", + "properties": { + "password": { + "type": "string" + }, + "username": { + "type": "string" + } + } + } + }, "externalDocs": { "description": "OpenAPI", "url": "https://swagger.io/resources/open-api/" diff --git a/backend/internal/handlers/handlers_user_related.go b/backend/internal/handlers/handlers_user_related.go index 0f7c047..e454f9f 100644 --- a/backend/internal/handlers/handlers_user_related.go +++ b/backend/internal/handlers/handlers_user_related.go @@ -16,9 +16,10 @@ import ( // @Tags User // @Accept json // @Produce json -// @Success 200 {string} string "User added" -// @Failure 400 {string} string "Bad request" -// @Failure 500 {string} string "Internal server error" +// @Param {string} body types.NewUser true "User to register" +// @Success 200 {string} string "User added" +// @Failure 400 {string} string "Bad request" +// @Failure 500 {string} string "Internal server error" // @Router /api/register [post] func (gs *GState) Register(c *fiber.Ctx) error { u := new(types.NewUser) From 2be4afd0e0892a35d4ec23e1f0aa2459fffe6418 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 18 Mar 2024 20:05:47 +0100 Subject: [PATCH 2/8] Correct ish swagger docstring --- backend/internal/handlers/handlers_user_related.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/internal/handlers/handlers_user_related.go b/backend/internal/handlers/handlers_user_related.go index e454f9f..9c98d4d 100644 --- a/backend/internal/handlers/handlers_user_related.go +++ b/backend/internal/handlers/handlers_user_related.go @@ -15,12 +15,12 @@ import ( // @Description Register a new user // @Tags User // @Accept json -// @Produce json -// @Param {string} body types.NewUser true "User to register" +// @Produce plain +// @Param NewUser body types.NewUser true "User to register" // @Success 200 {string} string "User added" // @Failure 400 {string} string "Bad request" // @Failure 500 {string} string "Internal server error" -// @Router /api/register [post] +// @Router /register [post] func (gs *GState) Register(c *fiber.Ctx) error { u := new(types.NewUser) if err := c.BodyParser(u); err != nil { From f3c5abf4f371ed4f763ccbe3b9684e60e2c69f4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20H=C3=B6gbom=20Aronson?= Date: Mon, 18 Mar 2024 22:40:51 +0100 Subject: [PATCH 3/8] added docs for loginrenew, login, regsiter --- backend/docs/docs.go | 134 +++++++++++++++++- .../handlers/handlers_user_related.go | 40 +++++- backend/internal/types/users.go | 5 + 3 files changed, 173 insertions(+), 6 deletions(-) diff --git a/backend/docs/docs.go b/backend/docs/docs.go index 75908b4..ac1589c 100644 --- a/backend/docs/docs.go +++ b/backend/docs/docs.go @@ -19,14 +19,101 @@ const docTemplate = `{ "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { - "/api/register": { + "/login": { + "post": { + "description": "logs the user in and returns a jwt token", + "consumes": [ + "application/json" + ], + "produces": [ + "text/plain" + ], + "tags": [ + "User" + ], + "summary": "login", + "parameters": [ + { + "description": "login info", + "name": "NewUser", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/types.NewUser" + } + } + ], + "responses": { + "200": { + "description": "Successfully signed token for user", + "schema": { + "type": "Token" + } + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "type": "string" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "type": "string" + } + } + } + } + }, + "/loginerenew": { + "post": { + "description": "renews the users token", + "consumes": [ + "application/json" + ], + "produces": [ + "text/plain" + ], + "tags": [ + "User" + ], + "summary": "renews the users token", + "responses": { + "200": { + "description": "Successfully signed token for user", + "schema": { + "type": "Token" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "type": "string" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "type": "string" + } + } + } + } + }, + "/register": { "post": { "description": "Register a new user", "consumes": [ "application/json" ], "produces": [ - "application/json" + "text/plain" ], "tags": [ "User" @@ -35,7 +122,7 @@ const docTemplate = `{ "parameters": [ { "description": "User to register", - "name": "{string}", + "name": "NewUser", "in": "body", "required": true, "schema": { @@ -64,6 +151,47 @@ const docTemplate = `{ } } } + }, + "/userdelete/{username}": { + "delete": { + "description": "UserDelete deletes a user from the database", + "consumes": [ + "application/json" + ], + "produces": [ + "text/plain" + ], + "tags": [ + "User" + ], + "summary": "Deletes a user", + "responses": { + "200": { + "description": "User deleted", + "schema": { + "type": "string" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "type": "string" + } + }, + "403": { + "description": "You can only delete yourself", + "schema": { + "type": "string" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "type": "string" + } + } + } + } } }, "definitions": { diff --git a/backend/internal/handlers/handlers_user_related.go b/backend/internal/handlers/handlers_user_related.go index 9c98d4d..c88d3d3 100644 --- a/backend/internal/handlers/handlers_user_related.go +++ b/backend/internal/handlers/handlers_user_related.go @@ -16,7 +16,7 @@ import ( // @Tags User // @Accept json // @Produce plain -// @Param NewUser body types.NewUser true "User to register" +// @Param NewUser body types.NewUser true "User to register" // @Success 200 {string} string "User added" // @Failure 400 {string} string "Bad request" // @Failure 500 {string} string "Internal server error" @@ -39,6 +39,17 @@ func (gs *GState) Register(c *fiber.Ctx) error { // This path should obviously be protected in the future // UserDelete deletes a user from the database +// +// @Summary Deletes a user +// @Description UserDelete deletes a user from the database +// @Tags User +// @Accept json +// @Produce plain +// @Success 200 {string} string "User deleted" +// @Failure 403 {string} string "You can only delete yourself" +// @Failure 500 {string} string "Internal server error" +// @Failure 401 {string} string "Unauthorized" +// @Router /userdelete/{username} [delete] func (gs *GState) UserDelete(c *fiber.Ctx) error { // Read from path parameters username := c.Params("username") @@ -58,8 +69,21 @@ func (gs *GState) UserDelete(c *fiber.Ctx) error { } // Login is a simple login handler that returns a JWT token +// +// @Summary login +// @Description logs the user in and returns a jwt token +// @Tags User +// @Accept json +// @Param NewUser body types.NewUser true "login info" +// @Produce plain +// @Success 200 Token types.Token "Successfully signed token for user" +// @Failure 400 {string} string "Bad request" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" +// @Router /login [post] func (gs *GState) Login(c *fiber.Ctx) error { // The body type is identical to a NewUser + u := new(types.NewUser) if err := c.BodyParser(u); err != nil { println("Error parsing body") @@ -91,10 +115,20 @@ func (gs *GState) Login(c *fiber.Ctx) error { } println("Successfully signed token for user:", u.Username) - return c.JSON(fiber.Map{"token": t}) + return c.JSON(types.Token{Token: t}) } // LoginRenew is a simple handler that renews the token +// +// @Summary renews the users token +// @Description renews the users token +// @Tags User +// @Accept json +// @Produce plain +// @Success 200 Token types.Token "Successfully signed token for user" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" +// @Router /loginerenew [post] func (gs *GState) LoginRenew(c *fiber.Ctx) error { // For testing: curl localhost:3000/restricted -H "Authorization: Bearer " user := c.Locals("user").(*jwt.Token) @@ -110,7 +144,7 @@ func (gs *GState) LoginRenew(c *fiber.Ctx) error { if err != nil { return c.SendStatus(fiber.StatusInternalServerError) } - return c.JSON(fiber.Map{"token": t}) + return c.JSON(types.Token{Token: t}) } // ListAllUsers is a handler that returns a list of all users in the application database diff --git a/backend/internal/types/users.go b/backend/internal/types/users.go index e9dff67..d3f2170 100644 --- a/backend/internal/types/users.go +++ b/backend/internal/types/users.go @@ -27,3 +27,8 @@ type PublicUser struct { UserId string `json:"userId"` Username string `json:"username"` } + +// wrapper type for token +type Token struct { + Token string `json:"token"` +} From ad85194d4f5622edf7edeecad133b959c585d164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20H=C3=B6gbom=20Aronson?= Date: Mon, 18 Mar 2024 23:21:49 +0100 Subject: [PATCH 4/8] finished docs for user reletaded stucts and added endpoint for listallusers --- backend/docs/docs.go | 87 +++++++++++++++++++ .../handlers/handlers_user_related.go | 53 +++++++---- 2 files changed, 124 insertions(+), 16 deletions(-) diff --git a/backend/docs/docs.go b/backend/docs/docs.go index ac1589c..1898804 100644 --- a/backend/docs/docs.go +++ b/backend/docs/docs.go @@ -106,6 +106,58 @@ const docTemplate = `{ } } }, + "/promoteToAdmin": { + "post": { + "description": "promote chosen user to admin", + "consumes": [ + "application/json" + ], + "produces": [ + "text/plain" + ], + "tags": [ + "User" + ], + "summary": "promote user to admin", + "parameters": [ + { + "description": "user info", + "name": "NewUser", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/types.NewUser" + } + } + ], + "responses": { + "200": { + "description": "Successfully prometed user", + "schema": { + "type": "json" + } + }, + "400": { + "description": "bad request", + "schema": { + "type": "string" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "type": "string" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "type": "string" + } + } + } + } + }, "/register": { "post": { "description": "Register a new user", @@ -192,6 +244,41 @@ const docTemplate = `{ } } } + }, + "/users/all": { + "get": { + "description": "lists all users", + "consumes": [ + "application/json" + ], + "produces": [ + "text/plain" + ], + "tags": [ + "User" + ], + "summary": "Lists users", + "responses": { + "200": { + "description": "Successfully signed token for user", + "schema": { + "type": "json" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "type": "string" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "type": "string" + } + } + } + } } }, "definitions": { diff --git a/backend/internal/handlers/handlers_user_related.go b/backend/internal/handlers/handlers_user_related.go index c88d3d3..f5a6f1b 100644 --- a/backend/internal/handlers/handlers_user_related.go +++ b/backend/internal/handlers/handlers_user_related.go @@ -16,10 +16,10 @@ import ( // @Tags User // @Accept json // @Produce plain -// @Param NewUser body types.NewUser true "User to register" -// @Success 200 {string} string "User added" -// @Failure 400 {string} string "Bad request" -// @Failure 500 {string} string "Internal server error" +// @Param NewUser body types.NewUser true "User to register" +// @Success 200 {string} string "User added" +// @Failure 400 {string} string "Bad request" +// @Failure 500 {string} string "Internal server error" // @Router /register [post] func (gs *GState) Register(c *fiber.Ctx) error { u := new(types.NewUser) @@ -45,10 +45,10 @@ func (gs *GState) Register(c *fiber.Ctx) error { // @Tags User // @Accept json // @Produce plain -// @Success 200 {string} string "User deleted" -// @Failure 403 {string} string "You can only delete yourself" -// @Failure 500 {string} string "Internal server error" -// @Failure 401 {string} string "Unauthorized" +// @Success 200 {string} string "User deleted" +// @Failure 403 {string} string "You can only delete yourself" +// @Failure 500 {string} string "Internal server error" +// @Failure 401 {string} string "Unauthorized" // @Router /userdelete/{username} [delete] func (gs *GState) UserDelete(c *fiber.Ctx) error { // Read from path parameters @@ -74,12 +74,12 @@ func (gs *GState) UserDelete(c *fiber.Ctx) error { // @Description logs the user in and returns a jwt token // @Tags User // @Accept json -// @Param NewUser body types.NewUser true "login info" +// @Param NewUser body types.NewUser true "login info" // @Produce plain -// @Success 200 Token types.Token "Successfully signed token for user" -// @Failure 400 {string} string "Bad request" -// @Failure 401 {string} string "Unauthorized" -// @Failure 500 {string} string "Internal server error" +// @Success 200 Token types.Token "Successfully signed token for user" +// @Failure 400 {string} string "Bad request" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" // @Router /login [post] func (gs *GState) Login(c *fiber.Ctx) error { // The body type is identical to a NewUser @@ -125,9 +125,9 @@ func (gs *GState) Login(c *fiber.Ctx) error { // @Tags User // @Accept json // @Produce plain -// @Success 200 Token types.Token "Successfully signed token for user" -// @Failure 401 {string} string "Unauthorized" -// @Failure 500 {string} string "Internal server error" +// @Success 200 Token types.Token "Successfully signed token for user" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" // @Router /loginerenew [post] func (gs *GState) LoginRenew(c *fiber.Ctx) error { // For testing: curl localhost:3000/restricted -H "Authorization: Bearer " @@ -148,6 +148,16 @@ func (gs *GState) LoginRenew(c *fiber.Ctx) error { } // ListAllUsers is a handler that returns a list of all users in the application database +// +// @Summary Lists users +// @Description lists all users +// @Tags User +// @Accept json +// @Produce plain +// @Success 200 {json} json "Successfully signed token for user" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" +// @Router /users/all [get] func (gs *GState) ListAllUsers(c *fiber.Ctx) error { // Get all users from the database users, err := gs.Db.GetAllUsersApplication() @@ -159,6 +169,17 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error { return c.JSON(users) } +// @Summary promote user to admin +// @Description promote chosen user to admin +// @Tags User +// @Accept json +// @Produce plain +// @Param NewUser body types.NewUser true "user info" +// @Success 200 {json} json "Successfully prometed user" +// @Failure 400 {string} string "bad request" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" +// @Router /promoteToAdmin [post] func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error { // Extract the username from the request body var newUser types.NewUser From 8711f9a20d93a22ec1d24913b3c59f0b5e518b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20H=C3=B6gbom=20Aronson?= Date: Tue, 19 Mar 2024 00:27:31 +0100 Subject: [PATCH 5/8] tyding up and tryinng to get tokens in docs to work --- backend/docs/docs.go | 22 ++++++++++++++----- .../handlers/handlers_user_related.go | 11 +++++----- backend/main.go | 6 ++++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/backend/docs/docs.go b/backend/docs/docs.go index 1898804..322c812 100644 --- a/backend/docs/docs.go +++ b/backend/docs/docs.go @@ -73,6 +73,11 @@ const docTemplate = `{ }, "/loginerenew": { "post": { + "security": [ + { + "bererToken": [] + } + ], "description": "renews the users token", "consumes": [ "application/json" @@ -83,7 +88,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "renews the users token", + "summary": "LoginRenews", "responses": { "200": { "description": "Successfully signed token for user", @@ -118,7 +123,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "promote user to admin", + "summary": "PromoteToAdmin", "parameters": [ { "description": "user info", @@ -170,7 +175,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "Register a new user", + "summary": "Register", "parameters": [ { "description": "User to register", @@ -216,7 +221,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "Deletes a user", + "summary": "UserDelete", "responses": { "200": { "description": "User deleted", @@ -257,7 +262,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "Lists users", + "summary": "ListsAllUsers", "responses": { "200": { "description": "Successfully signed token for user", @@ -294,6 +299,13 @@ const docTemplate = `{ } } }, + "securityDefinitions": { + "bererToken": { + "type": "apiKey", + "name": "Authorization", + "in": "header" + } + }, "externalDocs": { "description": "OpenAPI", "url": "https://swagger.io/resources/open-api/" diff --git a/backend/internal/handlers/handlers_user_related.go b/backend/internal/handlers/handlers_user_related.go index f5a6f1b..943a977 100644 --- a/backend/internal/handlers/handlers_user_related.go +++ b/backend/internal/handlers/handlers_user_related.go @@ -11,7 +11,7 @@ import ( // Register is a simple handler that registers a new user // -// @Summary Register a new user +// @Summary Register // @Description Register a new user // @Tags User // @Accept json @@ -40,7 +40,7 @@ func (gs *GState) Register(c *fiber.Ctx) error { // This path should obviously be protected in the future // UserDelete deletes a user from the database // -// @Summary Deletes a user +// @Summary UserDelete // @Description UserDelete deletes a user from the database // @Tags User // @Accept json @@ -120,8 +120,9 @@ func (gs *GState) Login(c *fiber.Ctx) error { // LoginRenew is a simple handler that renews the token // -// @Summary renews the users token +// @Summary LoginRenews // @Description renews the users token +// @Security bererToken // @Tags User // @Accept json // @Produce plain @@ -149,7 +150,7 @@ func (gs *GState) LoginRenew(c *fiber.Ctx) error { // ListAllUsers is a handler that returns a list of all users in the application database // -// @Summary Lists users +// @Summary ListsAllUsers // @Description lists all users // @Tags User // @Accept json @@ -169,7 +170,7 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error { return c.JSON(users) } -// @Summary promote user to admin +// @Summary PromoteToAdmin // @Description promote chosen user to admin // @Tags User // @Accept json diff --git a/backend/main.go b/backend/main.go index 3e2fb75..c19533e 100644 --- a/backend/main.go +++ b/backend/main.go @@ -22,6 +22,10 @@ import ( // @license.name AGPL // @license.url https://www.gnu.org/licenses/agpl-3.0.html +//@securityDefinitions.apikey bererToken +//@in header +//@name Authorization + // @host localhost:8080 // @BasePath /api @@ -83,7 +87,7 @@ func main() { server.Post("/api/signReport", gs.SignReport) server.Put("/api/addUserToProject", gs.AddUserToProjectHandler) server.Post("/api/promoteToAdmin", gs.PromoteToAdmin) - + server.Get("/api/users/all", gs.ListAllUsers) // Announce the port we are listening on and start the server err = server.Listen(fmt.Sprintf(":%d", conf.Port)) if err != nil { From 3e35586bbea83e73c83a61c1ff6a10a1014f16d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20H=C3=B6gbom=20Aronson?= Date: Mon, 18 Mar 2024 23:21:49 +0100 Subject: [PATCH 6/8] finished docs for user reletaded stucts and added endpoint for listallusers --- backend/docs/docs.go | 87 +++++++++++++++++++ .../handlers/handlers_user_related.go | 53 +++++++---- 2 files changed, 124 insertions(+), 16 deletions(-) diff --git a/backend/docs/docs.go b/backend/docs/docs.go index ac1589c..1898804 100644 --- a/backend/docs/docs.go +++ b/backend/docs/docs.go @@ -106,6 +106,58 @@ const docTemplate = `{ } } }, + "/promoteToAdmin": { + "post": { + "description": "promote chosen user to admin", + "consumes": [ + "application/json" + ], + "produces": [ + "text/plain" + ], + "tags": [ + "User" + ], + "summary": "promote user to admin", + "parameters": [ + { + "description": "user info", + "name": "NewUser", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/types.NewUser" + } + } + ], + "responses": { + "200": { + "description": "Successfully prometed user", + "schema": { + "type": "json" + } + }, + "400": { + "description": "bad request", + "schema": { + "type": "string" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "type": "string" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "type": "string" + } + } + } + } + }, "/register": { "post": { "description": "Register a new user", @@ -192,6 +244,41 @@ const docTemplate = `{ } } } + }, + "/users/all": { + "get": { + "description": "lists all users", + "consumes": [ + "application/json" + ], + "produces": [ + "text/plain" + ], + "tags": [ + "User" + ], + "summary": "Lists users", + "responses": { + "200": { + "description": "Successfully signed token for user", + "schema": { + "type": "json" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "type": "string" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "type": "string" + } + } + } + } } }, "definitions": { diff --git a/backend/internal/handlers/handlers_user_related.go b/backend/internal/handlers/handlers_user_related.go index 1d94270..1ed6bd4 100644 --- a/backend/internal/handlers/handlers_user_related.go +++ b/backend/internal/handlers/handlers_user_related.go @@ -17,10 +17,10 @@ import ( // @Tags User // @Accept json // @Produce plain -// @Param NewUser body types.NewUser true "User to register" -// @Success 200 {string} string "User added" -// @Failure 400 {string} string "Bad request" -// @Failure 500 {string} string "Internal server error" +// @Param NewUser body types.NewUser true "User to register" +// @Success 200 {string} string "User added" +// @Failure 400 {string} string "Bad request" +// @Failure 500 {string} string "Internal server error" // @Router /register [post] func (gs *GState) Register(c *fiber.Ctx) error { u := new(types.NewUser) @@ -47,10 +47,10 @@ func (gs *GState) Register(c *fiber.Ctx) error { // @Tags User // @Accept json // @Produce plain -// @Success 200 {string} string "User deleted" -// @Failure 403 {string} string "You can only delete yourself" -// @Failure 500 {string} string "Internal server error" -// @Failure 401 {string} string "Unauthorized" +// @Success 200 {string} string "User deleted" +// @Failure 403 {string} string "You can only delete yourself" +// @Failure 500 {string} string "Internal server error" +// @Failure 401 {string} string "Unauthorized" // @Router /userdelete/{username} [delete] func (gs *GState) UserDelete(c *fiber.Ctx) error { // Read from path parameters @@ -79,12 +79,12 @@ func (gs *GState) UserDelete(c *fiber.Ctx) error { // @Description logs the user in and returns a jwt token // @Tags User // @Accept json -// @Param NewUser body types.NewUser true "login info" +// @Param NewUser body types.NewUser true "login info" // @Produce plain -// @Success 200 Token types.Token "Successfully signed token for user" -// @Failure 400 {string} string "Bad request" -// @Failure 401 {string} string "Unauthorized" -// @Failure 500 {string} string "Internal server error" +// @Success 200 Token types.Token "Successfully signed token for user" +// @Failure 400 {string} string "Bad request" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" // @Router /login [post] func (gs *GState) Login(c *fiber.Ctx) error { // The body type is identical to a NewUser @@ -130,9 +130,9 @@ func (gs *GState) Login(c *fiber.Ctx) error { // @Tags User // @Accept json // @Produce plain -// @Success 200 Token types.Token "Successfully signed token for user" -// @Failure 401 {string} string "Unauthorized" -// @Failure 500 {string} string "Internal server error" +// @Success 200 Token types.Token "Successfully signed token for user" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" // @Router /loginerenew [post] func (gs *GState) LoginRenew(c *fiber.Ctx) error { user := c.Locals("user").(*jwt.Token) @@ -158,6 +158,16 @@ func (gs *GState) LoginRenew(c *fiber.Ctx) error { } // ListAllUsers is a handler that returns a list of all users in the application database +// +// @Summary Lists users +// @Description lists all users +// @Tags User +// @Accept json +// @Produce plain +// @Success 200 {json} json "Successfully signed token for user" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" +// @Router /users/all [get] func (gs *GState) ListAllUsers(c *fiber.Ctx) error { // Get all users from the database users, err := gs.Db.GetAllUsersApplication() @@ -171,6 +181,17 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error { return c.JSON(users) } +// @Summary promote user to admin +// @Description promote chosen user to admin +// @Tags User +// @Accept json +// @Produce plain +// @Param NewUser body types.NewUser true "user info" +// @Success 200 {json} json "Successfully prometed user" +// @Failure 400 {string} string "bad request" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" +// @Router /promoteToAdmin [post] func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error { // Extract the username from the request body var newUser types.NewUser From 9ce70e74e922279f4881407b49b096ba5a4681fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20H=C3=B6gbom=20Aronson?= Date: Tue, 19 Mar 2024 00:27:31 +0100 Subject: [PATCH 7/8] tyding up and tryinng to get tokens in docs to work --- backend/docs/docs.go | 22 ++++++++++++++----- .../handlers/handlers_user_related.go | 11 +++++----- backend/main.go | 6 ++++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/backend/docs/docs.go b/backend/docs/docs.go index 1898804..322c812 100644 --- a/backend/docs/docs.go +++ b/backend/docs/docs.go @@ -73,6 +73,11 @@ const docTemplate = `{ }, "/loginerenew": { "post": { + "security": [ + { + "bererToken": [] + } + ], "description": "renews the users token", "consumes": [ "application/json" @@ -83,7 +88,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "renews the users token", + "summary": "LoginRenews", "responses": { "200": { "description": "Successfully signed token for user", @@ -118,7 +123,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "promote user to admin", + "summary": "PromoteToAdmin", "parameters": [ { "description": "user info", @@ -170,7 +175,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "Register a new user", + "summary": "Register", "parameters": [ { "description": "User to register", @@ -216,7 +221,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "Deletes a user", + "summary": "UserDelete", "responses": { "200": { "description": "User deleted", @@ -257,7 +262,7 @@ const docTemplate = `{ "tags": [ "User" ], - "summary": "Lists users", + "summary": "ListsAllUsers", "responses": { "200": { "description": "Successfully signed token for user", @@ -294,6 +299,13 @@ const docTemplate = `{ } } }, + "securityDefinitions": { + "bererToken": { + "type": "apiKey", + "name": "Authorization", + "in": "header" + } + }, "externalDocs": { "description": "OpenAPI", "url": "https://swagger.io/resources/open-api/" diff --git a/backend/internal/handlers/handlers_user_related.go b/backend/internal/handlers/handlers_user_related.go index 1ed6bd4..96fddb7 100644 --- a/backend/internal/handlers/handlers_user_related.go +++ b/backend/internal/handlers/handlers_user_related.go @@ -12,7 +12,7 @@ import ( // Register is a simple handler that registers a new user // -// @Summary Register a new user +// @Summary Register // @Description Register a new user // @Tags User // @Accept json @@ -42,7 +42,7 @@ func (gs *GState) Register(c *fiber.Ctx) error { // This path should obviously be protected in the future // UserDelete deletes a user from the database // -// @Summary Deletes a user +// @Summary UserDelete // @Description UserDelete deletes a user from the database // @Tags User // @Accept json @@ -125,8 +125,9 @@ func (gs *GState) Login(c *fiber.Ctx) error { // LoginRenew is a simple handler that renews the token // -// @Summary renews the users token +// @Summary LoginRenews // @Description renews the users token +// @Security bererToken // @Tags User // @Accept json // @Produce plain @@ -159,7 +160,7 @@ func (gs *GState) LoginRenew(c *fiber.Ctx) error { // ListAllUsers is a handler that returns a list of all users in the application database // -// @Summary Lists users +// @Summary ListsAllUsers // @Description lists all users // @Tags User // @Accept json @@ -181,7 +182,7 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error { return c.JSON(users) } -// @Summary promote user to admin +// @Summary PromoteToAdmin // @Description promote chosen user to admin // @Tags User // @Accept json diff --git a/backend/main.go b/backend/main.go index 9abe995..76c5f99 100644 --- a/backend/main.go +++ b/backend/main.go @@ -23,6 +23,10 @@ import ( // @license.name AGPL // @license.url https://www.gnu.org/licenses/agpl-3.0.html +//@securityDefinitions.apikey bererToken +//@in header +//@name Authorization + // @host localhost:8080 // @BasePath /api @@ -89,7 +93,7 @@ func main() { server.Post("/api/signReport", gs.SignReport) server.Put("/api/addUserToProject", gs.AddUserToProjectHandler) server.Post("/api/promoteToAdmin", gs.PromoteToAdmin) - + server.Get("/api/users/all", gs.ListAllUsers) // Announce the port we are listening on and start the server err = server.Listen(fmt.Sprintf(":%d", conf.Port)) if err != nil { From b174ec8922aa7761ba5095f64ea2752aa3f10d06 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 19 Mar 2024 00:41:30 +0100 Subject: [PATCH 8/8] Rename for clarity and consistensy with TS api --- backend/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/main.go b/backend/main.go index 76c5f99..e578c52 100644 --- a/backend/main.go +++ b/backend/main.go @@ -83,7 +83,7 @@ func main() { })) // Protected routes (require a valid JWT bearer token authentication header) - server.Post("/api/submitReport", gs.SubmitWeeklyReport) + server.Post("/api/submitWeeklyReport", gs.SubmitWeeklyReport) server.Get("/api/getUserProjects", gs.GetUserProjects) server.Post("/api/loginrenew", gs.LoginRenew) server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches