gtsocial-umbx

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

transport.go (2780B)


      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 	"fmt"
     23 	"net/url"
     24 
     25 	"github.com/superseriousbusiness/activity/pub"
     26 	"github.com/superseriousbusiness/gotosocial/internal/uris"
     27 )
     28 
     29 // NewTransport returns a new Transport on behalf of a specific actor.
     30 //
     31 // The actorBoxIRI will be either the inbox or outbox of an actor who is
     32 // attempting to do the dereferencing or delivery. Any authentication
     33 // scheme applied on the request must be based on this actor. The
     34 // request must contain some sort of credential of the user, such as a
     35 // HTTP Signature.
     36 //
     37 // The gofedAgent passed in should be used by the Transport
     38 // implementation in the User-Agent, as well as the application-specific
     39 // user agent string. The gofedAgent will indicate this library's use as
     40 // well as the library's version number.
     41 //
     42 // Any server-wide rate-limiting that needs to occur should happen in a
     43 // Transport implementation. This factory function allows this to be
     44 // created, so peer servers are not DOS'd.
     45 //
     46 // Any retry logic should also be handled by the Transport
     47 // implementation.
     48 //
     49 // Note that the library will not maintain a long-lived pointer to the
     50 // returned Transport so that any private credentials are able to be
     51 // garbage collected.
     52 func (f *federator) NewTransport(ctx context.Context, actorBoxIRI *url.URL, gofedAgent string) (pub.Transport, error) {
     53 	var username string
     54 	var err error
     55 
     56 	switch {
     57 	case uris.IsInboxPath(actorBoxIRI):
     58 		username, err = uris.ParseInboxPath(actorBoxIRI)
     59 		if err != nil {
     60 			return nil, fmt.Errorf("couldn't parse path %s as an inbox: %s", actorBoxIRI.String(), err)
     61 		}
     62 	case uris.IsOutboxPath(actorBoxIRI):
     63 		username, err = uris.ParseOutboxPath(actorBoxIRI)
     64 		if err != nil {
     65 			return nil, fmt.Errorf("couldn't parse path %s as an outbox: %s", actorBoxIRI.String(), err)
     66 		}
     67 	default:
     68 		return nil, fmt.Errorf("id %s was neither an inbox path nor an outbox path", actorBoxIRI.String())
     69 	}
     70 
     71 	return f.transportController.NewTransportForUsername(ctx, username)
     72 }