Developing, yo
This commit is contained in:
@@ -105,6 +105,10 @@ code {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
#login-box-boundaries {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
textarea {
|
||||
scrollbar-width: none;
|
||||
width: 100%;
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
<div class="center">
|
||||
<form
|
||||
id="fax-form"
|
||||
action="submit"
|
||||
hx-post="/send-fax"
|
||||
hx-swap="innerHTML"
|
||||
hx-target="#req-result">
|
||||
|
||||
<fieldset id="fax-message-boundaries">
|
||||
<legend>FAX</legend>
|
||||
<textarea name="fax-message" id="fax-message"></textarea>
|
||||
</fieldset>
|
||||
|
||||
<br>
|
||||
<button id="fax-submit-button" type="button">Submit</button>
|
||||
<br>
|
||||
|
||||
<div id="req-result">-></div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Get a reference to the submit button
|
||||
var submitBtn = document.getElementById("fax-submit-button");
|
||||
|
||||
// Add a click event listener to the submit button
|
||||
submitBtn.addEventListener("click", function() {
|
||||
// Get the fax message from the textarea
|
||||
var faxMessage = document.getElementById("fax-message").value;
|
||||
|
||||
// Create the JSON object to send
|
||||
var jsonData = {
|
||||
fax_message: faxMessage
|
||||
};
|
||||
|
||||
// Configure the fetch request
|
||||
fetch("/send-fax", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(jsonData)
|
||||
})
|
||||
.then(response => {
|
||||
// Check if the response is OK
|
||||
if (response.ok) {
|
||||
// Request was successful, handle response if needed
|
||||
return response.text();
|
||||
} else {
|
||||
// Request failed, handle error if needed
|
||||
throw new Error("Error: " + response.statusText);
|
||||
}
|
||||
})
|
||||
.then(data => {
|
||||
console.log("Success:", data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
@@ -1,40 +1,38 @@
|
||||
<div class="center">
|
||||
<form
|
||||
id="fax-form"
|
||||
id="login-form"
|
||||
action="submit"
|
||||
hx-post="/send-fax"
|
||||
hx-post="/send-login"
|
||||
hx-swap="innerHTML"
|
||||
hx-target="#req-result">
|
||||
|
||||
<fieldset id="fax-message-boundaries">
|
||||
<legend>FAX</legend>
|
||||
<textarea name="fax-message" id="fax-message"></textarea>
|
||||
<fieldset id="login-box-boundaries">
|
||||
<legend>LOGIN</legend>
|
||||
<input type="text" name="login-user" id="login-user"><br>
|
||||
<input type="text" name="login-pass" id="login-pass"><br>
|
||||
<button type="submit" id="login-submit-button">Login</button>
|
||||
</fieldset>
|
||||
|
||||
<br>
|
||||
<button id="fax-submit-button" type="button">Submit</button>
|
||||
<br>
|
||||
|
||||
<div id="req-result">-></div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Get a reference to the submit button
|
||||
var submitBtn = document.getElementById("fax-submit-button");
|
||||
var submitBtn = document.getElementById("login-submit-button");
|
||||
|
||||
// Add a click event listener to the submit button
|
||||
submitBtn.addEventListener("click", function() {
|
||||
// Get the fax message from the textarea
|
||||
var faxMessage = document.getElementById("fax-message").value;
|
||||
// Get the login message from the textarea
|
||||
var loginUser = document.getElementById("login-user").value;
|
||||
var loginPass = document.getElementById("login-pass").value;
|
||||
|
||||
// Create the JSON object to send
|
||||
var jsonData = {
|
||||
fax_message: faxMessage
|
||||
username: loginUser,
|
||||
password: loginPass,
|
||||
};
|
||||
|
||||
// Configure the fetch request
|
||||
fetch("/send-fax", {
|
||||
fetch("/send-login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
@@ -52,6 +50,7 @@
|
||||
}
|
||||
})
|
||||
.then(data => {
|
||||
window.location.replace("http://localhost:8083/")
|
||||
console.log("Success:", data);
|
||||
})
|
||||
.catch(error => {
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
+4
-1
@@ -2,4 +2,7 @@ module thermalFax
|
||||
|
||||
go 1.22.2
|
||||
|
||||
require golang.org/x/crypto v0.22.0
|
||||
require (
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0
|
||||
golang.org/x/crypto v0.22.0
|
||||
)
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||
|
||||
+29
-17
@@ -5,30 +5,42 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"thermalFax/controllers"
|
||||
"thermalFax/models"
|
||||
"thermalFax/views"
|
||||
)
|
||||
|
||||
// launchServer is the main server function
|
||||
func launchServer(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
|
||||
// Authenticate the user
|
||||
// TODO: I should make this a bit better since we want to use another service
|
||||
// and that one should handle the authentication and this one make use of it
|
||||
var auth models.Authentication
|
||||
if !auth.Authenticate(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
views.EditorPage(w, r)
|
||||
func checkMethod(method string, r *http.Request) bool {
|
||||
return r.Method == method
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Set up a very basic http server
|
||||
http.HandleFunc("/", launchServer)
|
||||
// TODO: display users main page on /
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !checkMethod(http.MethodGet, r) {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
http.HandleFunc("/send-fax", controllers.SendFax)
|
||||
controllers.EditorPage(w, r)
|
||||
})
|
||||
|
||||
// Both should be POST only
|
||||
http.HandleFunc("/send-fax", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !checkMethod(http.MethodPost, r) {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
controllers.SendFax(w, r)
|
||||
})
|
||||
|
||||
http.HandleFunc("/send-login", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !checkMethod(http.MethodPost, r) {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
controllers.Authenticate(w, r)
|
||||
})
|
||||
|
||||
// Serve the CSS and JS files, we might have to change this
|
||||
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
@@ -50,3 +51,23 @@ func (a *Authentication) Authenticate(w http.ResponseWriter, r *http.Request) bo
|
||||
log.Printf("User %s logged in successfully", username)
|
||||
return true
|
||||
}
|
||||
|
||||
type LoginForm struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type Claims struct {
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func ValidateCredentials(lf *LoginForm) bool {
|
||||
p, ok := DATABASE[lf.Username]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
err := bcrypt.CompareHashAndPassword([]byte(p), []byte(lf.Password))
|
||||
|
||||
return err == nil
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ func EditorPage(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
mainPage := htmlTemplate("./assets/htmx/terminal.tmpl", map[string]interface{}{
|
||||
"content": htmlTemplate("./assets/htmx/login.html", nil),
|
||||
"content": htmlTemplate("./assets/htmx/editor.html", nil),
|
||||
"renderNavBar": true,
|
||||
"page": "fax",
|
||||
})
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package views
|
||||
|
||||
import "net/http"
|
||||
|
||||
func LoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
loginPage := htmlTemplate("./assets/htmx/terminal.tmpl", map[string]interface{}{
|
||||
"content": htmlTemplate("./assets/htmx/login.html", nil),
|
||||
"renderNavBar": true,
|
||||
"page": "fax",
|
||||
})
|
||||
|
||||
_, err := w.Write([]byte(loginPage))
|
||||
if err != nil {
|
||||
http.Error(w, "Internal error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user