2017-05-22 15:49:32 +00:00
|
|
|
package devices
|
|
|
|
|
2017-05-23 16:43:05 +00:00
|
|
|
import (
|
2017-09-18 14:51:26 +00:00
|
|
|
"context"
|
2020-02-13 17:27:33 +00:00
|
|
|
"net/url"
|
2017-05-25 12:33:50 +00:00
|
|
|
|
2017-05-23 16:43:05 +00: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 12:33:50 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-05-23 16:43:05 +00: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 12:33:50 +00: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 11:16:19 +00: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 11:16:19 +00:00
|
|
|
}
|
2017-10-15 10:29:47 +00:00
|
|
|
}
|