| 
									
										
										
										
											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-03-01 18:52:44 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 12:22:28 +00: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 12:22:28 +00: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 12:22:28 +00: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-07-07 14:58:53 +02:00
										 |  |  | 	// Ensure request signed, and use signature URI to | 
					
						
							|  |  |  | 	// get requesting account, dereferencing if necessary. | 
					
						
							| 
									
										
										
										
											2023-11-20 12:22:28 +00: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-21 10:35:30 +00:00
										 |  |  | 	if pubKeyAuth.Handshaking { | 
					
						
							|  |  |  | 		// This should happen very rarely, we are in the middle of handshaking. | 
					
						
							|  |  |  | 		err := gtserror.Newf("network race handshaking %s", pubKeyAuth.OwnerURI) | 
					
						
							|  |  |  | 		return nil, nil, gtserror.NewErrorInternalError(err) | 
					
						
							| 
									
										
										
										
											2023-03-01 18:52:44 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							| 
									
										
										
										
											2023-11-20 12:22:28 +00:00
										 |  |  | 	blocked, err := p.state.DB.IsEitherBlocked(ctx, receiver.ID, requester.ID) | 
					
						
							| 
									
										
										
										
											2023-03-01 18:52:44 +01: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, nil, gtserror.NewErrorInternalError(err) | 
					
						
							| 
									
										
										
										
											2023-11-20 12:22:28 +00:00
										 |  |  | 	} else if blocked { | 
					
						
							| 
									
										
										
										
											2023-11-21 10:35:30 +00:00
										 |  |  | 		const text = "block exists between accounts" | 
					
						
							|  |  |  | 		return nil, nil, gtserror.NewErrorForbidden(errors.New(text)) | 
					
						
							| 
									
										
										
										
											2023-03-01 18:52:44 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 12:22:28 +00:00
										 |  |  | 	return requester, receiver, nil | 
					
						
							| 
									
										
										
										
											2023-03-01 18:52:44 +01:00
										 |  |  | } |