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 (
"errors"
"unsafe"
)
@ -69,11 +68,6 @@ const (
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
// and password against the database.
//
@ -91,7 +85,7 @@ func (c *SQLiteConn) Authenticate(username, password string) error {
rv := c.authenticate(username, password)
switch rv {
case C.SQLITE_ERROR, C.SQLITE_AUTH:
return ErrUnauthorized
return Error{Code: ErrNo(rv)}
case C.SQLITE_OK:
return nil
default:
@ -138,7 +132,7 @@ func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error {
rv := c.authUserAdd(username, password, isAdmin)
switch rv {
case C.SQLITE_ERROR, C.SQLITE_AUTH:
return ErrAdminRequired
return Error{Code: ErrNo(rv)}
case C.SQLITE_OK:
return nil
default:
@ -187,7 +181,7 @@ func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error
rv := c.authUserChange(username, password, isAdmin)
switch rv {
case C.SQLITE_ERROR, C.SQLITE_AUTH:
return ErrAdminRequired
return Error{Code: ErrNo(rv)}
case C.SQLITE_OK:
return nil
default:
@ -234,7 +228,7 @@ func (c *SQLiteConn) AuthUserDelete(username string) error {
rv := c.authUserDelete(username)
switch rv {
case C.SQLITE_ERROR, C.SQLITE_AUTH:
return ErrAdminRequired
return Error{Code: ErrNo(rv)}
case C.SQLITE_OK:
return nil
default:

View File

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