ThePlaceHolders/users.go

208 lines
5.5 KiB
Go
Raw Normal View History

2024-09-26 01:11:22 -05:00
package main
import (
"fmt"
2024-09-26 01:11:22 -05:00
"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"
2024-11-06 18:02:08 -06:00
const SESSION_EMAIL = "email"
2024-09-26 01:11:22 -05:00
const SESSION_AUTH = "auth"
const SESSION_STARTED = "age"
2024-09-26 01:11:22 -05:00
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
2024-11-06 18:02:08 -06:00
EmailCode string
Verified bool
2024-09-27 15:57:10 -05:00
}
func validate_email(email string) (string, bool) {
email = strings.ToLower(email)
2024-11-06 18:02:08 -06:00
regex := regexp.MustCompile("^[a-z0-9]+.[a-z0-9]+@(my.)?utsa.edu")
2024-09-27 15:57:10 -05:00
ok := regex.MatchString(email)
return email, ok
}
// Encrypts a password
2024-09-27 15:57:10 -05:00
func hash_password(password string) string {
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), ENCRYPTION_STRENGTH)
return string(bytes)
2024-09-26 01:11:22 -05:00
}
// Compares an unencrpyted password to an encrypted password
func check_password_hash(password string, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
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/login.html")
2024-09-27 14:37:49 -05:00
case http.MethodPost:
// Get data from form
2024-11-06 18:02:08 -06:00
email := strings.ToLower(r.FormValue("email"))
2024-09-27 14:37:49 -05:00
password := r.FormValue("password")
// Get user from database
user, ok := s.Users[email]
// If user does not exist
if !ok {
http.Error(w, "User not found", http.StatusForbidden)
2024-09-27 14:37:49 -05:00
return
}
// If password does not match
if !check_password_hash(password, user.Password) {
http.Error(w, "Passwords dont match", http.StatusForbidden)
2024-09-27 14:37:49 -05:00
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
}
now := time.Now()
2024-09-27 14:37:49 -05:00
session.Values[SESSION_AUTH] = true
2024-09-29 19:05:16 -05:00
session.Values[SESSION_STARTED] = now.String()
2024-11-06 18:02:08 -06:00
session.Values[SESSION_EMAIL] = user.Email
2024-09-27 14:37:49 -05:00
session.Save(r, w)
// Update last-login on DB
user.LastLogin = now
2024-11-06 18:02:08 -06:00
// If email not verified, go to verified page
if user.Verified {
s.handle_confirmation(w, r)
return
}
2024-09-27 14:37:49 -05:00
s.Users[email] = user
// Redirect to index.html
fmt.Println("Logged in user: ", email)
2024-09-27 14:37:49 -05:00
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")
if len(password) < 8 || len(password) >= 70 {
2024-09-27 15:57:10 -05:00
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
}
now := time.Now()
2024-09-27 14:37:49 -05:00
// Save user information to DB
s.Users[email] = UserData{
Email: email,
Password: hash_password(password),
AccountCreated: now,
LastLogin: now,
2024-11-06 18:02:08 -06:00
EmailCode: "123456",
Verified: false,
2024-09-27 14:37:49 -05:00
}
// Make session valid
session.Values[SESSION_AUTH] = true
2024-09-29 19:05:16 -05:00
session.Values[SESSION_STARTED] = now.String()
2024-11-06 18:02:08 -06:00
session.Values[SESSION_EMAIL] = email
2024-09-27 14:37:49 -05:00
// Send session token to browser
session.Save(r, w)
// Redirect to index.html
fmt.Println("Registered user: ", email)
http.Redirect(w, r, "/confirm-email", http.StatusFound)
2024-09-27 14:37:49 -05:00
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_confirmation(w http.ResponseWriter, r *http.Request) {
2024-10-27 03:47:12 -05:00
session, err := s.Sessions.Get(r, SESSION_COOKIE_NAME)
if err != nil {
s.handle_logout(w, r)
http.Redirect(w, r, "/register", http.StatusFound)
return
}
2024-11-06 18:02:08 -06:00
email := session.Values[SESSION_EMAIL].(string)
user, ok := s.Users[email]
if !ok {
return
}
2024-10-23 18:09:48 -05:00
switch r.Method {
case http.MethodGet:
2024-11-06 18:02:08 -06:00
fmt.Println("User email confirmed: ", user.Verified)
if user.Verified {
2024-10-27 03:47:12 -05:00
http.Redirect(w, r, "/", http.StatusFound)
} else {
http.ServeFile(w, r, "./static/confirmation.html")
2024-10-23 18:09:48 -05:00
}
case http.MethodPost:
2024-11-06 18:02:08 -06:00
code := r.FormValue("code")
if user.EmailCode == code {
http.Redirect(w, r, "/", http.StatusAccepted)
} else {
http.Redirect(w, r, "/confirm-email", http.StatusForbidden)
}
}
}
2024-09-29 19:05:16 -05:00
func (s *Server) secret(w http.ResponseWriter, r *http.Request) {
session, _ := s.Sessions.Get(r, SESSION_COOKIE_NAME)
// Check if user is authenticated
if auth, ok := session.Values[SESSION_AUTH].(bool); !ok || !auth {
http.Error(w, "Not logged in", http.StatusForbidden)
return
}
// Print secret message
fmt.Fprintln(w, "Successfully logged in!")
}
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,
// Negative max age immediately removes the cookie
2024-09-27 14:37:49 -05:00
MaxAge: -1,
})
}