gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

create.go (4017B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package account
     19 
     20 import (
     21 	"context"
     22 	"fmt"
     23 
     24 	"github.com/superseriousbusiness/gotosocial/internal/ap"
     25 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     26 	"github.com/superseriousbusiness/gotosocial/internal/config"
     27 	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
     28 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     29 	"github.com/superseriousbusiness/gotosocial/internal/log"
     30 	"github.com/superseriousbusiness/gotosocial/internal/messages"
     31 	"github.com/superseriousbusiness/gotosocial/internal/text"
     32 	"github.com/superseriousbusiness/oauth2/v4"
     33 )
     34 
     35 // Create processes the given form for creating a new account, returning an oauth token for that account if successful.
     36 func (p *Processor) Create(ctx context.Context, applicationToken oauth2.TokenInfo, application *gtsmodel.Application, form *apimodel.AccountCreateRequest) (*apimodel.Token, gtserror.WithCode) {
     37 	emailAvailable, err := p.state.DB.IsEmailAvailable(ctx, form.Email)
     38 	if err != nil {
     39 		return nil, gtserror.NewErrorBadRequest(err)
     40 	}
     41 	if !emailAvailable {
     42 		return nil, gtserror.NewErrorConflict(fmt.Errorf("email address %s is not available", form.Email))
     43 	}
     44 
     45 	usernameAvailable, err := p.state.DB.IsUsernameAvailable(ctx, form.Username)
     46 	if err != nil {
     47 		return nil, gtserror.NewErrorBadRequest(err)
     48 	}
     49 	if !usernameAvailable {
     50 		return nil, gtserror.NewErrorConflict(fmt.Errorf("username %s in use", form.Username))
     51 	}
     52 
     53 	reasonRequired := config.GetAccountsReasonRequired()
     54 	approvalRequired := config.GetAccountsApprovalRequired()
     55 
     56 	// don't store a reason if we don't require one
     57 	reason := form.Reason
     58 	if !reasonRequired {
     59 		reason = ""
     60 	}
     61 
     62 	log.Trace(ctx, "creating new username and account")
     63 	user, err := p.state.DB.NewSignup(ctx, form.Username, text.SanitizePlaintext(reason), approvalRequired, form.Email, form.Password, form.IP, form.Locale, application.ID, false, "", false)
     64 	if err != nil {
     65 		return nil, gtserror.NewErrorInternalError(fmt.Errorf("error creating new signup in the database: %s", err))
     66 	}
     67 
     68 	log.Tracef(ctx, "generating a token for user %s with account %s and application %s", user.ID, user.AccountID, application.ID)
     69 	accessToken, err := p.oauthServer.GenerateUserAccessToken(ctx, applicationToken, application.ClientSecret, user.ID)
     70 	if err != nil {
     71 		return nil, gtserror.NewErrorInternalError(fmt.Errorf("error creating new access token for user %s: %s", user.ID, err))
     72 	}
     73 
     74 	if user.Account == nil {
     75 		a, err := p.state.DB.GetAccountByID(ctx, user.AccountID)
     76 		if err != nil {
     77 			return nil, gtserror.NewErrorInternalError(fmt.Errorf("error getting new account from the database: %s", err))
     78 		}
     79 		user.Account = a
     80 	}
     81 
     82 	// there are side effects for creating a new account (sending confirmation emails etc)
     83 	// so pass a message to the processor so that it can do it asynchronously
     84 	p.state.Workers.EnqueueClientAPI(ctx, messages.FromClientAPI{
     85 		APObjectType:   ap.ObjectProfile,
     86 		APActivityType: ap.ActivityCreate,
     87 		GTSModel:       user.Account,
     88 		OriginAccount:  user.Account,
     89 	})
     90 
     91 	return &apimodel.Token{
     92 		AccessToken: accessToken.GetAccess(),
     93 		TokenType:   "Bearer",
     94 		Scope:       accessToken.GetScope(),
     95 		CreatedAt:   accessToken.GetAccessCreateAt().Unix(),
     96 	}, nil
     97 }