Commit 1e84ce82 authored by ale's avatar ale

Move DeviceInfo struct to id/usermetadb

Removes the dependency loop between the two projects.
parent 1af2f196
Pipeline #10579 passed with stages
in 1 minute and 58 seconds
module git.autistici.org/id/auth
go 1.15
go 1.14
require (
git.autistici.org/ai3/go-common v0.0.0-20210110180225-a05c683cfe23
git.autistici.org/id/usermetadb v0.0.0-20201004075630-d413cacd9af0
git.autistici.org/id/usermetadb v0.0.0-20210112103616-f674f50555fc
github.com/boombuler/barcode v0.0.0-20170618053812-56ef0af91246 // indirect
github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737
github.com/cenkalti/backoff v2.2.1+incompatible
......@@ -13,7 +13,7 @@ require (
github.com/go-sql-driver/mysql v1.4.0
github.com/google/go-cmp v0.5.4
github.com/lib/pq v0.0.0-20190326042056-d6156e141ac6
github.com/mattn/go-sqlite3 v0.0.0-20180926090220-0a88db3545c4
github.com/mattn/go-sqlite3 v0.0.0-20200618132256-aa77c03e2fcb
github.com/patrickmn/go-cache v0.0.0-20180815053127-5633e0862627
github.com/pquerna/otp v1.0.0
github.com/prometheus/client_golang v1.9.0
......
......@@ -2,14 +2,22 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
contrib.go.opencensus.io/exporter/zipkin v0.1.2 h1:YqE293IZrKtqPnpwDPH/lOqTWD/s3Iwabycam74JV3g=
contrib.go.opencensus.io/exporter/zipkin v0.1.2/go.mod h1:mP5xM3rrgOjpn79MM8fZbj3gsxcuytSqtH0dxSWW1RE=
git.autistici.org/ai3/go-common v0.0.0-20210109170950-49f8d26bcc81/go.mod h1:nuLJyKZZaC3DBPN4gA1qdGXcm0U5WCcus1z3pI8RdTE=
git.autistici.org/ai3/go-common v0.0.0-20210110180225-a05c683cfe23 h1:YHSG7Vr8nPRmXa7tW2UR8kfzwerjA5GD+bI84nxq2kA=
git.autistici.org/ai3/go-common v0.0.0-20210110180225-a05c683cfe23/go.mod h1:Iik+i0XmqNPTBjWl3vicFz0kjfFK5HBvyFsfIC4S1Ik=
git.autistici.org/id/auth v0.0.0-20210110171913-dd493db32815/go.mod h1:Hq4zcqE2hbrXsC9j79kzfnBf2BqlGmuVCRIz+AwX/FY=
git.autistici.org/id/usermetadb v0.0.0-20190209105239-61e5a7b24130/go.mod h1:mChOzl9ekSRcfHoFwe3Uv1mccoaZwurlDsIuKRxM8no=
git.autistici.org/id/usermetadb v0.0.0-20201004075630-d413cacd9af0 h1:axPNeleZfEWlpAYGoKg8rdtDmuH25eVC0ekFIPWfVss=
git.autistici.org/id/usermetadb v0.0.0-20201004075630-d413cacd9af0/go.mod h1:mChOzl9ekSRcfHoFwe3Uv1mccoaZwurlDsIuKRxM8no=
git.autistici.org/id/usermetadb v0.0.0-20210110182959-9095e7a8c58f h1:cECwqx9Q5nIiWpfv5nTbjxKEGq4XMGWw8mNTkDjq6KU=
git.autistici.org/id/usermetadb v0.0.0-20210110182959-9095e7a8c58f/go.mod h1:8V7AJ8ziCBO9AJeElcEzmFOAGT6NZn4B1+ScL2Z/ZwU=
git.autistici.org/id/usermetadb v0.0.0-20210112103616-f674f50555fc h1:DSjHrWknHNFgon7n+fPHJhJP9+Z1TY2d5JI8dwBVO/Y=
git.autistici.org/id/usermetadb v0.0.0-20210112103616-f674f50555fc/go.mod h1:jvYKNZZdNfD7rJBqEJaLzl37r/6TRMFgEJAXt6ZM+MU=
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c h1:/IBSNwUN8+eKzUzbJPqhK839ygXJ82sde8x3ogr6R28=
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
......@@ -21,6 +29,7 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/amoghe/go-crypt v0.0.0-20191109212615-b2ff80594b7f h1:JxPBJknH9/9Yp0BPLZII8Cn4vaWPNsFOdkmpIwPhO8A=
github.com/amoghe/go-crypt v0.0.0-20191109212615-b2ff80594b7f/go.mod h1:eFiR01PwTcpbzXtdMces7zxg6utvFM5puiWHpWB8D/k=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
......@@ -53,7 +62,10 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7 h1:u9SHYsPQNyt5tgDm3YN7+9dYrpK96E5wFilTFWIDZOM=
github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd/v22 v22.1.0 h1:kq/SbG2BCKLkDKkjQf5OWwKWUKj1lgs3lFI4PxnR5lg=
github.com/coreos/go-systemd/v22 v22.1.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
......@@ -144,6 +156,7 @@ github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
......@@ -198,12 +211,15 @@ github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-b
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40/go.mod h1:vy1vK6wD6j7xX6O6hXe621WabdtNkou2h7uRtTfRMyg=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/mattes/migrate v0.0.0-20180508041624-4768a648fbd9/go.mod h1:LJcqgpj1jQoxv3m2VXd3drv0suK5CbN/RCX7MXwgnVI=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-sqlite3 v0.0.0-20180926090220-0a88db3545c4 h1:yqLtdnsIwi5hBOhHZyF0JDPMLKiPT3R3rBIND41j7mk=
github.com/mattn/go-sqlite3 v0.0.0-20180926090220-0a88db3545c4/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v0.0.0-20200618132256-aa77c03e2fcb h1:bbMxpirqnxN6EmkaG8nxhaDyqDjjOfA59ha+uYWeaMU=
github.com/mattn/go-sqlite3 v0.0.0-20200618132256-aa77c03e2fcb/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
......@@ -360,6 +376,7 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
......@@ -376,6 +393,8 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
......@@ -469,6 +488,7 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
......
......@@ -3,48 +3,10 @@ package auth
import (
"fmt"
"git.autistici.org/id/usermetadb"
"github.com/tstranex/u2f"
)
// DeviceInfo holds information about the client device. We use a
// simple persistent cookie to track the same client device across
// multiple session.
type DeviceInfo struct {
ID string `json:"id"`
RemoteAddr string `json:"remote_addr"`
RemoteZone string `json:"remote_zone"`
UserAgent string `json:"user_agent"`
Browser string `json:"browser"`
OS string `json:"os"`
Mobile bool `json:"mobile"`
}
func (d *DeviceInfo) encodeToMap(m map[string]string, prefix string) {
m[prefix+"id"] = d.ID
m[prefix+"remote_addr"] = d.RemoteAddr
m[prefix+"remote_zone"] = d.RemoteZone
m[prefix+"browser"] = d.Browser
m[prefix+"os"] = d.OS
m[prefix+"user_agent"] = d.UserAgent
if d.Mobile {
m[prefix+"mobile"] = "true"
} else {
m[prefix+"mobile"] = "false"
}
}
func decodeDeviceInfoFromMap(m map[string]string, prefix string) *DeviceInfo {
return &DeviceInfo{
ID: m[prefix+"id"],
RemoteAddr: m[prefix+"remote_addr"],
RemoteZone: m[prefix+"remote_zone"],
Browser: m[prefix+"browser"],
OS: m[prefix+"os"],
UserAgent: m[prefix+"user_agent"],
Mobile: m[prefix+"mobile"] == "true",
}
}
// Request to authenticate a user. It supports multiple methods for
// authentication including challenge-response 2FA.
type Request struct {
......@@ -54,7 +16,7 @@ type Request struct {
OTP string
U2FAppID string
U2FResponse *u2f.SignResponse
DeviceInfo *DeviceInfo
DeviceInfo *usermetadb.DeviceInfo
}
func (r *Request) EncodeToMap(m map[string]string, prefix string) {
......@@ -72,7 +34,7 @@ func (r *Request) EncodeToMap(m map[string]string, prefix string) {
encodeU2FResponseToMap(r.U2FResponse, m, prefix+"u2f_response.")
}
if r.DeviceInfo != nil {
r.DeviceInfo.encodeToMap(m, prefix+"device.")
r.DeviceInfo.EncodeToMap(m, prefix+"device.")
}
}
......@@ -83,7 +45,7 @@ func (r *Request) DecodeFromMap(m map[string]string, prefix string) {
r.OTP = m[prefix+"otp"]
r.U2FAppID = m[prefix+"u2f_app_id"]
r.U2FResponse = decodeU2FResponseFromMap(m, prefix+"u2f_response.")
r.DeviceInfo = decodeDeviceInfoFromMap(m, prefix+"device.")
r.DeviceInfo = usermetadb.DecodeDeviceInfoFromMap(m, prefix+"device.")
}
// UserInfo contains optional user information that may be useful to
......
......@@ -5,6 +5,8 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/tstranex/u2f"
"git.autistici.org/id/usermetadb"
)
func TestProtocol_SerializeRequest(t *testing.T) {
......@@ -21,7 +23,7 @@ func TestProtocol_SerializeRequest(t *testing.T) {
SignatureData: "signatureData",
ClientData: "clientData",
},
DeviceInfo: &DeviceInfo{
DeviceInfo: &usermetadb.DeviceInfo{
ID: "ah9hf3kh2",
RemoteAddr: "1.2.3.4",
RemoteZone: "cc",
......
......@@ -13,6 +13,7 @@ import (
"git.autistici.org/id/auth"
"git.autistici.org/id/auth/client"
"git.autistici.org/id/usermetadb"
)
type testServer struct {
......@@ -310,7 +311,7 @@ func TestAuthServer_Blacklist_UnknownUser(t *testing.T) {
Service: "test",
Username: fmt.Sprintf("nonexistinguser%d", i),
Password: []byte("bad_password"),
DeviceInfo: &auth.DeviceInfo{
DeviceInfo: &usermetadb.DeviceInfo{
RemoteAddr: "1.2.3.4",
},
})
......
......@@ -7,6 +7,7 @@ import (
"git.autistici.org/ai3/go-common/clientutil"
"git.autistici.org/id/auth/backend"
"git.autistici.org/id/usermetadb"
"git.autistici.org/id/usermetadb/client"
"git.autistici.org/id/auth"
......@@ -14,7 +15,7 @@ import (
// Inject an interface for testing purposes.
type checkDeviceClient interface {
CheckDevice(context.Context, string, string, *auth.DeviceInfo) (bool, error)
CheckDevice(context.Context, string, string, *usermetadb.DeviceInfo) (bool, error)
}
type deviceFilter struct {
......@@ -65,7 +66,7 @@ func (f *deviceFilter) Filter(user *backend.User, req *auth.Request, resp *auth.
return resp
}
func (f *deviceFilter) sendNewDeviceEmail(user *backend.User, dev *auth.DeviceInfo) error {
func (f *deviceFilter) sendNewDeviceEmail(user *backend.User, dev *usermetadb.DeviceInfo) error {
// TODO: Not implemented.
log.Printf("new device for user %s: %+v", user.Name, dev)
return nil
......
include: "https://git.autistici.org/ai3/build-deb/raw/master/ci-common.yml"
include: "https://git.autistici.org/ai3/build-deb/raw/master/ci-nextstable.yml"
......@@ -4,14 +4,12 @@ import (
"context"
"git.autistici.org/ai3/go-common/clientutil"
"git.autistici.org/id/auth"
"git.autistici.org/id/usermetadb"
)
// Client for the user-meta-server API.
type Client interface {
CheckDevice(context.Context, string, string, *auth.DeviceInfo) (bool, error)
CheckDevice(context.Context, string, string, *usermetadb.DeviceInfo) (bool, error)
AddLog(context.Context, string, *usermetadb.LogEntry) error
GetUserDevices(context.Context, string, string) ([]*usermetadb.MetaDeviceInfo, error)
GetUserLogs(context.Context, string, string, int, int) ([]*usermetadb.LogEntry, error)
......@@ -32,7 +30,7 @@ func New(config *clientutil.BackendConfig) (Client, error) {
return &udbClient{be}, nil
}
func (c *udbClient) CheckDevice(ctx context.Context, shard, username string, dev *auth.DeviceInfo) (bool, error) {
func (c *udbClient) CheckDevice(ctx context.Context, shard, username string, dev *usermetadb.DeviceInfo) (bool, error) {
req := usermetadb.CheckDeviceRequest{
Username: username,
DeviceInfo: dev,
......
module git.autistici.org/id/usermetadb
go 1.14
require (
git.autistici.org/ai3/go-common v0.0.0-20210110180225-a05c683cfe23
github.com/google/go-cmp v0.5.4
github.com/gorilla/mux v1.8.0 // indirect
github.com/mattes/migrate v0.0.0-20180508041624-4768a648fbd9
github.com/mattn/go-sqlite3 v0.0.0-20200618132256-aa77c03e2fcb
gopkg.in/yaml.v2 v2.3.0
)
This diff is collapsed.
......@@ -5,13 +5,11 @@ package usermetadb
import (
"errors"
"time"
"git.autistici.org/id/auth"
)
type CheckDeviceRequest struct {
Username string `json:"username"`
DeviceInfo *auth.DeviceInfo `json:"device_info"`
Username string `json:"username"`
DeviceInfo *DeviceInfo `json:"device_info"`
}
type CheckDeviceResponse struct {
......@@ -33,14 +31,54 @@ const (
LoginMethodU2F = "u2f"
)
// DeviceInfo holds information about the client device. We use a
// simple persistent cookie to track the same client device across
// multiple session.
type DeviceInfo struct {
ID string `json:"id"`
RemoteAddr string `json:"remote_addr"`
RemoteZone string `json:"remote_zone"`
UserAgent string `json:"user_agent"`
Browser string `json:"browser"`
OS string `json:"os"`
Mobile bool `json:"mobile"`
}
func (d *DeviceInfo) EncodeToMap(m map[string]string, prefix string) {
m[prefix+"id"] = d.ID
m[prefix+"remote_addr"] = d.RemoteAddr
m[prefix+"remote_zone"] = d.RemoteZone
m[prefix+"browser"] = d.Browser
m[prefix+"os"] = d.OS
m[prefix+"user_agent"] = d.UserAgent
if d.Mobile {
m[prefix+"mobile"] = "true"
} else {
m[prefix+"mobile"] = "false"
}
}
func DecodeDeviceInfoFromMap(m map[string]string, prefix string) *DeviceInfo {
return &DeviceInfo{
ID: m[prefix+"id"],
RemoteAddr: m[prefix+"remote_addr"],
RemoteZone: m[prefix+"remote_zone"],
Browser: m[prefix+"browser"],
OS: m[prefix+"os"],
UserAgent: m[prefix+"user_agent"],
Mobile: m[prefix+"mobile"] == "true",
}
}
// LogEntry represents an authentication event in the user-specific log.
type LogEntry struct {
Timestamp time.Time `json:"timestamp"`
Username string `json:"username"`
Type string `json:"log_type"`
Message string `json:"message,omitempty"`
Service string `json:"service,omitempty"`
LoginMethod string `json:"login_method,omitempty"`
DeviceInfo *auth.DeviceInfo `json:"device_info,omitempty"`
Timestamp time.Time `json:"timestamp"`
Username string `json:"username"`
Type string `json:"log_type"`
Message string `json:"message,omitempty"`
Service string `json:"service,omitempty"`
LoginMethod string `json:"login_method,omitempty"`
DeviceInfo *DeviceInfo `json:"device_info,omitempty"`
}
func (e *LogEntry) Validate() error {
......@@ -73,10 +111,10 @@ type GetUserDevicesRequest struct {
}
type MetaDeviceInfo struct {
DeviceInfo *auth.DeviceInfo `json:"device_info"`
FirstSeen time.Time `json:"first_seen"`
LastSeen time.Time `json:"last_seen"`
NumLogins int `json:"num_logins"`
DeviceInfo *DeviceInfo `json:"device_info"`
FirstSeen time.Time `json:"first_seen"`
LastSeen time.Time `json:"last_seen"`
NumLogins int `json:"num_logins"`
}
type GetUserDevicesResponse struct {
......
......@@ -8,33 +8,28 @@ addons:
apt:
update: true
env:
matrix:
- GOTAGS=
- GOTAGS=libsqlite3
- GOTAGS="sqlite_allow_uri_authority sqlite_app_armor sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_userauth sqlite_vacuum_incr sqlite_vtable"
- GOTAGS=sqlite_vacuum_full
go:
- 1.9.x
- 1.10.x
- 1.11.x
- 1.12.x
- 1.13.x
- master
before_install:
- |
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
brew update
fi
- |
go get github.com/smartystreets/goconvey
if [[ "${GOOS}" != "windows" ]]; then
go get github.com/mattn/goveralls
go get golang.org/x/tools/cmd/cover
fi
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
script:
- GOOS=$(go env GOOS) GOARCH=$(go env GOARCH) go build -v -tags "${GOTAGS}" .
- |
if [[ "${GOOS}" != "windows" ]]; then
$HOME/gopath/bin/goveralls -repotoken 3qJVUE0iQwqnCbmNcDsjYu1nh4J4KIFXx
go test -race -v . -tags "${GOTAGS}"
fi
- $HOME/gopath/bin/goveralls -repotoken 3qJVUE0iQwqnCbmNcDsjYu1nh4J4KIFXx -parallel
- go test -race -v . -tags ""
- go test -race -v . -tags "libsqlite3"
- go test -race -v . -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_userauth sqlite_vacuum_incr sqlite_vtable sqlite_unlock_notify"
- go test -race -v . -tags "sqlite_vacuum_full"
notifications:
webhooks: https://coveralls.io/webhook?repo_token=3qJVUE0iQwqnCbmNcDsjYu1nh4J4KIFXx
......@@ -3,30 +3,39 @@ go-sqlite3
[![GoDoc Reference](https://godoc.org/github.com/mattn/go-sqlite3?status.svg)](http://godoc.org/github.com/mattn/go-sqlite3)
[![Build Status](https://travis-ci.org/mattn/go-sqlite3.svg?branch=master)](https://travis-ci.org/mattn/go-sqlite3)
[![Financial Contributors on Open Collective](https://opencollective.com/mattn-go-sqlite3/all/badge.svg?label=financial+contributors)](https://opencollective.com/mattn-go-sqlite3)
[![Coverage Status](https://coveralls.io/repos/mattn/go-sqlite3/badge.svg?branch=master)](https://coveralls.io/r/mattn/go-sqlite3?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/mattn/go-sqlite3)](https://goreportcard.com/report/github.com/mattn/go-sqlite3)
Latest stable version is v1.14 or later not v2.
~~**NOTE:** The increase to v2 was an accident. There were no major changes or features.~~
# Description
sqlite3 driver conforming to the built-in database/sql interface
Supported Golang version:
- 1.9.x
- 1.10.x
Supported Golang version: See .travis.yml
[This package follows the official Golang Release Policy.](https://golang.org/doc/devel/release.html#policy)
### Overview
- [go-sqlite3](#go-sqlite3)
- [Description](#description)
- [Overview](#overview)
- [Installation](#installation)
- [API Reference](#api-reference)
- [Connection String](#connection-string)
- [DSN Examples](#dsn-examples)
- [Features](#features)
- [Usage](#usage)
- [Feature / Extension List](#feature--extension-list)
- [Compilation](#compilation)
- [Android](#android)
- [ARM](#arm)
- [Cross Compile](#cross-compile)
- [Google Cloud Platform](#google-cloud-platform)
- [ARM](#arm)
- [Cross Compile](#cross-compile)
- [Google Cloud Platform](#google-cloud-platform)
- [Linux](#linux)
- [Alpine](#alpine)
- [Fedora](#fedora)
......@@ -36,11 +45,22 @@ Supported Golang version:
- [Errors](#errors)
- [User Authentication](#user-authentication)
- [Compile](#compile)
- [Usage](#usage)
- [Usage](#usage-1)
- [Create protected database](#create-protected-database)
- [Password Encoding](#password-encoding)
- [Available Encoders](#available-encoders)
- [Restrictions](#restrictions)
- [Support](#support)
- [User Management](#user-management)
- [SQL](#sql)
- [Examples](#examples)
- [*SQLiteConn](#sqliteconn)
- [Attached database](#attached-database)
- [Extensions](#extensions)
- [Spatialite](#spatialite)
- [FAQ](#faq)
- [License](#license)
- [Author](#author)
# Installation
......@@ -67,6 +87,7 @@ This is also known as a DSN string. (Data Source Name).
Options are append after the filename of the SQLite database.
The database filename and options are seperated by an `?` (Question Mark).
Options should be URL-encoded (see [url.QueryEscape](https://golang.org/pkg/net/url/#QueryEscape)).
This also applies when using an in-memory database instead of a file.
......@@ -150,6 +171,7 @@ go build --tags "icu json1 fts5 secure_delete"
| International Components for Unicode | sqlite_icu | This option causes the International Components for Unicode or "ICU" extension to SQLite to be added to the build |
| Introspect PRAGMAS | sqlite_introspect | This option adds some extra PRAGMA statements. <ul><li>PRAGMA function_list</li><li>PRAGMA module_list</li><li>PRAGMA pragma_list</li></ul> |
| JSON SQL Functions | sqlite_json | When this option is defined in the amalgamation, the JSON SQL functions are added to the build automatically |
| Pre Update Hook | sqlite_preupdate_hook | Registers a callback function that is invoked prior to each INSERT, UPDATE, and DELETE operation on a database table. |
| 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 |
| Secure Delete (FAST) | sqlite_secure_delete_fast | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) |
| Tracing / Debug | sqlite_trace | Activate trace functions |
......@@ -192,13 +214,19 @@ This library can be cross-compiled.
In some cases you are required to the `CC` environment variable with the cross compiler.
Additional information:
- [#491](https://github.com/mattn/go-sqlite3/issues/491)
- [#560](https://github.com/mattn/go-sqlite3/issues/560)
## Cross Compiling from MAC OSX
The simplest way to cross compile from OSX is to use [xgo](https://github.com/karalabe/xgo).
Steps:
- Install [xgo](https://github.com/karalabe/xgo) (`go get github.com/karalabe/xgo`).
- Ensure that your project is within your `GOPATH`.
- Run `xgo local/path/to/project`.
Please refer to the project's [README](https://github.com/karalabe/xgo/blob/master/README.md) for further information.
# Google Cloud Platform
Building on GCP is not possible because `Google Cloud Platform does not allow `gcc` to be executed.
Building on GCP is not possible because Google Cloud Platform does not allow `gcc` to be executed.
Please work only with compiled final binaries.
......@@ -248,7 +276,7 @@ Required dependency
brew install sqlite3
```
For OSX there is an additional package install which is required if you whish to build the `icu` extension.
For OSX there is an additional package install which is required if you wish to build the `icu` extension.
This additional package can be installed with `homebrew`.
......@@ -281,7 +309,7 @@ To compile this package on Windows OS you must have the `gcc` compiler installed
3) Open a terminal for the TDM-GCC toolchain, can be found in the Windows Start menu.
4) Navigate to your project folder and run the `go build ...` command for this package.
For example the TDM-GCC Toolchain can be found [here](ttps://sourceforge.net/projects/tdm-gcc/).
For example the TDM-GCC Toolchain can be found [here](https://sourceforge.net/projects/tdm-gcc/).
## Errors
......@@ -290,7 +318,7 @@ For example the TDM-GCC Toolchain can be found [here](ttps://sourceforge.net/pro
When receiving a compile time error referencing recompile with `-FPIC` then you
are probably using a hardend system.
You can copile the library on a hardend system with the following command.
You can compile the library on a hardend system with the following command.
```bash
go build -ldflags '-extldflags=-fno-PIC'
......@@ -433,6 +461,16 @@ If you want your own extension to be listed here or you want to add a reference
Spatialite is available as an extension to SQLite, and can be used in combination with this repository.
For an example see [shaxbee/go-spatialite](https://github.com/shaxbee/go-spatialite).
## extension-functions.c from SQLite3 Contrib
extension-functions.c is available as an extension to SQLite, and provides the following functions:
- Math: acos, asin, atan, atn2, atan2, acosh, asinh, atanh, difference, degrees, radians, cos, sin, tan, cot, cosh, sinh, tanh, coth, exp, log, log10, power, sign, sqrt, square, ceil, floor, pi.
- String: replicate, charindex, leftstr, rightstr, ltrim, rtrim, trim, replace, reverse, proper, padl, padr, padc, strfilter.
- Aggregate: stdev, variance, mode, median, lower_quartile, upper_quartile
For an example see [dinedal/go-sqlite3-extension-functions](https://github.com/dinedal/go-sqlite3-extension-functions).
# FAQ
- Getting insert error while query is opened.
......@@ -457,15 +495,19 @@ For an example see [shaxbee/go-spatialite](https://github.com/shaxbee/go-spatial
Why is it racy if I use a `sql.Open("sqlite3", ":memory:")` database?
Each connection to :memory: opens a brand new in-memory sql database, so if
Each connection to `":memory:"` opens a brand new in-memory sql database, so if
the stdlib's sql engine happens to open another connection and you've only