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/>.
|
2023-03-01 20:52:44 +03:00
|
|
|
|
|
|
|
package fedi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-07 15:58:53 +03:00
|
|
|
"errors"
|
2024-06-11 12:54:59 +03:00
|
|
|
"net/url"
|
2023-03-01 20:52:44 +03:00
|
|
|
|
2023-07-07 15:58:53 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2023-03-01 20:52:44 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
)
|
|
|
|
|
2024-06-11 12:54:59 +03:00
|
|
|
type commonAuth struct {
|
|
|
|
handshakingURI *url.URL // Set to requestingAcct's URI if we're currently handshaking them.
|
|
|
|
requestingAcct *gtsmodel.Account // Remote account making request to this instance.
|
|
|
|
receivingAcct *gtsmodel.Account // Local account receiving the request.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Processor) authenticate(ctx context.Context, requestedUser string) (*commonAuth, gtserror.WithCode) {
|
2023-11-20 15:22:28 +03:00
|
|
|
// First get the requested (receiving) LOCAL account with username from database.
|
|
|
|
receiver, err := p.state.DB.GetAccountByUsernameDomain(ctx, requestedUser, "")
|
2023-03-01 20:52:44 +03:00
|
|
|
if err != nil {
|
2023-07-07 15:58:53 +03:00
|
|
|
if !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
// Real db error.
|
2023-11-20 15:22:28 +03:00
|
|
|
err = gtserror.Newf("db error getting account %s: %w", requestedUser, err)
|
2024-06-11 12:54:59 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
2023-07-07 15:58:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Account just not found in the db.
|
2024-06-11 12:54:59 +03:00
|
|
|
return nil, gtserror.NewErrorNotFound(err)
|
2023-03-01 20:52:44 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 15:58:53 +03:00
|
|
|
// Ensure request signed, and use signature URI to
|
|
|
|
// get requesting account, dereferencing if necessary.
|
2023-11-20 15:22:28 +03:00
|
|
|
pubKeyAuth, errWithCode := p.federator.AuthenticateFederatedRequest(ctx, requestedUser)
|
2023-03-01 20:52:44 +03:00
|
|
|
if errWithCode != nil {
|
2024-06-11 12:54:59 +03:00
|
|
|
return nil, errWithCode
|
2023-03-01 20:52:44 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 13:35:30 +03:00
|
|
|
if pubKeyAuth.Handshaking {
|
2024-06-11 12:54:59 +03:00
|
|
|
// We're still handshaking so we
|
|
|
|
// don't know the requester yet.
|
|
|
|
return &commonAuth{
|
|
|
|
handshakingURI: pubKeyAuth.OwnerURI,
|
|
|
|
receivingAcct: receiver,
|
|
|
|
}, nil
|
2023-03-01 20:52:44 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 13:35:30 +03:00
|
|
|
// Get requester from auth.
|
|
|
|
requester := pubKeyAuth.Owner
|
2023-11-10 19:16:58 +03:00
|
|
|
|
2024-06-11 12:54:59 +03:00
|
|
|
// Ensure block does not exist between receiver and requester.
|
2023-11-20 15:22:28 +03:00
|
|
|
blocked, err := p.state.DB.IsEitherBlocked(ctx, receiver.ID, requester.ID)
|
2023-03-01 20:52:44 +03:00
|
|
|
if err != nil {
|
2023-11-21 13:35:30 +03:00
|
|
|
err := gtserror.Newf("error checking block: %w", err)
|
2024-06-11 12:54:59 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
2023-11-20 15:22:28 +03:00
|
|
|
} else if blocked {
|
2023-11-21 13:35:30 +03:00
|
|
|
const text = "block exists between accounts"
|
2024-06-11 12:54:59 +03:00
|
|
|
return nil, gtserror.NewErrorForbidden(errors.New(text))
|
2023-03-01 20:52:44 +03:00
|
|
|
}
|
|
|
|
|
2024-06-11 12:54:59 +03:00
|
|
|
return &commonAuth{
|
|
|
|
requestingAcct: requester,
|
|
|
|
receivingAcct: receiver,
|
|
|
|
}, nil
|
2023-03-01 20:52:44 +03:00
|
|
|
}
|