diff --git a/pub/fed.go b/pub/fed.go index 675b6c3..6e616e8 100644 --- a/pub/fed.go +++ b/pub/fed.go @@ -25,14 +25,37 @@ var ( // Pubber provides methods for interacting with ActivityPub clients and // ActivityPub federating servers. type Pubber interface { + // PostInbox returns true if the request was handled as an ActivityPub + // POST to an actor's inbox. If false, the request was not an + // ActivityPub request. + // + // If the error is nil, then the ResponseWriter's headers and response + // has already been written. If a non-nil error is returned, then no + // response has been written. PostInbox(c context.Context, w http.ResponseWriter, r *http.Request) (bool, error) + // GetInbox returns true if the request was handled as an ActivityPub + // GET to an actor's inbox. If false, the request was not an ActivityPub + // request. + // + // If the error is nil, then the ResponseWriter's headers and response + // has already been written. If a non-nil error is returned, then no + // response has been written. GetInbox(c context.Context, w http.ResponseWriter, r *http.Request) (bool, error) - // PostOutbox provides a HTTP handler for ActivityPub requests for the given id - // token. The client ID token is passed forwards to other interfaces for - // application specific behavior. The handler will return true if it handled - // the request as an ActivityPub request. If it returns an error, it is up to - // the client to determine how to respond via HTTP. + // PostOutbox returns true if the request was handled as an ActivityPub + // POST to an actor's outbox. If false, the request was not an + // ActivityPub request. + // + // If the error is nil, then the ResponseWriter's headers and response + // has already been written. If a non-nil error is returned, then no + // response has been written. PostOutbox(c context.Context, w http.ResponseWriter, r *http.Request) (bool, error) + // GetOutbox returns true if the request was handled as an ActivityPub + // GET to an actor's outbox. If false, the request was not an + // ActivityPub request. + // + // If the error is nil, then the ResponseWriter's headers and response + // has already been written. If a non-nil error is returned, then no + // response has been written. GetOutbox(c context.Context, w http.ResponseWriter, r *http.Request) (bool, error) } diff --git a/pub/handlers.go b/pub/handlers.go index 4d7746d..8bc406a 100644 --- a/pub/handlers.go +++ b/pub/handlers.go @@ -14,16 +14,25 @@ import ( // in the request. Note that requests may be signed with HTTP signatures or be // permitted without any authentication scheme. To change this default behavior, // use ServeActivityPubObjectWithVerificationMethod instead. -func ServeActivityPubObject(c context.Context, a Application, clock Clock, w http.ResponseWriter, r *http.Request) (handled bool, err error) { - return serveActivityPubObject(c, a, clock, w, r, nil) +func ServeActivityPubObject(a Application, clock Clock) HandlerFunc { + return func(c context.Context, w http.ResponseWriter, r *http.Request) (bool, error) { + return serveActivityPubObject(c, a, clock, w, r, nil) + } } // ServeActivityPubObjectWithVerificationMethod will serve the ActivityPub // object with the given IRI in the request. The rules for accessing the data // are governed by the SocialAPIVerifier's behavior and may permit accessing // data without having any credentials in the request. -func ServeActivityPubObjectWithVerificationMethod(c context.Context, a Application, clock Clock, w http.ResponseWriter, r *http.Request, verifier SocialAPIVerifier) (handled bool, err error) { - return serveActivityPubObject(c, a, clock, w, r, verifier) +func ServeActivityPubObjectWithVerificationMethod(a Application, clock Clock, verifierFn func(context.Context) SocialAPIVerifier) HandlerFunc { + return func(c context.Context, w http.ResponseWriter, r *http.Request) (bool, error) { + if verifierFn != nil { + verifier := verifierFn(c) + return serveActivityPubObject(c, a, clock, w, r, verifier) + } else { + return serveActivityPubObject(c, a, clock, w, r, nil) + } + } } func serveActivityPubObject(c context.Context, a Application, clock Clock, w http.ResponseWriter, r *http.Request, verifier SocialAPIVerifier) (handled bool, err error) { diff --git a/pub/handlers_test.go b/pub/handlers_test.go index 87dbcec..59697a9 100644 --- a/pub/handlers_test.go +++ b/pub/handlers_test.go @@ -144,7 +144,8 @@ func TestServeActivityPubObject(t *testing.T) { for _, test := range tests { t.Logf("Running table test case %q", test.name) resp := httptest.NewRecorder() - handled, err := ServeActivityPubObject(context.Background(), test.app, test.clock, resp, test.input) + fnUnderTest := ServeActivityPubObject(test.app, test.clock) + handled, err := fnUnderTest(context.Background(), resp, test.input) if err != nil { t.Fatalf("(%q) %s", test.name, err) } else if handled != test.expectHandled { @@ -518,13 +519,16 @@ func TestServeActivityPubObjectWithVerificationMethod(t *testing.T) { for _, test := range tests { t.Logf("Running table test case %q", test.name) resp := httptest.NewRecorder() - var handled bool - var err error + var fnUnderTest HandlerFunc if test.verifier != nil { - handled, err = ServeActivityPubObjectWithVerificationMethod(context.Background(), test.app, test.clock, resp, test.input, test.verifier) + verifierFn := func(c context.Context) SocialAPIVerifier { + return test.verifier + } + fnUnderTest = ServeActivityPubObjectWithVerificationMethod(test.app, test.clock, verifierFn) } else { - handled, err = ServeActivityPubObjectWithVerificationMethod(context.Background(), test.app, test.clock, resp, test.input, nil) + fnUnderTest = ServeActivityPubObjectWithVerificationMethod(test.app, test.clock, nil) } + handled, err := fnUnderTest(context.Background(), resp, test.input) if err != nil { t.Fatalf("(%q) %s", test.name, err) } else if handled != test.expectHandled { diff --git a/pub/interfaces.go b/pub/interfaces.go index 40c6d47..4cee939 100644 --- a/pub/interfaces.go +++ b/pub/interfaces.go @@ -128,7 +128,6 @@ const ( // SocialAPI is provided by users of this library and designed to handle // receiving messages from ActivityPub clients through the Social API. type SocialAPI interface { - // AddToOutboxResolver(c context.Context) (*streams.Resolver, error) // ActorIRI returns the actor's IRI associated with the given request. ActorIRI(c context.Context, r *http.Request) (*url.URL, error) // GetSocialAPIVerifier returns the authentication mechanism used for