2023-03-12 16:00:57 +01: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/>.
|
2023-03-01 18:52:44 +01:00
|
|
|
|
|
|
|
package fedi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-07 14:58:53 +02:00
|
|
|
"errors"
|
2023-03-01 18:52:44 +01:00
|
|
|
|
2023-07-07 14:58:53 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2023-04-28 17:45:21 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
2023-03-01 18:52:44 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
)
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
func (p *Processor) authenticate(ctx context.Context, requestedUser string) (
|
|
|
|
*gtsmodel.Account, // requester: i.e. user making the request
|
|
|
|
*gtsmodel.Account, // receiver: i.e. the receiving inbox user
|
2023-07-07 14:58:53 +02:00
|
|
|
gtserror.WithCode,
|
|
|
|
) {
|
2023-11-20 13:22:28 +01:00
|
|
|
// First get the requested (receiving) LOCAL account with username from database.
|
|
|
|
receiver, err := p.state.DB.GetAccountByUsernameDomain(ctx, requestedUser, "")
|
2023-03-01 18:52:44 +01:00
|
|
|
if err != nil {
|
2023-07-07 14:58:53 +02:00
|
|
|
if !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
// Real db error.
|
2023-11-20 13:22:28 +01:00
|
|
|
err = gtserror.Newf("db error getting account %s: %w", requestedUser, err)
|
2023-07-07 14:58:53 +02:00
|
|
|
return nil, nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Account just not found in the db.
|
|
|
|
return nil, nil, gtserror.NewErrorNotFound(err)
|
2023-03-01 18:52:44 +01:00
|
|
|
}
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
var requester *gtsmodel.Account
|
|
|
|
|
2023-07-07 14:58:53 +02:00
|
|
|
// Ensure request signed, and use signature URI to
|
|
|
|
// get requesting account, dereferencing if necessary.
|
2023-11-20 13:22:28 +01:00
|
|
|
pubKeyAuth, errWithCode := p.federator.AuthenticateFederatedRequest(ctx, requestedUser)
|
2023-03-01 18:52:44 +01:00
|
|
|
if errWithCode != nil {
|
2023-07-07 14:58:53 +02:00
|
|
|
return nil, nil, errWithCode
|
2023-03-01 18:52:44 +01:00
|
|
|
}
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
if requester = pubKeyAuth.Owner; requester == nil {
|
|
|
|
requester, _, err = p.federator.GetAccountByURI(
|
|
|
|
gtscontext.SetFastFail(ctx),
|
|
|
|
requestedUser,
|
|
|
|
pubKeyAuth.OwnerURI,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
err = gtserror.Newf("error getting account %s: %w", pubKeyAuth.OwnerURI, err)
|
|
|
|
return nil, nil, gtserror.NewErrorUnauthorized(err)
|
|
|
|
}
|
2023-03-01 18:52:44 +01:00
|
|
|
}
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
if !requester.SuspendedAt.IsZero() {
|
2023-11-10 17:16:58 +01:00
|
|
|
// Account was marked as suspended by a
|
|
|
|
// local admin action. Stop request early.
|
2023-11-20 13:22:28 +01:00
|
|
|
const text = "requesting account is suspended"
|
|
|
|
return nil, nil, gtserror.NewErrorForbidden(errors.New(text))
|
2023-11-10 17:16:58 +01:00
|
|
|
}
|
|
|
|
|
2023-07-07 14:58:53 +02:00
|
|
|
// Ensure no block exists between requester + requested.
|
2023-11-20 13:22:28 +01:00
|
|
|
blocked, err := p.state.DB.IsEitherBlocked(ctx, receiver.ID, requester.ID)
|
2023-03-01 18:52:44 +01:00
|
|
|
if err != nil {
|
2023-07-07 14:58:53 +02:00
|
|
|
err = gtserror.Newf("db error getting checking block: %w", err)
|
|
|
|
return nil, nil, gtserror.NewErrorInternalError(err)
|
2023-11-20 13:22:28 +01:00
|
|
|
} else if blocked {
|
|
|
|
err = gtserror.Newf("block exists between accounts %s and %s", requester.ID, receiver.ID)
|
2023-11-10 17:16:58 +01:00
|
|
|
return nil, nil, gtserror.NewErrorForbidden(err)
|
2023-03-01 18:52:44 +01:00
|
|
|
}
|
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
return requester, receiver, nil
|
2023-03-01 18:52:44 +01:00
|
|
|
}
|