Skip to content

Commit 83213c4

Browse files
committed
userauth: don't register no-op auth functions and unify public API
This commit changes Open to not register any of the auth functions when userauth is disabled. It also unifies the public API of this library for when userauth is enabled/disabled. goos: darwin goarch: arm64 pkg: github.com/charlievieth/go-sqlite3 cpu: Apple M4 Pro │ y1.txt │ y3.txt │ │ sec/op │ sec/op vs base │ Suite/BenchmarkOpen-14 13.76µ ± 1% 12.37µ ± 1% -10.11% (p=0.000 n=10) │ y1.txt │ y3.txt │ │ B/op │ B/op vs base │ Suite/BenchmarkOpen-14 712.0 ± 0% 111.0 ± 1% -84.41% (p=0.000 n=10) │ y1.txt │ y3.txt │ │ allocs/op │ allocs/op vs base │ Suite/BenchmarkOpen-14 20.000 ± 0% 3.000 ± 0% -85.00% (p=0.000 n=10)
1 parent 7fbde5e commit 83213c4

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

sqlite3.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ const (
273273
SQLITE_OK = C.SQLITE_OK
274274
SQLITE_IGNORE = C.SQLITE_IGNORE
275275
SQLITE_DENY = C.SQLITE_DENY
276+
SQLITE_AUTH = C.SQLITE_AUTH
276277

277278
// different actions query tries to do - passed as argument to authorizer
278279
SQLITE_CREATE_INDEX = C.SQLITE_CREATE_INDEX
@@ -1601,7 +1602,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
16011602
//
16021603
// If the SQLITE_USER table is not present in the database file, then
16031604
// this interface is a harmless no-op returnning SQLITE_OK.
1604-
if err := conn.RegisterFunc("authenticate", conn.authenticate, true); err != nil {
1605+
if err := conn.registerAuthFunc("authenticate", conn.authenticate, true); err != nil {
16051606
return err
16061607
}
16071608
//
@@ -1614,7 +1615,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
16141615
// The AuthUserAdd only works for the "main" database, not
16151616
// for any ATTACH-ed databases. Any call to AuthUserAdd by a
16161617
// non-admin user results in an error.
1617-
if err := conn.RegisterFunc("auth_user_add", conn.authUserAdd, true); err != nil {
1618+
if err := conn.registerAuthFunc("auth_user_add", conn.authUserAdd, true); err != nil {
16181619
return err
16191620
}
16201621
//
@@ -1624,7 +1625,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
16241625
// login credentials. Only an admin user can change another users login
16251626
// credentials or admin privilege setting. No user may change their own
16261627
// admin privilege setting.
1627-
if err := conn.RegisterFunc("auth_user_change", conn.authUserChange, true); err != nil {
1628+
if err := conn.registerAuthFunc("auth_user_change", conn.authUserChange, true); err != nil {
16281629
return err
16291630
}
16301631
//
@@ -1634,13 +1635,13 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
16341635
// which guarantees that there is always an admin user and hence that
16351636
// the database cannot be converted into a no-authentication-required
16361637
// database.
1637-
if err := conn.RegisterFunc("auth_user_delete", conn.authUserDelete, true); err != nil {
1638+
if err := conn.registerAuthFunc("auth_user_delete", conn.authUserDelete, true); err != nil {
16381639
return err
16391640
}
16401641

16411642
// Register: auth_enabled
16421643
// auth_enabled can be used to check if user authentication is enabled
1643-
if err := conn.RegisterFunc("auth_enabled", conn.authEnabled, true); err != nil {
1644+
if err := conn.registerAuthFunc("auth_enabled", conn.authEnabled, true); err != nil {
16441645
return err
16451646
}
16461647

sqlite3_opt_userauth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ import (
6565
"unsafe"
6666
)
6767

68-
const (
69-
SQLITE_AUTH = C.SQLITE_AUTH
70-
)
71-
7268
var (
7369
ErrUnauthorized = errors.New("SQLITE_AUTH: Unauthorized")
7470
ErrAdminRequired = errors.New("SQLITE_AUTH: Unauthorized; Admin Privileges Required")
@@ -292,4 +288,8 @@ func (c *SQLiteConn) authEnabled() int {
292288
return int(C._sqlite3_auth_enabled(c.db))
293289
}
294290

291+
func (c *SQLiteConn) registerAuthFunc(name string, impl any, pure bool) error {
292+
return c.RegisterFunc(name, impl, pure)
293+
}
294+
295295
// EOF

sqlite3_opt_userauth_omit.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
package sqlite3
1010

11-
import (
12-
"C"
11+
import "errors"
12+
13+
var (
14+
ErrUnauthorized = errors.New("SQLITE_AUTH: Unauthorized")
15+
ErrAdminRequired = errors.New("SQLITE_AUTH: Unauthorized; Admin Privileges Required")
1316
)
1417

1518
// Authenticate will perform an authentication of the provided username
@@ -155,4 +158,9 @@ func (c *SQLiteConn) authEnabled() int {
155158
return 0
156159
}
157160

161+
func (c *SQLiteConn) registerAuthFunc(_ string, _ any, _ bool) error {
162+
// NOOP
163+
return nil
164+
}
165+
158166
// EOF

0 commit comments

Comments
 (0)