diff options
Diffstat (limited to 'internal/federation')
-rw-r--r-- | internal/federation/commonbehavior.go | 7 | ||||
-rw-r--r-- | internal/federation/federatingdb/followers.go | 14 | ||||
-rw-r--r-- | internal/federation/federatingdb/following.go | 13 | ||||
-rw-r--r-- | internal/federation/federatingdb/lock.go | 7 | ||||
-rw-r--r-- | internal/federation/federatingprotocol.go | 5 | ||||
-rw-r--r-- | internal/federation/federator.go | 3 | ||||
-rw-r--r-- | internal/federation/finger.go | 69 |
7 files changed, 109 insertions, 9 deletions
diff --git a/internal/federation/commonbehavior.go b/internal/federation/commonbehavior.go index 8ed6fd2cb..fab9ce112 100644 --- a/internal/federation/commonbehavior.go +++ b/internal/federation/commonbehavior.go @@ -25,6 +25,7 @@ import ( "net/url" "github.com/go-fed/activity/pub" + "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/util" @@ -59,7 +60,7 @@ import ( func (f *federator) AuthenticateGetInbox(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, bool, error) { // IMPLEMENTATION NOTE: For GoToSocial, we serve GETS to outboxes and inboxes through // the CLIENT API, not through the federation API, so we just do nothing here. - return nil, false, nil + return ctx, false, nil } // AuthenticateGetOutbox delegates the authentication of a GET to an @@ -84,7 +85,7 @@ func (f *federator) AuthenticateGetInbox(ctx context.Context, w http.ResponseWri func (f *federator) AuthenticateGetOutbox(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, bool, error) { // IMPLEMENTATION NOTE: For GoToSocial, we serve GETS to outboxes and inboxes through // the CLIENT API, not through the federation API, so we just do nothing here. - return nil, false, nil + return ctx, false, nil } // GetOutbox returns the OrderedCollection inbox of the actor for this @@ -98,7 +99,7 @@ func (f *federator) AuthenticateGetOutbox(ctx context.Context, w http.ResponseWr func (f *federator) GetOutbox(ctx context.Context, r *http.Request) (vocab.ActivityStreamsOrderedCollectionPage, error) { // IMPLEMENTATION NOTE: For GoToSocial, we serve GETS to outboxes and inboxes through // the CLIENT API, not through the federation API, so we just do nothing here. - return nil, nil + return streams.NewActivityStreamsOrderedCollectionPage(), nil } // NewTransport returns a new Transport on behalf of a specific actor. diff --git a/internal/federation/federatingdb/followers.go b/internal/federation/federatingdb/followers.go index 28f3bb6d1..7cba101dd 100644 --- a/internal/federation/federatingdb/followers.go +++ b/internal/federation/federatingdb/followers.go @@ -10,6 +10,7 @@ import ( "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/util" ) // Followers obtains the Followers Collection for an actor with the @@ -28,8 +29,17 @@ func (f *federatingDB) Followers(c context.Context, actorIRI *url.URL) (follower l.Debugf("entering FOLLOWERS function with actorIRI %s", actorIRI.String()) acct := >smodel.Account{} - if err := f.db.GetWhere([]db.Where{{Key: "uri", Value: actorIRI.String()}}, acct); err != nil { - return nil, fmt.Errorf("db error getting account with uri %s: %s", actorIRI.String(), err) + + if util.IsUserPath(actorIRI) { + if err := f.db.GetWhere([]db.Where{{Key: "uri", Value: actorIRI.String()}}, acct); err != nil { + return nil, fmt.Errorf("db error getting account with uri %s: %s", actorIRI.String(), err) + } + } else if util.IsFollowersPath(actorIRI) { + if err := f.db.GetWhere([]db.Where{{Key: "followers_uri", Value: actorIRI.String()}}, acct); err != nil { + return nil, fmt.Errorf("db error getting account with followers uri %s: %s", actorIRI.String(), err) + } + } else { + return nil, fmt.Errorf("could not parse actor IRI %s as users or followers path", actorIRI.String()) } acctFollowers := []gtsmodel.Follow{} diff --git a/internal/federation/federatingdb/following.go b/internal/federation/federatingdb/following.go index 342250880..f34f252a5 100644 --- a/internal/federation/federatingdb/following.go +++ b/internal/federation/federatingdb/following.go @@ -10,6 +10,7 @@ import ( "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/util" ) // Following obtains the Following Collection for an actor with the @@ -28,8 +29,16 @@ func (f *federatingDB) Following(c context.Context, actorIRI *url.URL) (followin l.Debugf("entering FOLLOWING function with actorIRI %s", actorIRI.String()) acct := >smodel.Account{} - if err := f.db.GetWhere([]db.Where{{Key: "uri", Value: actorIRI.String()}}, acct); err != nil { - return nil, fmt.Errorf("db error getting account with uri %s: %s", actorIRI.String(), err) + if util.IsUserPath(actorIRI) { + if err := f.db.GetWhere([]db.Where{{Key: "uri", Value: actorIRI.String()}}, acct); err != nil { + return nil, fmt.Errorf("db error getting account with uri %s: %s", actorIRI.String(), err) + } + } else if util.IsFollowingPath(actorIRI) { + if err := f.db.GetWhere([]db.Where{{Key: "following_uri", Value: actorIRI.String()}}, acct); err != nil { + return nil, fmt.Errorf("db error getting account with following uri %s: %s", actorIRI.String(), err) + } + } else { + return nil, fmt.Errorf("could not parse actor IRI %s as users or following path", actorIRI.String()) } acctFollowing := []gtsmodel.Follow{} diff --git a/internal/federation/federatingdb/lock.go b/internal/federation/federatingdb/lock.go index 417fd79b2..c9062da89 100644 --- a/internal/federation/federatingdb/lock.go +++ b/internal/federation/federatingdb/lock.go @@ -42,6 +42,10 @@ func (f *federatingDB) Lock(c context.Context, id *url.URL) error { // Strategy: create a new lock, if stored, continue. Otherwise, lock the // existing mutex. + if id == nil { + return errors.New("Lock: id was nil") + } + mu := &sync.Mutex{} mu.Lock() // Optimistically lock if we do store it. i, loaded := f.locks.LoadOrStore(id.String(), mu) @@ -59,6 +63,9 @@ func (f *federatingDB) Lock(c context.Context, id *url.URL) error { func (f *federatingDB) Unlock(c context.Context, id *url.URL) error { // Once Go-Fed is done calling Database methods, the relevant `id` // entries are unlocked. + if id == nil { + return errors.New("Unlock: id was nil") + } i, ok := f.locks.Load(id.String()) if !ok { diff --git a/internal/federation/federatingprotocol.go b/internal/federation/federatingprotocol.go index e1c1ab184..e05bdb7b9 100644 --- a/internal/federation/federatingprotocol.go +++ b/internal/federation/federatingprotocol.go @@ -26,6 +26,7 @@ import ( "net/url" "github.com/go-fed/activity/pub" + "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -310,7 +311,7 @@ func (f *federator) MaxDeliveryRecursionDepth(ctx context.Context) int { // logic to be used, but the implementation must not modify it. func (f *federator) FilterForwarding(ctx context.Context, potentialRecipients []*url.URL, a pub.Activity) ([]*url.URL, error) { // TODO - return nil, nil + return []*url.URL{}, nil } // GetInbox returns the OrderedCollection inbox of the actor for this @@ -324,5 +325,5 @@ func (f *federator) FilterForwarding(ctx context.Context, potentialRecipients [] func (f *federator) GetInbox(ctx context.Context, r *http.Request) (vocab.ActivityStreamsOrderedCollectionPage, error) { // IMPLEMENTATION NOTE: For GoToSocial, we serve GETS to outboxes and inboxes through // the CLIENT API, not through the federation API, so we just do nothing here. - return nil, nil + return streams.NewActivityStreamsOrderedCollectionPage(), nil } diff --git a/internal/federation/federator.go b/internal/federation/federator.go index 149f68426..016a6fb68 100644 --- a/internal/federation/federator.go +++ b/internal/federation/federator.go @@ -40,6 +40,9 @@ type Federator interface { // AuthenticateFederatedRequest can be used to check the authenticity of incoming http-signed requests for federating resources. // The given username will be used to create a transport for making outgoing requests. See the implementation for more detailed comments. AuthenticateFederatedRequest(username string, r *http.Request) (*url.URL, error) + // FingerRemoteAccount performs a webfinger lookup for a remote account, using the .well-known path. It will return the ActivityPub URI for that + // account, or an error if it doesn't exist or can't be retrieved. + FingerRemoteAccount(requestingUsername string, targetUsername string, targetDomain string) (*url.URL, error) // DereferenceRemoteAccount can be used to get the representation of a remote account, based on the account ID (which is a URI). // The given username will be used to create a transport for making outgoing requests. See the implementation for more detailed comments. DereferenceRemoteAccount(username string, remoteAccountID *url.URL) (typeutils.Accountable, error) diff --git a/internal/federation/finger.go b/internal/federation/finger.go new file mode 100644 index 000000000..9afe83edf --- /dev/null +++ b/internal/federation/finger.go @@ -0,0 +1,69 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + 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/>. +*/ + +package federation + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/url" + "strings" + + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" +) + +func (f *federator) FingerRemoteAccount(requestingUsername string, targetUsername string, targetDomain string) (*url.URL, error) { + + t, err := f.GetTransportForUser(requestingUsername) + if err != nil { + return nil, fmt.Errorf("FingerRemoteAccount: error getting transport for username %s while dereferencing @%s@%s: %s", requestingUsername, targetUsername, targetDomain, err) + } + + b, err := t.Finger(context.Background(), targetUsername, targetDomain) + if err != nil { + return nil, fmt.Errorf("FingerRemoteAccount: error doing request on behalf of username %s while dereferencing @%s@%s: %s", requestingUsername, targetUsername, targetDomain, err) + } + + resp := &apimodel.WebfingerAccountResponse{} + if err := json.Unmarshal(b, resp); err != nil { + return nil, fmt.Errorf("FingerRemoteAccount: could not unmarshal server response as WebfingerAccountResponse on behalf of username %s while dereferencing @%s@%s: %s", requestingUsername, targetUsername, targetDomain, err) + } + + if len(resp.Links) == 0 { + return nil, fmt.Errorf("FingerRemoteAccount: no links found in webfinger response %s", string(b)) + } + + // look through the links for the first one that matches "application/activity+json", this is what we need + for _, l := range resp.Links { + if strings.EqualFold(l.Type, "application/activity+json") { + if l.Href == "" || l.Rel != "self" { + continue + } + accountURI, err := url.Parse(l.Href) + if err != nil { + return nil, fmt.Errorf("FingerRemoteAccount: couldn't parse url %s: %s", l.Href, err) + } + // found it! + return accountURI, nil + } + } + + return nil, errors.New("FingerRemoteAccount: no match found in webfinger response") +} |