Using/setting up user authentication for sqlite3 in golang

huangapple go评论71阅读模式
英文:

Using/setting up user authentication for sqlite3 in golang

问题

我必须在学校的任务中将我的数据库设置为密码保护。例如,如果有人试图访问我的数据库,它将要求输入密码。
我正在尝试使用go-sqlite3包,并已经阅读了官方指南。
第一步是使用go build --tags <FEATURE>
它给我一个错误信息:build .: cannot find module for path .
我不知道为什么,首先我们要构建什么。我还尝试搜索了一些实际的例子,但没有找到任何内容。

你能解释一下如何使用Go语言的go-sqlite3包来设置数据库的用户认证吗?
包的链接

英文:

I have to make my database password protected as a task in my school. For example if anyone tries to access my database it will ask the password.<br>
I am trying to use go-sqlite3 package and I have tried reading the official guide.<br>
First step is to use go build --tags &lt;FEATURE&gt;.<br>
It gaves me an error build .: cannot find module for path .<br>
I dont know why and what are we building in the first place. I tried searching for practical examples also and didnt found any.<br><br>
Can you explain to me how I can setup user authentication for my database using the golangs go-sqlite3 package?<br>
Link to the package

答案1

得分: 3

你需要将该指令中的&lt;FEATURE&gt;替换为你想要启用的扩展名,可以从下表中选择(似乎README中有一个错误,示例中去掉了sqlite_前缀;构建标签确实是sqlite_userauth)。

因此,要启用用户身份验证,可以使用go build -tags "sqlite_userauth"

在依赖go-sqlite3模块的项目中,只需确保使用-tags sqlite_userauth进行构建。

以下是一个最简示例,展示了如何在项目中使用此功能:

mkdir sqlite3auth
cd sqlite3auth
go mod init sqlite3auth
touch main.go

main.go:

package main

import (
        "database/sql"
        "log"

        "github.com/mattn/go-sqlite3"
)

func main() {
        // 这不是必需的;只是为了查看是否启用了认证扩展
        sql.Register("sqlite3_log", &sqlite3.SQLiteDriver{
                ConnectHook: func(conn *sqlite3.SQLiteConn) error {
                        log.Printf("Auth enabled: %v\n", conn.AuthEnabled())
                        return nil
                },
        })

        // 这是常规的数据库操作(除了我们使用的sqlite3_log驱动程序)
        db, err := sql.Open("sqlite3_log", "file:test.db?_auth&_auth_user=admin&_auth_pass=admin")
        if err != nil {
                log.Fatal(err)
        }
        defer db.Close()

        _, err = db.Exec(`select 1`)
        if err != nil {
                log.Fatal(err)
        }
}
go mod tidy
go: finding module for package github.com/mattn/go-sqlite3
go: found github.com/mattn/go-sqlite3 in github.com/mattn/go-sqlite3 v1.14.10
# 首先使用认证扩展进行构建(-o NAME只是为二进制文件命名)
go build -tags sqlite_userauth -o auth .
# 然后再次构建,不使用认证扩展
go build -o noauth .

./auth
2022/01/27 21:47:46 Auth enabled: true
./noauth
2022/01/27 21:47:46 Auth enabled: false
英文:

You need to replace &lt;FEATURE&gt; in that instruction by extension name(s) you want to enable from table below (Seems there's an error in README and it has sqlite_ prefix stripped in example; build tag is indeed sqlite_userauth).

So, to enable user authentication that will be go build -tags &quot;sqlite_userauth&quot;.

In your project with go-sqlite3 module dependency just make sure that you build with -tags sqlite_userauth.

Here is minimal example showing how you would work with this in your project:

mkdir sqlite3auth
cd sqlite3auth
go mod init sqlite3auth
touch main.go

main.go:

package main

import (
        &quot;database/sql&quot;
        &quot;log&quot;

        &quot;github.com/mattn/go-sqlite3&quot;
)

func main() {
        // This is not necessary; just to see if auth extension is enabled
        sql.Register(&quot;sqlite3_log&quot;, &amp;sqlite3.SQLiteDriver{
                ConnectHook: func(conn *sqlite3.SQLiteConn) error {
                        log.Printf(&quot;Auth enabled: %v\n&quot;, conn.AuthEnabled())
                        return nil
                },
        })

        // This is usual DB stuff (except with our sqlite3_log driver)
        db, err := sql.Open(&quot;sqlite3_log&quot;, &quot;file:test.db?_auth&amp;_auth_user=admin&amp;_auth_pass=admin&quot;)
        if err != nil {
                log.Fatal(err)
        }
        defer db.Close()

        _, err = db.Exec(`select 1`)
        if err != nil {
                log.Fatal(err)
        }
}
go mod tidy
go: finding module for package github.com/mattn/go-sqlite3
go: found github.com/mattn/go-sqlite3 in github.com/mattn/go-sqlite3 v1.14.10
# First build with auth extension (-o NAME is just to give binary a name)
go build -tags sqlite_userauth -o auth .
# then build without it
go build -o noauth .

./auth
2022/01/27 21:47:46 Auth enabled: true
./noauth
2022/01/27 21:47:46 Auth enabled: false

huangapple
  • 本文由 发表于 2022年1月27日 18:45:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/70877203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定