Fix comments and HandlerFunc
このコミットが含まれているのは:
コミット
0fc1556d17
33
pub/fed.go
33
pub/fed.go
|
@ -25,14 +25,37 @@ var (
|
||||||
// Pubber provides methods for interacting with ActivityPub clients and
|
// Pubber provides methods for interacting with ActivityPub clients and
|
||||||
// ActivityPub federating servers.
|
// ActivityPub federating servers.
|
||||||
type Pubber interface {
|
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)
|
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)
|
GetInbox(c context.Context, w http.ResponseWriter, r *http.Request) (bool, error)
|
||||||
// PostOutbox provides a HTTP handler for ActivityPub requests for the given id
|
// PostOutbox returns true if the request was handled as an ActivityPub
|
||||||
// token. The client ID token is passed forwards to other interfaces for
|
// POST to an actor's outbox. If false, the request was not an
|
||||||
// application specific behavior. The handler will return true if it handled
|
// ActivityPub request.
|
||||||
// the request as an ActivityPub request. If it returns an error, it is up to
|
//
|
||||||
// the client to determine how to respond via HTTP.
|
// 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)
|
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)
|
GetOutbox(c context.Context, w http.ResponseWriter, r *http.Request) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,16 +14,25 @@ import (
|
||||||
// in the request. Note that requests may be signed with HTTP signatures or be
|
// in the request. Note that requests may be signed with HTTP signatures or be
|
||||||
// permitted without any authentication scheme. To change this default behavior,
|
// permitted without any authentication scheme. To change this default behavior,
|
||||||
// use ServeActivityPubObjectWithVerificationMethod instead.
|
// use ServeActivityPubObjectWithVerificationMethod instead.
|
||||||
func ServeActivityPubObject(c context.Context, a Application, clock Clock, w http.ResponseWriter, r *http.Request) (handled bool, err error) {
|
func ServeActivityPubObject(a Application, clock Clock) HandlerFunc {
|
||||||
return serveActivityPubObject(c, a, clock, w, r, nil)
|
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
|
// ServeActivityPubObjectWithVerificationMethod will serve the ActivityPub
|
||||||
// object with the given IRI in the request. The rules for accessing the data
|
// 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
|
// are governed by the SocialAPIVerifier's behavior and may permit accessing
|
||||||
// data without having any credentials in the request.
|
// 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) {
|
func ServeActivityPubObjectWithVerificationMethod(a Application, clock Clock, verifierFn func(context.Context) SocialAPIVerifier) HandlerFunc {
|
||||||
return serveActivityPubObject(c, a, clock, w, r, verifier)
|
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) {
|
func serveActivityPubObject(c context.Context, a Application, clock Clock, w http.ResponseWriter, r *http.Request, verifier SocialAPIVerifier) (handled bool, err error) {
|
||||||
|
|
|
@ -144,7 +144,8 @@ func TestServeActivityPubObject(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Logf("Running table test case %q", test.name)
|
t.Logf("Running table test case %q", test.name)
|
||||||
resp := httptest.NewRecorder()
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("(%q) %s", test.name, err)
|
t.Fatalf("(%q) %s", test.name, err)
|
||||||
} else if handled != test.expectHandled {
|
} else if handled != test.expectHandled {
|
||||||
|
@ -518,13 +519,16 @@ func TestServeActivityPubObjectWithVerificationMethod(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Logf("Running table test case %q", test.name)
|
t.Logf("Running table test case %q", test.name)
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
var handled bool
|
var fnUnderTest HandlerFunc
|
||||||
var err error
|
|
||||||
if test.verifier != nil {
|
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 {
|
} 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 {
|
if err != nil {
|
||||||
t.Fatalf("(%q) %s", test.name, err)
|
t.Fatalf("(%q) %s", test.name, err)
|
||||||
} else if handled != test.expectHandled {
|
} else if handled != test.expectHandled {
|
||||||
|
|
|
@ -128,7 +128,6 @@ const (
|
||||||
// SocialAPI is provided by users of this library and designed to handle
|
// SocialAPI is provided by users of this library and designed to handle
|
||||||
// receiving messages from ActivityPub clients through the Social API.
|
// receiving messages from ActivityPub clients through the Social API.
|
||||||
type SocialAPI interface {
|
type SocialAPI interface {
|
||||||
// AddToOutboxResolver(c context.Context) (*streams.Resolver, error)
|
|
||||||
// ActorIRI returns the actor's IRI associated with the given request.
|
// ActorIRI returns the actor's IRI associated with the given request.
|
||||||
ActorIRI(c context.Context, r *http.Request) (*url.URL, error)
|
ActorIRI(c context.Context, r *http.Request) (*url.URL, error)
|
||||||
// GetSocialAPIVerifier returns the authentication mechanism used for
|
// GetSocialAPIVerifier returns the authentication mechanism used for
|
||||||
|
|
読み込み中…
新しいイシューから参照