From 1da0d49de771aa7be917a76fe01d66e79283c5a0 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Wed, 30 Mar 2022 15:52:52 -0500 Subject: [PATCH] Clean up some variable declarations --- integrations/api_activitypub_person_test.go | 12 +++++------- modules/activitypub/client.go | 6 +++--- routers/api/v1/activitypub/person.go | 7 +++++-- routers/api/v1/activitypub/reqsignature.go | 18 ++++++------------ 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/integrations/api_activitypub_person_test.go b/integrations/api_activitypub_person_test.go index 75daf0d81e..107ea10bf0 100644 --- a/integrations/api_activitypub_person_test.go +++ b/integrations/api_activitypub_person_test.go @@ -14,7 +14,6 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/activitypub" - "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" "github.com/go-fed/activity/pub" @@ -35,8 +34,7 @@ func TestActivityPubPerson(t *testing.T) { resp := MakeRequest(t, req, http.StatusOK) assert.Contains(t, resp.Body.String(), "@context") var m map[string]interface{} - err := json.Unmarshal(resp.Body.Bytes(), &m) - assert.Equal(t, err, nil) + DecodeJSON(t, resp, &m) var person vocab.ActivityStreamsPerson resolver, _ := streams.NewJSONResolver(func(c context.Context, p vocab.ActivityStreamsPerson) error { @@ -44,8 +42,8 @@ func TestActivityPubPerson(t *testing.T) { return nil }) ctx := context.Background() - err = resolver.Resolve(ctx, m) - assert.Equal(t, err, nil) + err := resolver.Resolve(ctx, m) + assert.NoError(t, err) assert.Equal(t, "Person", person.GetTypeName()) assert.Equal(t, username, person.GetActivityStreamsName().Begin().GetXMLSchemaString()) keyID := person.GetJSONLDId().GetIRI().String() @@ -124,10 +122,10 @@ func TestActivityPubPersonInbox(t *testing.T) { // Signed request succeeds resp, err := c.Post([]byte{}, user2inboxurl) assert.NoError(t, err) - assert.Equal(t, 204, resp.StatusCode) + assert.Equal(t, http.StatusNoContent, resp.StatusCode) // Unsigned request fails req := NewRequest(t, "POST", user2inboxurl) - MakeRequest(t, req, 500) + MakeRequest(t, req, http.StatusInternalServerError) }) } diff --git a/modules/activitypub/client.go b/modules/activitypub/client.go index 5c9ea2dd3f..0720909126 100644 --- a/modules/activitypub/client.go +++ b/modules/activitypub/client.go @@ -23,7 +23,7 @@ import ( const ( // ActivityStreamsContentType const - ActivityStreamsContentType = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" + ActivityStreamsContentType = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"` ) func containsRequiredHTTPHeaders(method string, headers []string) error { @@ -31,13 +31,13 @@ func containsRequiredHTTPHeaders(method string, headers []string) error { for _, header := range headers { hasRequestTarget = hasRequestTarget || header == httpsig.RequestTarget hasDate = hasDate || header == "Date" - hasDigest = method == "GET" || hasDigest || header == "Digest" + hasDigest = hasDigest || header == "Digest" } if !hasRequestTarget { return fmt.Errorf("missing http header for %s: %s", method, httpsig.RequestTarget) } else if !hasDate { return fmt.Errorf("missing http header for %s: Date", method) - } else if !hasDigest { + } else if !hasDigest && method != http.MethodGet { return fmt.Errorf("missing http header for %s: Digest", method) } return nil diff --git a/routers/api/v1/activitypub/person.go b/routers/api/v1/activitypub/person.go index 638ea2ae4e..4be076a9ce 100644 --- a/routers/api/v1/activitypub/person.go +++ b/routers/api/v1/activitypub/person.go @@ -78,6 +78,7 @@ func Person(ctx *context.APIContext) { publicKeyPemProp := streams.NewW3IDSecurityV1PublicKeyPemProperty() if publicKeyPem, err := activitypub.GetPublicKey(user); err != nil { ctx.Error(http.StatusInternalServerError, "GetPublicKey", err) + return } else { publicKeyPemProp.Set(publicKeyPem) } @@ -86,8 +87,10 @@ func Person(ctx *context.APIContext) { publicKeyProp.AppendW3IDSecurityV1PublicKey(publicKeyType) person.SetW3IDSecurityV1PublicKey(publicKeyProp) - var jsonmap map[string]interface{} - jsonmap, _ = streams.Serialize(person) + jsonmap, err := streams.Serialize(person) + if err != nil { + ctx.Error(http.StatusInternalServerError, "Serialize", err) + } ctx.JSON(http.StatusOK, jsonmap) } diff --git a/routers/api/v1/activitypub/reqsignature.go b/routers/api/v1/activitypub/reqsignature.go index fc51410fd3..b9f665da10 100644 --- a/routers/api/v1/activitypub/reqsignature.go +++ b/routers/api/v1/activitypub/reqsignature.go @@ -79,8 +79,7 @@ func getPublicKeyFromResponse(ctx context.Context, b []byte, keyID *url.URL) (p return } pubKeyPem := pkPemProp.Get() - var block *pem.Block - block, _ = pem.Decode([]byte(pubKeyPem)) + block, _ := pem.Decode([]byte(pubKeyPem)) if block == nil || block.Type != "PUBLIC KEY" { err = fmt.Errorf("could not decode publicKeyPem to PUBLIC KEY pem block type") return @@ -90,8 +89,7 @@ func getPublicKeyFromResponse(ctx context.Context, b []byte, keyID *url.URL) (p } func fetch(iri *url.URL) (b []byte, err error) { - var req *httplib.Request - req = httplib.NewRequest(iri.String(), http.MethodGet) + req := httplib.NewRequest(iri.String(), http.MethodGet) req.Header("Accept", activitypub.ActivityStreamsContentType) req.Header("Accept-Charset", "utf-8") clock, err := activitypub.NewClock() @@ -99,8 +97,7 @@ func fetch(iri *url.URL) (b []byte, err error) { return } req.Header("Date", fmt.Sprintf("%s GMT", clock.Now().UTC().Format(time.RFC1123))) - var resp *http.Response - resp, err = req.Response() + resp, err := req.Response() if err != nil { return } @@ -118,20 +115,17 @@ func verifyHTTPSignatures(ctx *gitea_context.APIContext) (authenticated bool, er r := ctx.Req // 1. Figure out what key we need to verify - var v httpsig.Verifier - v, err = httpsig.NewVerifier(r) + v, err := httpsig.NewVerifier(r) if err != nil { return } ID := v.KeyId() - var idIRI *url.URL - idIRI, err = url.Parse(ID) + idIRI, err := url.Parse(ID) if err != nil { return } // 2. Fetch the public key of the other actor - var b []byte - b, err = fetch(idIRI) + b, err := fetch(idIRI) if err != nil { return }