| 
									
										
										
										
											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/>. | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | package fedi | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-13 17:29:43 +01:00
										 |  |  | 	"github.com/superseriousbusiness/activity/streams/vocab" | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/ap" | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2021-12-20 15:19:53 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/uris" | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 16:05:26 +01:00
										 |  |  | func (p *Processor) UserGet(ctx context.Context, requestedUsername string, requestURL *url.URL) (interface{}, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	// (Try to) get the requested local account from the db. | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 	receiver, err := p.state.DB.GetAccountByUsernameDomain(ctx, requestedUsername, "") | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 11:57:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 20:03:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 14:33:01 +02:00
										 |  |  | 	if uris.IsPublicKeyPath(requestURL) { | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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 | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 		minimalPerson, err := p.converter.AccountToASMinimal(ctx, receiver) | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 			err := gtserror.Newf("error converting to minimal account: %w", err) | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 			return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 		// Return early with bare minimum data. | 
					
						
							|  |  |  | 		return data(minimalPerson) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02: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. | 
					
						
							| 
									
										
										
										
											2023-09-12 11:43:12 +02:00
										 |  |  | 	pubKeyAuth, errWithCode := p.federator.AuthenticateFederatedRequest(ctx, requestedUsername) | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	if errWithCode != nil { | 
					
						
							|  |  |  | 		return nil, errWithCode // likely 401 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Auth passed, generate the proper AP representation. | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 	person, err := p.converter.AccountToAS(ctx, receiver) | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 		err := gtserror.Newf("error converting account: %w", err) | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 	if pubKeyAuth.Handshaking { | 
					
						
							|  |  |  | 		// 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. | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 		return data(person) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 	// Get requester from auth. | 
					
						
							|  |  |  | 	requester := pubKeyAuth.Owner | 
					
						
							| 
									
										
										
										
											2023-11-10 17:16:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 	// Check that block does not exist between receiver and requester. | 
					
						
							|  |  |  | 	blocked, err := p.state.DB.IsBlocked(ctx, receiver.ID, requester.ID) | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 		err := gtserror.Newf("error checking block: %w", err) | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 	} else if blocked { | 
					
						
							|  |  |  | 		const text = "block exists between accounts" | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorForbidden(errors.New(text)) | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return data(person) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func data(requestedPerson vocab.ActivityStreamsPerson) (interface{}, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2023-05-09 12:16:10 +02:00
										 |  |  | 	data, err := ap.Serialize(requestedPerson) | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-07-07 14:58:53 +02:00
										 |  |  | 		err := gtserror.Newf("error serializing person: %w", err) | 
					
						
							| 
									
										
										
										
											2021-10-24 11:57:39 +02:00
										 |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return data, nil | 
					
						
							|  |  |  | } |