mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 23:13:11 -06:00
Add in devices_table to store device information
This commit is contained in:
parent
3b9222e8f7
commit
1e8f174837
|
|
@ -16,8 +16,9 @@ package authtypes
|
||||||
|
|
||||||
// Device represents a client's device (mobile, web, etc)
|
// Device represents a client's device (mobile, web, etc)
|
||||||
type Device struct {
|
type Device struct {
|
||||||
ID string
|
UserID string
|
||||||
UserID string
|
// The access_token granted to this device.
|
||||||
|
// This uniquely identifies the device from all other devices and clients.
|
||||||
AccessToken string
|
AccessToken string
|
||||||
// TODO: display name, last used timestamp, keys, etc
|
// TODO: display name, last used timestamp, keys, etc
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,95 @@
|
||||||
|
// Copyright 2017 Vector Creations Ltd
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package devices
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
)
|
||||||
|
|
||||||
|
const devicesSchema = `
|
||||||
|
-- Stores data about devices.
|
||||||
|
CREATE TABLE IF NOT EXISTS devices (
|
||||||
|
-- The access token granted to this device. This has to be the primary key
|
||||||
|
-- so we can distinguish which device is making a given request.
|
||||||
|
access_token TEXT NOT NULL PRIMARY KEY,
|
||||||
|
-- The Matrix user ID localpart for this device
|
||||||
|
localpart TEXT NOT NULL,
|
||||||
|
-- When this devices was first recognised on the network, as a unix timestamp (ms resolution).
|
||||||
|
created_ts BIGINT NOT NULL
|
||||||
|
-- TODO: device keys, device display names, last used ts and IP address?, token restrictions (if 3rd-party OAuth app)
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
const insertDeviceSQL = "" +
|
||||||
|
"INSERT INTO devices(access_token, localpart, created_ts) VALUES ($1, $2, $3)"
|
||||||
|
|
||||||
|
const selectDeviceByTokenSQL = "" +
|
||||||
|
"SELECT localpart FROM devices WHERE access_token = $1"
|
||||||
|
|
||||||
|
// TODO: List devices, delete device API
|
||||||
|
|
||||||
|
type devicesStatements struct {
|
||||||
|
insertDeviceStmt *sql.Stmt
|
||||||
|
selectDeviceByTokenStmt *sql.Stmt
|
||||||
|
serverName gomatrixserverlib.ServerName
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *devicesStatements) prepare(db *sql.DB, server gomatrixserverlib.ServerName) (err error) {
|
||||||
|
_, err = db.Exec(devicesSchema)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.insertDeviceStmt, err = db.Prepare(insertDeviceSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.selectDeviceByTokenStmt, err = db.Prepare(selectDeviceByTokenSQL); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.serverName = server
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// insertDevice creates a new device. Returns an error if a device with the same access token already exists.
|
||||||
|
// Returns the device on success.
|
||||||
|
func (s *devicesStatements) insertDevice(localpart, accessToken string) (dev *authtypes.Device, err error) {
|
||||||
|
createdTimeMS := time.Now().UnixNano() / 1000000
|
||||||
|
if _, err = s.insertDeviceStmt.Exec(accessToken, localpart, createdTimeMS); err == nil {
|
||||||
|
dev = &authtypes.Device{
|
||||||
|
UserID: makeUserID(localpart, s.serverName),
|
||||||
|
AccessToken: accessToken,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *devicesStatements) selectDeviceByToken(accessToken string) (*authtypes.Device, error) {
|
||||||
|
var dev authtypes.Device
|
||||||
|
var localpart string
|
||||||
|
err := s.selectDeviceByTokenStmt.QueryRow(accessToken).Scan(&localpart)
|
||||||
|
if err != nil {
|
||||||
|
dev.UserID = makeUserID(localpart, s.serverName)
|
||||||
|
dev.AccessToken = accessToken
|
||||||
|
}
|
||||||
|
return &dev, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeUserID(localpart string, server gomatrixserverlib.ServerName) string {
|
||||||
|
return fmt.Sprintf("@%s:%s", localpart, string(server))
|
||||||
|
}
|
||||||
|
|
@ -15,17 +15,30 @@
|
||||||
package devices
|
package devices
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Database represents a device database.
|
// Database represents a device database.
|
||||||
type Database struct {
|
type Database struct {
|
||||||
// TODO
|
db *sql.DB
|
||||||
|
devices devicesStatements
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDatabase creates a new device database
|
// NewDatabase creates a new device database
|
||||||
func NewDatabase(dataSource string) (*Database, error) {
|
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) {
|
||||||
return &Database{}, nil
|
var db *sql.DB
|
||||||
|
var err error
|
||||||
|
if db, err = sql.Open("postgres", dataSourceName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
d := devicesStatements{}
|
||||||
|
if err = d.prepare(db, serverName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Database{db, d}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDeviceByAccessToken returns the device matching the given access token.
|
// GetDeviceByAccessToken returns the device matching the given access token.
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Failed to setup account database(%s): %s", accountDataSource, err.Error())
|
log.Panicf("Failed to setup account database(%s): %s", accountDataSource, err.Error())
|
||||||
}
|
}
|
||||||
deviceDB, err := devices.NewDatabase(accountDataSource)
|
deviceDB, err := devices.NewDatabase(accountDataSource, serverName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Failed to setup device database(%s): %s", accountDataSource, err.Error())
|
log.Panicf("Failed to setup device database(%s): %s", accountDataSource, err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,9 @@ func loadConfig(configPath string) (*config.Sync, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// check required fields
|
// check required fields
|
||||||
|
if cfg.ServerName == "" {
|
||||||
|
log.Fatalf("'server_name' must be supplied in %s", configPath)
|
||||||
|
}
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,7 +77,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: DO NOT USE THIS DATA SOURCE (it's the sync one, not devices!)
|
// TODO: DO NOT USE THIS DATA SOURCE (it's the sync one, not devices!)
|
||||||
deviceDB, err := devices.NewDatabase(cfg.DataSource)
|
deviceDB, err := devices.NewDatabase(cfg.DataSource, cfg.ServerName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("startup: failed to create device database with data source %s : %s", cfg.DataSource, err)
|
log.Panicf("startup: failed to create device database with data source %s : %s", cfg.DataSource, err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,7 @@ func getLastRequestError() error {
|
||||||
var syncServerConfigFileContents = (`consumer_uris: ["` + kafkaURI + `"]
|
var syncServerConfigFileContents = (`consumer_uris: ["` + kafkaURI + `"]
|
||||||
roomserver_topic: "` + inputTopic + `"
|
roomserver_topic: "` + inputTopic + `"
|
||||||
database: "` + testDatabase + `"
|
database: "` + testDatabase + `"
|
||||||
|
server_name: "localhost"
|
||||||
`)
|
`)
|
||||||
|
|
||||||
func defaulting(value, defaultValue string) string {
|
func defaulting(value, defaultValue string) string {
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,10 @@
|
||||||
|
|
||||||
package config
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
)
|
||||||
|
|
||||||
// Sync contains the config information necessary to spin up a sync-server process.
|
// Sync contains the config information necessary to spin up a sync-server process.
|
||||||
type Sync struct {
|
type Sync struct {
|
||||||
// The topic for events which are written by the room server output log.
|
// The topic for events which are written by the room server output log.
|
||||||
|
|
@ -22,4 +26,6 @@ type Sync struct {
|
||||||
KafkaConsumerURIs []string `yaml:"consumer_uris"`
|
KafkaConsumerURIs []string `yaml:"consumer_uris"`
|
||||||
// The postgres connection config for connecting to the database e.g a postgres:// URI
|
// The postgres connection config for connecting to the database e.g a postgres:// URI
|
||||||
DataSource string `yaml:"database"`
|
DataSource string `yaml:"database"`
|
||||||
|
// The server_name of the running process e.g "localhost"
|
||||||
|
ServerName gomatrixserverlib.ServerName `yaml:"server_name"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue