Log a couple more things.

Also change sequence=1 to its own const. Eventually we may want to make it variable per user when we do server switching.
This commit is contained in:
Daniel Krol 2022-08-25 12:42:49 -04:00
parent 48c74350e0
commit b86687a0c5
4 changed files with 23 additions and 10 deletions

View file

@ -122,6 +122,7 @@ modes:
// TODO StatusCreated also for first wallet and/or for get auth token? // TODO StatusCreated also for first wallet and/or for get auth token?
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, string(response)) fmt.Fprintf(w, string(response))
log.Printf("User %s has registered", registerRequest.Email)
} }
// TODO - There's probably a struct-based solution here like with POST/PUT. // TODO - There's probably a struct-based solution here like with POST/PUT.
@ -228,4 +229,8 @@ func (s *Server) verify(w http.ResponseWriter, req *http.Request) {
} }
fmt.Fprintf(w, "Your account has been verified.") fmt.Fprintf(w, "Your account has been verified.")
// if we really want to log the user's email at some point
// we can put in the effort then to fetch it
log.Printf("User has been verified with token %s", token)
} }

View file

@ -3,6 +3,7 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -30,7 +31,7 @@ func (r *WalletRequest) validate() error {
if r.Hmac == "" { if r.Hmac == "" {
return fmt.Errorf("Missing 'hmac'") return fmt.Errorf("Missing 'hmac'")
} }
if r.Sequence < 1 { if r.Sequence < store.InitialWalletSequence {
return fmt.Errorf("Missing or zero-value 'sequence'") return fmt.Errorf("Missing or zero-value 'sequence'")
} }
return nil return nil
@ -156,4 +157,7 @@ func (s *Server) postWallet(w http.ResponseWriter, req *http.Request) {
} }
fmt.Fprintf(w, string(response)) fmt.Fprintf(w, string(response))
if walletRequest.Sequence == store.InitialWalletSequence {
log.Printf("Initial wallet created for user id %d", authToken.UserId)
}
} }

View file

@ -37,6 +37,10 @@ var (
const ( const (
AuthTokenLifespan = time.Hour * 24 * 14 AuthTokenLifespan = time.Hour * 24 * 14
VerifyTokenLifespan = time.Hour * 24 * 2 VerifyTokenLifespan = time.Hour * 24 * 2
// Eventually it could become variable when we introduce server switching. A user
// might be on a later sequence when they switch from another server.
InitialWalletSequence = 1
) )
// For test stubs // For test stubs
@ -281,12 +285,12 @@ func (s *Store) insertFirstWallet(
encryptedWallet wallet.EncryptedWallet, encryptedWallet wallet.EncryptedWallet,
hmac wallet.WalletHmac, hmac wallet.WalletHmac,
) (err error) { ) (err error) {
// This will only be used to attempt to insert the first wallet (sequence=1). // This will only be used to attempt to insert the first wallet (sequence=InitialWalletSequence).
// The database will enforce that this will not be set if this user already // The database will enforce that this will not be set if this user already
// has a wallet. // has a wallet.
_, err = s.db.Exec( _, err = s.db.Exec(
"INSERT INTO wallets (user_id, encrypted_wallet, sequence, hmac, updated) VALUES(?,?,?,?, datetime('now'))", "INSERT INTO wallets (user_id, encrypted_wallet, sequence, hmac, updated) VALUES(?,?,?,?, datetime('now'))",
userId, encryptedWallet, 1, hmac, userId, encryptedWallet, InitialWalletSequence, hmac,
) )
var sqliteErr sqlite3.Error var sqliteErr sqlite3.Error
@ -309,7 +313,7 @@ func (s *Store) updateWalletToSequence(
sequence wallet.Sequence, sequence wallet.Sequence,
hmac wallet.WalletHmac, hmac wallet.WalletHmac,
) (err error) { ) (err error) {
// This will be used for wallets with sequence > 1. // This will be used for wallets with sequence > InitialWalletSequence.
// Use the database to enforce that we only update if we are incrementing the sequence. // Use the database to enforce that we only update if we are incrementing the sequence.
// This way, if two clients attempt to update at the same time, it will return // This way, if two clients attempt to update at the same time, it will return
// an error for the second one. // an error for the second one.
@ -333,22 +337,22 @@ func (s *Store) updateWalletToSequence(
return return
} }
// Assumption: Sequence has been validated (>=1) // Assumption: Sequence has been validated (>=InitialWalletSequence)
// Assumption: Auth token has been checked (thus account is verified) // Assumption: Auth token has been checked (thus account is verified)
func (s *Store) SetWallet(userId auth.UserId, encryptedWallet wallet.EncryptedWallet, sequence wallet.Sequence, hmac wallet.WalletHmac) (err error) { func (s *Store) SetWallet(userId auth.UserId, encryptedWallet wallet.EncryptedWallet, sequence wallet.Sequence, hmac wallet.WalletHmac) (err error) {
if sequence == 1 { if sequence == InitialWalletSequence {
// If sequence == 1, the client assumed that this is our first // If sequence == InitialWalletSequence, the client assumed that this is our first
// wallet. Try to insert. If we get a conflict, the client // wallet. Try to insert. If we get a conflict, the client
// assumed incorrectly and we proceed below to return the latest // assumed incorrectly and we proceed below to return the latest
// wallet from the db. // wallet from the db.
err = s.insertFirstWallet(userId, encryptedWallet, hmac) err = s.insertFirstWallet(userId, encryptedWallet, hmac)
if err == ErrDuplicateWallet { if err == ErrDuplicateWallet {
// A wallet already exists. That means the input sequence should not be 1. // A wallet already exists. That means the input sequence should not be InitialWalletSequence.
// To the caller, this means the sequence was wrong. // To the caller, this means the sequence was wrong.
err = ErrWrongSequence err = ErrWrongSequence
} }
} else { } else {
// If sequence > 1, the client assumed that it is replacing wallet // If sequence > InitialWalletSequence, the client assumed that it is replacing wallet
// with sequence - 1. Explicitly try to update the wallet with // with sequence - 1. Explicitly try to update the wallet with
// sequence - 1. If we updated no rows, the client assumed incorrectly // sequence - 1. If we updated no rows, the client assumed incorrectly
// and we proceed below to return the latest wallet from the db. // and we proceed below to return the latest wallet from the db.

View file

@ -174,7 +174,7 @@ func TestStoreUpdateWallet(t *testing.T) {
// NOTE - the "behind the scenes" comments give a view of what we're expecting // NOTE - the "behind the scenes" comments give a view of what we're expecting
// to happen, and why we're testing what we are. Sometimes it should insert, // to happen, and why we're testing what we are. Sometimes it should insert,
// sometimes it should update. It depends on whether it's the first wallet // sometimes it should update. It depends on whether it's the first wallet
// submitted, and that's easily determined by sequence=1. However, if we switch // submitted, and that's easily determined by sequence=store.InitialWalletSequence. However, if we switch
// to a database with "upserts" and take advantage of it, what happens behind // to a database with "upserts" and take advantage of it, what happens behind
// the scenes will change a little, so the comments should be updated. Though, // the scenes will change a little, so the comments should be updated. Though,
// we'd probably best test the same cases. // we'd probably best test the same cases.