Session timestamp and user IDs

This commit is contained in:
Magnus Åhall 2024-01-05 09:00:09 +01:00
parent 6424512612
commit 61a36b87bb
4 changed files with 12 additions and 7 deletions

View File

@ -55,7 +55,7 @@ func webserviceSQLProvider(dbname string, version int) ([]byte, bool) { // {{{
"name" varchar NOT NULL,
"username" varchar NOT NULL,
"password" char(96) NOT NULL,
last_login timetz NOT NULL DEFAULT '1970-01-01 00:00:00',
last_login timestamp with time zone NOT NULL DEFAULT '1970-01-01 00:00:00',
CONSTRAINT user_pk PRIMARY KEY (id),
CONSTRAINT user_un UNIQUE (username)
);
@ -64,7 +64,8 @@ func webserviceSQLProvider(dbname string, version int) ([]byte, bool) { // {{{
id serial NOT NULL,
user_id int4 NULL,
"uuid" char(36) NOT NULL,
created time with time zone NOT NULL DEFAULT NOW(),
created timestamp with time zone NOT NULL DEFAULT NOW(),
last_used timestamp with time zone NOT NULL DEFAULT NOW(),
CONSTRAINT session_pk PRIMARY KEY (id),
CONSTRAINT session_un UNIQUE ("uuid"),
CONSTRAINT session_user_fk FOREIGN KEY (user_id) REFERENCES "_webservice"."user"(id) ON DELETE CASCADE ON UPDATE CASCADE
@ -92,9 +93,6 @@ func webserviceSQLProvider(dbname string, version int) ([]byte, bool) { // {{{
END;
$$;
`,
2: `
ALTER TABLE _webservice.session ADD last_used timetz NOT NULL DEFAULT NOW();
`,
}
statement, found := sql[version]
@ -154,6 +152,7 @@ func (db *T) Connect() (err error) { // {{{
return
} // }}}
func (db *T) Authenticate(username, password string) (authenticated bool, userID int, err error) { // {{{
var rows *sql.Rows
if rows, err = db.Conn.Query(`
@ -195,7 +194,8 @@ func (db *T) RetrieveSession(uuid string) (sess *session.T, err error) {// {{{
SELECT
sd.uuid, sd.created, sd.last_used,
COALESCE(u.username, '') AS username,
COALESCE(u.name, '') AS name
COALESCE(u.name, '') AS name,
COALESCE(u.id, 0) AS user_id
FROM session_data sd
LEFT JOIN _webservice.user u ON sd.user_id = u.id
`,
@ -209,6 +209,7 @@ func (db *T) RetrieveSession(uuid string) (sess *session.T, err error) {// {{{
for rows.Next() {
sess = new(session.T)
err = rows.StructScan(sess)
sess.Authenticated = sess.UserID > 0
}
return
}// }}}
@ -219,6 +220,7 @@ func (db *T) SetSessionUser(uuid string, userID int) (err error) { // {{{
}
return
} // }}}
func (db *T) CreateUser(username, password, name string) (err error) {// {{{
_, err = db.Conn.Exec(`
INSERT INTO _webservice.user(username, password, name)

2
pkg.go
View File

@ -91,7 +91,7 @@ func New(configFilename string) (service *Service, err error) { // {{{
func (service *Service) defaultAuthenticationHandler(req AuthenticationRequest, alreadyAuthenticated bool) (resp AuthenticationResponse, err error) { // {{{
resp.Authenticated = alreadyAuthenticated
service.logger.Info("webservice", "op", "authentication", "request", req, "authenticated", resp.Authenticated)
service.logger.Info("webservice", "op", "authentication", "username", req.Username, "authenticated", resp.Authenticated)
return
} // }}}
func (service *Service) defaultAuthorizationHandler(sess *session.T, r *http.Request) (resp bool, err error) { // {{{

View File

@ -58,6 +58,8 @@ func (service *Service) sessionNew(w http.ResponseWriter, r *http.Request, sess
}
service.logger.Info("session", "op", "new", "uuid", session.UUID)
respJSON, _ := json.Marshal(
struct {
OK bool

View File

@ -11,6 +11,7 @@ type T struct {
LastUsed time.Time `db:"last_used"`
Authenticated bool
UserID int `db:"user_id"`
Username string
Name string
}