Compare commits

..

No commits in common. "2ffbc2f9fdf8578c61829d31adaefff8ac87b561" and "fe08d01e15ed9e293e8c02449d947bc6f002697b" have entirely different histories.

3 changed files with 10 additions and 202 deletions

View file

@ -19,117 +19,19 @@ const docTemplate = `{
"host": "{{.Host}}", "host": "{{.Host}}",
"basePath": "{{.BasePath}}", "basePath": "{{.BasePath}}",
"paths": { "paths": {
"/login": { "/api/register": {
"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": { "post": {
"description": "Register a new user", "description": "Register a new user",
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
"produces": [ "produces": [
"text/plain" "application/json"
], ],
"tags": [ "tags": [
"User" "User"
], ],
"summary": "Register a new user", "summary": "Register a new user",
"parameters": [
{
"description": "User to register",
"name": "NewUser",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/types.NewUser"
}
}
],
"responses": { "responses": {
"200": { "200": {
"description": "User added", "description": "User added",
@ -151,60 +53,6 @@ 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": {
"types.NewUser": {
"type": "object",
"properties": {
"password": {
"type": "string"
},
"username": {
"type": "string"
}
}
} }
}, },
"externalDocs": { "externalDocs": {

View file

@ -16,12 +16,11 @@ import (
// @Description Register a new user // @Description Register a new user
// @Tags User // @Tags User
// @Accept json // @Accept json
// @Produce plain // @Produce json
// @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 /api/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)
if err := c.BodyParser(u); err != nil { if err := c.BodyParser(u); err != nil {
@ -41,17 +40,6 @@ 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
// @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 { func (gs *GState) UserDelete(c *fiber.Ctx) error {
// Read from path parameters // Read from path parameters
username := c.Params("username") username := c.Params("username")
@ -74,21 +62,8 @@ func (gs *GState) UserDelete(c *fiber.Ctx) error {
} }
// Login is a simple login handler that returns a JWT token // 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 { func (gs *GState) Login(c *fiber.Ctx) error {
// The body type is identical to a NewUser // The body type is identical to a NewUser
u := new(types.NewUser) u := new(types.NewUser)
if err := c.BodyParser(u); err != nil { if err := c.BodyParser(u); err != nil {
log.Warn("Error parsing body") log.Warn("Error parsing body")
@ -119,21 +94,11 @@ func (gs *GState) Login(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusInternalServerError) return c.SendStatus(fiber.StatusInternalServerError)
} }
println("Successfully signed token for user:", u.Username) log.Info("Successfully signed token for user:", u.Username)
return c.JSON(types.Token{Token: t}) return c.JSON(fiber.Map{"token": t})
} }
// LoginRenew is a simple handler that renews the token // 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 { func (gs *GState) LoginRenew(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token) user := c.Locals("user").(*jwt.Token)
@ -154,7 +119,7 @@ func (gs *GState) LoginRenew(c *fiber.Ctx) error {
} }
log.Info("Successfully renewed token for user:", user.Claims.(jwt.MapClaims)["name"]) log.Info("Successfully renewed token for user:", user.Claims.(jwt.MapClaims)["name"])
return c.JSON(types.Token{Token: t}) return c.JSON(fiber.Map{"token": t})
} }
// 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

View file

@ -27,8 +27,3 @@ type PublicUser struct {
UserId string `json:"userId"` UserId string `json:"userId"`
Username string `json:"username"` Username string `json:"username"`
} }
// wrapper type for token
type Token struct {
Token string `json:"token"`
}