Clean up some variable declarations

This commit is contained in:
Anthony Wang 2022-03-30 15:52:52 -05:00
parent 65016b2664
commit 1da0d49de7
Signed by: a
GPG key ID: BC96B00AEC5F2D76
4 changed files with 19 additions and 24 deletions

View file

@ -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)
})
}

View file

@ -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

View file

@ -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)
}

View file

@ -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
}