mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-15 20:37:30 -06:00
[chore] Refactor AP authentication, other small bits of tidying up (#1874)
This commit is contained in:
parent
433b56d2f9
commit
24fbdf2b0a
38 changed files with 1280 additions and 996 deletions
|
|
@ -41,12 +41,9 @@ func (f *federatingDB) Accept(ctx context.Context, accept vocab.ActivityStreamsA
|
|||
l.Debug("entering Accept")
|
||||
}
|
||||
|
||||
receivingAccount, _ := extractFromCtx(ctx)
|
||||
if receivingAccount == nil {
|
||||
// If the receiving account wasn't set on the context, that means this request didn't pass
|
||||
// through the API, but came from inside GtS as the result of another activity on this instance. That being so,
|
||||
// we can safely just ignore this activity, since we know we've already processed it elsewhere.
|
||||
return nil
|
||||
receivingAccount, _, internal := extractFromCtx(ctx)
|
||||
if internal {
|
||||
return nil // Already processed.
|
||||
}
|
||||
|
||||
acceptObject := accept.GetActivityStreamsObject()
|
||||
|
|
|
|||
|
|
@ -39,12 +39,9 @@ func (f *federatingDB) Announce(ctx context.Context, announce vocab.ActivityStre
|
|||
l.Debug("entering Announce")
|
||||
}
|
||||
|
||||
receivingAccount, _ := extractFromCtx(ctx)
|
||||
if receivingAccount == nil {
|
||||
// If the receiving account wasn't set on the context, that means this request didn't pass
|
||||
// through the API, but came from inside GtS as the result of another activity on this instance. That being so,
|
||||
// we can safely just ignore this activity, since we know we've already processed it elsewhere.
|
||||
return nil
|
||||
receivingAccount, _, internal := extractFromCtx(ctx)
|
||||
if internal {
|
||||
return nil // Already processed.
|
||||
}
|
||||
|
||||
boost, isNew, err := f.typeConverter.ASAnnounceToStatus(ctx, announce)
|
||||
|
|
|
|||
|
|
@ -57,12 +57,9 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error {
|
|||
l.Trace("entering Create")
|
||||
}
|
||||
|
||||
receivingAccount, requestingAccount := extractFromCtx(ctx)
|
||||
if receivingAccount == nil {
|
||||
// If the receiving account wasn't set on the context, that means this request didn't pass
|
||||
// through the API, but came from inside GtS as the result of another activity on this instance. That being so,
|
||||
// we can safely just ignore this activity, since we know we've already processed it elsewhere.
|
||||
return nil
|
||||
receivingAccount, requestingAccount, internal := extractFromCtx(ctx)
|
||||
if internal {
|
||||
return nil // Already processed.
|
||||
}
|
||||
|
||||
switch asType.GetTypeName() {
|
||||
|
|
|
|||
|
|
@ -40,12 +40,9 @@ func (f *federatingDB) Delete(ctx context.Context, id *url.URL) error {
|
|||
}...)
|
||||
l.Debug("entering Delete")
|
||||
|
||||
receivingAccount, requestingAccount := extractFromCtx(ctx)
|
||||
if receivingAccount == nil {
|
||||
// If the receiving account wasn't set on the context, that means this request didn't pass
|
||||
// through the API, but came from inside GtS as the result of another activity on this instance. That being so,
|
||||
// we can safely just ignore this activity, since we know we've already processed it elsewhere.
|
||||
return nil
|
||||
receivingAccount, requestingAccount, internal := extractFromCtx(ctx)
|
||||
if internal {
|
||||
return nil // Already processed.
|
||||
}
|
||||
|
||||
// in a delete we only get the URI, we can't know if we have a status or a profile or something else,
|
||||
|
|
|
|||
|
|
@ -21,9 +21,9 @@ import (
|
|||
"context"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/state"
|
||||
|
|
@ -107,7 +107,7 @@ func (suite *FederatingDBTestSuite) TearDownTest() {
|
|||
|
||||
func createTestContext(receivingAccount *gtsmodel.Account, requestingAccount *gtsmodel.Account) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, ap.ContextReceivingAccount, receivingAccount)
|
||||
ctx = context.WithValue(ctx, ap.ContextRequestingAccount, requestingAccount)
|
||||
ctx = gtscontext.SetReceivingAccount(ctx, receivingAccount)
|
||||
ctx = gtscontext.SetRequestingAccount(ctx, requestingAccount)
|
||||
return ctx
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,12 +40,9 @@ func (f *federatingDB) Reject(ctx context.Context, reject vocab.ActivityStreamsR
|
|||
l.Debug("entering Reject")
|
||||
}
|
||||
|
||||
receivingAccount, _ := extractFromCtx(ctx)
|
||||
if receivingAccount == nil {
|
||||
// If the receiving account or federator channel wasn't set on the context, that means this request didn't pass
|
||||
// through the API, but came from inside GtS as the result of another activity on this instance. That being so,
|
||||
// we can safely just ignore this activity, since we know we've already processed it elsewhere.
|
||||
return nil
|
||||
receivingAccount, _, internal := extractFromCtx(ctx)
|
||||
if internal {
|
||||
return nil // Already processed.
|
||||
}
|
||||
|
||||
rejectObject := reject.GetActivityStreamsObject()
|
||||
|
|
|
|||
|
|
@ -43,12 +43,9 @@ func (f *federatingDB) Undo(ctx context.Context, undo vocab.ActivityStreamsUndo)
|
|||
l.Debug("entering Undo")
|
||||
}
|
||||
|
||||
receivingAccount, _ := extractFromCtx(ctx)
|
||||
if receivingAccount == nil {
|
||||
// If the receiving account wasn't set on the context, that means this request didn't pass
|
||||
// through the API, but came from inside GtS as the result of another activity on this instance. That being so,
|
||||
// we can safely just ignore this activity, since we know we've already processed it elsewhere.
|
||||
return nil
|
||||
receivingAccount, _, internal := extractFromCtx(ctx)
|
||||
if internal {
|
||||
return nil // Already processed.
|
||||
}
|
||||
|
||||
undoObject := undo.GetActivityStreamsObject()
|
||||
|
|
|
|||
|
|
@ -52,28 +52,14 @@ func (f *federatingDB) Update(ctx context.Context, asType vocab.Type) error {
|
|||
l.Debug("entering Update")
|
||||
}
|
||||
|
||||
receivingAccount, _ := extractFromCtx(ctx)
|
||||
if receivingAccount == nil {
|
||||
// If the receiving account wasn't set on the context, that means
|
||||
// this request didn't pass through the API, but came from inside
|
||||
// GtS as the result of another activity on this instance. As such,
|
||||
// we must have already processed it in order to reach this stage.
|
||||
return nil
|
||||
}
|
||||
|
||||
requestingAcctI := ctx.Value(ap.ContextRequestingAccount)
|
||||
if requestingAcctI == nil {
|
||||
return errors.New("Update: requesting account wasn't set on context")
|
||||
}
|
||||
|
||||
requestingAcct, ok := requestingAcctI.(*gtsmodel.Account)
|
||||
if !ok {
|
||||
return errors.New("Update: requesting account was set on context but couldn't be parsed")
|
||||
receivingAccount, requestingAccount, internal := extractFromCtx(ctx)
|
||||
if internal {
|
||||
return nil // Already processed.
|
||||
}
|
||||
|
||||
switch asType.GetTypeName() {
|
||||
case ap.ActorApplication, ap.ActorGroup, ap.ActorOrganization, ap.ActorPerson, ap.ActorService:
|
||||
return f.updateAccountable(ctx, receivingAccount, requestingAcct, asType)
|
||||
return f.updateAccountable(ctx, receivingAccount, requestingAccount, asType)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/id"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/log"
|
||||
|
|
@ -296,30 +297,23 @@ func (f *federatingDB) collectIRIs(ctx context.Context, iris []*url.URL) (vocab.
|
|||
return collection, nil
|
||||
}
|
||||
|
||||
// extractFromCtx extracts some useful values from a context passed into the federatingDB via the API:
|
||||
// - The target account that owns the inbox or URI being interacted with.
|
||||
// - The requesting account that posted to the inbox.
|
||||
// - A channel that messages for the processor can be placed into.
|
||||
// extractFromCtx extracts some useful values from a context passed into the federatingDB:
|
||||
//
|
||||
// If a value is not present, nil will be returned for it. It's up to the caller to check this and respond appropriately.
|
||||
func extractFromCtx(ctx context.Context) (receivingAccount, requestingAccount *gtsmodel.Account) {
|
||||
receivingAccountI := ctx.Value(ap.ContextReceivingAccount)
|
||||
if receivingAccountI != nil {
|
||||
var ok bool
|
||||
receivingAccount, ok = receivingAccountI.(*gtsmodel.Account)
|
||||
if !ok {
|
||||
log.Panicf(ctx, "context entry with key %s could not be asserted to *gtsmodel.Account", ap.ContextReceivingAccount)
|
||||
}
|
||||
}
|
||||
// - The account that owns the inbox or URI being interacted with.
|
||||
// - The account that POSTed a request to the inbox.
|
||||
// - Whether this is an internal request (one originating not from
|
||||
// the API but from inside the instance).
|
||||
//
|
||||
// If the request is internal, the caller can assume that the activity has
|
||||
// already been processed elsewhere, and should return with no further action.
|
||||
func extractFromCtx(ctx context.Context) (receivingAccount *gtsmodel.Account, requestingAccount *gtsmodel.Account, internal bool) {
|
||||
receivingAccount = gtscontext.ReceivingAccount(ctx)
|
||||
requestingAccount = gtscontext.RequestingAccount(ctx)
|
||||
|
||||
requestingAcctI := ctx.Value(ap.ContextRequestingAccount)
|
||||
if requestingAcctI != nil {
|
||||
var ok bool
|
||||
requestingAccount, ok = requestingAcctI.(*gtsmodel.Account)
|
||||
if !ok {
|
||||
log.Panicf(ctx, "context entry with key %s could not be asserted to *gtsmodel.Account", ap.ContextRequestingAccount)
|
||||
}
|
||||
}
|
||||
// If the receiving account wasn't set on the context, that
|
||||
// means this request didn't pass through the API, but
|
||||
// came from inside GtS as the result of a local activity.
|
||||
internal = receivingAccount == nil
|
||||
|
||||
return
|
||||
}
|
||||
|
|
@ -329,9 +323,11 @@ func marshalItem(item vocab.Type) (string, error) {
|
|||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
b, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(b), nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue