Skip to content

Commit 4218441

Browse files
authored
Merge pull request #586 from mattn/feature/userauth
Feature/userauth
2 parents 6d0b39d + 4ba9507 commit 4218441

17 files changed

+2955
-115
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@
55

66
# VSCode
77
.vscode
8+
9+
# Exclude from upgrade
10+
upgrade/*.c
11+
upgrade/*.h
12+
13+
# Exclude upgrade binary
14+
upgrade/upgrade

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ before_install:
8787
brew upgrade icu4c
8888
fi
8989
- |
90+
go get github.com/smartystreets/goconvey
9091
if [[ "${GOOS}" != "windows" ]]; then
9192
go get github.com/mattn/goveralls
9293
go get golang.org/x/tools/cmd/cover

README.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Supported Golang version:
3333
- [Mac OSX](#mac-osx)
3434
- [Windows](#windows)
3535
- [Errors](#errors)
36+
- [User Authentication](#user-authentication)
37+
- [Compile](#compile)
38+
- [Usage](#usage)
39+
-
3640
- [Extensions](#extensions)
3741
- [Spatialite](#spatialite)
3842
- [FAQ](#faq)
@@ -76,6 +80,11 @@ Boolean values can be one of:
7680

7781
| Name | Key | Value(s) | Description |
7882
|------|-----|----------|-------------|
83+
| UA - Create | `_auth` | - | Create User Authentication, for more information see [User Authentication](#user-authentication) |
84+
| UA - Username | `_auth_user` | `string` | Username for User Authentication, for more information see [User Authentication](#user-authentication) |
85+
| UA - Password | `_auth_pass` | `string` | Password for User Authentication, for more information see [User Authentication](#user-authentication) |
86+
| UA - Crypt | `_auth_crypt` | <ul><li>SHA1</li><li>SSHA1</li><li>SHA256</li><li>SSHA256</li><li>SHA384</li><li>SSHA384</li><li>SHA512</li><li>SSHA512</li></ul> | Password encoder to use for User Authentication, for more information see [User Authentication](#user-authentication) |
87+
| UA - Salt | `_auth_salt` | `string` | Salt to use if the configure password encoder requires a salt, for User Authentication, for more information see [User Authentication](#user-authentication) |
7988
| Auto Vacuum | `_auto_vacuum` \| `_vacuum` | <ul><li>`0` \| `none`</li><li>`1` \| `full`</li><li>`2` \| `incremental`</li></ul> | For more information see [PRAGMA auto_vacuum](https://www.sqlite.org/pragma.html#pragma_auto_vacuum) |
8089
| Busy Timeout | `_busy_timeout` \| `_timeout` | `int` | Specify value for sqlite3_busy_timeout. For more information see [PRAGMA busy_timeout](https://www.sqlite.org/pragma.html#pragma_busy_timeout) |
8190
| Case Sensitive LIKE | `_case_sensitive_like` \| `_cslike` | `boolean` | For more information see [PRAGMA case_sensitive_like](https://www.sqlite.org/pragma.html#pragma_case_sensitive_like) |
@@ -144,6 +153,7 @@ go build --tags "icu json1 fts5 secure_delete"
144153
| Secure Delete | sqlite_secure_delete | This compile-time option changes the default setting of the secure_delete pragma.<br><br>When this option is not used, secure_delete defaults to off. When this option is present, secure_delete defaults to on.<br><br>The secure_delete setting causes deleted content to be overwritten with zeros. There is a small performance penalty since additional I/O must occur.<br><br>On the other hand, secure_delete can prevent fragments of sensitive information from lingering in unused parts of the database file after it has been deleted. See the documentation on the secure_delete pragma for additional information |
145154
| Secure Delete (FAST) | sqlite_secure_delete_fast | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) |
146155
| Tracing / Debug | sqlite_trace | Activate trace functions |
156+
| User Authentication | sqlite_userauth | SQLite User Authentication see [User Authentication](#user-authentication) for more information. |
147157

148158
# Compilation
149159

@@ -303,6 +313,117 @@ For example the TDM-GCC Toolchain can be found [here](ttps://sourceforge.net/pro
303313
go install github.com/mattn/go-sqlite3
304314
```
305315
316+
# User Authentication
317+
318+
This package supports the SQLite User Authentication module.
319+
320+
## Compile
321+
322+
To use the User authentication module the package has to be compiled with the tag `sqlite_userauth`. See [Features](#features).
323+
324+
## Usage
325+
326+
### Create protected database
327+
328+
To create a database protected by user authentication provide the following argument to the connection string `_auth`.
329+
This will enable user authentication within the database. This option however requires two additional arguments:
330+
331+
- `_auth_user`
332+
- `_auth_pass`
333+
334+
When `_auth` is present on the connection string user authentication will be enabled and the provided user will be created
335+
as an `admin` user. After initial creation, the parameter `_auth` has no effect anymore and can be omitted from the connection string.
336+
337+
Example connection string:
338+
339+
Create an user authentication database with user `admin` and password `admin`.
340+
341+
`file:test.s3db?_auth&_auth_user=admin&_auth_pass=admin`
342+
343+
Create an user authentication database with user `admin` and password `admin` and use `SHA1` for the password encoding.
344+
345+
`file:test.s3db?_auth&_auth_user=admin&_auth_pass=admin&_auth_crypt=sha1`
346+
347+
### Password Encoding
348+
349+
The passwords within the user authentication module of SQLite are encoded with the SQLite function `sqlite_cryp`.
350+
This function uses a ceasar-cypher which is quite insecure.
351+
This library provides several additional password encoders which can be configured through the connection string.
352+
353+
The password cypher can be configured with the key `_auth_crypt`. And if the configured password encoder also requires an
354+
salt this can be configured with `_auth_salt`.
355+
356+
#### Available Encoders
357+
358+
- SHA1
359+
- SSHA1 (Salted SHA1)
360+
- SHA256
361+
- SSHA256 (salted SHA256)
362+
- SHA384
363+
- SSHA384 (salted SHA384)
364+
- SHA512
365+
- SSHA512 (salted SHA512)
366+
367+
### Restrictions
368+
369+
Operations on the database regarding to user management can only be preformed by an administrator user.
370+
371+
### Support
372+
373+
The user authentication supports two kinds of users
374+
375+
- administrators
376+
- regular users
377+
378+
### User Management
379+
380+
User management can be done by directly using the `*SQLiteConn` or by SQL.
381+
382+
#### SQL
383+
384+
The following sql functions are available for user management.
385+
386+
| Function | Arguments | Description |
387+
|----------|-----------|-------------|
388+
| `authenticate` | username `string`, password `string` | Will authenticate an user, this is done by the connection; and should not be used manually. |
389+
| `auth_user_add` | username `string`, password `string`, admin `int` | This function will add an user to the database.<br>if the database is not protected by user authentication it will enable it. Argument `admin` is an integer identifying if the added user should be an administrator. Only Administrators can add administrators. |
390+
| `auth_user_change` | username `string`, password `string`, admin `int` | Function to modify an user. Users can change their own password, but only an administrator can change the administrator flag. |
391+
| `authUserDelete` | username `string` | Delete an user from the database. Can only be used by an administrator. The current logged in administrator cannot be deleted. This is to make sure their is always an administrator remaining. |
392+
393+
These functions will return an integer.
394+
395+
- 0 (SQLITE_OK)
396+
- 23 (SQLITE_AUTH) Failed to perform due to authentication or insufficient privileges
397+
398+
##### Examples
399+
400+
```sql
401+
// Autheticate user
402+
// Create Admin User
403+
SELECT auth_user_add('admin2', 'admin2', 1);
404+
405+
// Change password for user
406+
SELECT auth_user_change('user', 'userpassword', 0);
407+
408+
// Delete user
409+
SELECT user_delete('user');
410+
```
411+
412+
#### *SQLiteConn
413+
414+
The following functions are available for User authentication from the `*SQLiteConn`.
415+
416+
| Function | Description |
417+
|----------|-------------|
418+
| `Authenticate(username, password string) error` | Authenticate user |
419+
| `AuthUserAdd(username, password string, admin bool) error` | Add user |
420+
| `AuthUserChange(username, password string, admin bool) error` | Modify user |
421+
| `AuthUserDelete(username string) error` | Delete user |
422+
423+
### Attached database
424+
425+
When using attached databases. SQLite will use the authentication from the `main` database for the attached database(s).
426+
306427
# Extensions
307428
308429
If you want your own extension to be listed here or you want to add a reference to an extension; please submit an Issue for this.

callback.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,18 @@ func callbackRetText(ctx *C.sqlite3_context, v reflect.Value) error {
331331
return nil
332332
}
333333

334+
func callbackRetNil(ctx *C.sqlite3_context, v reflect.Value) error {
335+
return nil
336+
}
337+
334338
func callbackRet(typ reflect.Type) (callbackRetConverter, error) {
335339
switch typ.Kind() {
340+
case reflect.Interface:
341+
errorInterface := reflect.TypeOf((*error)(nil)).Elem()
342+
if typ.Implements(errorInterface) {
343+
return callbackRetNil, nil
344+
}
345+
fallthrough
336346
case reflect.Slice:
337347
if typ.Elem().Kind() != reflect.Uint8 {
338348
return nil, errors.New("the only supported slice type is []byte")

0 commit comments

Comments
 (0)