Skip to content

Commit 3e190cb

Browse files
Merge pull request #17 from sqlitecloud/15-Support-to-authorization-via-token
feat(token): support to access tokens auth
2 parents fdca2a7 + c3d9eb1 commit 3e190cb

File tree

17 files changed

+140
-36
lines changed

17 files changed

+140
-36
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM mcr.microsoft.com/devcontainers/go:1-1-bullseye
1+
FROM mcr.microsoft.com/devcontainers/go:1.18
22

33
# Python environment
44
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \

.github/workflows/testing.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
go-version: '1.18'
2020
- name: Quality Assurance
2121
run: |
22-
go install golang.org/x/tools/cmd/goimports@latest
22+
go install golang.org/x/tools/cmd/goimports@v0.24.0
2323
gofmt -l ./*.go
2424
goimports -e -d ./*.go
2525
- name: golangci-lint

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ cli/.vscode
66
pkg/
77
bin
88
.env
9+
.cache

.golangci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
linters:
2+
exclusions:
3+
# Disable ST1006 (receiver name style) to allow existing receiver names in server.go
4+
rules:
5+
- path: server.go
6+
linters:
7+
- staticcheck
8+
- stylecheck
9+
text: ST1006

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ repos:
2828
- -w
2929
require_serial: true
3030
- repo: https://github.com/golangci/golangci-lint
31-
rev: v1.59.1
31+
rev: v1.50.1
3232
hooks:
3333
- id: golangci-lint
3434
exclude: ^pkg/

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ setup-ide:
99

1010
# Test SDK
1111
test:
12-
cd test; go test -v .
12+
cd test; go mod tidy && go test -v .
1313

1414
test-codecov:
15-
cd test; go test -v -race -coverprofile=coverage.out -covermode=atomic .
15+
cd test; go mod tidy && go test -v -race -coverprofile=coverage.out -covermode=atomic .
1616

1717
# GO SDK
1818
sdk: *.go

cli/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
)
1010

1111
require (
12-
github.com/google/go-cmp v0.5.9 // indirect
1312
github.com/kr/pretty v0.3.1 // indirect
1413
github.com/mattn/go-runewidth v0.0.3 // indirect
1514
github.com/pierrec/lz4 v2.6.1+incompatible // indirect

cli/go.sum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
22
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
33
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
44
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
5-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
6-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
5+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
76
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
87
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
98
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=

cli/sqlc.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ General Options:
7474
Output Format Options:
7575
-o, --output FILE Switch to BATCH mode, execute SQL Commands and send output to FILE, then exit.
7676
In BATCH mode, the default output format is switched to QUOTE.
77-
77+
7878
--echo Disables --quiet, print command(s) before execution
7979
--quiet Disables --echo, run command(s) quietly (no messages, only query output)
8080
--noheader Turn headers off
@@ -96,6 +96,7 @@ Connection Options:
9696
-e, --create Create database if it does not exist
9797
-i, --nonlinearizable Use non-linearizable mode for queries
9898
-a, --apikey KEY Use API key for authentication
99+
-k, --token TOKEN Use TOKEN for authentication
99100
-n, --noblob Disable BLOB support
100101
-x, --maxdata SIZE Set maximum data size for queries
101102
-y, --maxrows ROWS Set maximum number of rows for queries
@@ -150,6 +151,7 @@ type Parameter struct {
150151
Create bool `docopt:"--create"`
151152
NonLinearizable bool `docopt:"--nonlinearizable"`
152153
ApiKey string `docopt:"--apikey"`
154+
Token string `docopt:"--token"`
153155
NoBlob bool `docopt:"--noblob"`
154156
MaxData int `docopt:"--maxdata"`
155157
MaxRows int `docopt:"--maxrows"`
@@ -294,6 +296,7 @@ func parseParameters() (Parameter, error) {
294296
p["--tls"] = "SKIP"
295297
}
296298
p["--apikey"] = getFirstNoneEmptyString([]string{dropError(p.String("--apikey")), conf.ApiKey})
299+
p["--token"] = getFirstNoneEmptyString([]string{dropError(p.String("--token")), conf.Token})
297300
if conf.NoBlob {
298301
if b, err := p.Bool("--noblob"); err == nil {
299302
p["--noblob"] = b || conf.NoBlob
@@ -418,6 +421,7 @@ func main() {
418421
Port: parameter.Port,
419422
Username: parameter.User,
420423
Password: parameter.Password,
424+
Token: parameter.Token,
421425
Database: parameter.Database,
422426
Timeout: time.Duration(parameter.Timeout) * time.Second,
423427
Compression: parameter.Compress == sqlitecloud.CompressModeLZ4,

connection.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
type SQCloudConfig struct {
3535
Host string
3636
Port int
37+
ProjectID string // ProjectID to identify the user's node
3738
Username string
3839
Password string
3940
Database string
@@ -49,10 +50,11 @@ type SQCloudConfig struct {
4950
TlsInsecureSkipVerify bool // Accept invalid TLS certificates (no_verify_certificate)
5051
Pem string
5152
ApiKey string
52-
NoBlob bool // flag to tell the server to not send BLOB columns
53-
MaxData int // value to tell the server to not send columns with more than max_data bytes
54-
MaxRows int // value to control rowset chunks based on the number of rows
55-
MaxRowset int // value to control the maximum allowed size for a rowset
53+
Token string // Access Token for authentication
54+
NoBlob bool // flag to tell the server to not send BLOB columns
55+
MaxData int // value to tell the server to not send columns with more than max_data bytes
56+
MaxRows int // value to control rowset chunks based on the number of rows
57+
MaxRowset int // value to control the maximum allowed size for a rowset
5658
}
5759

5860
type SQCloud struct {
@@ -131,6 +133,7 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
131133
config.MaxRows = 0
132134
config.MaxRowset = 0
133135
config.ApiKey = ""
136+
config.Token = ""
134137

135138
sPort := strings.TrimSpace(u.Port())
136139
if len(sPort) > 0 {
@@ -139,6 +142,12 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
139142
}
140143
}
141144

145+
// eg: project ID "abvqqetyhq" in "abvqqetyhq.global3.ryujaz.sqlite.cloud"
146+
config.ProjectID = strings.Split(config.Host, ".")[0]
147+
if config.ProjectID == "" {
148+
return nil, fmt.Errorf("invalid connection string: missing project ID in host")
149+
}
150+
142151
for key, values := range u.Query() {
143152
lastLiteral := strings.TrimSpace(values[len(values)-1])
144153
switch strings.ToLower(strings.TrimSpace(key)) {
@@ -195,6 +204,8 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
195204
config.Secure, config.TlsInsecureSkipVerify, config.Pem = ParseTlsString(lastLiteral)
196205
case "apikey":
197206
config.ApiKey = lastLiteral
207+
case "token":
208+
config.Token = lastLiteral
198209
case "noblob":
199210
if b, err := parseBool(lastLiteral, config.NoBlob); err == nil {
200211
config.NoBlob = b
@@ -434,12 +445,14 @@ func connectionCommands(config SQCloudConfig) (string, []interface{}) {
434445
}
435446

436447
if config.ApiKey != "" {
437-
c, a := authWithKeyCommand(config.ApiKey)
448+
c, a := authWithApiKeyCommand(config.ApiKey)
438449
buffer += c
439450
args = append(args, a...)
440-
}
441-
442-
if config.Username != "" && config.Password != "" {
451+
} else if config.Token != "" {
452+
c, a := authWithTokenCommand(config.Token)
453+
buffer += c
454+
args = append(args, a...)
455+
} else if config.Username != "" && config.Password != "" {
443456
c, a := authCommand(config.Username, config.Password, config.PasswordHashed)
444457
buffer += c
445458
args = append(args, a...)

0 commit comments

Comments
 (0)