2017-05-22 16:49:32 +01:00
|
|
|
package devices
|
|
|
|
|
2017-05-23 17:43:05 +01:00
|
|
|
import (
|
2017-09-18 15:51:26 +01:00
|
|
|
"context"
|
2020-02-13 17:27:33 +00:00
|
|
|
"net/url"
|
2017-05-25 13:33:50 +01:00
|
|
|
|
2017-05-23 17:43:05 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2020-02-13 17:27:33 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices/postgres"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices/sqlite3"
|
2017-05-25 13:33:50 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-05-23 17:43:05 +01:00
|
|
|
)
|
|
|
|
|
2020-02-13 17:27:33 +00: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 13:33:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-13 17:27:33 +00:00
|
|
|
func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (Database, error) {
|
|
|
|
uri, err := url.Parse(dataSourceName)
|
2018-06-01 12:16:19 +01:00
|
|
|
if err != nil {
|
2020-02-13 17:27:33 +00: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 12:16:19 +01:00
|
|
|
}
|
2017-10-15 11:29:47 +01:00
|
|
|
}
|