6 Commits

Author SHA1 Message Date
6da7d408f9 Add .gitattributes to force LF line endings 2023-05-05 12:19:17 -05:00
max
e993bcf317 Update dependency versions 2023-05-04 09:00:35 -05:00
9b231a73d6 Update README.md 2023-04-07 21:32:39 -05:00
34acd0fa8d Remove old session query 2023-04-07 21:27:14 -05:00
71d3bd77d0 Add ability to get session given an AuthToken, fix GetCurrentUser() 2023-04-07 21:23:46 -05:00
1451abcca4 Formatting 2023-04-06 12:01:29 -05:00
7 changed files with 27 additions and 16 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
* text=auto eol=lf

View File

@ -15,6 +15,7 @@ fine with getting your hands dirty, but I plan on having it ready to go for more
- CSRF protection - CSRF protection
- Minimal user login/registration + sessions - Minimal user login/registration + sessions
- Config file handling - Config file handling
- Scheduled tasks
- Entire website compiles into a single binary (~10mb) (excluding env.json) - Entire website compiles into a single binary (~10mb) (excluding env.json)
- Minimal dependencies (just standard library, postgres driver, and experimental package for bcrypt) - Minimal dependencies (just standard library, postgres driver, and experimental package for bcrypt)

4
go.mod
View File

@ -3,6 +3,6 @@ module GoWeb
go 1.20 go 1.20
require ( require (
github.com/lib/pq v1.10.7 github.com/lib/pq v1.10.9
golang.org/x/crypto v0.7.0 golang.org/x/crypto v0.8.0
) )

8
go.sum
View File

@ -1,4 +1,4 @@
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=

View File

@ -78,6 +78,7 @@ func main() {
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
stop := make(chan struct{}) stop := make(chan struct{})
go app.RunScheduledTasks(&appLoaded, 100, stop) go app.RunScheduledTasks(&appLoaded, 100, stop)
<-interrupt <-interrupt
log.Println("Interrupt signal received. Shutting down server...") log.Println("Interrupt signal received. Shutting down server...")

View File

@ -64,6 +64,18 @@ func CreateSession(app *app.App, w http.ResponseWriter, userId int64, remember b
return session, nil return session, nil
} }
func GetSessionByAuthToken(app *app.App, authToken string) (Session, error) {
session := Session{}
err := app.Db.QueryRow(selectSessionByAuthToken, authToken).Scan(&session.Id, &session.UserId, &session.AuthToken, &session.RememberMe, &session.CreatedAt)
if err != nil {
log.Println("Error getting session by auth token")
return Session{}, err
}
return session, nil
}
// Generates a random 64-byte string // Generates a random 64-byte string
func generateAuthToken(app *app.App) string { func generateAuthToken(app *app.App) string {
// Generate random bytes // Generate random bytes

View File

@ -23,10 +23,9 @@ const userColumns = "\"Id\", " + userColumnsNoId
const userTable = "public.\"User\"" const userTable = "public.\"User\""
const ( const (
selectSessionIdByAuthToken = "SELECT \"Id\" FROM public.\"Session\" WHERE \"AuthToken\" = $1" selectUserById = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Id\" = $1"
selectUserById = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Id\" = $1" selectUserByUsername = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Username\" = $1"
selectUserByUsername = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Username\" = $1" insertUser = "INSERT INTO " + userTable + " (" + userColumnsNoId + ") VALUES ($1, $2, $3, $4) RETURNING \"Id\""
insertUser = "INSERT INTO " + userTable + " (" + userColumnsNoId + ") VALUES ($1, $2, $3, $4) RETURNING \"Id\""
) )
// GetCurrentUser finds the currently logged-in user by session cookie // GetCurrentUser finds the currently logged-in user by session cookie
@ -37,16 +36,13 @@ func GetCurrentUser(app *app.App, r *http.Request) (User, error) {
return User{}, err return User{}, err
} }
var userId int64 session, err := GetSessionByAuthToken(app, cookie.Value)
// Query row by AuthToken
err = app.Db.QueryRow(selectSessionIdByAuthToken, cookie.Value).Scan(&userId)
if err != nil { if err != nil {
log.Println("Error querying session row with session: " + cookie.Value) log.Println("Error getting session by auth token")
return User{}, err return User{}, err
} }
return GetUserById(app, userId) return GetUserById(app, session.UserId)
} }
// GetUserById finds a User table row in the database by id and returns a struct representing this row // GetUserById finds a User table row in the database by id and returns a struct representing this row