2023-03-12 18:00:57 +03:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-08-20 13:26:56 +03:00
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
package bundb
|
2021-08-20 13:26:56 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2023-07-23 13:33:17 +03:00
|
|
|
"errors"
|
2021-08-20 13:26:56 +03:00
|
|
|
"fmt"
|
|
|
|
"net/mail"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-08-31 16:59:12 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2021-08-20 13:26:56 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2023-07-23 13:33:17 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-08-20 13:26:56 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/id"
|
2022-07-19 11:47:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2022-12-08 20:35:14 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/state"
|
2021-12-20 17:19:53 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/uris"
|
2023-08-07 20:38:11 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2022-10-08 14:50:48 +03:00
|
|
|
"github.com/uptrace/bun"
|
2021-08-20 13:26:56 +03:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2022-10-08 14:50:48 +03:00
|
|
|
// generate RSA keys of this length
|
|
|
|
const rsaKeyBits = 2048
|
|
|
|
|
2021-08-20 13:26:56 +03:00
|
|
|
type adminDB struct {
|
2023-07-25 11:34:05 +03:00
|
|
|
db *WrappedDB
|
2022-12-08 20:35:14 +03:00
|
|
|
state *state.State
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-07-25 11:34:05 +03:00
|
|
|
func (a *adminDB) IsUsernameAvailable(ctx context.Context, username string) (bool, error) {
|
|
|
|
q := a.db.
|
2021-08-25 16:34:33 +03:00
|
|
|
NewSelect().
|
2022-10-08 14:50:48 +03:00
|
|
|
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
|
|
|
|
Column("account.id").
|
|
|
|
Where("? = ?", bun.Ident("account.username"), username).
|
|
|
|
Where("? IS NULL", bun.Ident("account.domain"))
|
2023-07-25 11:34:05 +03:00
|
|
|
return a.db.NotExists(ctx, q)
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-07-25 11:34:05 +03:00
|
|
|
func (a *adminDB) IsEmailAvailable(ctx context.Context, email string) (bool, error) {
|
2021-08-20 13:26:56 +03:00
|
|
|
// parse the domain from the email
|
|
|
|
m, err := mail.ParseAddress(email)
|
|
|
|
if err != nil {
|
2021-08-25 16:34:33 +03:00
|
|
|
return false, fmt.Errorf("error parsing email address %s: %s", email, err)
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
domain := strings.Split(m.Address, "@")[1] // domain will always be the second part after @
|
|
|
|
|
|
|
|
// check if the email domain is blocked
|
2023-07-25 11:34:05 +03:00
|
|
|
emailDomainBlockedQ := a.db.
|
2021-08-25 16:34:33 +03:00
|
|
|
NewSelect().
|
2022-10-08 14:50:48 +03:00
|
|
|
TableExpr("? AS ?", bun.Ident("email_domain_blocks"), bun.Ident("email_domain_block")).
|
|
|
|
Column("email_domain_block.id").
|
|
|
|
Where("? = ?", bun.Ident("email_domain_block.domain"), domain)
|
2023-07-25 11:34:05 +03:00
|
|
|
emailDomainBlocked, err := a.db.Exists(ctx, emailDomainBlockedQ)
|
2022-10-08 14:50:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if emailDomainBlocked {
|
2021-08-25 16:34:33 +03:00
|
|
|
return false, fmt.Errorf("email domain %s is blocked", domain)
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if this email is associated with a user already
|
2023-07-25 11:34:05 +03:00
|
|
|
q := a.db.
|
2021-08-25 16:34:33 +03:00
|
|
|
NewSelect().
|
2022-10-08 14:50:48 +03:00
|
|
|
TableExpr("? AS ?", bun.Ident("users"), bun.Ident("user")).
|
|
|
|
Column("user.id").
|
|
|
|
Where("? = ?", bun.Ident("user.email"), email).
|
|
|
|
WhereOr("? = ?", bun.Ident("user.unconfirmed_email"), email)
|
2023-07-25 11:34:05 +03:00
|
|
|
return a.db.NotExists(ctx, q)
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-07-25 11:34:05 +03:00
|
|
|
func (a *adminDB) NewSignup(ctx context.Context, newSignup gtsmodel.NewSignup) (*gtsmodel.User, error) {
|
2023-07-23 13:33:17 +03:00
|
|
|
// If something went wrong previously while doing a new
|
|
|
|
// sign up with this username, we might already have an
|
|
|
|
// account, so check first.
|
|
|
|
account, err := a.state.DB.GetAccountByUsernameDomain(ctx, newSignup.Username, "")
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
// Real error occurred.
|
|
|
|
err := gtserror.Newf("error checking for existing account: %w", err)
|
2021-08-20 13:26:56 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
// If we didn't yet have an account
|
|
|
|
// with this username, create one now.
|
|
|
|
if account == nil {
|
|
|
|
uris := uris.GenerateURIsForAccount(newSignup.Username)
|
|
|
|
|
|
|
|
accountID, err := id.NewRandomULID()
|
|
|
|
if err != nil {
|
|
|
|
err := gtserror.Newf("error creating new account id: %w", err)
|
2022-10-08 14:50:48 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-31 18:03:47 +03:00
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
privKey, err := rsa.GenerateKey(rand.Reader, rsaKeyBits)
|
2021-08-20 13:26:56 +03:00
|
|
|
if err != nil {
|
2023-07-23 13:33:17 +03:00
|
|
|
err := gtserror.Newf("error creating new rsa private key: %w", err)
|
2021-08-20 13:26:56 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
account = >smodel.Account{
|
2021-12-20 17:19:53 +03:00
|
|
|
ID: accountID,
|
2023-07-23 13:33:17 +03:00
|
|
|
Username: newSignup.Username,
|
|
|
|
DisplayName: newSignup.Username,
|
|
|
|
Reason: newSignup.Reason,
|
2021-09-13 18:35:33 +03:00
|
|
|
Privacy: gtsmodel.VisibilityDefault,
|
2023-07-23 13:33:17 +03:00
|
|
|
URI: uris.UserURI,
|
|
|
|
URL: uris.UserURL,
|
|
|
|
InboxURI: uris.InboxURI,
|
|
|
|
OutboxURI: uris.OutboxURI,
|
|
|
|
FollowingURI: uris.FollowingURI,
|
|
|
|
FollowersURI: uris.FollowersURI,
|
|
|
|
FeaturedCollectionURI: uris.FeaturedCollectionURI,
|
2021-08-31 16:59:12 +03:00
|
|
|
ActorType: ap.ActorPerson,
|
2023-07-23 13:33:17 +03:00
|
|
|
PrivateKey: privKey,
|
|
|
|
PublicKey: &privKey.PublicKey,
|
|
|
|
PublicKeyURI: uris.PublicKeyURI,
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
2022-01-31 18:03:47 +03:00
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
// Insert the new account!
|
|
|
|
if err := a.state.DB.PutAccount(ctx, account); err != nil {
|
2022-11-15 21:45:15 +03:00
|
|
|
return nil, err
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
// Created or already had an account.
|
|
|
|
// Ensure user not already created.
|
|
|
|
user, err := a.state.DB.GetUserByAccountID(ctx, account.ID)
|
|
|
|
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
// Real error occurred.
|
|
|
|
err := gtserror.Newf("error checking for existing user: %w", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// Pin account to (new)
|
|
|
|
// user before returning.
|
|
|
|
user.Account = account
|
|
|
|
}()
|
2022-10-08 14:50:48 +03:00
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
if user != nil {
|
|
|
|
// Already had a user for this
|
|
|
|
// account, just return that.
|
|
|
|
return user, nil
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
// Had no user for this account, time to create one!
|
2021-08-20 13:26:56 +03:00
|
|
|
newUserID, err := id.NewRandomULID()
|
|
|
|
if err != nil {
|
2023-07-23 13:33:17 +03:00
|
|
|
err := gtserror.Newf("error creating new user id: %w", err)
|
2021-08-20 13:26:56 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
encryptedPassword, err := bcrypt.GenerateFromPassword(
|
|
|
|
[]byte(newSignup.Password),
|
|
|
|
bcrypt.DefaultCost,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
err := gtserror.Newf("error hashing password: %w", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user = >smodel.User{
|
2021-08-20 13:26:56 +03:00
|
|
|
ID: newUserID,
|
2023-07-23 13:33:17 +03:00
|
|
|
AccountID: account.ID,
|
|
|
|
Account: account,
|
|
|
|
EncryptedPassword: string(encryptedPassword),
|
|
|
|
SignUpIP: newSignup.SignUpIP.To4(),
|
|
|
|
Locale: newSignup.Locale,
|
|
|
|
UnconfirmedEmail: newSignup.Email,
|
|
|
|
CreatedByApplicationID: newSignup.AppID,
|
|
|
|
ExternalID: newSignup.ExternalID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if newSignup.EmailVerified {
|
|
|
|
// Mark given email as confirmed.
|
|
|
|
user.ConfirmedAt = time.Now()
|
|
|
|
user.Email = newSignup.Email
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
if newSignup.Admin {
|
|
|
|
// Make new user mod + admin.
|
2023-08-07 20:38:11 +03:00
|
|
|
user.Moderator = util.Ptr(true)
|
|
|
|
user.Admin = util.Ptr(true)
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
if newSignup.PreApproved {
|
|
|
|
// Mark new user as approved.
|
2023-08-07 20:38:11 +03:00
|
|
|
user.Approved = util.Ptr(true)
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
// Insert the user!
|
|
|
|
if err := a.state.DB.PutUser(ctx, user); err != nil {
|
|
|
|
err := gtserror.Newf("db error inserting user: %w", err)
|
2022-11-15 21:45:15 +03:00
|
|
|
return nil, err
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-07-23 13:33:17 +03:00
|
|
|
return user, nil
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
|
|
|
|
2023-07-25 11:34:05 +03:00
|
|
|
func (a *adminDB) CreateInstanceAccount(ctx context.Context) error {
|
2022-05-30 15:41:24 +03:00
|
|
|
username := config.GetHost()
|
2021-08-25 16:34:33 +03:00
|
|
|
|
2023-07-25 11:34:05 +03:00
|
|
|
q := a.db.
|
2021-08-25 16:34:33 +03:00
|
|
|
NewSelect().
|
2022-10-08 14:50:48 +03:00
|
|
|
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
|
|
|
|
Column("account.id").
|
|
|
|
Where("? = ?", bun.Ident("account.username"), username).
|
2023-07-05 13:34:37 +03:00
|
|
|
Where("? IS NULL", bun.Ident("account.domain"))
|
2021-11-17 15:40:09 +03:00
|
|
|
|
2023-07-25 11:34:05 +03:00
|
|
|
exists, err := a.db.Exists(ctx, q)
|
2021-11-17 15:40:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Infof(ctx, "instance account %s already exists", username)
|
2021-08-25 16:34:33 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-08 14:50:48 +03:00
|
|
|
key, err := rsa.GenerateKey(rand.Reader, rsaKeyBits)
|
2021-08-20 13:26:56 +03:00
|
|
|
if err != nil {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Errorf(ctx, "error creating new rsa key: %s", err)
|
2021-08-20 13:26:56 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
aID, err := id.NewRandomULID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:19:53 +03:00
|
|
|
newAccountURIs := uris.GenerateURIsForAccount(username)
|
2021-08-20 13:26:56 +03:00
|
|
|
acct := >smodel.Account{
|
|
|
|
ID: aID,
|
2021-12-07 15:31:39 +03:00
|
|
|
Username: username,
|
2021-08-20 13:26:56 +03:00
|
|
|
DisplayName: username,
|
|
|
|
URL: newAccountURIs.UserURL,
|
|
|
|
PrivateKey: key,
|
|
|
|
PublicKey: &key.PublicKey,
|
|
|
|
PublicKeyURI: newAccountURIs.PublicKeyURI,
|
2021-08-31 16:59:12 +03:00
|
|
|
ActorType: ap.ActorPerson,
|
2021-08-20 13:26:56 +03:00
|
|
|
URI: newAccountURIs.UserURI,
|
|
|
|
InboxURI: newAccountURIs.InboxURI,
|
|
|
|
OutboxURI: newAccountURIs.OutboxURI,
|
|
|
|
FollowersURI: newAccountURIs.FollowersURI,
|
|
|
|
FollowingURI: newAccountURIs.FollowingURI,
|
2023-03-01 20:52:44 +03:00
|
|
|
FeaturedCollectionURI: newAccountURIs.FeaturedCollectionURI,
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
2021-08-25 16:34:33 +03:00
|
|
|
|
2022-11-15 21:45:15 +03:00
|
|
|
// insert the new account!
|
2022-12-08 20:35:14 +03:00
|
|
|
if err := a.state.DB.PutAccount(ctx, acct); err != nil {
|
2022-11-15 21:45:15 +03:00
|
|
|
return err
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
2021-08-25 16:34:33 +03:00
|
|
|
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Infof(ctx, "instance account %s CREATED with id %s", username, acct.ID)
|
2021-08-20 13:26:56 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:34:05 +03:00
|
|
|
func (a *adminDB) CreateInstanceInstance(ctx context.Context) error {
|
2022-05-30 15:41:24 +03:00
|
|
|
protocol := config.GetProtocol()
|
|
|
|
host := config.GetHost()
|
2021-08-25 16:34:33 +03:00
|
|
|
|
|
|
|
// check if instance entry already exists
|
2023-07-25 11:34:05 +03:00
|
|
|
q := a.db.
|
2021-08-25 16:34:33 +03:00
|
|
|
NewSelect().
|
2022-10-08 14:50:48 +03:00
|
|
|
Column("instance.id").
|
|
|
|
TableExpr("? AS ?", bun.Ident("instances"), bun.Ident("instance")).
|
|
|
|
Where("? = ?", bun.Ident("instance.domain"), host)
|
2021-08-25 16:34:33 +03:00
|
|
|
|
2023-07-25 11:34:05 +03:00
|
|
|
exists, err := a.db.Exists(ctx, q)
|
2021-08-26 18:22:41 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Infof(ctx, "instance entry already exists")
|
2021-08-26 18:46:24 +03:00
|
|
|
return nil
|
2021-08-25 16:34:33 +03:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:26:56 +03:00
|
|
|
iID, err := id.NewRandomULID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i := >smodel.Instance{
|
|
|
|
ID: iID,
|
2021-12-07 15:31:39 +03:00
|
|
|
Domain: host,
|
|
|
|
Title: host,
|
|
|
|
URI: fmt.Sprintf("%s://%s", protocol, host),
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
2021-08-25 16:34:33 +03:00
|
|
|
|
2023-07-25 11:34:05 +03:00
|
|
|
insertQ := a.db.
|
2021-08-25 16:34:33 +03:00
|
|
|
NewInsert().
|
|
|
|
Model(i)
|
|
|
|
|
2021-08-26 18:22:41 +03:00
|
|
|
_, err = insertQ.Exec(ctx)
|
2021-08-29 17:41:41 +03:00
|
|
|
if err != nil {
|
2023-07-25 11:34:05 +03:00
|
|
|
return a.db.ProcessError(err)
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|
2021-08-29 17:41:41 +03:00
|
|
|
|
2023-02-17 14:02:29 +03:00
|
|
|
log.Infof(ctx, "created instance instance %s with id %s", host, i.ID)
|
2021-08-29 17:41:41 +03:00
|
|
|
return nil
|
2021-08-20 13:26:56 +03:00
|
|
|
}
|