Skip to content

Commit d8246d9

Browse files
committed
feat(token): support to access tokens auth
1 parent fdca2a7 commit d8246d9

File tree

12 files changed

+113
-19
lines changed

12 files changed

+113
-19
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ 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:
1515
cd test; go test -v -race -coverprofile=coverage.out -covermode=atomic .

connection.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ type SQCloudConfig struct {
4949
TlsInsecureSkipVerify bool // Accept invalid TLS certificates (no_verify_certificate)
5050
Pem string
5151
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
52+
Token string // Access Token for authentication
53+
NoBlob bool // flag to tell the server to not send BLOB columns
54+
MaxData int // value to tell the server to not send columns with more than max_data bytes
55+
MaxRows int // value to control rowset chunks based on the number of rows
56+
MaxRowset int // value to control the maximum allowed size for a rowset
5657
}
5758

5859
type SQCloud struct {
@@ -131,6 +132,7 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
131132
config.MaxRows = 0
132133
config.MaxRowset = 0
133134
config.ApiKey = ""
135+
config.Token = ""
134136

135137
sPort := strings.TrimSpace(u.Port())
136138
if len(sPort) > 0 {
@@ -195,6 +197,8 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
195197
config.Secure, config.TlsInsecureSkipVerify, config.Pem = ParseTlsString(lastLiteral)
196198
case "apikey":
197199
config.ApiKey = lastLiteral
200+
case "token":
201+
config.Token = lastLiteral
198202
case "noblob":
199203
if b, err := parseBool(lastLiteral, config.NoBlob); err == nil {
200204
config.NoBlob = b
@@ -434,12 +438,14 @@ func connectionCommands(config SQCloudConfig) (string, []interface{}) {
434438
}
435439

436440
if config.ApiKey != "" {
437-
c, a := authWithKeyCommand(config.ApiKey)
441+
c, a := authWithApiKeyCommand(config.ApiKey)
438442
buffer += c
439443
args = append(args, a...)
440-
}
441-
442-
if config.Username != "" && config.Password != "" {
444+
} else if config.Token != "" {
445+
c, a := authWithTokenCommand(config.Token)
446+
buffer += c
447+
args = append(args, a...)
448+
} else if config.Username != "" && config.Password != "" {
443449
c, a := authCommand(config.Username, config.Password, config.PasswordHashed)
444450
buffer += c
445451
args = append(args, a...)

connection_internal_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sqlitecloud
2+
3+
import (
4+
"reflect"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestConnectionCommandsAuthPreference(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
config SQCloudConfig
13+
wantCmd string
14+
wantArgs []interface{}
15+
}{
16+
{"api key wins", SQCloudConfig{ApiKey: "k", Token: "t", Username: "u", Password: "p"}, "AUTH APIKEY ?;", []interface{}{"k"}},
17+
{"token next", SQCloudConfig{Token: "t", Username: "u", Password: "p"}, "AUTH TOKEN ?;", []interface{}{"t"}},
18+
{"user/pass fallback", SQCloudConfig{Username: "u", Password: "p"}, "AUTH USER ? PASSWORD ?;", []interface{}{"u", "p"}},
19+
{"hashed password", SQCloudConfig{Username: "u", Password: "hash", PasswordHashed: true}, "AUTH USER ? HASH ?;", []interface{}{"u", "hash"}},
20+
{"no credentials", SQCloudConfig{}, "", []interface{}{}},
21+
}
22+
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
gotBuf, gotArgs := connectionCommands(tt.config)
26+
if tt.wantCmd == "" && gotBuf != "" {
27+
t.Fatalf("expected no auth command, got %q", gotBuf)
28+
}
29+
if tt.wantCmd != "" && !strings.Contains(gotBuf, tt.wantCmd) {
30+
t.Fatalf("expected %q in buffer, got %q", tt.wantCmd, gotBuf)
31+
}
32+
if !reflect.DeepEqual(gotArgs, tt.wantArgs) {
33+
t.Fatalf("args mismatch: want %v got %v", tt.wantArgs, gotArgs)
34+
}
35+
})
36+
}
37+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ require (
1010

1111
require (
1212
github.com/frankban/quicktest v1.14.3 // indirect
13-
golang.org/x/sys v0.7.0 // indirect
13+
github.com/google/go-cmp v0.6.0 // indirect
14+
golang.org/x/sys v0.6.0 // indirect
1415
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
22
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
33
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
4-
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
54
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
5+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
67
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
78
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
89
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
@@ -16,11 +17,10 @@ github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBO
1617
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
1718
github.com/xo/dburl v0.13.1 h1:EV+BCdo539sc/mBrny0VxaEGLM0b1U0mJA9RpP80ux0=
1819
github.com/xo/dburl v0.13.1/go.mod h1:B7/G9FGungw6ighV8xJNwWYQPMfn3gsi2sn5SE8Bzco=
19-
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
20-
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
20+
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
21+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2122
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
2223
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
23-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
2424
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2525
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2626
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=

0 commit comments

Comments
 (0)