shared

package
v0.13.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Err3PIDInUse = errors.New("this third-party identifier is already in use")

Err3PIDInUse is the error returned when trying to save an association involving a third-party identifier which is already associated to a local user.

Functions

This section is empty.

Types

type Database

type Database struct {
	DB                    *sql.DB
	Writer                sqlutil.Writer
	RegistrationTokens    tables.RegistrationTokensTable
	Accounts              tables.AccountsTable
	Profiles              tables.ProfileTable
	AccountDatas          tables.AccountDataTable
	ThreePIDs             tables.ThreePIDTable
	OpenIDTokens          tables.OpenIDTable
	KeyBackups            tables.KeyBackupTable
	KeyBackupVersions     tables.KeyBackupVersionTable
	Devices               tables.DevicesTable
	LoginTokens           tables.LoginTokenTable
	Notifications         tables.NotificationTable
	Pushers               tables.PusherTable
	Stats                 tables.StatsTable
	LoginTokenLifetime    time.Duration
	ServerName            spec.ServerName
	BcryptCost            int
	OpenIDTokenLifetimeMS int64
}

Database represents an account database

func (*Database) CheckAccountAvailability

func (d *Database) CheckAccountAvailability(ctx context.Context, localpart string, serverName spec.ServerName) (bool, error)

CheckAccountAvailability checks if the username/localpart is already present in the database. If the DB returns sql.ErrNoRows the Localpart isn't taken.

func (*Database) CountBackupKeys

func (d *Database) CountBackupKeys(
	ctx context.Context, version, userID string,
) (count int64, err error)

func (*Database) CreateAccount

func (d *Database) CreateAccount(
	ctx context.Context, localpart string, serverName spec.ServerName,
	plaintextPassword, appserviceID string, accountType api.AccountType,
) (acc *api.Account, err error)

CreateAccount makes a new account with the given login name and password, and creates an empty profile for this account. If no password is supplied, the account will be a passwordless account. If the account already exists, it will return nil, ErrUserExists.

func (*Database) CreateDevice

func (d *Database) CreateDevice(
	ctx context.Context, localpart string, serverName spec.ServerName,
	deviceID *string, accessToken string, displayName *string, ipAddr, userAgent string,
) (dev *api.Device, returnErr error)

CreateDevice makes a new device associated with the given user ID localpart. If there is already a device with the same device ID for this user, that access token will be revoked and replaced with the given accessToken. If the given accessToken is already in use for another device, an error will be returned. If no device ID is given one is generated. Returns the device on success.

func (*Database) CreateKeyBackup

func (d *Database) CreateKeyBackup(
	ctx context.Context, userID, algorithm string, authData json.RawMessage,
) (version string, err error)

func (*Database) CreateLoginToken

func (d *Database) CreateLoginToken(ctx context.Context, data *api.LoginTokenData) (*api.LoginTokenMetadata, error)

CreateLoginToken generates a token, stores and returns it. The lifetime is determined by the loginTokenLifetime given to the Database constructor.

func (*Database) CreateOpenIDToken

func (d *Database) CreateOpenIDToken(
	ctx context.Context,
	token, userID string,
) (int64, error)

CreateOpenIDToken persists a new token that was issued for OpenID Connect

func (*Database) DailyRoomsMessages added in v0.10.7

func (d *Database) DailyRoomsMessages(
	ctx context.Context, serverName spec.ServerName,
) (stats types.MessageStats, activeRooms, activeE2EERooms int64, err error)

func (*Database) DeactivateAccount

func (d *Database) DeactivateAccount(ctx context.Context, localpart string, serverName spec.ServerName) (err error)

DeactivateAccount deactivates the user's account, removing all ability for the user to login again.

func (*Database) DeleteKeyBackup

func (d *Database) DeleteKeyBackup(
	ctx context.Context, userID, version string,
) (exists bool, err error)

func (*Database) DeleteNotificationsUpTo added in v0.6.5

func (d *Database) DeleteNotificationsUpTo(ctx context.Context, localpart string, serverName spec.ServerName, roomID string, pos uint64) (affected bool, err error)

func (*Database) DeleteOldNotifications added in v0.6.5

func (d *Database) DeleteOldNotifications(ctx context.Context) error

func (*Database) DeleteRegistrationToken added in v0.13.0

func (d *Database) DeleteRegistrationToken(ctx context.Context, tokenString string) (err error)

func (*Database) GetAccountByLocalpart

func (d *Database) GetAccountByLocalpart(ctx context.Context, localpart string, serverName spec.ServerName,
) (*api.Account, error)

