Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Setup config

Smaug#6739fc15bde42e24
diff --git a/backend/.env b/backend/.env
index 509f2099..b4fc4b5e 100644
--- a/backend/.env
+++ b/backend/.env
@@ -1 +1,4 @@
-JWT_SECRET=your_secret_key
\ No newline at end of file
+JWT_SECRET=your_secret_key
+DATABASE_USER=root
+DATABASE_PASSWORD=root
+FRONT_DOMAIN=localhost
\ No newline at end of file
diff --git a/backend/app/app.go b/backend/app/app.go
index ad2ed5ed..2475d8cf 100644
--- a/backend/app/app.go
+++ b/backend/app/app.go
@@ -7,14 +7,34 @@ import (
_ "github.com/go-sql-driver/mysql"
)
+type Config struct {
+ Port int
+ Database struct {
+ Host string
+ Port int
+ Name string
+ Driver string
+ }
+ Cdn struct {
+ MaxSize float64
+ MaxUploadsSize float64
+ }
+ Auth struct {
+ AccessTokenExpiry int
+ RefreshTokenExpiry int
+ }
+}
+
type App struct {
DB *sql.DB
Snowflake *utils.Snowflake
+ Config Config
}
-func InitApp() *App {
+func InitApp(config Config) *App {
var app App
- app.DB = DBConection()
+ app.DB = DBConection(config)
app.Snowflake = utils.NewSnowflake(1609459200000)
+ app.Config = config
return &app
}
diff --git a/backend/app/database.go b/backend/app/database.go
index 0c878e2a..13b27223 100644
--- a/backend/app/database.go
+++ b/backend/app/database.go
@@ -2,16 +2,18 @@ package app
import (
"database/sql"
+ "fmt"
+ "os"
"time"
)
-func DBConection() (conecction *sql.DB) {
- Driver := "mysql"
- User := "root"
- Password := "root"
- Host := "localhost"
- Port := "3306"
- Database := "alexandrie"
+func DBConection(config Config) (conecction *sql.DB) {
+ Driver := config.Database.Driver
+ User := os.Getenv("DATABASE_USER")
+ Password := os.Getenv("DATABASE_PASSWORD")
+ Host := config.Database.Host
+ Port := fmt.Sprint(config.Database.Port)
+ Database := config.Database.Name
conection, err := sql.Open(Driver, User+":"+Password+"@tcp("+Host+":"+Port+")/"+Database)
diff --git a/backend/config.toml b/backend/config.toml
new file mode 100644
index 00000000..84a920c7
--- /dev/null
+++ b/backend/config.toml
@@ -0,0 +1,16 @@
+Port = 3001
+
+[Database]
+Driver = "mysql"
+Host = "localhost"
+Name = "alexandrie"
+Port = 3306
+
+[Cdn]
+MaxSize = 2e+7 # 20MB
+MaxUploadsSize = 1e+9 # 1GB
+
+[Auth]
+AccessTokenExpiry = 1800 # 30 minutes
+RefreshTokenExpiry = 604800 # 7 days
+
diff --git a/backend/controllers/authentification.controller.go b/backend/controllers/authentification.controller.go
index 6a2f7408..fc8a6745 100644
--- a/backend/controllers/authentification.controller.go
+++ b/backend/controllers/authentification.controller.go
@@ -57,7 +57,7 @@ func (dc *Controller) Login(c *gin.Context) {
return
}
- c.SetCookie("Authorization", tokenString, 1800, "/", "localhost", false, true) // TODO: Replace with config
+ c.SetCookie("Authorization", tokenString, 1800, "/", "localhost", false, true)
c.SetCookie("RefreshToken", session.RefreshToken, int(time.Duration(refresh_token_expiration).Seconds()), "/", "localhost", false, true)
c.JSON(200, utils.Success(user))
@@ -100,21 +100,22 @@ func (dc *Controller) RefreshSession(c *gin.Context) {
return
}
- c.SetCookie("Authorization", tokenString, 1800, "/", "localhost", false, true) // TODO: Replace with config
- c.SetCookie("RefreshToken", session.RefreshToken, int(time.Duration(refresh_token_expiration).Seconds()), "/", "localhost", false, true)
+ c.SetCookie("Authorization", tokenString, dc.app.Config.Auth.AccessTokenExpiry, "/", "localhost", false, true)
+ c.SetCookie("RefreshToken", session.RefreshToken, dc.app.Config.Auth.RefreshTokenExpiry, "/", "localhost", false, true)
c.JSON(200, utils.Success("Token refreshed."))
}
func (dc *Controller) signAccessToken(user models.User) (string, error) {
+ fmt.Println("Signing token for user", user.Username, "with role", user.Role)
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
- "sub": user.Username, // Subject (user identifier)
- "iss": "alexandrie", // Issuer
- "aud": user.Role, // Audience (user role)
- "exp": time.Now().Add(time.Second * 15).Unix(), // Expiration time // TODO: Replace with config
- "iat": time.Now().Unix(), // Issued at
+ "sub": user.Id, // Subject (user identifier)
+ "iss": "alexandrie", // Issuer
+ "exp": time.Now().Add(time.Duration(time.Second * time.Duration(dc.app.Config.Auth.RefreshTokenExpiry))).Unix(), // Expiration time
+ "iat": time.Now().Unix(), // Issued at
+ "role": user.Role,
})
- tokenString, err := claims.SignedString([]byte(os.Getenv("JWT_SECRET"))) // TODO: Replace with config
+ tokenString, err := claims.SignedString([]byte(os.Getenv("JWT_SECRET")))
if err != nil {
return "", err
}
diff --git a/backend/go.mod b/backend/go.mod
index 596de8a4..885f49dc 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -4,6 +4,7 @@ go 1.23.1
require (
filippo.io/edwards25519 v1.1.0 // indirect
+ github.com/BurntSushi/toml v1.5.0 // indirect
github.com/bytedance/sonic v1.13.1 // indirect
github.com/bytedance/sonic/loader v0.2.4 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
diff --git a/backend/go.sum b/backend/go.sum
index 05b2d3d8..ec509882 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -1,5 +1,7 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
+github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
+github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g=
github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
diff --git a/backend/main.go b/backend/main.go
index 75ce738f..edd9289f 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -3,7 +3,10 @@ package main
import (
"Smaug6739/Alexandrie/app"
"Smaug6739/Alexandrie/router"
+ "fmt"
+ "os"
+ "github.com/BurntSushi/toml"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
@@ -11,13 +14,18 @@ import (
func main() {
godotenv.Load()
+ config := app.Config{}
- app := app.InitApp()
+ _, err := toml.DecodeFile("config.toml", &config)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+ app := app.InitApp(config)
defer app.DB.Close()
app_router := router.InitRouter(app)
// Recovery middleware recovers from any panics and writes a 500 if there was one.
app_router.Use(gin.Recovery())
-
app_router.Run("localhost:8080")
}
diff --git a/backend/middlewares/admin.go b/backend/middlewares/admin.go
new file mode 100644
index 00000000..62a226f2
--- /dev/null
+++ b/backend/middlewares/admin.go
@@ -0,0 +1,40 @@
+package middlewares
+
+import (
+ "Smaug6739/Alexandrie/utils"
+
+ "github.com/gin-gonic/gin"
+ "github.com/golang-jwt/jwt/v5"
+)
+
+func Admin() gin.HandlerFunc {
+ return func(c *gin.Context) {
+ claims, _ := c.Get("claims")
+ if claims == nil {
+ c.JSON(401, utils.Error("Unauthorized"))
+ c.Abort()
+ return
+ }
+
+ mapClaims, ok := claims.(jwt.MapClaims)
+ if !ok {
+ c.JSON(401, utils.Error("Unauthorized"))
+ c.Abort()
+ return
+ }
+ role, ok := mapClaims["role"] // JWT stocke souvent les nombres sous forme de float64
+ if !ok {
+ c.JSON(401, utils.Error("Invalid role in token"))
+ c.Abort()
+ return
+ }
+
+ if role.(float64) != 2 {
+ c.JSON(401, utils.Error("Unauthorized"))
+ c.Abort()
+ return
+ }
+ c.Next()
+ }
+
+}
diff --git a/backend/middlewares/authentification.go b/backend/middlewares/authentification.go
index 83fdd814..2048ea6b 100644
--- a/backend/middlewares/authentification.go
+++ b/backend/middlewares/authentification.go
@@ -2,6 +2,7 @@ package middlewares
import (
"Smaug6739/Alexandrie/utils"
+ "fmt"
"net/http"
"os"
@@ -35,6 +36,7 @@ func Auth() gin.HandlerFunc {
// Set the token claims to the context
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
c.Set("claims", claims)
+ fmt.Println(claims)
} else {
c.JSON(http.StatusUnauthorized, utils.Error("Unauthorized"))
c.Abort()
diff --git a/backend/models/user.model.go b/backend/models/user.model.go
index f9aed175..0bc958c9 100644
--- a/backend/models/user.model.go
+++ b/backend/models/user.model.go
@@ -10,7 +10,7 @@ type User struct {
Username string `json:"username"`
Firstname *string `json:"firstname"`
Lastname *string `json:"lastname"`
- Role int `json:"role"`
+ Role int `json:"role"` // 1: user, 2: admin
Avatar *string `json:"avatar"`
Email *string `json:"email"`
Password string `json:"password,omitempty"`
diff --git a/backend/router/routes/users.go b/backend/router/routes/users.go
index f56b3698..523ea587 100644
--- a/backend/router/routes/users.go
+++ b/backend/router/routes/users.go
@@ -13,6 +13,6 @@ func Users(app *app.App, router *gin.RouterGroup) {
usr.Use(middlewares.Auth())
usrCtrl := controllers.NewController(app)
- usr.GET("/", usrCtrl.GetUsers)
+ usr.GET("/", middlewares.Admin(), usrCtrl.GetUsers)
}