2023-01-17 13:46:22 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
Implement login cookie, its verification, and logout
At first i thought that i would need to implement sessions, the ones
that keep small files onto the disk, to know which user is talking to
the server, but then i realized that, for now at least, i only need a
very large number, plus the email address, to be used as a lookup, and
that can be stored in the user table, in a separate schema.
Had to change login to avoid raising exceptions when login failed
because i now keep a record of login attemps, and functions are always
run in a single transaction, thus the exception would prevent me to
insert into login_attempt. Even if i use a separate procedure, i could
not keep the records.
I did not want to add a parameter to the logout function because i was
afraid that it could be called from separate users. I do not know
whether it is possible with the current approach, since the settings
variable is also set by the same applications; time will tell.
2023-01-17 19:48:50 +00:00
|
|
|
"net/http"
|
2023-01-17 13:46:22 +00:00
|
|
|
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
)
|
|
|
|
|
Implement login cookie, its verification, and logout
At first i thought that i would need to implement sessions, the ones
that keep small files onto the disk, to know which user is talking to
the server, but then i realized that, for now at least, i only need a
very large number, plus the email address, to be used as a lookup, and
that can be stored in the user table, in a separate schema.
Had to change login to avoid raising exceptions when login failed
because i now keep a record of login attemps, and functions are always
run in a single transaction, thus the exception would prevent me to
insert into login_attempt. Even if i use a separate procedure, i could
not keep the records.
I did not want to add a parameter to the logout function because i was
afraid that it could be called from separate users. I do not know
whether it is possible with the current approach, since the settings
variable is also set by the same applications; time will tell.
2023-01-17 19:48:50 +00:00
|
|
|
type Db struct {
|
|
|
|
pool *pgxpool.Pool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatabase(ctx context.Context, connString string) (*Db, error) {
|
2023-01-17 13:46:22 +00:00
|
|
|
config, err := pgxpool.ParseConfig(connString)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
|
|
|
|
_, err := conn.Exec(context.Background(), "SET search_path TO numerus, public")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.BeforeAcquire = func(ctx context.Context, conn *pgx.Conn) bool {
|
Implement login cookie, its verification, and logout
At first i thought that i would need to implement sessions, the ones
that keep small files onto the disk, to know which user is talking to
the server, but then i realized that, for now at least, i only need a
very large number, plus the email address, to be used as a lookup, and
that can be stored in the user table, in a separate schema.
Had to change login to avoid raising exceptions when login failed
because i now keep a record of login attemps, and functions are always
run in a single transaction, thus the exception would prevent me to
insert into login_attempt. Even if i use a separate procedure, i could
not keep the records.
I did not want to add a parameter to the logout function because i was
afraid that it could be called from separate users. I do not know
whether it is possible with the current approach, since the settings
variable is also set by the same applications; time will tell.
2023-01-17 19:48:50 +00:00
|
|
|
if user, ok := ctx.Value(ContextUserKey).(*AppUser); ok {
|
|
|
|
batch := &pgx.Batch{}
|
|
|
|
batch.Queue("select set_config('request.user', $1, false)", user.Email)
|
|
|
|
batch.Queue("select set_config('role', $1, false)", user.Role)
|
|
|
|
br := conn.SendBatch(ctx, batch)
|
|
|
|
defer br.Close()
|
|
|
|
for i := 0; i < batch.Len(); i++ {
|
|
|
|
if _, err := br.Exec(); err != nil {
|
|
|
|
log.Printf("ERROR - Failed to set role: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 13:46:22 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
config.AfterRelease = func(conn *pgx.Conn) bool {
|
|
|
|
if _, err := conn.Exec(context.Background(), "RESET ROLE"); err != nil {
|
|
|
|
log.Printf("ERROR - Failed to reset role: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
Implement login cookie, its verification, and logout
At first i thought that i would need to implement sessions, the ones
that keep small files onto the disk, to know which user is talking to
the server, but then i realized that, for now at least, i only need a
very large number, plus the email address, to be used as a lookup, and
that can be stored in the user table, in a separate schema.
Had to change login to avoid raising exceptions when login failed
because i now keep a record of login attemps, and functions are always
run in a single transaction, thus the exception would prevent me to
insert into login_attempt. Even if i use a separate procedure, i could
not keep the records.
I did not want to add a parameter to the logout function because i was
afraid that it could be called from separate users. I do not know
whether it is possible with the current approach, since the settings
variable is also set by the same applications; time will tell.
2023-01-17 19:48:50 +00:00
|
|
|
pool, err := pgxpool.ConnectConfig(ctx, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Db{pool}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Db) Close() {
|
|
|
|
db.pool.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Db) Text(r *http.Request, def string, sql string, args ...interface{}) string {
|
|
|
|
var result string
|
|
|
|
if err := db.pool.QueryRow(r.Context(), sql, args...).Scan(&result); err != nil {
|
|
|
|
if err == pgx.ErrNoRows {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Db) Exec(r *http.Request, sql string, args ...interface{}) {
|
|
|
|
if _, err := db.pool.Exec(r.Context(), sql, args...); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-01-17 13:46:22 +00:00
|
|
|
}
|