Compare commits
10 commits
5bcca0202b
...
5f88e92415
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5f88e92415 | ||
![]() |
09014c6659 | ||
![]() |
e498f0ed63 | ||
![]() |
c03be8c5d9 | ||
![]() |
b174ec8922 | ||
![]() |
2cfbcd15a7 | ||
![]() |
9ce70e74e9 | ||
![]() |
3e35586bbe | ||
![]() |
8711f9a20d | ||
![]() |
ad85194d4f |
4 changed files with 150 additions and 25 deletions
|
@ -73,6 +73,11 @@ const docTemplate = `{
|
||||||
},
|
},
|
||||||
"/loginerenew": {
|
"/loginerenew": {
|
||||||
"post": {
|
"post": {
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"bererToken": []
|
||||||
|
}
|
||||||
|
],
|
||||||
"description": "renews the users token",
|
"description": "renews the users token",
|
||||||
"consumes": [
|
"consumes": [
|
||||||
"application/json"
|
"application/json"
|
||||||
|
@ -83,7 +88,7 @@ const docTemplate = `{
|
||||||
"tags": [
|
"tags": [
|
||||||
"User"
|
"User"
|
||||||
],
|
],
|
||||||
"summary": "renews the users token",
|
"summary": "LoginRenews",
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "Successfully signed token for user",
|
"description": "Successfully signed token for user",
|
||||||
|
@ -106,6 +111,58 @@ const docTemplate = `{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/promoteToAdmin": {
|
||||||
|
"post": {
|
||||||
|
"description": "promote chosen user to admin",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"text/plain"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"User"
|
||||||
|
],
|
||||||
|
"summary": "PromoteToAdmin",
|
||||||
|
"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": {
|
"/register": {
|
||||||
"post": {
|
"post": {
|
||||||
"description": "Register a new user",
|
"description": "Register a new user",
|
||||||
|
@ -118,7 +175,7 @@ const docTemplate = `{
|
||||||
"tags": [
|
"tags": [
|
||||||
"User"
|
"User"
|
||||||
],
|
],
|
||||||
"summary": "Register a new user",
|
"summary": "Register",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"description": "User to register",
|
"description": "User to register",
|
||||||
|
@ -164,7 +221,7 @@ const docTemplate = `{
|
||||||
"tags": [
|
"tags": [
|
||||||
"User"
|
"User"
|
||||||
],
|
],
|
||||||
"summary": "Deletes a user",
|
"summary": "UserDelete",
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "User deleted",
|
"description": "User deleted",
|
||||||
|
@ -192,6 +249,41 @@ const docTemplate = `{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"/users/all": {
|
||||||
|
"get": {
|
||||||
|
"description": "lists all users",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"text/plain"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"User"
|
||||||
|
],
|
||||||
|
"summary": "ListsAllUsers",
|
||||||
|
"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": {
|
"definitions": {
|
||||||
|
@ -207,6 +299,13 @@ const docTemplate = `{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"securityDefinitions": {
|
||||||
|
"bererToken": {
|
||||||
|
"type": "apiKey",
|
||||||
|
"name": "Authorization",
|
||||||
|
"in": "header"
|
||||||
|
}
|
||||||
|
},
|
||||||
"externalDocs": {
|
"externalDocs": {
|
||||||
"description": "OpenAPI",
|
"description": "OpenAPI",
|
||||||
"url": "https://swagger.io/resources/open-api/"
|
"url": "https://swagger.io/resources/open-api/"
|
||||||
|
|
|
@ -12,15 +12,15 @@ import (
|
||||||
|
|
||||||
// Register is a simple handler that registers a new user
|
// Register is a simple handler that registers a new user
|
||||||
//
|
//
|
||||||
// @Summary Register a new user
|
// @Summary Register
|
||||||
// @Description Register a new user
|
// @Description Register a new user
|
||||||
// @Tags User
|
// @Tags User
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce plain
|
// @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"
|
// @Success 200 {string} string "User added"
|
||||||
// @Failure 400 {string} string "Bad request"
|
// @Failure 400 {string} string "Bad request"
|
||||||
// @Failure 500 {string} string "Internal server error"
|
// @Failure 500 {string} string "Internal server error"
|
||||||
// @Router /register [post]
|
// @Router /register [post]
|
||||||
func (gs *GState) Register(c *fiber.Ctx) error {
|
func (gs *GState) Register(c *fiber.Ctx) error {
|
||||||
u := new(types.NewUser)
|
u := new(types.NewUser)
|
||||||
|
@ -42,15 +42,15 @@ func (gs *GState) Register(c *fiber.Ctx) error {
|
||||||
// This path should obviously be protected in the future
|
// This path should obviously be protected in the future
|
||||||
// UserDelete deletes a user from the database
|
// UserDelete deletes a user from the database
|
||||||
//
|
//
|
||||||
// @Summary Deletes a user
|
// @Summary UserDelete
|
||||||
// @Description UserDelete deletes a user from the database
|
// @Description UserDelete deletes a user from the database
|
||||||
// @Tags User
|
// @Tags User
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce plain
|
// @Produce plain
|
||||||
// @Success 200 {string} string "User deleted"
|
// @Success 200 {string} string "User deleted"
|
||||||
// @Failure 403 {string} string "You can only delete yourself"
|
// @Failure 403 {string} string "You can only delete yourself"
|
||||||
// @Failure 500 {string} string "Internal server error"
|
// @Failure 500 {string} string "Internal server error"
|
||||||
// @Failure 401 {string} string "Unauthorized"
|
// @Failure 401 {string} string "Unauthorized"
|
||||||
// @Router /userdelete/{username} [delete]
|
// @Router /userdelete/{username} [delete]
|
||||||
func (gs *GState) UserDelete(c *fiber.Ctx) error {
|
func (gs *GState) UserDelete(c *fiber.Ctx) error {
|
||||||
// Read from path parameters
|
// 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
|
// @Description logs the user in and returns a jwt token
|
||||||
// @Tags User
|
// @Tags User
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Param NewUser body types.NewUser true "login info"
|
// @Param NewUser body types.NewUser true "login info"
|
||||||
// @Produce plain
|
// @Produce plain
|
||||||
// @Success 200 Token types.Token "Successfully signed token for user"
|
// @Success 200 Token types.Token "Successfully signed token for user"
|
||||||
// @Failure 400 {string} string "Bad request"
|
// @Failure 400 {string} string "Bad request"
|
||||||
// @Failure 401 {string} string "Unauthorized"
|
// @Failure 401 {string} string "Unauthorized"
|
||||||
// @Failure 500 {string} string "Internal server error"
|
// @Failure 500 {string} string "Internal server error"
|
||||||
// @Router /login [post]
|
// @Router /login [post]
|
||||||
func (gs *GState) Login(c *fiber.Ctx) error {
|
func (gs *GState) Login(c *fiber.Ctx) error {
|
||||||
// The body type is identical to a NewUser
|
// The body type is identical to a NewUser
|
||||||
|
@ -125,14 +125,15 @@ func (gs *GState) Login(c *fiber.Ctx) error {
|
||||||
|
|
||||||
// LoginRenew is a simple handler that renews the token
|
// LoginRenew is a simple handler that renews the token
|
||||||
//
|
//
|
||||||
// @Summary renews the users token
|
// @Summary LoginRenews
|
||||||
// @Description renews the users token
|
// @Description renews the users token
|
||||||
|
// @Security bererToken
|
||||||
// @Tags User
|
// @Tags User
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce plain
|
// @Produce plain
|
||||||
// @Success 200 Token types.Token "Successfully signed token for user"
|
// @Success 200 Token types.Token "Successfully signed token for user"
|
||||||
// @Failure 401 {string} string "Unauthorized"
|
// @Failure 401 {string} string "Unauthorized"
|
||||||
// @Failure 500 {string} string "Internal server error"
|
// @Failure 500 {string} string "Internal server error"
|
||||||
// @Router /loginerenew [post]
|
// @Router /loginerenew [post]
|
||||||
func (gs *GState) LoginRenew(c *fiber.Ctx) error {
|
func (gs *GState) LoginRenew(c *fiber.Ctx) error {
|
||||||
user := c.Locals("user").(*jwt.Token)
|
user := c.Locals("user").(*jwt.Token)
|
||||||
|
@ -158,6 +159,16 @@ func (gs *GState) LoginRenew(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListAllUsers is a handler that returns a list of all users in the application database
|
// ListAllUsers is a handler that returns a list of all users in the application database
|
||||||
|
//
|
||||||
|
// @Summary ListsAllUsers
|
||||||
|
// @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 {
|
func (gs *GState) ListAllUsers(c *fiber.Ctx) error {
|
||||||
// Get all users from the database
|
// Get all users from the database
|
||||||
users, err := gs.Db.GetAllUsersApplication()
|
users, err := gs.Db.GetAllUsersApplication()
|
||||||
|
@ -171,6 +182,17 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error {
|
||||||
return c.JSON(users)
|
return c.JSON(users)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @Summary PromoteToAdmin
|
||||||
|
// @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 {
|
func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error {
|
||||||
// Extract the username from the request body
|
// Extract the username from the request body
|
||||||
var newUser types.NewUser
|
var newUser types.NewUser
|
||||||
|
|
|
@ -23,6 +23,10 @@ import (
|
||||||
// @license.name AGPL
|
// @license.name AGPL
|
||||||
// @license.url https://www.gnu.org/licenses/agpl-3.0.html
|
// @license.url https://www.gnu.org/licenses/agpl-3.0.html
|
||||||
|
|
||||||
|
//@securityDefinitions.apikey bererToken
|
||||||
|
//@in header
|
||||||
|
//@name Authorization
|
||||||
|
|
||||||
// @host localhost:8080
|
// @host localhost:8080
|
||||||
// @BasePath /api
|
// @BasePath /api
|
||||||
|
|
||||||
|
@ -79,7 +83,7 @@ func main() {
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Protected routes (require a valid JWT bearer token authentication header)
|
// 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.Get("/api/getUserProjects", gs.GetUserProjects)
|
||||||
server.Post("/api/loginrenew", gs.LoginRenew)
|
server.Post("/api/loginrenew", gs.LoginRenew)
|
||||||
server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches
|
server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches
|
||||||
|
@ -89,7 +93,7 @@ func main() {
|
||||||
server.Post("/api/signReport", gs.SignReport)
|
server.Post("/api/signReport", gs.SignReport)
|
||||||
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
|
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
|
||||||
server.Post("/api/promoteToAdmin", gs.PromoteToAdmin)
|
server.Post("/api/promoteToAdmin", gs.PromoteToAdmin)
|
||||||
|
server.Get("/api/users/all", gs.ListAllUsers)
|
||||||
// Announce the port we are listening on and start the server
|
// Announce the port we are listening on and start the server
|
||||||
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
|
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -30,7 +30,7 @@ base_url = "http://localhost:8080"
|
||||||
registerPath = base_url + "/api/register"
|
registerPath = base_url + "/api/register"
|
||||||
loginPath = base_url + "/api/login"
|
loginPath = base_url + "/api/login"
|
||||||
addProjectPath = base_url + "/api/project"
|
addProjectPath = base_url + "/api/project"
|
||||||
submitReportPath = base_url + "/api/submitReport"
|
submitReportPath = base_url + "/api/submitWeeklyReport"
|
||||||
getWeeklyReportPath = base_url + "/api/getWeeklyReport"
|
getWeeklyReportPath = base_url + "/api/getWeeklyReport"
|
||||||
getProjectPath = base_url + "/api/project"
|
getProjectPath = base_url + "/api/project"
|
||||||
signReportPath = base_url + "/api/signReport"
|
signReportPath = base_url + "/api/signReport"
|
||||||
|
|
Loading…
Add table
Reference in a new issue