diff options
author | 2023-07-07 14:58:53 +0200 | |
---|---|---|
committer | 2023-07-07 14:58:53 +0200 | |
commit | ac564c18624aea229defc38bc1cc516d6c787520 (patch) | |
tree | 00fabbb17f9432bbb8fd9b2de4b185ede40f3f39 /internal/processing/fedi/common.go | |
parent | [docs] Rework backups a bit (#1942) (diff) | |
download | gotosocial-ac564c18624aea229defc38bc1cc516d6c787520.tar.xz |
[bugfix] Reorder web view logic, other small fixes (#1954)
Diffstat (limited to 'internal/processing/fedi/common.go')
-rw-r--r-- | internal/processing/fedi/common.go | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/internal/processing/fedi/common.go b/internal/processing/fedi/common.go index 68a84f303..1331a20e0 100644 --- a/internal/processing/fedi/common.go +++ b/internal/processing/fedi/common.go @@ -19,41 +19,61 @@ package fedi import ( "context" + "errors" "fmt" - "net/url" + "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtscontext" "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -func (p *Processor) authenticate(ctx context.Context, requestedUsername string) (requestedAccount, requestingAccount *gtsmodel.Account, errWithCode gtserror.WithCode) { +func (p *Processor) authenticate(ctx context.Context, requestedUsername string) ( + *gtsmodel.Account, // requestedAccount + *gtsmodel.Account, // requestingAccount + gtserror.WithCode, +) { + // Get LOCAL account with the requested username. requestedAccount, err := p.state.DB.GetAccountByUsernameDomain(ctx, requestedUsername, "") if err != nil { - errWithCode = gtserror.NewErrorNotFound(fmt.Errorf("database error getting account with username %s: %s", requestedUsername, err)) - return + if !errors.Is(err, db.ErrNoEntries) { + // Real db error. + err = gtserror.Newf("db error getting account %s: %w", requestedUsername, err) + return nil, nil, gtserror.NewErrorInternalError(err) + } + + // Account just not found in the db. + return nil, nil, gtserror.NewErrorNotFound(err) } - var requestingAccountURI *url.URL - requestingAccountURI, errWithCode = p.federator.AuthenticateFederatedRequest(ctx, requestedUsername) + // Ensure request signed, and use signature URI to + // get requesting account, dereferencing if necessary. + requestingAccountURI, errWithCode := p.federator.AuthenticateFederatedRequest(ctx, requestedUsername) if errWithCode != nil { - return + return nil, nil, errWithCode } - if requestingAccount, _, err = p.federator.GetAccountByURI(gtscontext.SetFastFail(ctx), requestedUsername, requestingAccountURI); err != nil { - errWithCode = gtserror.NewErrorUnauthorized(err) - return + requestingAccount, _, err := p.federator.GetAccountByURI( + gtscontext.SetFastFail(ctx), + requestedUsername, + requestingAccountURI, + ) + if err != nil { + err = gtserror.Newf("error getting account %s: %w", requestingAccountURI, err) + return nil, nil, gtserror.NewErrorUnauthorized(err) } + // Ensure no block exists between requester + requested. blocked, err := p.state.DB.IsEitherBlocked(ctx, requestedAccount.ID, requestingAccount.ID) if err != nil { - errWithCode = gtserror.NewErrorInternalError(err) - return + err = gtserror.Newf("db error getting checking block: %w", err) + return nil, nil, gtserror.NewErrorInternalError(err) } if blocked { - errWithCode = gtserror.NewErrorUnauthorized(fmt.Errorf("block exists between accounts %s and %s", requestedAccount.ID, requestingAccount.ID)) + err = fmt.Errorf("block exists between accounts %s and %s", requestedAccount.ID, requestingAccount.ID) + return nil, nil, gtserror.NewErrorUnauthorized(err) } - return + return requestedAccount, requestingAccount, nil } |