From 1549ea7a5abb2f34cbcd39557558361bc70bc0ba Mon Sep 17 00:00:00 2001 From: Gert-Jan Timmer Date: Wed, 13 Jun 2018 16:46:25 +0200 Subject: [PATCH] Fix: UserAuth Error * Replaced normal error with `Error` and SQLite return code. * Fix tests --- sqlite3_opt_userauth.go | 14 ++++---------- sqlite3_opt_userauth_test.go | 14 +++++++------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/sqlite3_opt_userauth.go b/sqlite3_opt_userauth.go index c73828b..0812a32 100644 --- a/sqlite3_opt_userauth.go +++ b/sqlite3_opt_userauth.go @@ -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: diff --git a/sqlite3_opt_userauth_test.go b/sqlite3_opt_userauth_test.go index b4ac4a9..fa0f986 100644 --- a/sqlite3_opt_userauth_test.go +++ b/sqlite3_opt_userauth_test.go @@ -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") } }