gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

federator.go (4016B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package federation
     19 
     20 import (
     21 	"context"
     22 	"net/url"
     23 
     24 	"github.com/superseriousbusiness/activity/pub"
     25 	"github.com/superseriousbusiness/gotosocial/internal/db"
     26 	"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
     27 	"github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb"
     28 	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
     29 	"github.com/superseriousbusiness/gotosocial/internal/media"
     30 	"github.com/superseriousbusiness/gotosocial/internal/state"
     31 	"github.com/superseriousbusiness/gotosocial/internal/transport"
     32 	"github.com/superseriousbusiness/gotosocial/internal/typeutils"
     33 )
     34 
     35 // Federator wraps various interfaces and functions to manage activitypub federation from gotosocial
     36 type Federator interface {
     37 	// FederatingActor returns the underlying pub.FederatingActor, which can be used to send activities, and serve actors at inboxes/outboxes.
     38 	FederatingActor() pub.FederatingActor
     39 	// FederatingDB returns the underlying FederatingDB interface.
     40 	FederatingDB() federatingdb.DB
     41 	// TransportController returns the underlying transport controller.
     42 	TransportController() transport.Controller
     43 
     44 	// AuthenticateFederatedRequest can be used to check the authenticity of incoming http-signed requests for federating resources.
     45 	// The given username will be used to create a transport for making outgoing requests. See the implementation for more detailed comments.
     46 	//
     47 	// If the request is valid and passes authentication, the URL of the key owner ID will be returned, as well as true, and nil.
     48 	//
     49 	// If the request does not pass authentication, or there's a domain block, nil, false, nil will be returned.
     50 	//
     51 	// If something goes wrong during authentication, nil, false, and an error will be returned.
     52 	AuthenticateFederatedRequest(ctx context.Context, username string) (*url.URL, gtserror.WithCode)
     53 
     54 	pub.CommonBehavior
     55 	pub.FederatingProtocol
     56 	dereferencing.Dereferencer
     57 }
     58 
     59 type federator struct {
     60 	db                  db.DB
     61 	federatingDB        federatingdb.DB
     62 	clock               pub.Clock
     63 	typeConverter       typeutils.TypeConverter
     64 	transportController transport.Controller
     65 	mediaManager        *media.Manager
     66 	actor               pub.FederatingActor
     67 	dereferencing.Dereferencer
     68 }
     69 
     70 // NewFederator returns a new federator
     71 func NewFederator(state *state.State, federatingDB federatingdb.DB, transportController transport.Controller, typeConverter typeutils.TypeConverter, mediaManager *media.Manager) Federator {
     72 	dereferencer := dereferencing.NewDereferencer(state, typeConverter, transportController, mediaManager)
     73 
     74 	clock := &Clock{}
     75 	f := &federator{
     76 		db:                  state.DB,
     77 		federatingDB:        federatingDB,
     78 		clock:               &Clock{},
     79 		typeConverter:       typeConverter,
     80 		transportController: transportController,
     81 		mediaManager:        mediaManager,
     82 		Dereferencer:        dereferencer,
     83 	}
     84 	actor := newFederatingActor(f, f, federatingDB, clock)
     85 	f.actor = actor
     86 	return f
     87 }
     88 
     89 func (f *federator) FederatingActor() pub.FederatingActor {
     90 	return f.actor
     91 }
     92 
     93 func (f *federator) FederatingDB() federatingdb.DB {
     94 	return f.federatingDB
     95 }
     96 
     97 func (f *federator) TransportController() transport.Controller {
     98 	return f.transportController
     99 }