Checkpoint 2

This commit is contained in:
Isaac Shoebottom 2024-08-22 20:50:40 -03:00
parent d38c1d7fd6
commit dd34fe2707
3 changed files with 149 additions and 75 deletions

1
format.ps1 Normal file
View File

@ -0,0 +1 @@
npx prettier . --write --use-tabs

View File

@ -1,13 +1,14 @@
<!DOCTYPE html> <!doctype html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{.Title}}</title> <title>{{.Title}}</title>
<style> <style>
* { * {
font-family: Tahoma, sans-serif; font-family: Tahoma, sans-serif;
font-size: large; font-size: large;
} }
html { html {
background-color: #222222; background-color: #222222;
color: #eeeeee; color: #eeeeee;
@ -15,26 +16,54 @@
</style> </style>
</head> </head>
<body> <body>
<div style="display: flex; align-items: center; flex-direction: column"> <div style="display: flex; align-items: center; flex-direction: column">
<p>{{.Message}}</p> <p>{{.Message}}</p>
{{if .ShowInput}} {{if not .Authenticated}}
<form action="/post" method="post"> <form
<label> action="/login"
How many hours should the timeout be? method="post"
<input type="number" name="next" min="1" max="24" value="24" step="1" required> style="display: flex; flex-direction: column; align-items: stretch"
</label> >
<button type="submit">Submit</button> <label>
</form> Username:
{{end}} <input type="text" name="username" required />
{{if .ShowLogin}} </label>
<form action="/login" method="post"> <label>
<label> Password:
<input type="text" name="username" required> <input type="password" name="password" required />
<input type="password" name="password" required> </label>
</label> <button type="submit">Login</button>
<button type="submit">Login</button> </form>
</form> {{end}} {{if .ShowInput}}
{{end}} <form action="/post" method="post">
<label>
</div> How many hours should the timeout be?
<input
type="number"
name="next"
min="1"
max="24"
value="24"
step="1"
required
/>
</label>
<button type="submit">Submit</button>
</form>
{{end}}
{{ range .Timestamps}}
<table style="text-align: center">
<tr>
<th>Taken</th>
<th>Ends</th>
<th>Has Ended</th>
</tr>
<tr>
<td>{{ index . 0}}</td>
<td>{{ index . 1}}</td>
<td>{{ index . 2}}</td>
</tr>
</table>
{{end}}
</div>
</body> </body>

142
main.go
View File

@ -24,10 +24,12 @@ type Store struct {
} }
type PageData struct { type PageData struct {
Title string Title string
Message string Message string
ShowLogin bool ShowLogin bool
ShowInput bool ShowInput bool
Authenticated bool
Timestamps [][]string
} }
var Username string var Username string
@ -41,14 +43,14 @@ func main() {
Username = os.Getenv("USERNAME") Username = os.Getenv("USERNAME")
fmt.Println("Username: ", Username) fmt.Println("Username: ", Username)
Password = os.Getenv("API_KEY") Password = os.Getenv("PASSWORD")
fmt.Println("API_KEY: ", Password) fmt.Println("Password: ", Password)
DataDir = os.Getenv("DATA_DIR") DataDir = os.Getenv("DATA_DIR")
if DataDir == "" { if DataDir == "" {
DataDir = "./data" DataDir = "./data"
} }
fmt.Println("DATA_DIR: ", DataDir) fmt.Println("DataDir: ", DataDir)
DataStore = DataDir + "/data.json" DataStore = DataDir + "/data.json"
@ -145,7 +147,7 @@ func commit(next int64) {
} }
} }
func query() bool { func queryCanPost() bool {
// Check if file exists // Check if file exists
if file, err := os.Open(DataStore); err != nil { if file, err := os.Open(DataStore); err != nil {
fmt.Println("Error opening file, check permissions") fmt.Println("Error opening file, check permissions")
@ -179,47 +181,57 @@ func query() bool {
return false return false
} }
func index(w http.ResponseWriter, r *http.Request) { func queryData() []Timestamp {
tmpl := template.Must(template.ParseFiles("index.html")) // Check if file exists
var data PageData if file, err := os.Open(DataStore); err != nil {
fmt.Println("GET request received") fmt.Println("Error opening file, check permissions")
unauthenticated := PageData{ os.Exit(1)
Title: "Log in",
Message: "Please enter your username and password",
ShowLogin: true,
ShowInput: false,
}
authenticated := PageData{
Title: "View data",
Message: "Welcome back " + Username + ". Check in after the timeout",
ShowLogin: false,
ShowInput: true,
}
// Check for session cookie
cookie, err := r.Cookie("session")
if err != nil {
data = unauthenticated
} else { } else {
// Parse cookie to int64 // Decode file
if session, err := strconv.ParseInt(cookie.Value, 10, 64); err != nil { decoder := json.NewDecoder(file)
fmt.Println("Error parsing cookie") store := Store{}
data = unauthenticated if err = decoder.Decode(&store); err != nil {
} else { fmt.Println("Error decoding file")
found := false os.Exit(1)
for _, uuid := range Authorised {
if uuid == session {
found = true
}
}
if found {
data = authenticated
} else {
// If cookie is not in Authorised list, remove it
data = unauthenticated
}
} }
if err = file.Close(); err != nil {
fmt.Println("Error closing file")
os.Exit(1)
}
return store.Timestamps
}
return nil
}
func index(w http.ResponseWriter, r *http.Request) {
fmt.Println("GET request received")
tmpl := template.Must(template.ParseFiles("index.html"))
auth := checkAuth(r)
data := PageData{
Authenticated: auth,
}
if auth {
data.Title = "View data"
data.Message = "Welcome back " + Username + ". Check in after the timeout"
data.ShowInput = queryCanPost()
var timestamps [][]string
for _, ts := range queryData() {
one := time.Unix(ts.Current, 0)
two := time.Unix(ts.Next, 0)
ended := time.Now().Unix() > ts.Next
str := strconv.FormatBool(ended)
timestamps = append(timestamps, []string{one.Format(time.DateTime), two.Format(time.DateTime), str})
}
data.Timestamps = timestamps
} else {
data.Title = "Log in"
data.Message = "Please enter your username and password"
data.ShowInput = false
data.Timestamps = nil
} }
authenticated.ShowInput = query()
if err := tmpl.Execute(w, data); err != nil { if err := tmpl.Execute(w, data); err != nil {
http.Error(w, "Error rendering template", http.StatusInternalServerError) http.Error(w, "Error rendering template", http.StatusInternalServerError)
} }
@ -227,6 +239,12 @@ func index(w http.ResponseWriter, r *http.Request) {
func post(w http.ResponseWriter, r *http.Request) { func post(w http.ResponseWriter, r *http.Request) {
fmt.Println("POST request received") fmt.Println("POST request received")
if !checkAuth(r) {
http.Error(w, "Not authorised", http.StatusUnauthorized)
return
}
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
http.Error(w, "Error parsing form", http.StatusBadRequest) http.Error(w, "Error parsing form", http.StatusBadRequest)
return return
@ -273,12 +291,38 @@ func login(w http.ResponseWriter, r *http.Request) {
func logout(w http.ResponseWriter, r *http.Request) { func logout(w http.ResponseWriter, r *http.Request) {
fmt.Println("LOGOUT request received") fmt.Println("LOGOUT request received")
if !checkAuth(r) {
http.Error(w, "Not authorised", http.StatusUnauthorized)
return
}
cookie := http.Cookie{ cookie := http.Cookie{
Name: "session", Name: "session",
Value: "", Value: "",
Expires: time.Now().Add(-1 * time.Hour),
} }
http.SetCookie(w, &cookie) http.SetCookie(w, &cookie)
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
return return
} }
func checkAuth(r *http.Request) bool {
// Check for session cookie
cookie, err := r.Cookie("session")
if err != nil {
return false
} else {
// Parse cookie to int64
if session, err := strconv.ParseInt(cookie.Value, 10, 64); err != nil {
fmt.Println("Error parsing cookie")
return false
} else {
for _, uuid := range Authorised {
if uuid == session {
return true
}
}
}
}
return false
}