diff --git a/.env.example b/.env.example index 1b7bad1..329fb7f 100644 --- a/.env.example +++ b/.env.example @@ -21,4 +21,8 @@ CLEANUP_MAX_AGE=43200 # Default is a month # Maximum pad file lenght, this is in characters, a character is one byte. # Default: 524288 ( 500kb ) -MAXIMUM_PAD_SIZE=524288 \ No newline at end of file +MAXIMUM_PAD_SIZE=524288 + +# Your admin access token +# If the value is not defined the admin interface will not be available +# ADMIN_TOKEN=SUPER_SECRET_ADMIN_TOKEN \ No newline at end of file diff --git a/lib/controllers/controllers_admin.go b/lib/controllers/controllers_admin.go new file mode 100644 index 0000000..e08221c --- /dev/null +++ b/lib/controllers/controllers_admin.go @@ -0,0 +1,19 @@ +package controllers + +import ( + "fmt" + + "github.com/gin-gonic/gin" +) + +func AdminMiddleware(router *gin.RouterGroup) { + + // Handl + router.Use(func(ctx *gin.Context) { + + // Check which route we are accessing + fmt.Println(`Accesing: `, ctx.Request.RequestURI) + + }) + +} diff --git a/lib/helper/helper_main.go b/lib/helper/helper_main.go index ef5f905..7c1f158 100644 --- a/lib/helper/helper_main.go +++ b/lib/helper/helper_main.go @@ -72,3 +72,18 @@ func GetCacheMapLimit() int { return rez } + +// Get the admin token used to authenticate as an admin +func GetAdminToken() string { + // Get the admin login from the environment + adminToken, exists := os.LookupEnv("ADMIN_TOKEN") + + // Check if the admin token was defined + if !exists { + // The admin token was not defined, disable admin logins + return "" + } + + // Return the admin token + return adminToken +} diff --git a/lib/routes/routes_admin.go b/lib/routes/routes_admin.go new file mode 100644 index 0000000..fc1fbce --- /dev/null +++ b/lib/routes/routes_admin.go @@ -0,0 +1,80 @@ +package routes + +import ( + "encoding/hex" + "fmt" + "net/http" + + "github.com/JustKato/FreePad/lib/controllers" + "github.com/JustKato/FreePad/lib/helper" + "github.com/gin-gonic/gin" + + "crypto/sha512" +) + +var adminLoginToken string = "" + +func AdminRoutes(router *gin.RouterGroup) { + + adminLoginToken = helper.GetAdminToken() + + // Apply the admin middleware for identification + controllers.AdminMiddleware(router) + + // Admin login route + router.GET("/login", func(ctx *gin.Context) { + ctx.HTML(200, "admin_login.html", gin.H{ + "title": "Login Login", + "domain_base": helper.GetDomainBase(), + }) + }) + + router.POST("/login", func(ctx *gin.Context) { + + // Get the value of the admin token + adminToken := ctx.PostForm("admin-token") + + // Check if the input admin token matches our admin token + if adminLoginToken != "" && adminLoginToken == adminToken { + + sha512Hasher := sha512.New() + sha512Hasher.Write([]byte(adminToken)) + + // Set the cookie to be an admin + hashHexToken := sha512Hasher.Sum(nil) + hashToken := hex.EncodeToString(hashHexToken) + + fmt.Println(hashToken) + + // Set the cookie + ctx.SetCookie("admin_token", hashToken, 60*60, "/", helper.GetDomainBase(), true, true) + + ctx.Request.Method = "GET" + + // Redirect the user to the admin page + ctx.Redirect(http.StatusTemporaryRedirect, "/admin") + return + } else { + ctx.Request.Method = "GET" + + // Redirect the user to the admin page + ctx.Redirect(http.StatusTemporaryRedirect, "/admin/login?fail") + return + } + + }) + + // Admin view route + router.GET("/", func(ctx *gin.Context) { + + adminToken, err := ctx.Cookie("admin_token") + if err != nil { + adminToken = "" + } + + ctx.JSON(200, gin.H{ + `adminToken`: adminToken, + }) + }) + +} diff --git a/main.go b/main.go index 534bff1..6549a86 100644 --- a/main.go +++ b/main.go @@ -46,6 +46,9 @@ func main() { // Implement the rate limiter controllers.DoRateLimit(router) + // Admin Routing + routes.AdminRoutes(router.Group("/admin")) + // Add Routes routes.HomeRoutes(router) diff --git a/templates/pages/admin_login.html b/templates/pages/admin_login.html new file mode 100644 index 0000000..c2b8c7f --- /dev/null +++ b/templates/pages/admin_login.html @@ -0,0 +1,42 @@ +{{ template "inc/header.html" .}} + + + +
+
+ + + Logo + + +
+
+ + + + +
+ Access the admin interface for FreePad, this can only be done through the Admin Token. +
+
+ + + +
+ + {{ template "inc/theme-toggle.html" .}} + + +{{ template "inc/footer.html" .}} \ No newline at end of file