2024-09-26 01:11:22 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-09-27 15:57:10 -05:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2024-09-26 01:11:22 -05:00
|
|
|
"time"
|
2024-09-27 15:57:10 -05:00
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2024-09-26 01:11:22 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const SESSION_COOKIE_NAME = "utsa-place-session"
|
|
|
|
const SESSION_AUTH = "auth"
|
|
|
|
|
2024-09-27 15:57:10 -05:00
|
|
|
const ENCRYPTION_STRENGTH = 14
|
|
|
|
|
2024-09-26 01:11:22 -05:00
|
|
|
type UserData struct {
|
2024-09-27 15:57:10 -05:00
|
|
|
Email string
|
|
|
|
Password string
|
|
|
|
AccountCreated time.Time
|
|
|
|
LastLogin time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate_email(email string) (string, bool) {
|
|
|
|
email = strings.ToLower(email)
|
|
|
|
regex := regexp.MustCompile("^[a-z]+.[a-z]+@(my.)?utsa.edu")
|
|
|
|
ok := regex.MatchString(email)
|
|
|
|
return email, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash_password(password string) string {
|
|
|
|
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), ENCRYPTION_STRENGTH)
|
|
|
|
return string(bytes)
|
2024-09-26 01:11:22 -05:00
|
|
|
}
|
|
|
|
|
2024-09-27 14:37:49 -05:00
|
|
|
// Handles requests to /login.html
|
2024-09-26 01:11:22 -05:00
|
|
|
func (s *Server) handle_login(w http.ResponseWriter, r *http.Request) {
|
2024-09-27 14:37:49 -05:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
http.ServeFile(w, r, "./static/register.html")
|
|
|
|
case http.MethodPost:
|
|
|
|
// Get data from form
|
|
|
|
email := r.FormValue("email")
|
|
|
|
password := r.FormValue("password")
|
|
|
|
// Get user from database
|
|
|
|
user, ok := s.Users[email]
|
|
|
|
// If user does not exist
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If password does not match
|
|
|
|
if password != user.Password {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Generate session
|
|
|
|
session, err := s.Sessions.Get(r, SESSION_COOKIE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
s.handle_logout(w, r)
|
|
|
|
http.Error(w, "Invalid session", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
session.Values[SESSION_AUTH] = true
|
|
|
|
session.Save(r, w)
|
|
|
|
// Update last-login on DB
|
|
|
|
user.LastLogin = time.Now()
|
|
|
|
s.Users[email] = user
|
|
|
|
// Redirect to index.html
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
default:
|
2024-09-26 01:11:22 -05:00
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 14:37:49 -05:00
|
|
|
// Handles requests to /register.html
|
2024-09-26 01:11:22 -05:00
|
|
|
func (s *Server) handle_register(w http.ResponseWriter, r *http.Request) {
|
2024-09-27 14:37:49 -05:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
http.ServeFile(w, r, "./static/register.html")
|
|
|
|
case http.MethodPost:
|
|
|
|
// Get data from form
|
2024-09-27 15:57:10 -05:00
|
|
|
email, ok := validate_email(r.FormValue("email"))
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "Invalid email address", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2024-09-27 14:37:49 -05:00
|
|
|
password := r.FormValue("password")
|
2024-09-27 15:57:10 -05:00
|
|
|
if len(password) < 5 || len(password) >= 70 {
|
|
|
|
http.Error(w, "Invalid password length", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2024-09-27 14:37:49 -05:00
|
|
|
// Check that this email is not already registered
|
|
|
|
if _, ok := s.Users[email]; ok {
|
2024-09-27 15:57:10 -05:00
|
|
|
http.Error(w, "Already registered", http.StatusForbidden)
|
2024-09-27 14:37:49 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Generate session
|
|
|
|
session, err := s.Sessions.Get(r, SESSION_COOKIE_NAME)
|
|
|
|
// If session cookie invalid
|
|
|
|
if err != nil {
|
|
|
|
s.handle_logout(w, r)
|
|
|
|
http.Error(w, "Invalid session", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Save user information to DB
|
|
|
|
s.Users[email] = UserData{
|
|
|
|
Email: email,
|
|
|
|
Password: password,
|
|
|
|
AccountCreated: time.Now(),
|
|
|
|
LastLogin: time.Now(),
|
|
|
|
}
|
|
|
|
// Make session valid
|
|
|
|
session.Values[SESSION_AUTH] = true
|
|
|
|
// Send session token to browser
|
|
|
|
session.Save(r, w)
|
|
|
|
// Redirect to index.html
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
default:
|
2024-09-26 01:11:22 -05:00
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
}
|
2024-09-27 14:37:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handle_logout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// If session exists
|
|
|
|
if session, err := s.Sessions.Get(r, SESSION_COOKIE_NAME); err == nil {
|
|
|
|
// Remove authorization
|
|
|
|
session.Values[SESSION_AUTH] = false
|
|
|
|
session.Save(r, w)
|
2024-09-26 01:11:22 -05:00
|
|
|
}
|
2024-09-27 14:37:49 -05:00
|
|
|
// Remove session cookie
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: SESSION_COOKIE_NAME,
|
|
|
|
MaxAge: -1,
|
|
|
|
})
|
|
|
|
}
|