GetAccountByLocalpart returns the account associated with the given localpart. This function assumes the request is authenticated or the account data is used only internally. Returns sql.ErrNoRows if no account exists which matches the given localpart.

func (*Database) GetAccountByPassword

func (d *Database) GetAccountByPassword(
	ctx context.Context, localpart string, serverName spec.ServerName,
	plaintextPassword string,
) (*api.Account, error)

GetAccountByPassword returns the account associated with the given localpart and password. Returns sql.ErrNoRows if no account exists which matches the given localpart.

func (*Database) GetAccountData

func (d *Database) GetAccountData(ctx context.Context, localpart string, serverName spec.ServerName) (
	global map[string]json.RawMessage,
	rooms map[string]map[string]json.RawMessage,
	err error,
)

GetAccountData returns account data related to a given localpart If no account data could be found, returns an empty arrays Returns an error if there was an issue with the retrieval

func (*Database) GetAccountDataByType

func (d *Database) GetAccountDataByType(
	ctx context.Context, localpart string, serverName spec.ServerName,
	roomID, dataType string,
) (data json.RawMessage, err error)

GetAccountDataByType returns account data matching a given localpart, room ID and type. If no account data could be found, returns nil Returns an error if there was an issue with the retrieval

func (*Database) GetBackupKeys

func (d *Database) GetBackupKeys(
	ctx context.Context, version, userID, filterRoomID, filterSessionID string,
) (result map[string]map[string]api.KeyBackupSession, err error)

func (*Database) GetDeviceByAccessToken

func (d *Database) GetDeviceByAccessToken(
	ctx context.Context, token string,
) (*api.Device, error)

GetDeviceByAccessToken returns the device matching the given access token. Returns sql.ErrNoRows if no matching device was found.

func (*Database) GetDeviceByID

func (d *Database) GetDeviceByID(
	ctx context.Context,
	localpart string, serverName spec.ServerName,
	deviceID string,
) (*api.Device, error)

GetDeviceByID returns the device matching the given ID. Returns sql.ErrNoRows if no matching device was found.

func (*Database) GetDevicesByID

func (d *Database) GetDevicesByID(ctx context.Context, deviceIDs []string) ([]api.Device, error)

func (*Database) GetDevicesByLocalpart

func (d *Database) GetDevicesByLocalpart(
	ctx context.Context,
	localpart string, serverName spec.ServerName,
) ([]api.Device, error)

GetDevicesByLocalpart returns the devices matching the given localpart.

func (*Database) GetKeyBackup

func (d *Database) GetKeyBackup(
	ctx context.Context, userID, version string,
) (versionResult, algorithm string, authData json.RawMessage, etag string, deleted bool, err error)

func (*Database) GetLocalpartForThreePID

func (d *Database) GetLocalpartForThreePID(
	ctx context.Context, threepid string, medium string,
) (localpart string, serverName spec.ServerName, err error)

GetLocalpartForThreePID looks up the localpart associated with a given third-party identifier. If no association involves the given third-party idenfitier, returns an empty string. Returns an error if there was a problem talking to the database.

func (*Database) GetLoginTokenDataByToken

func (d *Database) GetLoginTokenDataByToken(ctx context.Context, token string) (*api.LoginTokenData, error)

GetLoginTokenDataByToken returns the data associated with the given token. May return sql.ErrNoRows.

func (*Database) GetNewNumericLocalpart

func (d *Database) GetNewNumericLocalpart(
	ctx context.Context, serverName spec.ServerName,
) (int64, error)

GetNewNumericLocalpart generates and returns a new unused numeric localpart

func (*Database) GetNotificationCount added in v0.6.5

func (d *Database) GetNotificationCount(ctx context.Context, localpart string, serverName spec.ServerName, filter tables.NotificationFilter) (int64, error)

func (*Database) GetNotifications added in v0.6.5

func (d *Database) GetNotifications(ctx context.Context, localpart string, serverName spec.ServerName, fromID int64, limit int, filter tables.NotificationFilter) ([]*api.Notification, int64, error)

func (*Database) GetOpenIDTokenAttributes

func (d *Database) GetOpenIDTokenAttributes(
	ctx context.Context,
	token string,
) (*api.OpenIDTokenAttributes, error)

GetOpenIDTokenAttributes gets the attributes of issued an OIDC auth token

func (*Database) GetProfileByLocalpart

func (d *Database) GetProfileByLocalpart(
	ctx context.Context,
	localpart string, serverName spec.ServerName,
) (*authtypes.Profile, error)

GetProfileByLocalpart returns the profile associated with the given localpart. Returns sql.ErrNoRows if no profile exists which matches the given localpart.

func (*Database) GetPushers added in v0.6.5

func (d *Database) GetPushers(
	ctx context.Context, localpart string, serverName spec.ServerName,
) ([]api.Pusher, error)

GetPushers returns the pushers matching the given localpart.

func (*Database) GetRegistrationToken added in v0.13.0

func (d *Database) GetRegistrationToken(ctx context.Context, tokenString string) (*clientapi.RegistrationToken, error)

func (*Database) GetRoomNotificationCounts added in v0.6.5

func (d *Database) GetRoomNotificationCounts(ctx context.Context, localpart string, serverName spec.ServerName, roomID string) (total int64, highlight int64, _ error)

func (*Database) GetThreePIDsForLocalpart

func (d *Database) GetThreePIDsForLocalpart(
	ctx context.Context,
	localpart string, serverName spec.ServerName,
) (threepids []authtypes.ThreePID, err error)

GetThreePIDsForLocalpart looks up the third-party identifiers associated with a given local user. If no association is known for this user, returns an empty slice. Returns an error if there was an issue talking to the database.

func (*Database) InsertNotification added in v0.6.5

func (d *Database) InsertNotification(ctx context.Context, localpart string, serverName spec.ServerName, eventID string, pos uint64, tweaks map[string]interface{}, n *api.Notification) error

func (*Database) InsertRegistrationToken added in v0.13.0

func (d *Database) InsertRegistrationToken(ctx context.Context, registrationToken *clientapi.RegistrationToken) (created bool, err error)

func (*Database) ListRegistrationTokens added in v0.13.0

func (d *Database) ListRegistrationTokens(ctx context.Context, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error)

func (*Database) QueryPushRules added in v0.9.7

func (d *Database) QueryPushRules(
	ctx context.Context,
	localpart string, serverName spec.ServerName,
) (*pushrules.AccountRuleSets, error)

func (*Database) RegistrationTokenExists added in v0.13.0

func (d *Database) RegistrationTokenExists(ctx context.Context, token string) (bool, error)

func (*Database) RemoveAllDevices

func (d *Database) RemoveAllDevices(
	ctx context.Context,
	localpart string, serverName spec.ServerName,
	exceptDeviceID string,
) (devices []api.Device, err error)

RemoveAllDevices revokes devices by deleting the entry in the database matching the given user ID localpart. If something went wrong during the deletion, it will return the SQL error.

func (*Database) RemoveDevices

func (d *Database) RemoveDevices(
	ctx context.Context,
	localpart string, serverName spec.ServerName,
	devices []string,
) error

RemoveDevices revokes one or more devices by deleting the entry in the database matching with the given device IDs and user ID localpart. If the devices don't exist, it will not return an error If something went wrong during the deletion, it will return the SQL error.

func (*Database) RemoveLoginToken

func (d *Database) RemoveLoginToken(ctx context.Context, token string) error

RemoveLoginToken removes the named token (and may clean up other expired tokens).

func (*Database) RemovePusher added in v0.6.5

func (d *Database) RemovePusher(
	ctx context.Context, appid, pushkey, localpart string, serverName spec.ServerName,
) error

RemovePusher deletes one pusher Invoked when `append` is true and `kind` is null in https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-pushers-set

func (*Database) RemovePushers added in v0.6.5

func (d *Database) RemovePushers(
	ctx context.Context, appid, pushkey string,
) error

RemovePushers deletes all pushers that match given App Id and Push Key pair. Invoked when `append` parameter is false in https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-pushers-set

func (*Database) RemoveThreePIDAssociation

func (d *Database) RemoveThreePIDAssociation(
	ctx context.Context, threepid string, medium string,
) (err error)

RemoveThreePIDAssociation removes the association involving a given third-party identifier. If no association exists involving this third-party identifier, returns nothing. If there was a problem talking to the database, returns an error.

func (*Database) SaveAccountData

func (d *Database) SaveAccountData(
	ctx context.Context, localpart string, serverName spec.ServerName,
	roomID, dataType string, content json.RawMessage,
) error

SaveAccountData saves new account data for a given user and a given room. If the account data is not specific to a room, the room ID should be an empty string If an account data already exists for a given set (user, room, data type), it will update the corresponding row with the new content Returns a SQL error if there was an issue with the insertion/update

func (*Database) SaveThreePIDAssociation

func (d *Database) SaveThreePIDAssociation(
	ctx context.Context, threepid string,
	localpart string, serverName spec.ServerName,
	medium string,
) (err error)

SaveThreePIDAssociation saves the association between a third party identifier and a local Matrix user (identified by the user's ID's local part). If the third-party identifier is already part of an association, returns Err3PIDInUse. Returns an error if there was a problem talking to the database.

func (*Database) SearchProfiles

func (d *Database) SearchProfiles(ctx context.Context, searchString string, limit int,
) ([]authtypes.Profile, error)

SearchProfiles returns all profiles where the provided localpart or display name match any part of the profiles in the database.

func (*Database) SetAvatarURL

func (d *Database) SetAvatarURL(
	ctx context.Context,
	localpart string, serverName spec.ServerName,
	avatarURL string,
) (profile *authtypes.Profile, changed bool, err error)

SetAvatarURL updates the avatar URL of the profile associated with the given localpart. Returns an error if something went wrong with the SQL query

func (*Database) SetDisplayName

func (d *Database) SetDisplayName(
	ctx context.Context,
	localpart string, serverName spec.ServerName,
	displayName string,
) (profile *authtypes.Profile, changed bool, err error)

SetDisplayName updates the display name of the profile associated with the given localpart. Returns an error if something went wrong with the SQL query

func (*Database) SetNotificationsRead added in v0.6.5

func (d *Database) SetNotificationsRead(ctx context.Context, localpart string, serverName spec.ServerName, roomID string, pos uint64, b bool) (affected bool, err error)

func (*Database) SetPassword

func (d *Database) SetPassword(
	ctx context.Context, localpart string, serverName spec.ServerName,
	plaintextPassword string,
) error

SetPassword sets the account password to the given hash.

func (*Database) UpdateDevice

func (d *Database) UpdateDevice(
	ctx context.Context,
	localpart string, serverName spec.ServerName,
	deviceID string, displayName *string,
) error

UpdateDevice updates the given device with the display name. Returns SQL error if there are problems and nil on success.

func (*Database) UpdateDeviceLastSeen

func (d *Database) UpdateDeviceLastSeen(ctx context.Context, localpart string, serverName spec.ServerName, deviceID, ipAddr, userAgent string) error

UpdateDeviceLastSeen updates a last seen timestamp and the ip address.

func (*Database) UpdateKeyBackupAuthData

func (d *Database) UpdateKeyBackupAuthData(
	ctx context.Context, userID, version string, authData json.RawMessage,
) (err error)

func (*Database) UpdateRegistrationToken added in v0.13.0

func (d *Database) UpdateRegistrationToken(ctx context.Context, tokenString string, newAttributes map[string]interface{}) (updatedToken *clientapi.RegistrationToken, err error)

func (*Database) UpsertBackupKeys

func (d *Database) UpsertBackupKeys(
	ctx context.Context, version, userID string, uploads []api.InternalKeyBackupSession,
) (count int64, etag string, err error)

nolint:nakedret

func (*Database) UpsertDailyRoomsMessages added in v0.10.7

func (d *Database) UpsertDailyRoomsMessages(ctx context.Context, serverName spec.ServerName, stats types.MessageStats, activeRooms, activeE2EERooms int64) error

func (*Database) UpsertPusher added in v0.6.5

func (d *Database) UpsertPusher(
	ctx context.Context, p api.Pusher,
	localpart string, serverName spec.ServerName,
) error

func (*Database) UserStatistics added in v0.8.3

func (d *Database) UserStatistics(ctx context.Context) (*types.UserStatistics, *types.DatabaseEngine, error)

UserStatistics populates types.UserStatistics, used in reports.

type KeyDatabase added in v0.12.0

type KeyDatabase struct {
	OneTimeKeysTable      tables.OneTimeKeys
	DeviceKeysTable       tables.DeviceKeys
	KeyChangesTable       tables.KeyChanges
	StaleDeviceListsTable tables.StaleDeviceLists
	CrossSigningKeysTable tables.CrossSigningKeys
	CrossSigningSigsTable tables.CrossSigningSigs
	DB                    *sql.DB
	Writer                sqlutil.Writer
}

func (*KeyDatabase) ClaimKeys added in v0.12.0

func (d *KeyDatabase) ClaimKeys(ctx context.Context, userToDeviceToAlgorithm map[string]map[string]string) ([]api.OneTimeKeys, error)

func (*KeyDatabase) CrossSigningKeysDataForUser added in v0.12.0

func (d *KeyDatabase) CrossSigningKeysDataForUser(ctx context.Context, userID string) (types.CrossSigningKeyMap, error)

CrossSigningKeysForUser returns the latest known cross-signing keys for a user, if any.

func (*KeyDatabase) CrossSigningKeysForUser added in v0.12.0

func (d *KeyDatabase) CrossSigningKeysForUser(ctx context.Context, userID string) (map[fclient.CrossSigningKeyPurpose]fclient.CrossSigningKey, error)

