|
|
|
|
@@ -1,7 +1,9 @@
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
_ "github.com/mattn/go-sqlite3" // Import the SQLite3 driver
|
|
|
|
|
@@ -9,432 +11,387 @@ import (
|
|
|
|
|
"go-nkode/internal/entities"
|
|
|
|
|
"go-nkode/internal/models"
|
|
|
|
|
"go-nkode/internal/security"
|
|
|
|
|
"go-nkode/internal/sqlc"
|
|
|
|
|
"go-nkode/internal/utils"
|
|
|
|
|
"log"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SqliteDB struct {
|
|
|
|
|
db *sql.DB
|
|
|
|
|
stop bool
|
|
|
|
|
writeQueue chan WriteTx
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
}
|
|
|
|
|
const writeBufferSize = 100
|
|
|
|
|
|
|
|
|
|
type sqlcGeneric func(*sqlc.Queries, context.Context, any) error
|
|
|
|
|
|
|
|
|
|
// WriteTx represents a write transaction
|
|
|
|
|
type WriteTx struct {
|
|
|
|
|
ErrChan chan error
|
|
|
|
|
Query string
|
|
|
|
|
Args []any
|
|
|
|
|
Query sqlcGeneric
|
|
|
|
|
Args interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
writeBuffer = 1000
|
|
|
|
|
)
|
|
|
|
|
// SqliteDB represents the SQLite database connection and write queue
|
|
|
|
|
type SqliteDB struct {
|
|
|
|
|
queries *sqlc.Queries
|
|
|
|
|
db *sql.DB
|
|
|
|
|
writeQueue chan WriteTx
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
ctx context.Context
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewSqliteDB initializes a new SqliteDB instance
|
|
|
|
|
func NewSqliteDB(path string) (*SqliteDB, error) {
|
|
|
|
|
if path == "" {
|
|
|
|
|
return nil, errors.New("database path is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSqliteDB(path string) *SqliteDB {
|
|
|
|
|
db, err := sql.Open("sqlite3", path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("database didn't open ", err)
|
|
|
|
|
return nil, fmt.Errorf("failed to open database: %w", err)
|
|
|
|
|
}
|
|
|
|
|
sqldb := SqliteDB{
|
|
|
|
|
|
|
|
|
|
if err := db.Ping(); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
sqldb := &SqliteDB{
|
|
|
|
|
queries: sqlc.New(db),
|
|
|
|
|
db: db,
|
|
|
|
|
stop: false,
|
|
|
|
|
writeQueue: make(chan WriteTx, writeBuffer),
|
|
|
|
|
writeQueue: make(chan WriteTx, writeBufferSize),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
cancel: cancel,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
for writeTx := range sqldb.writeQueue {
|
|
|
|
|
writeTx.ErrChan <- sqldb.writeToDb(writeTx.Query, writeTx.Args)
|
|
|
|
|
sqldb.wg.Done()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
sqldb.wg.Add(1)
|
|
|
|
|
go sqldb.processWriteQueue()
|
|
|
|
|
|
|
|
|
|
return &sqldb
|
|
|
|
|
return sqldb, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) CloseDb() {
|
|
|
|
|
d.stop = true
|
|
|
|
|
// processWriteQueue handles write transactions from the queue
|
|
|
|
|
func (d *SqliteDB) processWriteQueue() {
|
|
|
|
|
defer d.wg.Done()
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-d.ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
case writeTx := <-d.writeQueue:
|
|
|
|
|
err := writeTx.Query(d.queries, d.ctx, writeTx.Args)
|
|
|
|
|
writeTx.ErrChan <- err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) Close() error {
|
|
|
|
|
d.cancel()
|
|
|
|
|
d.wg.Wait()
|
|
|
|
|
if err := d.db.Close(); err != nil {
|
|
|
|
|
// If db.Close() returns an error, panic
|
|
|
|
|
panic(fmt.Sprintf("Failed to close the database: %v", err))
|
|
|
|
|
}
|
|
|
|
|
close(d.writeQueue)
|
|
|
|
|
return d.db.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) WriteNewCustomer(c entities.Customer) error {
|
|
|
|
|
query := `
|
|
|
|
|
INSERT INTO customer (
|
|
|
|
|
id
|
|
|
|
|
,max_nkode_len
|
|
|
|
|
,min_nkode_len
|
|
|
|
|
,distinct_sets
|
|
|
|
|
,distinct_attributes
|
|
|
|
|
,lock_out
|
|
|
|
|
,expiration
|
|
|
|
|
,attribute_values
|
|
|
|
|
,set_values
|
|
|
|
|
,last_renew
|
|
|
|
|
,created_at
|
|
|
|
|
)
|
|
|
|
|
VALUES (?,?,?,?,?,?,?,?,?,?,?)
|
|
|
|
|
`
|
|
|
|
|
args := []any{
|
|
|
|
|
uuid.UUID(c.Id), c.NKodePolicy.MaxNkodeLen, c.NKodePolicy.MinNkodeLen, c.NKodePolicy.DistinctSets,
|
|
|
|
|
c.NKodePolicy.DistinctAttributes, c.NKodePolicy.LockOut, c.NKodePolicy.Expiration,
|
|
|
|
|
c.Attributes.AttrBytes(), c.Attributes.SetBytes(), timeStamp(), timeStamp(),
|
|
|
|
|
func (d *SqliteDB) CreateCustomer(c entities.Customer) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
params, ok := args.(sqlc.CreateCustomerParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected CreateCustomerParams")
|
|
|
|
|
}
|
|
|
|
|
return d.addWriteTx(query, args)
|
|
|
|
|
return q.CreateCustomer(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d.enqueueWriteTx(queryFunc, c.ToSqlcCreateCustomerParams())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) WriteNewUser(u entities.User) error {
|
|
|
|
|
query := `
|
|
|
|
|
INSERT INTO user (
|
|
|
|
|
id
|
|
|
|
|
,email
|
|
|
|
|
,renew
|
|
|
|
|
,refresh_token
|
|
|
|
|
,customer_id
|
|
|
|
|
,code
|
|
|
|
|
,mask
|
|
|
|
|
,attributes_per_key
|
|
|
|
|
,number_of_keys
|
|
|
|
|
,alpha_key
|
|
|
|
|
,set_key
|
|
|
|
|
,pass_key
|
|
|
|
|
,mask_key
|
|
|
|
|
,salt
|
|
|
|
|
,max_nkode_len
|
|
|
|
|
,idx_interface
|
|
|
|
|
,svg_id_interface
|
|
|
|
|
,created_at
|
|
|
|
|
)
|
|
|
|
|
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
|
|
|
|
|
`
|
|
|
|
|
var renew int
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
params, ok := args.(sqlc.CreateUserParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected CreateUserParams")
|
|
|
|
|
}
|
|
|
|
|
return q.CreateUser(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
// Use the wrapped function in enqueueWriteTx
|
|
|
|
|
|
|
|
|
|
renew := 0
|
|
|
|
|
if u.Renew {
|
|
|
|
|
renew = 1
|
|
|
|
|
} else {
|
|
|
|
|
renew = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := []any{
|
|
|
|
|
uuid.UUID(u.Id), u.Email, renew, u.RefreshToken, uuid.UUID(u.CustomerId),
|
|
|
|
|
u.EncipheredPasscode.Code, u.EncipheredPasscode.Mask, u.Kp.AttrsPerKey, u.Kp.NumbOfKeys,
|
|
|
|
|
security.Uint64ArrToByteArr(u.CipherKeys.AlphaKey), security.Uint64ArrToByteArr(u.CipherKeys.SetKey),
|
|
|
|
|
security.Uint64ArrToByteArr(u.CipherKeys.PassKey), security.Uint64ArrToByteArr(u.CipherKeys.MaskKey),
|
|
|
|
|
u.CipherKeys.Salt, u.CipherKeys.MaxNKodeLen, security.IntArrToByteArr(u.Interface.IdxInterface),
|
|
|
|
|
security.IntArrToByteArr(u.Interface.SvgId), timeStamp(),
|
|
|
|
|
// Map entities.User to CreateUserParams
|
|
|
|
|
params := sqlc.CreateUserParams{
|
|
|
|
|
ID: uuid.UUID(u.Id).String(),
|
|
|
|
|
Email: string(u.Email),
|
|
|
|
|
Renew: int64(renew),
|
|
|
|
|
RefreshToken: sql.NullString{String: u.RefreshToken, Valid: u.RefreshToken != ""},
|
|
|
|
|
CustomerID: uuid.UUID(u.CustomerId).String(),
|
|
|
|
|
Code: u.EncipheredPasscode.Code,
|
|
|
|
|
Mask: u.EncipheredPasscode.Mask,
|
|
|
|
|
AttributesPerKey: int64(u.Kp.AttrsPerKey),
|
|
|
|
|
NumberOfKeys: int64(u.Kp.NumbOfKeys),
|
|
|
|
|
AlphaKey: security.Uint64ArrToByteArr(u.CipherKeys.AlphaKey),
|
|
|
|
|
SetKey: security.Uint64ArrToByteArr(u.CipherKeys.SetKey),
|
|
|
|
|
PassKey: security.Uint64ArrToByteArr(u.CipherKeys.PassKey),
|
|
|
|
|
MaskKey: security.Uint64ArrToByteArr(u.CipherKeys.MaskKey),
|
|
|
|
|
Salt: u.CipherKeys.Salt,
|
|
|
|
|
MaxNkodeLen: int64(u.CipherKeys.MaxNKodeLen),
|
|
|
|
|
IdxInterface: security.IntArrToByteArr(u.Interface.IdxInterface),
|
|
|
|
|
SvgIDInterface: security.IntArrToByteArr(u.Interface.SvgId),
|
|
|
|
|
CreatedAt: sql.NullString{String: utils.TimeStamp(), Valid: true},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d.addWriteTx(query, args)
|
|
|
|
|
return d.enqueueWriteTx(queryFunc, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) UpdateUserNKode(u entities.User) error {
|
|
|
|
|
query := `
|
|
|
|
|
UPDATE user
|
|
|
|
|
SET renew = ?
|
|
|
|
|
,refresh_token = ?
|
|
|
|
|
,code = ?
|
|
|
|
|
,mask = ?
|
|
|
|
|
,attributes_per_key = ?
|
|
|
|
|
,number_of_keys = ?
|
|
|
|
|
,alpha_key = ?
|
|
|
|
|
,set_key = ?
|
|
|
|
|
,pass_key = ?
|
|
|
|
|
,mask_key = ?
|
|
|
|
|
,salt = ?
|
|
|
|
|
,max_nkode_len = ?
|
|
|
|
|
,idx_interface = ?
|
|
|
|
|
,svg_id_interface = ?
|
|
|
|
|
WHERE email = ? AND customer_id = ?
|
|
|
|
|
`
|
|
|
|
|
var renew int
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
params, ok := args.(sqlc.UpdateUserParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected UpdateUserParams")
|
|
|
|
|
}
|
|
|
|
|
return q.UpdateUser(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
// Use the wrapped function in enqueueWriteTx
|
|
|
|
|
renew := 0
|
|
|
|
|
if u.Renew {
|
|
|
|
|
renew = 1
|
|
|
|
|
} else {
|
|
|
|
|
renew = 0
|
|
|
|
|
}
|
|
|
|
|
args := []any{renew, u.RefreshToken, u.EncipheredPasscode.Code, u.EncipheredPasscode.Mask, u.Kp.AttrsPerKey, u.Kp.NumbOfKeys, security.Uint64ArrToByteArr(u.CipherKeys.AlphaKey), security.Uint64ArrToByteArr(u.CipherKeys.SetKey), security.Uint64ArrToByteArr(u.CipherKeys.PassKey), security.Uint64ArrToByteArr(u.CipherKeys.MaskKey), u.CipherKeys.Salt, u.CipherKeys.MaxNKodeLen, security.IntArrToByteArr(u.Interface.IdxInterface), security.IntArrToByteArr(u.Interface.SvgId), string(u.Email), uuid.UUID(u.CustomerId)}
|
|
|
|
|
|
|
|
|
|
return d.addWriteTx(query, args)
|
|
|
|
|
params := sqlc.UpdateUserParams{
|
|
|
|
|
Email: string(u.Email),
|
|
|
|
|
Renew: int64(renew),
|
|
|
|
|
RefreshToken: sql.NullString{String: u.RefreshToken, Valid: u.RefreshToken != ""},
|
|
|
|
|
CustomerID: uuid.UUID(u.CustomerId).String(),
|
|
|
|
|
Code: u.EncipheredPasscode.Code,
|
|
|
|
|
Mask: u.EncipheredPasscode.Mask,
|
|
|
|
|
AttributesPerKey: int64(u.Kp.AttrsPerKey),
|
|
|
|
|
NumberOfKeys: int64(u.Kp.NumbOfKeys),
|
|
|
|
|
AlphaKey: security.Uint64ArrToByteArr(u.CipherKeys.AlphaKey),
|
|
|
|
|
SetKey: security.Uint64ArrToByteArr(u.CipherKeys.SetKey),
|
|
|
|
|
PassKey: security.Uint64ArrToByteArr(u.CipherKeys.PassKey),
|
|
|
|
|
MaskKey: security.Uint64ArrToByteArr(u.CipherKeys.MaskKey),
|
|
|
|
|
Salt: u.CipherKeys.Salt,
|
|
|
|
|
MaxNkodeLen: int64(u.CipherKeys.MaxNKodeLen),
|
|
|
|
|
IdxInterface: security.IntArrToByteArr(u.Interface.IdxInterface),
|
|
|
|
|
SvgIDInterface: security.IntArrToByteArr(u.Interface.SvgId),
|
|
|
|
|
}
|
|
|
|
|
return d.enqueueWriteTx(queryFunc, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) UpdateUserInterface(id models.UserId, ui entities.UserInterface) error {
|
|
|
|
|
query := `
|
|
|
|
|
UPDATE user SET idx_interface = ?, last_login = ? WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
args := []any{security.IntArrToByteArr(ui.IdxInterface), timeStamp(), uuid.UUID(id).String()}
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
params, ok := args.(sqlc.UpdateUserInterfaceParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected UpdateUserInterfaceParams")
|
|
|
|
|
}
|
|
|
|
|
return q.UpdateUserInterface(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
params := sqlc.UpdateUserInterfaceParams{
|
|
|
|
|
IdxInterface: security.IntArrToByteArr(ui.IdxInterface),
|
|
|
|
|
LastLogin: utils.TimeStamp(),
|
|
|
|
|
ID: uuid.UUID(id).String(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d.addWriteTx(query, args)
|
|
|
|
|
return d.enqueueWriteTx(queryFunc, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) UpdateUserRefreshToken(id models.UserId, refreshToken string) error {
|
|
|
|
|
query := `
|
|
|
|
|
UPDATE user SET refresh_token = ? WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
args := []any{refreshToken, uuid.UUID(id).String()}
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
params, ok := args.(sqlc.UpdateUserRefreshTokenParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected UpdateUserRefreshToken")
|
|
|
|
|
}
|
|
|
|
|
return q.UpdateUserRefreshToken(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
params := sqlc.UpdateUserRefreshTokenParams{
|
|
|
|
|
RefreshToken: sql.NullString{
|
|
|
|
|
String: refreshToken,
|
|
|
|
|
Valid: true,
|
|
|
|
|
},
|
|
|
|
|
ID: uuid.UUID(id).String(),
|
|
|
|
|
}
|
|
|
|
|
return d.enqueueWriteTx(queryFunc, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d.addWriteTx(query, args)
|
|
|
|
|
func (d *SqliteDB) RenewCustomer(renewParams sqlc.RenewCustomerParams) error {
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
params, ok := args.(sqlc.RenewCustomerParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return q.RenewCustomer(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
return d.enqueueWriteTx(queryFunc, renewParams)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) Renew(id models.CustomerId) error {
|
|
|
|
|
// TODO: How long does a renew take?
|
|
|
|
|
customer, err := d.GetCustomer(id)
|
|
|
|
|
setXor, attrXor, err := d.renewCustomer(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
setXor, attrXor, err := customer.RenewKeys()
|
|
|
|
|
customerId := models.CustomerIdToString(id)
|
|
|
|
|
userRenewRows, err := d.queries.GetUserRenew(d.ctx, customerId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
renewArgs := []any{security.Uint64ArrToByteArr(customer.Attributes.AttrVals), security.Uint64ArrToByteArr(customer.Attributes.SetVals), uuid.UUID(customer.Id).String()}
|
|
|
|
|
// TODO: replace with tx
|
|
|
|
|
renewQuery := `
|
|
|
|
|
UPDATE customer
|
|
|
|
|
SET attribute_values = ?, set_values = ?
|
|
|
|
|
WHERE id = ?;
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
userQuery := `
|
|
|
|
|
SELECT
|
|
|
|
|
id
|
|
|
|
|
,alpha_key
|
|
|
|
|
,set_key
|
|
|
|
|
,attributes_per_key
|
|
|
|
|
,number_of_keys
|
|
|
|
|
FROM user
|
|
|
|
|
WHERE customer_id = ?
|
|
|
|
|
`
|
|
|
|
|
tx, err := d.db.Begin()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
params, ok := args.(sqlc.RenewUserParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected RenewUserParams")
|
|
|
|
|
}
|
|
|
|
|
rows, err := tx.Query(userQuery, uuid.UUID(id).String())
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var userId string
|
|
|
|
|
var alphaBytes []byte
|
|
|
|
|
var setBytes []byte
|
|
|
|
|
var attrsPerKey int
|
|
|
|
|
var numbOfKeys int
|
|
|
|
|
err = rows.Scan(&userId, &alphaBytes, &setBytes, &attrsPerKey, &numbOfKeys)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
return q.RenewUser(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, row := range userRenewRows {
|
|
|
|
|
user := entities.User{
|
|
|
|
|
Id: models.UserId{},
|
|
|
|
|
Id: models.UserIdFromString(row.ID),
|
|
|
|
|
CustomerId: models.CustomerId{},
|
|
|
|
|
Email: "",
|
|
|
|
|
EncipheredPasscode: models.EncipheredNKode{},
|
|
|
|
|
Kp: entities.KeypadDimension{
|
|
|
|
|
AttrsPerKey: attrsPerKey,
|
|
|
|
|
NumbOfKeys: numbOfKeys,
|
|
|
|
|
AttrsPerKey: int(row.AttributesPerKey),
|
|
|
|
|
NumbOfKeys: int(row.NumberOfKeys),
|
|
|
|
|
},
|
|
|
|
|
CipherKeys: entities.UserCipherKeys{
|
|
|
|
|
AlphaKey: security.ByteArrToUint64Arr(alphaBytes),
|
|
|
|
|
SetKey: security.ByteArrToUint64Arr(setBytes),
|
|
|
|
|
AlphaKey: security.ByteArrToUint64Arr(row.AlphaKey),
|
|
|
|
|
SetKey: security.ByteArrToUint64Arr(row.SetKey),
|
|
|
|
|
},
|
|
|
|
|
Interface: entities.UserInterface{},
|
|
|
|
|
Renew: false,
|
|
|
|
|
}
|
|
|
|
|
err = user.RenewKeys(setXor, attrXor)
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
|
|
if err = user.RenewKeys(setXor, attrXor); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
renewQuery += `
|
|
|
|
|
UPDATE user
|
|
|
|
|
SET alpha_key = ?, set_key = ?, renew = ?
|
|
|
|
|
WHERE id = ?;
|
|
|
|
|
`
|
|
|
|
|
renewArgs = append(renewArgs, security.Uint64ArrToByteArr(user.CipherKeys.AlphaKey), security.Uint64ArrToByteArr(user.CipherKeys.SetKey), 1, userId)
|
|
|
|
|
params := sqlc.RenewUserParams{
|
|
|
|
|
AlphaKey: security.Uint64ArrToByteArr(user.CipherKeys.AlphaKey),
|
|
|
|
|
SetKey: security.Uint64ArrToByteArr(user.CipherKeys.SetKey),
|
|
|
|
|
Renew: 1,
|
|
|
|
|
ID: uuid.UUID(user.Id).String(),
|
|
|
|
|
}
|
|
|
|
|
renewQuery += `
|
|
|
|
|
`
|
|
|
|
|
err = tx.Commit()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err = d.enqueueWriteTx(queryFunc, params); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return d.addWriteTx(renewQuery, renewArgs)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) renewCustomer(id models.CustomerId) ([]uint64, []uint64, error) {
|
|
|
|
|
customer, err := d.GetCustomer(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
setXor, attrXor, err := customer.RenewKeys()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
params, ok := args.(sqlc.RenewCustomerParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected RenewCustomerParams")
|
|
|
|
|
}
|
|
|
|
|
return q.RenewCustomer(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
params := sqlc.RenewCustomerParams{
|
|
|
|
|
AttributeValues: security.Uint64ArrToByteArr(customer.Attributes.AttrVals),
|
|
|
|
|
SetValues: security.Uint64ArrToByteArr(customer.Attributes.SetVals),
|
|
|
|
|
ID: uuid.UUID(customer.Id).String(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = d.enqueueWriteTx(queryFunc, params); err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
return setXor, attrXor, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) RefreshUserPasscode(user entities.User, passcodeIdx []int, customerAttr entities.CustomerAttributes) error {
|
|
|
|
|
err := user.RefreshPasscode(passcodeIdx, customerAttr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err := user.RefreshPasscode(passcodeIdx, customerAttr); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
query := `
|
|
|
|
|
UPDATE user
|
|
|
|
|
SET
|
|
|
|
|
renew = ?
|
|
|
|
|
,code = ?
|
|
|
|
|
,mask = ?
|
|
|
|
|
,alpha_key = ?
|
|
|
|
|
,set_key = ?
|
|
|
|
|
,pass_key = ?
|
|
|
|
|
,mask_key = ?
|
|
|
|
|
,salt = ?
|
|
|
|
|
WHERE id = ?;
|
|
|
|
|
`
|
|
|
|
|
args := []any{user.RefreshToken, 0, user.EncipheredPasscode.Code, user.EncipheredPasscode.Mask, security.Uint64ArrToByteArr(user.CipherKeys.AlphaKey), security.Uint64ArrToByteArr(user.CipherKeys.SetKey), security.Uint64ArrToByteArr(user.CipherKeys.PassKey), security.Uint64ArrToByteArr(user.CipherKeys.MaskKey), user.CipherKeys.Salt, uuid.UUID(user.Id).String()}
|
|
|
|
|
return d.addWriteTx(query, args)
|
|
|
|
|
queryFunc := func(q *sqlc.Queries, ctx context.Context, args any) error {
|
|
|
|
|
params, ok := args.(sqlc.RefreshUserPasscodeParams)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("invalid argument type: expected RefreshUserPasscodeParams")
|
|
|
|
|
}
|
|
|
|
|
return q.RefreshUserPasscode(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
params := sqlc.RefreshUserPasscodeParams{
|
|
|
|
|
Renew: 0,
|
|
|
|
|
Code: user.EncipheredPasscode.Code,
|
|
|
|
|
Mask: user.EncipheredPasscode.Mask,
|
|
|
|
|
AlphaKey: security.Uint64ArrToByteArr(user.CipherKeys.AlphaKey),
|
|
|
|
|
SetKey: security.Uint64ArrToByteArr(user.CipherKeys.SetKey),
|
|
|
|
|
PassKey: security.Uint64ArrToByteArr(user.CipherKeys.PassKey),
|
|
|
|
|
MaskKey: security.Uint64ArrToByteArr(user.CipherKeys.MaskKey),
|
|
|
|
|
Salt: user.CipherKeys.Salt,
|
|
|
|
|
ID: uuid.UUID(user.Id).String(),
|
|
|
|
|
}
|
|
|
|
|
return d.enqueueWriteTx(queryFunc, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) GetCustomer(id models.CustomerId) (*entities.Customer, error) {
|
|
|
|
|
tx, err := d.db.Begin()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = tx.Rollback()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(fmt.Sprintf("Write new user won't roll back %+v", err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
selectCustomer := `
|
|
|
|
|
SELECT
|
|
|
|
|
max_nkode_len
|
|
|
|
|
,min_nkode_len
|
|
|
|
|
,distinct_sets
|
|
|
|
|
,distinct_attributes
|
|
|
|
|
,lock_out
|
|
|
|
|
,expiration
|
|
|
|
|
,attribute_values
|
|
|
|
|
,set_values
|
|
|
|
|
FROM customer
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
rows, err := tx.Query(selectCustomer, uuid.UUID(id))
|
|
|
|
|
customer, err := d.queries.GetCustomer(d.ctx, uuid.UUID(id).String())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !rows.Next() {
|
|
|
|
|
log.Printf("no new row for customer %s with err %s", id, rows.Err())
|
|
|
|
|
return nil, config.ErrCustomerDne
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var maxNKodeLen int
|
|
|
|
|
var minNKodeLen int
|
|
|
|
|
var distinctSets int
|
|
|
|
|
var distinctAttributes int
|
|
|
|
|
var lockOut int
|
|
|
|
|
var expiration int
|
|
|
|
|
var attributeValues []byte
|
|
|
|
|
var setValues []byte
|
|
|
|
|
err = rows.Scan(&maxNKodeLen, &minNKodeLen, &distinctSets, &distinctAttributes, &lockOut, &expiration, &attributeValues, &setValues)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
customer := entities.Customer{
|
|
|
|
|
return &entities.Customer{
|
|
|
|
|
Id: id,
|
|
|
|
|
NKodePolicy: models.NKodePolicy{
|
|
|
|
|
MaxNkodeLen: maxNKodeLen,
|
|
|
|
|
MinNkodeLen: minNKodeLen,
|
|
|
|
|
DistinctSets: distinctSets,
|
|
|
|
|
DistinctAttributes: distinctAttributes,
|
|
|
|
|
LockOut: lockOut,
|
|
|
|
|
Expiration: expiration,
|
|
|
|
|
MaxNkodeLen: int(customer.MaxNkodeLen),
|
|
|
|
|
MinNkodeLen: int(customer.MinNkodeLen),
|
|
|
|
|
DistinctSets: int(customer.DistinctSets),
|
|
|
|
|
DistinctAttributes: int(customer.DistinctAttributes),
|
|
|
|
|
LockOut: int(customer.LockOut),
|
|
|
|
|
Expiration: int(customer.Expiration),
|
|
|
|
|
},
|
|
|
|
|
Attributes: entities.NewCustomerAttributesFromBytes(attributeValues, setValues),
|
|
|
|
|
}
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &customer, nil
|
|
|
|
|
Attributes: entities.NewCustomerAttributesFromBytes(customer.AttributeValues, customer.SetValues),
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) GetUser(email models.UserEmail, customerId models.CustomerId) (*entities.User, error) {
|
|
|
|
|
tx, err := d.db.Begin()
|
|
|
|
|
userRow, err := d.queries.GetUser(d.ctx, sqlc.GetUserParams{
|
|
|
|
|
Email: string(email),
|
|
|
|
|
CustomerID: uuid.UUID(customerId).String(),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
userSelect := `
|
|
|
|
|
SELECT
|
|
|
|
|
id
|
|
|
|
|
,renew
|
|
|
|
|
,refresh_token
|
|
|
|
|
,code
|
|
|
|
|
,mask
|
|
|
|
|
,attributes_per_key
|
|
|
|
|
,number_of_keys
|
|
|
|
|
,alpha_key
|
|
|
|
|
,set_key
|
|
|
|
|
,pass_key
|
|
|
|
|
,mask_key
|
|
|
|
|
,salt
|
|
|
|
|
,max_nkode_len
|
|
|
|
|
,idx_interface
|
|
|
|
|
,svg_id_interface
|
|
|
|
|
FROM user
|
|
|
|
|
WHERE user.email = ? AND user.customer_id = ?
|
|
|
|
|
`
|
|
|
|
|
rows, err := tx.Query(userSelect, string(email), uuid.UUID(customerId).String())
|
|
|
|
|
if !rows.Next() {
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
var (
|
|
|
|
|
id string
|
|
|
|
|
renewVal int
|
|
|
|
|
refreshToken string
|
|
|
|
|
code string
|
|
|
|
|
mask string
|
|
|
|
|
attrsPerKey int
|
|
|
|
|
numbOfKeys int
|
|
|
|
|
alphaKey []byte
|
|
|
|
|
setKey []byte
|
|
|
|
|
passKey []byte
|
|
|
|
|
maskKey []byte
|
|
|
|
|
salt []byte
|
|
|
|
|
maxNKodeLen int
|
|
|
|
|
idxInterface []byte
|
|
|
|
|
svgIdInterface []byte
|
|
|
|
|
)
|
|
|
|
|
err = rows.Scan(&id, &renewVal, &refreshToken, &code, &mask, &attrsPerKey, &numbOfKeys, &alphaKey, &setKey, &passKey, &maskKey, &salt, &maxNKodeLen, &idxInterface, &svgIdInterface)
|
|
|
|
|
|
|
|
|
|
userId, err := uuid.Parse(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
return nil, fmt.Errorf("failed to get user: %w", err)
|
|
|
|
|
}
|
|
|
|
|
var renew bool
|
|
|
|
|
if renewVal == 0 {
|
|
|
|
|
renew = false
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
kp := entities.KeypadDimension{
|
|
|
|
|
AttrsPerKey: int(userRow.AttributesPerKey),
|
|
|
|
|
NumbOfKeys: int(userRow.NumberOfKeys),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renew := false
|
|
|
|
|
if userRow.Renew == 1 {
|
|
|
|
|
renew = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user := entities.User{
|
|
|
|
|
Id: models.UserId(userId),
|
|
|
|
|
Id: models.UserIdFromString(userRow.ID),
|
|
|
|
|
CustomerId: customerId,
|
|
|
|
|
Email: email,
|
|
|
|
|
EncipheredPasscode: models.EncipheredNKode{
|
|
|
|
|
Code: code,
|
|
|
|
|
Mask: mask,
|
|
|
|
|
},
|
|
|
|
|
Kp: entities.KeypadDimension{
|
|
|
|
|
AttrsPerKey: attrsPerKey,
|
|
|
|
|
NumbOfKeys: numbOfKeys,
|
|
|
|
|
Code: userRow.Code,
|
|
|
|
|
Mask: userRow.Mask,
|
|
|
|
|
},
|
|
|
|
|
Kp: kp,
|
|
|
|
|
CipherKeys: entities.UserCipherKeys{
|
|
|
|
|
AlphaKey: security.ByteArrToUint64Arr(alphaKey),
|
|
|
|
|
SetKey: security.ByteArrToUint64Arr(setKey),
|
|
|
|
|
PassKey: security.ByteArrToUint64Arr(passKey),
|
|
|
|
|
MaskKey: security.ByteArrToUint64Arr(maskKey),
|
|
|
|
|
Salt: salt,
|
|
|
|
|
MaxNKodeLen: maxNKodeLen,
|
|
|
|
|
Kp: nil,
|
|
|
|
|
AlphaKey: security.ByteArrToUint64Arr(userRow.AlphaKey),
|
|
|
|
|
SetKey: security.ByteArrToUint64Arr(userRow.SetKey),
|
|
|
|
|
PassKey: security.ByteArrToUint64Arr(userRow.PassKey),
|
|
|
|
|
MaskKey: security.ByteArrToUint64Arr(userRow.MaskKey),
|
|
|
|
|
Salt: userRow.Salt,
|
|
|
|
|
MaxNKodeLen: int(userRow.MaxNkodeLen),
|
|
|
|
|
Kp: &kp,
|
|
|
|
|
},
|
|
|
|
|
Interface: entities.UserInterface{
|
|
|
|
|
IdxInterface: security.ByteArrToIntArr(idxInterface),
|
|
|
|
|
SvgId: security.ByteArrToIntArr(svgIdInterface),
|
|
|
|
|
Kp: nil,
|
|
|
|
|
IdxInterface: security.ByteArrToIntArr(userRow.IdxInterface),
|
|
|
|
|
SvgId: security.ByteArrToIntArr(userRow.SvgIDInterface),
|
|
|
|
|
Kp: &kp,
|
|
|
|
|
},
|
|
|
|
|
Renew: renew,
|
|
|
|
|
RefreshToken: refreshToken,
|
|
|
|
|
}
|
|
|
|
|
user.Interface.Kp = &user.Kp
|
|
|
|
|
user.CipherKeys.Kp = &user.Kp
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
RefreshToken: userRow.RefreshToken.String,
|
|
|
|
|
}
|
|
|
|
|
return &user, nil
|
|
|
|
|
}
|
|
|
|
|
@@ -456,68 +413,30 @@ func (d *SqliteDB) GetSvgStringInterface(idxs models.SvgIdInterface) ([]string,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) getSvgsById(ids []int) ([]string, error) {
|
|
|
|
|
tx, err := d.db.Begin()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
selectId := `
|
|
|
|
|
SELECT svg
|
|
|
|
|
FROM svg_icon
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
svgs := make([]string, len(ids))
|
|
|
|
|
for idx, id := range ids {
|
|
|
|
|
rows, err := tx.Query(selectId, id)
|
|
|
|
|
svg, err := d.queries.GetSvgId(d.ctx, int64(id))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if !rows.Next() {
|
|
|
|
|
log.Printf("id not found: %d", id)
|
|
|
|
|
return nil, config.ErrSvgDne
|
|
|
|
|
}
|
|
|
|
|
if err = rows.Scan(&svgs[idx]); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
svgs[idx] = svg
|
|
|
|
|
}
|
|
|
|
|
return svgs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) writeToDb(query string, args []any) error {
|
|
|
|
|
tx, err := d.db.Begin()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = tx.Rollback()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("fatal error: write won't roll back %+v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
if _, err = tx.Exec(query, args...); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
func (d *SqliteDB) enqueueWriteTx(queryFunc sqlcGeneric, args any) error {
|
|
|
|
|
select {
|
|
|
|
|
case <-d.ctx.Done():
|
|
|
|
|
return errors.New("database is shutting down")
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *SqliteDB) addWriteTx(query string, args []any) error {
|
|
|
|
|
if d.stop {
|
|
|
|
|
return config.ErrStoppingDatabase
|
|
|
|
|
}
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
writeTx := WriteTx{
|
|
|
|
|
Query: query,
|
|
|
|
|
Query: queryFunc,
|
|
|
|
|
Args: args,
|
|
|
|
|
ErrChan: errChan,
|
|
|
|
|
}
|
|
|
|
|
d.wg.Add(1)
|
|
|
|
|
d.writeQueue <- writeTx
|
|
|
|
|
return <-errChan
|
|
|
|
|
}
|
|
|
|
|
@@ -559,7 +478,3 @@ func (d *SqliteDB) getRandomIds(count int) ([]int, error) {
|
|
|
|
|
|
|
|
|
|
return perm[:count], nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func timeStamp() string {
|
|
|
|
|
return time.Now().Format(time.RFC3339)
|
|
|
|
|
}
|
|
|
|
|
|