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-10-24 12:57:39 +03:00
|
|
|
|
2023-02-22 18:05:26 +03:00
|
|
|
package fedi
|
2021-10-24 12:57:39 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-07 15:58:53 +03:00
|
|
|
"errors"
|
2021-10-24 12:57:39 +03:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
2021-11-13 19:29:43 +03:00
|
|
|
"github.com/superseriousbusiness/activity/streams/vocab"
|
2023-05-09 13:16:10 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2023-07-07 15:58:53 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2023-04-28 18:45:21 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
2021-10-24 12:57:39 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-12-20 17:19:53 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/uris"
|
2021-10-24 12:57:39 +03:00
|
|
|
)
|
|
|
|
|
2023-07-07 15:58:53 +03:00
|
|
|
// UserGet handles the getting of a fedi/activitypub representation of a user/account,
|
|
|
|
// performing authentication before returning a JSON serializable interface to the caller.
|
2023-02-22 18:05:26 +03:00
|
|
|
func (p *Processor) UserGet(ctx context.Context, requestedUsername string, requestURL *url.URL) (interface{}, gtserror.WithCode) {
|
2023-07-07 15:58:53 +03:00
|
|
|
// (Try to) get the requested local account from the db.
|
2023-03-01 21:26:53 +03:00
|
|
|
requestedAccount, err := p.state.DB.GetAccountByUsernameDomain(ctx, requestedUsername, "")
|
2021-10-24 12:57:39 +03:00
|
|
|
if err != nil {
|
2023-07-07 15:58:53 +03:00
|
|
|
if errors.Is(err, db.ErrNoEntries) {
|
|
|
|
// Account just not found w/ this username.
|
|
|
|
err := fmt.Errorf("account with username %s not found in the db", requestedUsername)
|
|
|
|
return nil, gtserror.NewErrorNotFound(err)
|
|
|
|
}
|
2021-10-24 12:57:39 +03:00
|
|
|
|
2023-07-07 15:58:53 +03:00
|
|
|
// Real db error.
|
|
|
|
err := fmt.Errorf("db error getting account with username %s: %w", requestedUsername, err)
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
2023-02-03 23:03:05 +03:00
|
|
|
|
2022-04-15 15:33:01 +03:00
|
|
|
if uris.IsPublicKeyPath(requestURL) {
|
2023-07-07 15:58:53 +03:00
|
|
|
// If request is on a public key path, we don't need to
|
|
|
|
// authenticate this request. However, we'll only serve
|
|
|
|
// the bare minimum user profile needed for the pubkey.
|
|
|
|
//
|
|
|
|
// TODO: https://github.com/superseriousbusiness/gotosocial/issues/1186
|
|
|
|
minimalPerson, err := p.tc.AccountToASMinimal(ctx, requestedAccount)
|
2021-10-24 12:57:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
2023-07-07 15:58:53 +03:00
|
|
|
// Return early with bare minimum data.
|
|
|
|
return data(minimalPerson)
|
|
|
|
}
|
2021-10-24 12:57:39 +03:00
|
|
|
|
2023-07-07 15:58:53 +03:00
|
|
|
// If the request is not on a public key path, we want to
|
|
|
|
// try to authenticate it before we serve any data, so that
|
|
|
|
// we can serve a more complete profile.
|
|
|
|
requestingAccountURI, errWithCode := p.federator.AuthenticateFederatedRequest(ctx, requestedUsername)
|
|
|
|
if errWithCode != nil {
|
|
|
|
return nil, errWithCode // likely 401
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth passed, generate the proper AP representation.
|
|
|
|
person, err := p.tc.AccountToAS(ctx, requestedAccount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
2021-10-24 12:57:39 +03:00
|
|
|
}
|
|
|
|
|
2023-07-07 15:58:53 +03:00
|
|
|
// If we are currently handshaking with the remote account
|
|
|
|
// making the request, then don't be coy: just serve the AP
|
|
|
|
// representation of the target account.
|
|
|
|
//
|
|
|
|
// This handshake check ensures that we don't get stuck in
|
|
|
|
// a loop with another GtS instance, where each instance is
|
|
|
|
// trying repeatedly to dereference the other account that's
|
|
|
|
// making the request before it will reveal its own account.
|
|
|
|
//
|
|
|
|
// Instead, we end up in an 'I'll show you mine if you show me
|
|
|
|
// yours' situation, where we sort of agree to reveal each
|
|
|
|
// other's profiles at the same time.
|
|
|
|
if p.federator.Handshaking(requestedUsername, requestingAccountURI) {
|
|
|
|
return data(person)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're not currently handshaking with the requestingAccountURI,
|
|
|
|
// so fetch its details and ensure it's up to date + not blocked.
|
|
|
|
requestingAccount, _, err := p.federator.GetAccountByURI(
|
|
|
|
// On a hot path so fail quickly.
|
|
|
|
gtscontext.SetFastFail(ctx),
|
|
|
|
requestedUsername, requestingAccountURI,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
err := gtserror.Newf("error getting account %s: %w", requestingAccountURI, err)
|
|
|
|
return nil, gtserror.NewErrorUnauthorized(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
blocked, err := p.state.DB.IsBlocked(ctx, requestedAccount.ID, requestingAccount.ID)
|
|
|
|
if err != nil {
|
|
|
|
err := gtserror.Newf("error checking block from account %s to account %s: %w", requestedAccount.ID, requestingAccount.ID, err)
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if blocked {
|
|
|
|
err := fmt.Errorf("account %s blocks account %s", requestedAccount.ID, requestingAccount.ID)
|
|
|
|
return nil, gtserror.NewErrorUnauthorized(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return data(person)
|
|
|
|
}
|
|
|
|
|
|
|
|
func data(requestedPerson vocab.ActivityStreamsPerson) (interface{}, gtserror.WithCode) {
|
2023-05-09 13:16:10 +03:00
|
|
|
data, err := ap.Serialize(requestedPerson)
|
2021-10-24 12:57:39 +03:00
|
|
|
if err != nil {
|
2023-07-07 15:58:53 +03:00
|
|
|
err := gtserror.Newf("error serializing person: %w", err)
|
2021-10-24 12:57:39 +03:00
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|