@@ -42,7 +42,7 @@ _sqlite3_user_delete(sqlite3* db, const char* zUsername)
42
42
}
43
43
44
44
static int
45
- _sqlite3_auth_is_enabled (sqlite3* db)
45
+ _sqlite3_auth_enabled (sqlite3* db)
46
46
{
47
47
int exists = -1;
48
48
87
87
// If the SQLITE_USER table is not present in the database file, then
88
88
// this interface is a harmless no-op returnning SQLITE_OK.
89
89
func (c * SQLiteConn ) Authenticate (username , password string ) error {
90
+ rv := c .authenticate (username , password )
91
+ switch rv {
92
+ case C .SQLITE_ERROR , C .SQLITE_AUTH :
93
+ return ErrUnauthorized
94
+ case C .SQLITE_OK :
95
+ return nil
96
+ default :
97
+ return c .lastError ()
98
+ }
99
+ }
100
+
101
+ // authenticate provides the actual authentication to SQLite.
102
+ // This is not exported for usage in Go.
103
+ // It is however exported for usage within SQL by the user.
104
+ //
105
+ // Returns:
106
+ // C.SQLITE_OK (0)
107
+ // C.SQLITE_ERROR (1)
108
+ // C.SQLITE_AUTH (23)
109
+ func (c * SQLiteConn ) authenticate (username , password string ) int {
90
110
// Allocate C Variables
91
111
cuser := C .CString (username )
92
112
cpass := C .CString (password )
@@ -97,15 +117,7 @@ func (c *SQLiteConn) Authenticate(username, password string) error {
97
117
C .free (unsafe .Pointer (cpass ))
98
118
}()
99
119
100
- rv := C ._sqlite3_user_authenticate (c .db , cuser , cpass , C .int (len (password )))
101
- if rv == C .SQLITE_AUTH {
102
- return ErrUnauthorized
103
- }
104
- if rv != C .SQLITE_OK {
105
- return c .lastError ()
106
- }
107
-
108
- return nil
120
+ return int (C ._sqlite3_user_authenticate (c .db , cuser , cpass , C .int (len (password ))))
109
121
}
110
122
111
123
// AuthUserAdd can be used (by an admin user only)
@@ -124,7 +136,7 @@ func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error {
124
136
125
137
rv := c .authUserAdd (username , password , isAdmin )
126
138
switch rv {
127
- case C .SQLITE_AUTH :
139
+ case C .SQLITE_ERROR , C . SQLITE_AUTH :
128
140
return ErrAdminRequired
129
141
case C .SQLITE_OK :
130
142
return nil
@@ -133,6 +145,19 @@ func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error {
133
145
}
134
146
}
135
147
148
+ // authUserAdd enables the User Authentication if not enabled.
149
+ // Otherwise it will add a user.
150
+ //
151
+ // When user authentication is already enabled then this function
152
+ // can only be called by an admin.
153
+ //
154
+ // This is not exported for usage in Go.
155
+ // It is however exported for usage within SQL by the user.
156
+ //
157
+ // Returns:
158
+ // C.SQLITE_OK (0)
159
+ // C.SQLITE_ERROR (1)
160
+ // C.SQLITE_AUTH (23)
136
161
func (c * SQLiteConn ) authUserAdd (username , password string , admin int ) int {
137
162
// Allocate C Variables
138
163
cuser := C .CString (username )
@@ -158,6 +183,34 @@ func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error
158
183
isAdmin = 1
159
184
}
160
185
186
+ rv := c .authUserChange (username , password , isAdmin )
187
+ switch rv {
188
+ case C .SQLITE_ERROR , C .SQLITE_AUTH :
189
+ return ErrAdminRequired
190
+ case C .SQLITE_OK :
191
+ return nil
192
+ default :
193
+ return c .lastError ()
194
+ }
195
+ }
196
+
197
+ // authUserChange allows to modify a user.
198
+ // Users can change their own password.
199
+ //
200
+ // Only admins can change passwords for other users
201
+ // and modify the admin flag.
202
+ //
203
+ // The admin flag of the current logged in user cannot be changed.
204
+ // THis ensures that their is always an admin.
205
+ //
206
+ // This is not exported for usage in Go.
207
+ // It is however exported for usage within SQL by the user.
208
+ //
209
+ // Returns:
210
+ // C.SQLITE_OK (0)
211
+ // C.SQLITE_ERROR (1)
212
+ // C.SQLITE_AUTH (23)
213
+ func (c * SQLiteConn ) authUserChange (username , password string , admin int ) int {
161
214
// Allocate C Variables
162
215
cuser := C .CString (username )
163
216
cpass := C .CString (password )
@@ -168,15 +221,7 @@ func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error
168
221
C .free (unsafe .Pointer (cpass ))
169
222
}()
170
223
171
- rv := C ._sqlite3_user_change (c .db , cuser , cpass , C .int (len (password )), C .int (isAdmin ))
172
- if rv == C .SQLITE_AUTH {
173
- return ErrAdminRequired
174
- }
175
- if rv != C .SQLITE_OK {
176
- return c .lastError ()
177
- }
178
-
179
- return nil
224
+ return int (C ._sqlite3_user_change (c .db , cuser , cpass , C .int (len (password )), C .int (admin )))
180
225
}
181
226
182
227
// AuthUserDelete can be used (by an admin user only)
@@ -185,6 +230,29 @@ func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error
185
230
// the database cannot be converted into a no-authentication-required
186
231
// database.
187
232
func (c * SQLiteConn ) AuthUserDelete (username string ) error {
233
+ rv := c .authUserDelete (username )
234
+ switch rv {
235
+ case C .SQLITE_ERROR , C .SQLITE_AUTH :
236
+ return ErrAdminRequired
237
+ case C .SQLITE_OK :
238
+ return nil
239
+ default :
240
+ return c .lastError ()
241
+ }
242
+ }
243
+
244
+ // authUserDelete can be used to delete a user.
245
+ //
246
+ // This function can only be executed by an admin.
247
+ //
248
+ // This is not exported for usage in Go.
249
+ // It is however exported for usage within SQL by the user.
250
+ //
251
+ // Returns:
252
+ // C.SQLITE_OK (0)
253
+ // C.SQLITE_ERROR (1)
254
+ // C.SQLITE_AUTH (23)
255
+ func (c * SQLiteConn ) authUserDelete (username string ) int {
188
256
// Allocate C Variables
189
257
cuser := C .CString (username )
190
258
@@ -193,25 +261,29 @@ func (c *SQLiteConn) AuthUserDelete(username string) error {
193
261
C .free (unsafe .Pointer (cuser ))
194
262
}()
195
263
196
- rv := C ._sqlite3_user_delete (c .db , cuser )
197
- if rv == SQLITE_AUTH {
198
- return ErrAdminRequired
199
- }
200
- if rv != C .SQLITE_OK {
201
- return c .lastError ()
202
- }
203
-
204
- return nil
264
+ return int (C ._sqlite3_user_delete (c .db , cuser ))
205
265
}
206
266
207
- // Check is database is protected by user authentication
208
- func (c * SQLiteConn ) AuthIsEnabled () (exists bool ) {
209
- rv := C . _sqlite3_auth_is_enabled ( c . db )
267
+ // AuthEnabled checks if the database is protected by user authentication
268
+ func (c * SQLiteConn ) AuthEnabled () (exists bool ) {
269
+ rv := c . authEnabled ( )
210
270
if rv == 1 {
211
271
exists = true
212
272
}
213
273
214
274
return
215
275
}
216
276
277
+ // authEnabled perform the actual check for user authentication.
278
+ //
279
+ // This is not exported for usage in Go.
280
+ // It is however exported for usage within SQL by the user.
281
+ //
282
+ // Returns:
283
+ // 0 - Disabled
284
+ // 1 - Enabled
285
+ func (c * SQLiteConn ) authEnabled () int {
286
+ return int (C ._sqlite3_auth_enabled (c .db ))
287
+ }
288
+
217
289
// EOF
0 commit comments