Compare commits
No commits in common. "bc9b01d85a943a49f62b794031a576aa3ee28340" and "0792c6b8a3160f9096468438ce867389dac9cf37" have entirely different histories.
bc9b01d85a
...
0792c6b8a3
14 changed files with 102 additions and 548 deletions
|
@ -34,7 +34,6 @@ clean:
|
||||||
rm -f plantuml.jar
|
rm -f plantuml.jar
|
||||||
rm -f erd.png
|
rm -f erd.png
|
||||||
rm -f config.toml
|
rm -f config.toml
|
||||||
rm -f database.txt
|
|
||||||
|
|
||||||
# Test target
|
# Test target
|
||||||
test: db.sqlite3
|
test: db.sqlite3
|
||||||
|
@ -106,7 +105,6 @@ docs:
|
||||||
swag init -outputTypes go
|
swag init -outputTypes go
|
||||||
|
|
||||||
api: ./docs/swagger.json
|
api: ./docs/swagger.json
|
||||||
rm ../frontend/src/API/GenApi.ts
|
|
||||||
npx swagger-typescript-api \
|
npx swagger-typescript-api \
|
||||||
--api-class-name GenApi \
|
--api-class-name GenApi \
|
||||||
--path ./docs/swagger.json \
|
--path ./docs/swagger.json \
|
||||||
|
|
|
@ -21,21 +21,21 @@ const docTemplate = `{
|
||||||
"paths": {
|
"paths": {
|
||||||
"/login": {
|
"/login": {
|
||||||
"post": {
|
"post": {
|
||||||
"description": "Logs in a user and returns a JWT token",
|
"description": "logs the user in and returns a jwt token",
|
||||||
"consumes": [
|
"consumes": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
"produces": [
|
"produces": [
|
||||||
"application/json"
|
"text/plain"
|
||||||
],
|
],
|
||||||
"tags": [
|
"tags": [
|
||||||
"Auth"
|
"User"
|
||||||
],
|
],
|
||||||
"summary": "Login",
|
"summary": "login",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"description": "User credentials",
|
"description": "login info",
|
||||||
"name": "body",
|
"name": "NewUser",
|
||||||
"in": "body",
|
"in": "body",
|
||||||
"required": true,
|
"required": true,
|
||||||
"schema": {
|
"schema": {
|
||||||
|
@ -45,9 +45,9 @@ const docTemplate = `{
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "JWT token",
|
"description": "Successfully signed token for user",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/types.Token"
|
"type": "Token"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"400": {
|
||||||
|
@ -71,26 +71,29 @@ const docTemplate = `{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/loginrenew": {
|
"/loginerenew": {
|
||||||
"post": {
|
"post": {
|
||||||
"security": [
|
"security": [
|
||||||
{
|
{
|
||||||
"JWT": []
|
"bererToken": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"description": "Renews the users token.",
|
"description": "renews the users token",
|
||||||
"produces": [
|
"consumes": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
|
"produces": [
|
||||||
|
"text/plain"
|
||||||
|
],
|
||||||
"tags": [
|
"tags": [
|
||||||
"Auth"
|
"User"
|
||||||
],
|
],
|
||||||
"summary": "LoginRenews",
|
"summary": "LoginRenews",
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "Successfully signed token for user",
|
"description": "Successfully signed token for user",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/types.Token"
|
"type": "Token"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"401": {
|
"401": {
|
||||||
|
@ -110,12 +113,7 @@ const docTemplate = `{
|
||||||
},
|
},
|
||||||
"/promoteToAdmin": {
|
"/promoteToAdmin": {
|
||||||
"post": {
|
"post": {
|
||||||
"security": [
|
"description": "promote chosen user to admin",
|
||||||
{
|
|
||||||
"JWT": []
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": "Promote chosen user to site admin",
|
|
||||||
"consumes": [
|
"consumes": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
|
@ -141,7 +139,7 @@ const docTemplate = `{
|
||||||
"200": {
|
"200": {
|
||||||
"description": "Successfully promoted user",
|
"description": "Successfully promoted user",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/types.Token"
|
"type": "json"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"400": {
|
||||||
|
@ -175,7 +173,7 @@ const docTemplate = `{
|
||||||
"text/plain"
|
"text/plain"
|
||||||
],
|
],
|
||||||
"tags": [
|
"tags": [
|
||||||
"Auth"
|
"User"
|
||||||
],
|
],
|
||||||
"summary": "Register",
|
"summary": "Register",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
|
@ -213,11 +211,6 @@ const docTemplate = `{
|
||||||
},
|
},
|
||||||
"/userdelete/{username}": {
|
"/userdelete/{username}": {
|
||||||
"delete": {
|
"delete": {
|
||||||
"security": [
|
|
||||||
{
|
|
||||||
"JWT": []
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": "UserDelete deletes a user from the database",
|
"description": "UserDelete deletes a user from the database",
|
||||||
"consumes": [
|
"consumes": [
|
||||||
"application/json"
|
"application/json"
|
||||||
|
@ -259,27 +252,22 @@ const docTemplate = `{
|
||||||
},
|
},
|
||||||
"/users/all": {
|
"/users/all": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
|
||||||
{
|
|
||||||
"JWT": []
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": "lists all users",
|
"description": "lists all users",
|
||||||
"produces": [
|
"consumes": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
|
"produces": [
|
||||||
|
"text/plain"
|
||||||
|
],
|
||||||
"tags": [
|
"tags": [
|
||||||
"User"
|
"User"
|
||||||
],
|
],
|
||||||
"summary": "ListsAllUsers",
|
"summary": "ListsAllUsers",
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "Successfully returned all users",
|
"description": "Successfully signed token for user",
|
||||||
"schema": {
|
"schema": {
|
||||||
"type": "array",
|
"type": "json"
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"401": {
|
"401": {
|
||||||
|
@ -303,27 +291,16 @@ const docTemplate = `{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"password": {
|
"password": {
|
||||||
"type": "string",
|
"type": "string"
|
||||||
"example": "password123"
|
|
||||||
},
|
},
|
||||||
"username": {
|
"username": {
|
||||||
"type": "string",
|
|
||||||
"example": "username123"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"types.Token": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"token": {
|
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"securityDefinitions": {
|
"securityDefinitions": {
|
||||||
"JWT": {
|
"bererToken": {
|
||||||
"description": "Use the JWT token provided by the login endpoint to authenticate requests. **Prefix the token with \"Bearer \".**",
|
|
||||||
"type": "apiKey",
|
"type": "apiKey",
|
||||||
"name": "Authorization",
|
"name": "Authorization",
|
||||||
"in": "header"
|
"in": "header"
|
||||||
|
|
|
@ -7,17 +7,16 @@ import (
|
||||||
"github.com/gofiber/fiber/v2/log"
|
"github.com/gofiber/fiber/v2/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ListAllUsers is a handler that returns a list of all users in the application database
|
||||||
// @Summary ListsAllUsers
|
// @Summary ListsAllUsers
|
||||||
// @Description lists all users
|
// @Description lists all users
|
||||||
// @Tags User
|
// @Tags User
|
||||||
// @Produce json
|
// @Accept json
|
||||||
// @Security JWT
|
// @Produce plain
|
||||||
// @Success 200 {array} string "Successfully returned all users"
|
// @Success 200 {json} json "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 /users/all [get]
|
// @Router /users/all [get]
|
||||||
//
|
|
||||||
// ListAllUsers returns a list of all users in the application database
|
|
||||||
func ListAllUsers(c *fiber.Ctx) error {
|
func ListAllUsers(c *fiber.Ctx) error {
|
||||||
// Get all users from the database
|
// Get all users from the database
|
||||||
users, err := db.GetDb(c).GetAllUsersApplication()
|
users, err := db.GetDb(c).GetAllUsersApplication()
|
||||||
|
|
|
@ -10,19 +10,18 @@ import (
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// @Summary Login
|
// Login is a simple login handler that returns a JWT token
|
||||||
// @Description Logs in a user and returns a JWT token
|
// @Summary login
|
||||||
// @Tags Auth
|
// @Description logs the user in and returns a jwt token
|
||||||
|
// @Tags User
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Param NewUser body types.NewUser true "login info"
|
||||||
// @Param body body types.NewUser true "User credentials"
|
// @Produce plain
|
||||||
// @Success 200 {object} types.Token "JWT token"
|
// @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]
|
||||||
//
|
|
||||||
// Login logs in a user and returns a JWT token
|
|
||||||
func Login(c *fiber.Ctx) error {
|
func Login(c *fiber.Ctx) error {
|
||||||
// The body type is identical to a NewUser
|
// The body type is identical to a NewUser
|
||||||
|
|
||||||
|
|
|
@ -9,40 +9,34 @@ import (
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// LoginRenew is a simple handler that renews the token
|
||||||
// @Summary LoginRenews
|
// @Summary LoginRenews
|
||||||
// @Description Renews the users token.
|
// @Description renews the users token
|
||||||
// @Tags Auth
|
// @Security bererToken
|
||||||
// @Produce json
|
// @Tags User
|
||||||
// @Security JWT
|
// @Accept json
|
||||||
// @Success 200 {object} types.Token "Successfully signed token for user"
|
// @Produce plain
|
||||||
|
// @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 /loginrenew [post]
|
// @Router /loginerenew [post]
|
||||||
//
|
|
||||||
// LoginRenew renews the users token
|
|
||||||
func LoginRenew(c *fiber.Ctx) error {
|
func LoginRenew(c *fiber.Ctx) error {
|
||||||
user := c.Locals("user").(*jwt.Token)
|
user := c.Locals("user").(*jwt.Token)
|
||||||
|
|
||||||
log.Info("Renewing token for user:", user.Claims.(jwt.MapClaims)["name"])
|
log.Info("Renewing token for user:", user.Claims.(jwt.MapClaims)["name"])
|
||||||
|
|
||||||
// Renewing the token means we trust whatever is already in the token
|
|
||||||
claims := user.Claims.(jwt.MapClaims)
|
claims := user.Claims.(jwt.MapClaims)
|
||||||
|
|
||||||
// 72 hour expiration time
|
|
||||||
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
|
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
|
||||||
|
renewed := jwt.MapClaims{
|
||||||
// Create token with old claims, but new expiration time
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
||||||
"name": claims["name"],
|
"name": claims["name"],
|
||||||
"admin": claims["admin"],
|
"admin": claims["admin"],
|
||||||
"exp": claims["exp"],
|
"exp": claims["exp"],
|
||||||
})
|
}
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, renewed)
|
||||||
// Sign it with top secret key
|
|
||||||
t, err := token.SignedString([]byte("secret"))
|
t, err := token.SignedString([]byte("secret"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Error signing token")
|
log.Warn("Error signing token")
|
||||||
return c.SendStatus(fiber.StatusInternalServerError) // 500
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Successfully renewed token for user:", user.Claims.(jwt.MapClaims)["name"])
|
log.Info("Successfully renewed token for user:", user.Claims.(jwt.MapClaims)["name"])
|
||||||
|
|
|
@ -9,19 +9,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// @Summary PromoteToAdmin
|
// @Summary PromoteToAdmin
|
||||||
// @Description Promote chosen user to site admin
|
// @Description promote chosen user to admin
|
||||||
// @Tags User
|
// @Tags User
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce plain
|
// @Produce plain
|
||||||
// @Security JWT
|
|
||||||
// @Param NewUser body types.NewUser true "user info"
|
// @Param NewUser body types.NewUser true "user info"
|
||||||
// @Success 200 {object} types.Token "Successfully promoted user"
|
// @Success 200 {json} json "Successfully promoted 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 /promoteToAdmin [post]
|
// @Router /promoteToAdmin [post]
|
||||||
//
|
|
||||||
// PromoteToAdmin promotes a user to a site admin
|
|
||||||
func PromoteToAdmin(c *fiber.Ctx) error {
|
func 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
|
||||||
|
|
|
@ -8,9 +8,11 @@ import (
|
||||||
"github.com/gofiber/fiber/v2/log"
|
"github.com/gofiber/fiber/v2/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Register is a simple handler that registers a new user
|
||||||
|
//
|
||||||
// @Summary Register
|
// @Summary Register
|
||||||
// @Description Register a new user
|
// @Description Register a new user
|
||||||
// @Tags Auth
|
// @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"
|
||||||
|
@ -18,8 +20,6 @@ import (
|
||||||
// @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]
|
||||||
//
|
|
||||||
// Register is a simple handler that registers a new user
|
|
||||||
func Register(c *fiber.Ctx) error {
|
func 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 {
|
||||||
|
|
|
@ -8,19 +8,19 @@ import (
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// This path should obviously be protected in the future
|
||||||
|
// UserDelete deletes a user from the database
|
||||||
|
//
|
||||||
// @Summary UserDelete
|
// @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
|
||||||
// @Security JWT
|
|
||||||
// @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]
|
||||||
//
|
|
||||||
// UserDelete deletes a user from the database
|
|
||||||
func UserDelete(c *fiber.Ctx) error {
|
func UserDelete(c *fiber.Ctx) error {
|
||||||
// Read from path parameters
|
// Read from path parameters
|
||||||
username := c.Params("username")
|
username := c.Params("username")
|
||||||
|
|
|
@ -18,8 +18,8 @@ func (u *User) ToPublicUser() (*PublicUser, error) {
|
||||||
|
|
||||||
// Should be used when registering, for example
|
// Should be used when registering, for example
|
||||||
type NewUser struct {
|
type NewUser struct {
|
||||||
Username string `json:"username" example:"username123"`
|
Username string `json:"username"`
|
||||||
Password string `json:"password" example:"password123"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PublicUser represents a user that is safe to send over the API (no password)
|
// PublicUser represents a user that is safe to send over the API (no password)
|
||||||
|
|
|
@ -25,10 +25,9 @@ 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 JWT
|
//@securityDefinitions.apikey bererToken
|
||||||
//@in header
|
//@in header
|
||||||
//@name Authorization
|
//@name Authorization
|
||||||
// @description Use the JWT token provided by the login endpoint to authenticate requests. **Prefix the token with "Bearer ".**
|
|
||||||
|
|
||||||
// @host localhost:8080
|
// @host localhost:8080
|
||||||
// @BasePath /api
|
// @BasePath /api
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
goTypes.ts
|
|
||||||
GenApi.ts
|
|
|
@ -1,358 +0,0 @@
|
||||||
/* eslint-disable */
|
|
||||||
/* tslint:disable */
|
|
||||||
/*
|
|
||||||
* ---------------------------------------------------------------
|
|
||||||
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
|
|
||||||
* ## ##
|
|
||||||
* ## AUTHOR: acacode ##
|
|
||||||
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
|
|
||||||
* ---------------------------------------------------------------
|
|
||||||
*/
|
|
||||||
|
|
||||||
export interface TypesNewUser {
|
|
||||||
/** @example "password123" */
|
|
||||||
password?: string;
|
|
||||||
/** @example "username123" */
|
|
||||||
username?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TypesToken {
|
|
||||||
token?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type QueryParamsType = Record<string | number, any>;
|
|
||||||
export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
|
|
||||||
|
|
||||||
export interface FullRequestParams extends Omit<RequestInit, "body"> {
|
|
||||||
/** set parameter to `true` for call `securityWorker` for this request */
|
|
||||||
secure?: boolean;
|
|
||||||
/** request path */
|
|
||||||
path: string;
|
|
||||||
/** content type of request body */
|
|
||||||
type?: ContentType;
|
|
||||||
/** query params */
|
|
||||||
query?: QueryParamsType;
|
|
||||||
/** format of response (i.e. response.json() -> format: "json") */
|
|
||||||
format?: ResponseFormat;
|
|
||||||
/** request body */
|
|
||||||
body?: unknown;
|
|
||||||
/** base url */
|
|
||||||
baseUrl?: string;
|
|
||||||
/** request cancellation token */
|
|
||||||
cancelToken?: CancelToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query" | "path">;
|
|
||||||
|
|
||||||
export interface ApiConfig<SecurityDataType = unknown> {
|
|
||||||
baseUrl?: string;
|
|
||||||
baseApiParams?: Omit<RequestParams, "baseUrl" | "cancelToken" | "signal">;
|
|
||||||
securityWorker?: (securityData: SecurityDataType | null) => Promise<RequestParams | void> | RequestParams | void;
|
|
||||||
customFetch?: typeof fetch;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response {
|
|
||||||
data: D;
|
|
||||||
error: E;
|
|
||||||
}
|
|
||||||
|
|
||||||
type CancelToken = Symbol | string | number;
|
|
||||||
|
|
||||||
export enum ContentType {
|
|
||||||
Json = "application/json",
|
|
||||||
FormData = "multipart/form-data",
|
|
||||||
UrlEncoded = "application/x-www-form-urlencoded",
|
|
||||||
Text = "text/plain",
|
|
||||||
}
|
|
||||||
|
|
||||||
export class HttpClient<SecurityDataType = unknown> {
|
|
||||||
public baseUrl: string = "//localhost:8080/api";
|
|
||||||
private securityData: SecurityDataType | null = null;
|
|
||||||
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
|
|
||||||
private abortControllers = new Map<CancelToken, AbortController>();
|
|
||||||
private customFetch = (...fetchParams: Parameters<typeof fetch>) => fetch(...fetchParams);
|
|
||||||
|
|
||||||
private baseApiParams: RequestParams = {
|
|
||||||
credentials: "same-origin",
|
|
||||||
headers: {},
|
|
||||||
redirect: "follow",
|
|
||||||
referrerPolicy: "no-referrer",
|
|
||||||
};
|
|
||||||
|
|
||||||
constructor(apiConfig: ApiConfig<SecurityDataType> = {}) {
|
|
||||||
Object.assign(this, apiConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
public setSecurityData = (data: SecurityDataType | null) => {
|
|
||||||
this.securityData = data;
|
|
||||||
};
|
|
||||||
|
|
||||||
protected encodeQueryParam(key: string, value: any) {
|
|
||||||
const encodedKey = encodeURIComponent(key);
|
|
||||||
return `${encodedKey}=${encodeURIComponent(typeof value === "number" ? value : `${value}`)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected addQueryParam(query: QueryParamsType, key: string) {
|
|
||||||
return this.encodeQueryParam(key, query[key]);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected addArrayQueryParam(query: QueryParamsType, key: string) {
|
|
||||||
const value = query[key];
|
|
||||||
return value.map((v: any) => this.encodeQueryParam(key, v)).join("&");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected toQueryString(rawQuery?: QueryParamsType): string {
|
|
||||||
const query = rawQuery || {};
|
|
||||||
const keys = Object.keys(query).filter((key) => "undefined" !== typeof query[key]);
|
|
||||||
return keys
|
|
||||||
.map((key) => (Array.isArray(query[key]) ? this.addArrayQueryParam(query, key) : this.addQueryParam(query, key)))
|
|
||||||
.join("&");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected addQueryParams(rawQuery?: QueryParamsType): string {
|
|
||||||
const queryString = this.toQueryString(rawQuery);
|
|
||||||
return queryString ? `?${queryString}` : "";
|
|
||||||
}
|
|
||||||
|
|
||||||
private contentFormatters: Record<ContentType, (input: any) => any> = {
|
|
||||||
[ContentType.Json]: (input: any) =>
|
|
||||||
input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
|
|
||||||
[ContentType.Text]: (input: any) => (input !== null && typeof input !== "string" ? JSON.stringify(input) : input),
|
|
||||||
[ContentType.FormData]: (input: any) =>
|
|
||||||
Object.keys(input || {}).reduce((formData, key) => {
|
|
||||||
const property = input[key];
|
|
||||||
formData.append(
|
|
||||||
key,
|
|
||||||
property instanceof Blob
|
|
||||||
? property
|
|
||||||
: typeof property === "object" && property !== null
|
|
||||||
? JSON.stringify(property)
|
|
||||||
: `${property}`,
|
|
||||||
);
|
|
||||||
return formData;
|
|
||||||
}, new FormData()),
|
|
||||||
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
|
|
||||||
};
|
|
||||||
|
|
||||||
protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams {
|
|
||||||
return {
|
|
||||||
...this.baseApiParams,
|
|
||||||
...params1,
|
|
||||||
...(params2 || {}),
|
|
||||||
headers: {
|
|
||||||
...(this.baseApiParams.headers || {}),
|
|
||||||
...(params1.headers || {}),
|
|
||||||
...((params2 && params2.headers) || {}),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => {
|
|
||||||
if (this.abortControllers.has(cancelToken)) {
|
|
||||||
const abortController = this.abortControllers.get(cancelToken);
|
|
||||||
if (abortController) {
|
|
||||||
return abortController.signal;
|
|
||||||
}
|
|
||||||
return void 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const abortController = new AbortController();
|
|
||||||
this.abortControllers.set(cancelToken, abortController);
|
|
||||||
return abortController.signal;
|
|
||||||
};
|
|
||||||
|
|
||||||
public abortRequest = (cancelToken: CancelToken) => {
|
|
||||||
const abortController = this.abortControllers.get(cancelToken);
|
|
||||||
|
|
||||||
if (abortController) {
|
|
||||||
abortController.abort();
|
|
||||||
this.abortControllers.delete(cancelToken);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public request = async <T = any, E = any>({
|
|
||||||
body,
|
|
||||||
secure,
|
|
||||||
path,
|
|
||||||
type,
|
|
||||||
query,
|
|
||||||
format,
|
|
||||||
baseUrl,
|
|
||||||
cancelToken,
|
|
||||||
...params
|
|
||||||
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
|
|
||||||
const secureParams =
|
|
||||||
((typeof secure === "boolean" ? secure : this.baseApiParams.secure) &&
|
|
||||||
this.securityWorker &&
|
|
||||||
(await this.securityWorker(this.securityData))) ||
|
|
||||||
{};
|
|
||||||
const requestParams = this.mergeRequestParams(params, secureParams);
|
|
||||||
const queryString = query && this.toQueryString(query);
|
|
||||||
const payloadFormatter = this.contentFormatters[type || ContentType.Json];
|
|
||||||
const responseFormat = format || requestParams.format;
|
|
||||||
|
|
||||||
return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, {
|
|
||||||
...requestParams,
|
|
||||||
headers: {
|
|
||||||
...(requestParams.headers || {}),
|
|
||||||
...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}),
|
|
||||||
},
|
|
||||||
signal: (cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal) || null,
|
|
||||||
body: typeof body === "undefined" || body === null ? null : payloadFormatter(body),
|
|
||||||
}).then(async (response) => {
|
|
||||||
const r = response as HttpResponse<T, E>;
|
|
||||||
r.data = null as unknown as T;
|
|
||||||
r.error = null as unknown as E;
|
|
||||||
|
|
||||||
const data = !responseFormat
|
|
||||||
? r
|
|
||||||
: await response[responseFormat]()
|
|
||||||
.then((data) => {
|
|
||||||
if (r.ok) {
|
|
||||||
r.data = data;
|
|
||||||
} else {
|
|
||||||
r.error = data;
|
|
||||||
}
|
|
||||||
return r;
|
|
||||||
})
|
|
||||||
.catch((e) => {
|
|
||||||
r.error = e;
|
|
||||||
return r;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (cancelToken) {
|
|
||||||
this.abortControllers.delete(cancelToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.ok) throw data;
|
|
||||||
return data;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @title TTime API
|
|
||||||
* @version 0.0.1
|
|
||||||
* @license AGPL (https://www.gnu.org/licenses/agpl-3.0.html)
|
|
||||||
* @baseUrl //localhost:8080/api
|
|
||||||
* @externalDocs https://swagger.io/resources/open-api/
|
|
||||||
* @contact
|
|
||||||
*
|
|
||||||
* This is the API for TTime, a time tracking application.
|
|
||||||
*/
|
|
||||||
export class GenApi<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
|
||||||
login = {
|
|
||||||
/**
|
|
||||||
* @description Logs in a user and returns a JWT token
|
|
||||||
*
|
|
||||||
* @tags Auth
|
|
||||||
* @name LoginCreate
|
|
||||||
* @summary Login
|
|
||||||
* @request POST:/login
|
|
||||||
*/
|
|
||||||
loginCreate: (body: TypesNewUser, params: RequestParams = {}) =>
|
|
||||||
this.request<TypesToken, string>({
|
|
||||||
path: `/login`,
|
|
||||||
method: "POST",
|
|
||||||
body: body,
|
|
||||||
type: ContentType.Json,
|
|
||||||
format: "json",
|
|
||||||
...params,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
loginrenew = {
|
|
||||||
/**
|
|
||||||
* @description Renews the users token.
|
|
||||||
*
|
|
||||||
* @tags Auth
|
|
||||||
* @name LoginrenewCreate
|
|
||||||
* @summary LoginRenews
|
|
||||||
* @request POST:/loginrenew
|
|
||||||
* @secure
|
|
||||||
*/
|
|
||||||
loginrenewCreate: (params: RequestParams = {}) =>
|
|
||||||
this.request<TypesToken, string>({
|
|
||||||
path: `/loginrenew`,
|
|
||||||
method: "POST",
|
|
||||||
secure: true,
|
|
||||||
format: "json",
|
|
||||||
...params,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
promoteToAdmin = {
|
|
||||||
/**
|
|
||||||
* @description Promote chosen user to site admin
|
|
||||||
*
|
|
||||||
* @tags User
|
|
||||||
* @name PromoteToAdminCreate
|
|
||||||
* @summary PromoteToAdmin
|
|
||||||
* @request POST:/promoteToAdmin
|
|
||||||
* @secure
|
|
||||||
*/
|
|
||||||
promoteToAdminCreate: (NewUser: TypesNewUser, params: RequestParams = {}) =>
|
|
||||||
this.request<TypesToken, string>({
|
|
||||||
path: `/promoteToAdmin`,
|
|
||||||
method: "POST",
|
|
||||||
body: NewUser,
|
|
||||||
secure: true,
|
|
||||||
type: ContentType.Json,
|
|
||||||
...params,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
register = {
|
|
||||||
/**
|
|
||||||
* @description Register a new user
|
|
||||||
*
|
|
||||||
* @tags Auth
|
|
||||||
* @name RegisterCreate
|
|
||||||
* @summary Register
|
|
||||||
* @request POST:/register
|
|
||||||
*/
|
|
||||||
registerCreate: (NewUser: TypesNewUser, params: RequestParams = {}) =>
|
|
||||||
this.request<string, string>({
|
|
||||||
path: `/register`,
|
|
||||||
method: "POST",
|
|
||||||
body: NewUser,
|
|
||||||
type: ContentType.Json,
|
|
||||||
...params,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
userdelete = {
|
|
||||||
/**
|
|
||||||
* @description UserDelete deletes a user from the database
|
|
||||||
*
|
|
||||||
* @tags User
|
|
||||||
* @name UserdeleteDelete
|
|
||||||
* @summary UserDelete
|
|
||||||
* @request DELETE:/userdelete/{username}
|
|
||||||
* @secure
|
|
||||||
*/
|
|
||||||
userdeleteDelete: (username: string, params: RequestParams = {}) =>
|
|
||||||
this.request<string, string>({
|
|
||||||
path: `/userdelete/${username}`,
|
|
||||||
method: "DELETE",
|
|
||||||
secure: true,
|
|
||||||
type: ContentType.Json,
|
|
||||||
...params,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
users = {
|
|
||||||
/**
|
|
||||||
* @description lists all users
|
|
||||||
*
|
|
||||||
* @tags User
|
|
||||||
* @name GetUsers
|
|
||||||
* @summary ListsAllUsers
|
|
||||||
* @request GET:/users/all
|
|
||||||
* @secure
|
|
||||||
*/
|
|
||||||
getUsers: (params: RequestParams = {}) =>
|
|
||||||
this.request<string[], string>({
|
|
||||||
path: `/users/all`,
|
|
||||||
method: "GET",
|
|
||||||
secure: true,
|
|
||||||
format: "json",
|
|
||||||
...params,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -1,23 +0,0 @@
|
||||||
import { useEffect } from "react";
|
|
||||||
import { GenApi } from "../API/GenApi";
|
|
||||||
|
|
||||||
// Instantiation of the API
|
|
||||||
const api = new GenApi();
|
|
||||||
|
|
||||||
export function GenApiDemo(): JSX.Element {
|
|
||||||
// Sync wrapper around the loginCreate method
|
|
||||||
const register = async (): Promise<void> => {
|
|
||||||
const response = await api.login.loginCreate({
|
|
||||||
username: "admin",
|
|
||||||
password: "admin",
|
|
||||||
});
|
|
||||||
console.log(response.data); // This should be the inner type of the response
|
|
||||||
};
|
|
||||||
|
|
||||||
// Call the wrapper
|
|
||||||
useEffect(() => {
|
|
||||||
void register();
|
|
||||||
});
|
|
||||||
|
|
||||||
return <></>;
|
|
||||||
}
|
|
|
@ -124,44 +124,6 @@ export interface WeeklyReport {
|
||||||
*/
|
*/
|
||||||
signedBy?: number /* int */;
|
signedBy?: number /* int */;
|
||||||
}
|
}
|
||||||
export interface UpdateWeeklyReport {
|
|
||||||
/**
|
|
||||||
* The name of the project, as it appears in the database
|
|
||||||
*/
|
|
||||||
projectName: string;
|
|
||||||
/**
|
|
||||||
* The name of the user
|
|
||||||
*/
|
|
||||||
userName: string;
|
|
||||||
/**
|
|
||||||
* The week number
|
|
||||||
*/
|
|
||||||
week: number /* int */;
|
|
||||||
/**
|
|
||||||
* Total time spent on development
|
|
||||||
*/
|
|
||||||
developmentTime: number /* int */;
|
|
||||||
/**
|
|
||||||
* Total time spent in meetings
|
|
||||||
*/
|
|
||||||
meetingTime: number /* int */;
|
|
||||||
/**
|
|
||||||
* Total time spent on administrative tasks
|
|
||||||
*/
|
|
||||||
adminTime: number /* int */;
|
|
||||||
/**
|
|
||||||
* Total time spent on personal projects
|
|
||||||
*/
|
|
||||||
ownWorkTime: number /* int */;
|
|
||||||
/**
|
|
||||||
* Total time spent on studying
|
|
||||||
*/
|
|
||||||
studyTime: number /* int */;
|
|
||||||
/**
|
|
||||||
* Total time spent on testing
|
|
||||||
*/
|
|
||||||
testingTime: number /* int */;
|
|
||||||
}
|
|
||||||
|
|
||||||
//////////
|
//////////
|
||||||
// source: project.go
|
// source: project.go
|
||||||
|
@ -189,9 +151,16 @@ export interface NewProject {
|
||||||
*/
|
*/
|
||||||
export interface RoleChange {
|
export interface RoleChange {
|
||||||
username: string;
|
username: string;
|
||||||
role: 'project_manager' | 'user';
|
role: "project_manager" | "user";
|
||||||
projectname: string;
|
projectname: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface NewProjMember {
|
||||||
|
username: string;
|
||||||
|
projectname: string;
|
||||||
|
role: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface NameChange {
|
export interface NameChange {
|
||||||
id: number /* int */;
|
id: number /* int */;
|
||||||
name: string;
|
name: string;
|
||||||
|
@ -222,6 +191,11 @@ export interface PublicUser {
|
||||||
userId: string;
|
userId: string;
|
||||||
username: string;
|
username: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface UserProjectMember {
|
||||||
|
Username: string;
|
||||||
|
UserRole: string;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* wrapper type for token
|
* wrapper type for token
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue