2021-02-09 11:53:33 +00:00
// SPDX-License-Identifier: MIT
2021-03-10 15:44:46 +00:00
// Package roomdb implements all the persisted database needs of the room server.
2021-03-02 16:14:02 +00:00
// This includes authentication, allow/deny list managment, invite and alias creation and also the notice content for the CMS.
//
// The interfaces defined here are implemented twice. Once in SQLite for production and once as mocks for testing, generated by counterfeiter (https://github.com/maxbrunsfeld/counterfeiter).
//
2021-03-10 15:44:46 +00:00
// See the package documentation of roomdb/sqlite for how to update it.
2021-03-02 16:14:02 +00:00
// It's important not to use the types generated by sqlboiler (sqlite/models) in the argument and return values of the interfaces here.
2021-03-10 15:44:46 +00:00
// This would leak details of the internal implementation of the roomdb/sqlite package and we want to have full control over how these interfaces can be used.
package roomdb
2021-02-08 11:57:14 +00:00
import (
2021-02-09 11:39:57 +00:00
"context"
2021-02-08 11:57:14 +00:00
"go.mindeco.de/http/auth"
2021-02-11 15:43:19 +00:00
refs "go.mindeco.de/ssb-refs"
2021-02-08 11:57:14 +00:00
)
2021-03-24 09:58:32 +00:00
type RoomConfig interface {
GetPrivacyMode ( context . Context ) ( PrivacyMode , error )
2021-03-31 13:58:42 +00:00
SetPrivacyMode ( context . Context , PrivacyMode ) error
2021-04-16 12:33:31 +00:00
GetDefaultLanguage ( context . Context ) ( string , error )
SetDefaultLanguage ( context . Context , string ) error
2021-03-24 09:58:32 +00:00
}
2021-03-18 16:49:52 +00:00
// AuthFallbackService allows password authentication which might be helpful for scenarios
// where one lost access to his ssb device or key.
2021-02-08 16:47:42 +00:00
type AuthFallbackService interface {
2021-03-09 12:39:43 +00:00
// Check receives the username and password (in clear) and checks them accordingly.
2021-05-11 09:05:17 +00:00
// Login might be a registered alias or a ssb id who belongs to a member.
2021-03-09 12:39:43 +00:00
// If it's a valid combination it returns the user ID, or an error if they are not.
2021-02-08 11:57:14 +00:00
auth . Auther
2021-02-08 16:47:42 +00:00
2021-05-11 09:05:17 +00:00
// SetPassword creates or updates a fallback login password for this user.
2021-05-12 12:41:47 +00:00
SetPassword ( _ context . Context , memberID int64 , password string ) error
2021-05-11 09:05:17 +00:00
// CreateResetToken returns a token which can be used via SetPasswordWithToken() to reset the password of a member.
CreateResetToken ( _ context . Context , createdByMember , forMember int64 ) ( string , error )
// SetPasswordWithToken consumes a token created with CreateResetToken() and updates the password for that member accordingly.
2021-05-12 12:41:47 +00:00
SetPasswordWithToken ( _ context . Context , resetToken string , password string ) error
2021-02-08 11:57:14 +00:00
}
2021-03-17 09:46:05 +00:00
// AuthWithSSBService defines utility functions for the challenge/response system of sign-in with ssb
// They are particualarly of service to check valid sessions (after the client provided a solution for a challenge)
// And to log out valid sessions from the clients device.
type AuthWithSSBService interface {
// CreateToken is used to generate a token that is stored inside a cookie.
// It is used after a valid solution for a challenge was provided.
CreateToken ( ctx context . Context , memberID int64 ) ( string , error )
// CheckToken checks if the passed token is still valid and returns the member id if so
CheckToken ( ctx context . Context , token string ) ( int64 , error )
2021-03-24 17:31:37 +00:00
// RemoveToken removes a single token from the database
RemoveToken ( ctx context . Context , token string ) error
2021-03-17 09:46:05 +00:00
// WipeTokensForMember deletes all tokens currently held for that member
WipeTokensForMember ( ctx context . Context , memberID int64 ) error
}
2021-02-08 11:57:14 +00:00
2021-03-18 16:49:52 +00:00
// MembersService stores and retreives the list of internal users (members, mods and admins).
type MembersService interface {
// Add adds a new member
2021-03-29 08:23:03 +00:00
Add ( _ context . Context , pubKey refs . FeedRef , r Role ) ( int64 , error )
2021-03-18 16:49:52 +00:00
2021-03-19 09:32:04 +00:00
// GetByID returns the member if it exists
2021-03-18 16:49:52 +00:00
GetByID ( context . Context , int64 ) ( Member , error )
2021-03-19 09:32:04 +00:00
// GetByFeed returns the member if it exists
GetByFeed ( context . Context , refs . FeedRef ) ( Member , error )
2021-03-18 16:49:52 +00:00
// List returns a list of all the members.
List ( context . Context ) ( [ ] Member , error )
2021-03-29 14:33:15 +00:00
// Count returns the total number of members.
2021-03-29 15:21:26 +00:00
Count ( context . Context ) ( uint , error )
2021-03-29 14:33:15 +00:00
2021-03-18 16:49:52 +00:00
// RemoveFeed removes the feed from the list.
RemoveFeed ( context . Context , refs . FeedRef ) error
// RemoveID removes the feed for the ID from the list.
RemoveID ( context . Context , int64 ) error
2021-03-22 09:32:49 +00:00
// SetRole changes the role of the passed member id.
// It will return an error if the member doesn't exist.
// It should also return an error if call would remove the last admin,
// since only admins can change roles doing so would leave the room in a crippled state.
2021-03-18 16:49:52 +00:00
SetRole ( context . Context , int64 , Role ) error
}
2021-03-19 11:28:14 +00:00
// DeniedKeysService changes the lists of public keys that are not allowed to get into the room
type DeniedKeysService interface {
// Add adds the feed to the list, together with a comment for other members
Add ( ctx context . Context , ref refs . FeedRef , comment string ) error
2021-02-11 15:43:19 +00:00
2021-02-15 09:40:43 +00:00
// HasFeed returns true if a feed is on the list.
HasFeed ( context . Context , refs . FeedRef ) bool
2021-04-07 08:53:57 +00:00
// HasID returns true if a member id is on the list.
2021-02-15 09:40:43 +00:00
HasID ( context . Context , int64 ) bool
2021-02-11 15:43:19 +00:00
2021-02-15 10:02:45 +00:00
// GetByID returns the list entry for that ID or an error
GetByID ( context . Context , int64 ) ( ListEntry , error )
2021-02-11 15:43:19 +00:00
// List returns a list of all the feeds.
2021-03-18 16:49:52 +00:00
List ( context . Context ) ( [ ] ListEntry , error )
2021-02-15 09:40:43 +00:00
2021-03-29 14:33:15 +00:00
// Count returns the total number of denied keys.
2021-03-29 15:21:26 +00:00
Count ( context . Context ) ( uint , error )
2021-03-29 14:33:15 +00:00
2021-02-15 09:40:43 +00:00
// RemoveFeed removes the feed from the list.
RemoveFeed ( context . Context , refs . FeedRef ) error
2021-02-11 15:43:19 +00:00
2021-02-15 09:40:43 +00:00
// RemoveID removes the feed for the ID from the list.
RemoveID ( context . Context , int64 ) error
2021-02-11 15:43:19 +00:00
}
2021-02-08 11:57:14 +00:00
2021-03-18 16:49:52 +00:00
// AliasesService manages alias handle registration and lookup
type AliasesService interface {
2021-03-11 16:57:55 +00:00
// Resolve returns all the relevant information for that alias or an error if it doesnt exist
Resolve ( context . Context , string ) ( Alias , error )
// GetByID returns the alias for that ID or an error
GetByID ( context . Context , int64 ) ( Alias , error )
// List returns a list of all registerd aliases
List ( ctx context . Context ) ( [ ] Alias , error )
// Register receives an alias and signature for it. Validation needs to happen before this.
Register ( ctx context . Context , alias string , userFeed refs . FeedRef , signature [ ] byte ) error
// Revoke removes an alias from the system
Revoke ( ctx context . Context , alias string ) error
}
2021-02-08 11:57:14 +00:00
2021-03-18 16:49:52 +00:00
// InvitesService manages creation and consumption of invite tokens for joining the room.
type InvitesService interface {
2021-03-02 16:14:02 +00:00
// Create creates a new invite for a new member. It returns the token or an error.
// createdBy is user ID of the admin or moderator who created it.
// aliasSuggestion is optional (empty string is fine) but can be used to disambiguate open invites. (See https://github.com/ssb-ngi-pointer/rooms2/issues/21)
2021-03-29 08:23:03 +00:00
Create ( ctx context . Context , createdBy int64 ) ( string , error )
2021-03-02 16:14:02 +00:00
2021-03-05 09:46:59 +00:00
// Consume checks if the passed token is still valid.
// If it is it adds newMember to the members of the room and invalidates the token.
2021-03-02 16:14:02 +00:00
// If the token isn't valid, it returns an error.
Consume ( ctx context . Context , token string , newMember refs . FeedRef ) ( Invite , error )
2021-03-05 09:46:59 +00:00
// GetByToken returns the Invite if one for that token exists, or an error
GetByToken ( ctx context . Context , token string ) ( Invite , error )
// GetByToken returns the Invite if one for that ID exists, or an error
GetByID ( ctx context . Context , id int64 ) ( Invite , error )
2021-03-02 16:14:02 +00:00
// List returns a list of all the valid invites
List ( ctx context . Context ) ( [ ] Invite , error )
2021-03-29 14:33:15 +00:00
// Count returns the total number of invites.
2021-03-29 15:21:26 +00:00
Count ( context . Context ) ( uint , error )
2021-03-29 14:33:15 +00:00
2021-03-02 16:14:02 +00:00
// Revoke removes a active invite and invalidates it for future use.
Revoke ( ctx context . Context , id int64 ) error
}
2021-02-22 16:55:12 +00:00
// PinnedNoticesService allows an admin to assign Notices to specific placeholder pages.
// like updates, privacy policy, code of conduct
type PinnedNoticesService interface {
2021-03-05 10:27:32 +00:00
// List returns a list of all the pinned notices with their corresponding notices and languages
2021-02-23 19:23:50 +00:00
List ( context . Context ) ( PinnedNotices , error )
2021-02-22 16:55:12 +00:00
// Set assigns a fixed page name to an page ID and a language to allow for multiple translated versions of the same page.
2021-02-23 19:23:50 +00:00
Set ( ctx context . Context , name PinnedNoticeName , id int64 ) error
// Get returns a single notice for a name and a language
Get ( ctx context . Context , name PinnedNoticeName , language string ) ( * Notice , error )
2021-02-22 16:55:12 +00:00
}
2021-02-23 19:23:50 +00:00
// NoticesService is the low level store to manage single notices
2021-02-22 16:55:12 +00:00
type NoticesService interface {
// GetByID returns the page for that ID or an error
GetByID ( context . Context , int64 ) ( Notice , error )
// Save updates the passed page or creates it if it's ID is zero
Save ( context . Context , * Notice ) error
// RemoveID removes the page for that ID.
RemoveID ( context . Context , int64 ) error
}
2021-02-08 11:57:14 +00:00
// for tests we use generated mocks from these interfaces created with https://github.com/maxbrunsfeld/counterfeiter
2021-03-29 13:33:58 +00:00
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mockdb/aliases.go . AliasesService
2021-03-18 16:49:52 +00:00
2021-03-29 13:33:58 +00:00
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mockdb/auth.go . AuthWithSSBService
2021-02-08 11:57:14 +00:00
2021-03-29 13:33:58 +00:00
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mockdb/auth_fallback.go . AuthFallbackService
2021-02-08 11:57:14 +00:00
2021-03-29 13:33:58 +00:00
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mockdb/denied.go . DeniedKeysService
2021-02-08 11:57:14 +00:00
2021-03-29 13:33:58 +00:00
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mockdb/fixed_pages.go . PinnedNoticesService
2021-02-22 16:55:12 +00:00
2021-03-29 13:33:58 +00:00
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mockdb/invites.go . InvitesService
2021-03-02 16:14:02 +00:00
2021-03-29 13:33:58 +00:00
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mockdb/members.go . MembersService
2021-02-22 16:55:12 +00:00
2021-03-29 13:33:58 +00:00
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mockdb/pages.go . NoticesService
2021-04-06 15:21:41 +00:00
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mockdb/roomconfig.go . RoomConfig