Skip to content
Snippets Groups Projects
Commit 4120c10c authored by Michael Eischer's avatar Michael Eischer
Browse files

get rid of goto for db transaction

parent 058fdc76
No related branches found
No related tags found
1 merge request!3Accumulated extensions to goatherd from I4
......@@ -318,7 +318,7 @@ func checkHOTP(hotp *twofactor.HOTP, remote string, name string, offer string) (
debugf("[%v] autoresync in progress: %v", remote, s)
if uint64(time.Now().Unix()-s.Time) <= cfg.AutoresyncTime {
if s.Num >= cfg.AutoresyncRepeat && s.Counter-hotp.Counter() < cfg.AutoresyncLookahead {
// if the user had a sufficient number of successful tries that were not within
// if the user had a sufficient number of consecutive tries that were not within
// standard lookahead range but within cfg.AutoresyncLookahead within cfg.AutoresyncTime seconds,
// temporarily increase lookahead to authenticate and resync.
debugf("[%v] autoresync conditions: increasing lookahead to ", cfg.AutoresyncLookahead)
......@@ -374,49 +374,50 @@ func checkHOTP(hotp *twofactor.HOTP, remote string, name string, offer string) (
// transaction, retrying if it fails.
func checkOffer(remote string, name string, offer string) (bool, error) {
for {
debugf("[%v] begin transaction", remote)
tx, err := db.Begin()
errPanic(err)
inner := func(tx *sql.Tx) (bool, error) {
debugf("[%v] looking up data for %v", remote, name)
hotp, err := getOTP(tx, name)
if err != nil {
return false, err
}
ok := false
ok, err := checkHOTP(hotp, remote, name, offer)
if err != nil {
return false, err
}
debugf("[%v] looking up data for %v", remote, name)
hotp, err := getOTP(tx, name)
if transactionFailed(err) {
goto retry
} else if err == sql.ErrNoRows {
_ = tx.Rollback()
return false, err
}
errPanic(err)
if ok {
debugf("[%v] ok, set new count", remote)
err := setCount(tx, name, hotp.Counter())
if err != nil {
return false, err
}
}
ok, err = checkHOTP(hotp, remote, name, offer)
if transactionFailed(err) {
goto retry
debugf("[%v] commiting", remote)
err = tx.Commit()
if err != nil {
return false, err
}
return ok, nil
}
debugf("[%v] begin transaction", remote)
tx, err := db.Begin()
errPanic(err)
if ok {
debugf("[%v] ok, set new count", remote)
err := setCount(tx, name, hotp.Counter())
if transactionFailed(err) {
goto retry
ok, err := inner(tx)
if err != nil {
debugf("[%v] retry %v", remote, err)
_ = tx.Rollback()
if err == sql.ErrNoRows {
return false, nil
} else if transactionFailed(err) {
continue
}
errPanic(err)
}
debugf("[%v] commiting", remote)
err = tx.Commit()
if transactionFailed(err) {
goto retry
}
errPanic(err)
return ok, nil
retry:
debugf("[%v] retry", remote)
_ = tx.Rollback()
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment