Fix: UserAuth Error

* Replaced normal error with `Error` and SQLite return code.
* Fix tests
This commit is contained in:
Gert-Jan Timmer 2018-06-13 16:46:25 +02:00
parent 1592d366ed
commit 1549ea7a5a
2 changed files with 11 additions and 17 deletions

View File

@ -61,7 +61,6 @@ _sqlite3_auth_enabled(sqlite3* db)
*/ */
import "C" import "C"
import ( import (
"errors"
"unsafe" "unsafe"
) )
@ -69,11 +68,6 @@ const (
SQLITE_AUTH = C.SQLITE_AUTH SQLITE_AUTH = C.SQLITE_AUTH
) )
var (
ErrUnauthorized = errors.New("SQLITE_AUTH: Unauthorized")
ErrAdminRequired = errors.New("SQLITE_AUTH: Unauthorized; Admin Privileges Required")
)
// Authenticate will perform an authentication of the provided username // Authenticate will perform an authentication of the provided username
// and password against the database. // and password against the database.
// //
@ -91,7 +85,7 @@ func (c *SQLiteConn) Authenticate(username, password string) error {
rv := c.authenticate(username, password) rv := c.authenticate(username, password)
switch rv { switch rv {
case C.SQLITE_ERROR, C.SQLITE_AUTH: case C.SQLITE_ERROR, C.SQLITE_AUTH:
return ErrUnauthorized return Error{Code: ErrNo(rv)}
case C.SQLITE_OK: case C.SQLITE_OK:
return nil return nil
default: default:
@ -138,7 +132,7 @@ func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error {
rv := c.authUserAdd(username, password, isAdmin) rv := c.authUserAdd(username, password, isAdmin)
switch rv { switch rv {
case C.SQLITE_ERROR, C.SQLITE_AUTH: case C.SQLITE_ERROR, C.SQLITE_AUTH:
return ErrAdminRequired return Error{Code: ErrNo(rv)}
case C.SQLITE_OK: case C.SQLITE_OK:
return nil return nil
default: default:
@ -187,7 +181,7 @@ func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error
rv := c.authUserChange(username, password, isAdmin) rv := c.authUserChange(username, password, isAdmin)
switch rv { switch rv {
case C.SQLITE_ERROR, C.SQLITE_AUTH: case C.SQLITE_ERROR, C.SQLITE_AUTH:
return ErrAdminRequired return Error{Code: ErrNo(rv)}
case C.SQLITE_OK: case C.SQLITE_OK:
return nil return nil
default: default:
@ -234,7 +228,7 @@ func (c *SQLiteConn) AuthUserDelete(username string) error {
rv := c.authUserDelete(username) rv := c.authUserDelete(username)
switch rv { switch rv {
case C.SQLITE_ERROR, C.SQLITE_AUTH: case C.SQLITE_ERROR, C.SQLITE_AUTH:
return ErrAdminRequired return Error{Code: ErrNo(rv)}
case C.SQLITE_OK: case C.SQLITE_OK:
return nil return nil
default: default:

View File

@ -191,14 +191,14 @@ func TestUserAuthLogin(t *testing.T) {
if err == nil { if err == nil {
t.Fatal("Login successful while expecting to fail") t.Fatal("Login successful while expecting to fail")
} }
if err != ErrUnauthorized { if err.(Error).Code != SQLITE_AUTH {
t.Fatal(err) t.Fatal(err)
} }
err = c2.Authenticate("admin", "invalid") err = c2.Authenticate("admin", "invalid")
if err == nil { if err == nil {
t.Fatal("Login successful while expecting to fail") t.Fatal("Login successful while expecting to fail")
} }
if err != ErrUnauthorized { if err.(Error).Code != SQLITE_AUTH {
t.Fatal(err) t.Fatal(err)
} }
} }
@ -339,7 +339,7 @@ func TestUserAuthAddUser(t *testing.T) {
} }
err = c2.AuthUserAdd("admin3", "admin3", true) err = c2.AuthUserAdd("admin3", "admin3", true)
if err != ErrAdminRequired { if err.(Error).Code != SQLITE_AUTH {
t.Fatal("Created admin user while not allowed") t.Fatal("Created admin user while not allowed")
} }
@ -353,7 +353,7 @@ func TestUserAuthAddUser(t *testing.T) {
} }
err = c2.AuthUserAdd("user4", "user4", false) err = c2.AuthUserAdd("user4", "user4", false)
if err != ErrAdminRequired { if err.(Error).Code != SQLITE_AUTH {
t.Fatal("Created user while not allowed") t.Fatal("Created user while not allowed")
} }
} }
@ -397,7 +397,7 @@ func TestUserAuthModifyUser(t *testing.T) {
// Because we are current logged in as 'admin' // Because we are current logged in as 'admin'
// Changing our own admin flag should fail. // Changing our own admin flag should fail.
err = c1.AuthUserChange("admin", "admin3", false) err = c1.AuthUserChange("admin", "admin3", false)
if err != ErrAdminRequired { if err.(Error).Code != SQLITE_AUTH {
t.Fatal("Successfully changed admin flag while not allowed") t.Fatal("Successfully changed admin flag while not allowed")
} }
@ -452,7 +452,7 @@ func TestUserAuthModifyUser(t *testing.T) {
// Modify other user password and flag through *SQLiteConn // Modify other user password and flag through *SQLiteConn
err = c2.AuthUserChange("user2", "invalid", false) err = c2.AuthUserChange("user2", "invalid", false)
if err != ErrAdminRequired { if err.(Error).Code != SQLITE_AUTH {
t.Fatal("Password change succesful while not allowed") t.Fatal("Password change succesful while not allowed")
} }
} }
@ -583,7 +583,7 @@ func TestUserAuthDeleteUser(t *testing.T) {
// Delete user while logged in as normal user // Delete user while logged in as normal user
// through *SQLiteConn // through *SQLiteConn
err = c2.AuthUserDelete("user2") err = c2.AuthUserDelete("user2")
if err != ErrAdminRequired { if err.(Error).Code != SQLITE_AUTH {
t.Fatal("Successfully deleted user wthout proper privileges") t.Fatal("Successfully deleted user wthout proper privileges")
} }
} }