CrossSigningKeysForUser returns the latest known cross-signing keys for a user, if any.

func (*KeyDatabase) CrossSigningSigsForTarget added in v0.12.0

func (d *KeyDatabase) CrossSigningSigsForTarget(ctx context.Context, originUserID, targetUserID string, targetKeyID gomatrixserverlib.KeyID) (types.CrossSigningSigMap, error)

CrossSigningSigsForTarget returns the signatures for a given user's key ID, if any.

func (*KeyDatabase) DeleteDeviceKeys added in v0.12.0

func (d *KeyDatabase) DeleteDeviceKeys(ctx context.Context, userID string, deviceIDs []gomatrixserverlib.KeyID) error

DeleteDeviceKeys removes the device keys for a given user/device, and any accompanying cross-signing signatures relating to that device.

func (*KeyDatabase) DeleteStaleDeviceLists added in v0.12.0

func (d *KeyDatabase) DeleteStaleDeviceLists(
	ctx context.Context,
	userIDs []string,
) error

DeleteStaleDeviceLists deletes stale device list entries for users we don't share a room with anymore.

func (*KeyDatabase) DeviceKeysForUser added in v0.12.0

func (d *KeyDatabase) DeviceKeysForUser(ctx context.Context, userID string, deviceIDs []string, includeEmpty bool) ([]api.DeviceMessage, error)

func (*KeyDatabase) DeviceKeysJSON added in v0.12.0

func (d *KeyDatabase) DeviceKeysJSON(ctx context.Context, keys []api.DeviceMessage) error

func (*KeyDatabase) ExistingOneTimeKeys added in v0.12.0

func (d *KeyDatabase) ExistingOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error)

func (*KeyDatabase) KeyChanges added in v0.12.0

func (d *KeyDatabase) KeyChanges(ctx context.Context, fromOffset, toOffset int64) (userIDs []string, latestOffset int64, err error)

func (*KeyDatabase) MarkDeviceListStale added in v0.12.0

func (d *KeyDatabase) MarkDeviceListStale(ctx context.Context, userID string, isStale bool) error

MarkDeviceListStale sets the stale bit for this user to isStale.

func (*KeyDatabase) OneTimeKeysCount added in v0.12.0

func (d *KeyDatabase) OneTimeKeysCount(ctx context.Context, userID, deviceID string) (*api.OneTimeKeysCount, error)

func (*KeyDatabase) PrevIDsExists added in v0.12.0

func (d *KeyDatabase) PrevIDsExists(ctx context.Context, userID string, prevIDs []int64) (bool, error)

func (*KeyDatabase) StaleDeviceLists added in v0.12.0

func (d *KeyDatabase) StaleDeviceLists(ctx context.Context, domains []spec.ServerName) ([]string, error)

StaleDeviceLists returns a list of user IDs ending with the domains provided who have stale device lists. If no domains are given, all user IDs with stale device lists are returned.

func (*KeyDatabase) StoreCrossSigningKeysForUser added in v0.12.0

func (d *KeyDatabase) StoreCrossSigningKeysForUser(ctx context.Context, userID string, keyMap types.CrossSigningKeyMap) error

StoreCrossSigningKeysForUser stores the latest known cross-signing keys for a user.

func (*KeyDatabase) StoreCrossSigningSigsForTarget added in v0.12.0

func (d *KeyDatabase) StoreCrossSigningSigsForTarget(
	ctx context.Context,
	originUserID string, originKeyID gomatrixserverlib.KeyID,
	targetUserID string, targetKeyID gomatrixserverlib.KeyID,
	signature spec.Base64Bytes,
) error

StoreCrossSigningSigsForTarget stores a signature for a target user ID and key/dvice.

func (*KeyDatabase) StoreKeyChange added in v0.12.0

func (d *KeyDatabase) StoreKeyChange(ctx context.Context, userID string) (id int64, err error)

func (*KeyDatabase) StoreLocalDeviceKeys added in v0.12.0

func (d *KeyDatabase) StoreLocalDeviceKeys(ctx context.Context, keys []api.DeviceMessage) error

func (*KeyDatabase) StoreOneTimeKeys added in v0.12.0

func (d *KeyDatabase) StoreOneTimeKeys(ctx context.Context, keys api.OneTimeKeys) (counts *api.OneTimeKeysCount, err error)

func (*KeyDatabase) StoreRemoteDeviceKeys added in v0.12.0

func (d *KeyDatabase) StoreRemoteDeviceKeys(ctx context.Context, keys []api.DeviceMessage, clearUserIDs []string) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL