Notes/key.go

72 lines
1.3 KiB
Go
Raw Normal View History

2023-07-01 20:33:26 +02:00
package main
import (
// External
"github.com/jmoiron/sqlx"
// Standard
)
type Key struct {
ID int
UserID int `db:"user_id"`
Description string
Key string
}
2023-07-12 22:35:38 +02:00
func (session Session) Keys() (keys []Key, err error) {// {{{
2023-07-01 20:33:26 +02:00
var rows *sqlx.Rows
if rows, err = db.Queryx(`SELECT * FROM crypto_key WHERE user_id=$1`, session.UserID); err != nil {
return
}
defer rows.Close()
keys = []Key{}
for rows.Next() {
key := Key{}
if err = rows.StructScan(&key); err != nil {
return
}
keys = append(keys, key)
}
return
2023-07-12 22:35:38 +02:00
}// }}}
func (session Session) KeyCreate(description, keyEncoded string) (key Key, err error) {// {{{
var row *sqlx.Rows
if row, err = db.Queryx(
`INSERT INTO crypto_key(user_id, description, key)
VALUES($1, $2, $3)
RETURNING *`,
session.UserID,
description,
keyEncoded,
); err != nil {
return
}
defer row.Close()
key = Key{}
for row.Next() {
if err = row.StructScan(&key); err != nil {
return
}
}
return
}// }}}
func (session Session) KeyCounter() (counter int64, err error) {// {{{
var rows *sqlx.Rows
rows, err = db.Queryx(`SELECT nextval('aes_ccm_counter') AS counter`)
if err != nil {
return
}
defer rows.Close()
rows.Next()
err = rows.Scan(&counter)
return
}// }}}
// vim: foldmethod=marker