2017-05-22 17:49:32 +02:00
|
|
|
package devices
|
|
|
|
|
2017-05-23 18:43:05 +02:00
|
|
|
import (
|
2017-09-18 16:51:26 +02:00
|
|
|
"context"
|
2020-02-13 18:27:33 +01:00
|
|
|
"net/url"
|
2017-05-25 14:33:50 +02:00
|
|
|
|
2017-05-23 18:43:05 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2020-02-13 18:27:33 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices/postgres"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices/sqlite3"
|
2017-05-25 14:33:50 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-05-23 18:43:05 +02:00
|
|
|
)
|
|
|
|
|
2020-02-13 18:27:33 +01:00
|
|
|
type Database interface {
|
|
|
|
GetDeviceByAccessToken(ctx context.Context, token string) (*authtypes.Device, error)
|
|
|
|
GetDeviceByID(ctx context.Context, localpart, deviceID string) (*authtypes.Device, error)
|
|
|
|
GetDevicesByLocalpart(ctx context.Context, localpart string) ([]authtypes.Device, error)
|
|
|
|
CreateDevice(ctx context.Context, localpart string, deviceID *string, accessToken string, displayName *string) (dev *authtypes.Device, returnErr error)
|
|
|
|
UpdateDevice(ctx context.Context, localpart, deviceID string, displayName *string) error
|
|
|
|
RemoveDevice(ctx context.Context, deviceID, localpart string) error
|
|
|
|
RemoveDevices(ctx context.Context, localpart string, devices []string) error
|
|
|
|
RemoveAllDevices(ctx context.Context, localpart string) error
|
2017-05-25 14:33:50 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 18:27:33 +01:00
|
|
|
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (Database, error) {
|
|
|
|
uri, err := url.Parse(dataSourceName)
|
2018-06-01 13:16:19 +02:00
|
|
|
if err != nil {
|
2020-02-13 18:27:33 +01:00
|
|
|
return postgres.NewDatabase(dataSourceName, serverName)
|
|
|
|
}
|
|
|
|
switch uri.Scheme {
|
|
|
|
case "postgres":
|
|
|
|
return postgres.NewDatabase(dataSourceName, serverName)
|
|
|
|
case "file":
|
|
|
|
return sqlite3.NewDatabase(dataSourceName, serverName)
|
|
|
|
default:
|
|
|
|
return postgres.NewDatabase(dataSourceName, serverName)
|
2018-06-01 13:16:19 +02:00
|
|
|
}
|
2017-10-15 12:29:47 +02:00
|
|
|
}
|