Developing, yo
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"thermalFax/models"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
func isAuthenticated(r *http.Request) bool {
|
||||
cookies := r.Cookies()
|
||||
for _, c := range cookies {
|
||||
if c.Name == "token" {
|
||||
// Check if the token exists and if its date is valid
|
||||
expirationTime := time.Now()
|
||||
return expirationTime.Unix() >= c.Expires.Unix()
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func Authenticate(w http.ResponseWriter, r *http.Request) {
|
||||
// first we have to verify if this request brings any login data
|
||||
// Otherwise we login, which we will do everytime lmao
|
||||
|
||||
data, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Printf("Error: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
var loginForm models.LoginForm
|
||||
err = json.Unmarshal(data, &loginForm)
|
||||
if err != nil {
|
||||
log.Printf("Error: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !models.ValidateCredentials(&loginForm) {
|
||||
log.Printf("Invalid credentials.")
|
||||
return
|
||||
}
|
||||
|
||||
// Generate the thing
|
||||
expirationTime := time.Now().Add(2 * time.Minute)
|
||||
claims := models.Claims{
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
Subject: loginForm.Username,
|
||||
ExpiresAt: jwt.NewNumericDate(expirationTime),
|
||||
},
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
tokenString, err := token.SignedString([]byte("secret"))
|
||||
if err != nil {
|
||||
log.Printf("Failed to create the token.")
|
||||
return
|
||||
}
|
||||
|
||||
cookie := http.Cookie{
|
||||
Name: "token",
|
||||
Value: tokenString,
|
||||
Expires: expirationTime,
|
||||
}
|
||||
http.SetCookie(w, &cookie)
|
||||
|
||||
log.Printf(`| {"user": %s, "pass": %s}`, loginForm.Username, loginForm.Password)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"thermalFax/views"
|
||||
)
|
||||
|
||||
func EditorPage(w http.ResponseWriter, r *http.Request) {
|
||||
if !isAuthenticated(r) {
|
||||
views.LoginPage(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
views.EditorPage(w, r)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"thermalFax/views"
|
||||
)
|
||||
|
||||
type PrintServerResponse struct {
|
||||
@@ -57,6 +58,11 @@ func sendRequestToPrinter(msg string) (PrintServerResponse, error) {
|
||||
// sendFax is a function that receives the data from the fronted and
|
||||
// sends it to the print server
|
||||
func SendFax(w http.ResponseWriter, r *http.Request) {
|
||||
if !isAuthenticated(r) {
|
||||
views.LoginPage(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
data, _ := io.ReadAll(r.Body)
|
||||
log.Printf("| Received Request: %s", data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user