Clean up some variable declarations

This commit is contained in:
Anthony Wang 2022-03-30 15:52:52 -05:00
parent 65016b2664
commit 1da0d49de7
No known key found for this signature in database
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" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/activitypub"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"github.com/go-fed/activity/pub" "github.com/go-fed/activity/pub"
@ -35,8 +34,7 @@ func TestActivityPubPerson(t *testing.T) {
resp := MakeRequest(t, req, http.StatusOK) resp := MakeRequest(t, req, http.StatusOK)
assert.Contains(t, resp.Body.String(), "@context") assert.Contains(t, resp.Body.String(), "@context")
var m map[string]interface{} var m map[string]interface{}
err := json.Unmarshal(resp.Body.Bytes(), &m) DecodeJSON(t, resp, &m)
assert.Equal(t, err, nil)
var person vocab.ActivityStreamsPerson var person vocab.ActivityStreamsPerson
resolver, _ := streams.NewJSONResolver(func(c context.Context, p vocab.ActivityStreamsPerson) error { resolver, _ := streams.NewJSONResolver(func(c context.Context, p vocab.ActivityStreamsPerson) error {
@ -44,8 +42,8 @@ func TestActivityPubPerson(t *testing.T) {
return nil return nil
}) })
ctx := context.Background() ctx := context.Background()
err = resolver.Resolve(ctx, m) err := resolver.Resolve(ctx, m)
assert.Equal(t, err, nil) assert.NoError(t, err)
assert.Equal(t, "Person", person.GetTypeName()) assert.Equal(t, "Person", person.GetTypeName())
assert.Equal(t, username, person.GetActivityStreamsName().Begin().GetXMLSchemaString()) assert.Equal(t, username, person.GetActivityStreamsName().Begin().GetXMLSchemaString())
keyID := person.GetJSONLDId().GetIRI().String() keyID := person.GetJSONLDId().GetIRI().String()
@ -124,10 +122,10 @@ func TestActivityPubPersonInbox(t *testing.T) {
// Signed request succeeds // Signed request succeeds
resp, err := c.Post([]byte{}, user2inboxurl) resp, err := c.Post([]byte{}, user2inboxurl)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, 204, resp.StatusCode) assert.Equal(t, http.StatusNoContent, resp.StatusCode)
// Unsigned request fails // Unsigned request fails
req := NewRequest(t, "POST", user2inboxurl) req := NewRequest(t, "POST", user2inboxurl)
MakeRequest(t, req, 500) MakeRequest(t, req, http.StatusInternalServerError)
}) })
} }

View File

@ -23,7 +23,7 @@ import (
const ( const (
// ActivityStreamsContentType 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 { func containsRequiredHTTPHeaders(method string, headers []string) error {
@ -31,13 +31,13 @@ func containsRequiredHTTPHeaders(method string, headers []string) error {
for _, header := range headers { for _, header := range headers {
hasRequestTarget = hasRequestTarget || header == httpsig.RequestTarget hasRequestTarget = hasRequestTarget || header == httpsig.RequestTarget
hasDate = hasDate || header == "Date" hasDate = hasDate || header == "Date"
hasDigest = method == "GET" || hasDigest || header == "Digest" hasDigest = hasDigest || header == "Digest"
} }
if !hasRequestTarget { if !hasRequestTarget {
return fmt.Errorf("missing http header for %s: %s", method, httpsig.RequestTarget) return fmt.Errorf("missing http header for %s: %s", method, httpsig.RequestTarget)
} else if !hasDate { } else if !hasDate {
return fmt.Errorf("missing http header for %s: Date", method) 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 fmt.Errorf("missing http header for %s: Digest", method)
} }
return nil return nil

View File

@ -78,6 +78,7 @@ func Person(ctx *context.APIContext) {
publicKeyPemProp := streams.NewW3IDSecurityV1PublicKeyPemProperty() publicKeyPemProp := streams.NewW3IDSecurityV1PublicKeyPemProperty()
if publicKeyPem, err := activitypub.GetPublicKey(user); err != nil { if publicKeyPem, err := activitypub.GetPublicKey(user); err != nil {
ctx.Error(http.StatusInternalServerError, "GetPublicKey", err) ctx.Error(http.StatusInternalServerError, "GetPublicKey", err)
return
} else { } else {
publicKeyPemProp.Set(publicKeyPem) publicKeyPemProp.Set(publicKeyPem)
} }
@ -86,8 +87,10 @@ func Person(ctx *context.APIContext) {
publicKeyProp.AppendW3IDSecurityV1PublicKey(publicKeyType) publicKeyProp.AppendW3IDSecurityV1PublicKey(publicKeyType)
person.SetW3IDSecurityV1PublicKey(publicKeyProp) person.SetW3IDSecurityV1PublicKey(publicKeyProp)
var jsonmap map[string]interface{} jsonmap, err := streams.Serialize(person)
jsonmap, _ = streams.Serialize(person) if err != nil {
ctx.Error(http.StatusInternalServerError, "Serialize", err)
}
ctx.JSON(http.StatusOK, jsonmap) ctx.JSON(http.StatusOK, jsonmap)
} }

View File

@ -79,8 +79,7 @@ func getPublicKeyFromResponse(ctx context.Context, b []byte, keyID *url.URL) (p
return return
} }
pubKeyPem := pkPemProp.Get() pubKeyPem := pkPemProp.Get()
var block *pem.Block block, _ := pem.Decode([]byte(pubKeyPem))
block, _ = pem.Decode([]byte(pubKeyPem))
if block == nil || block.Type != "PUBLIC KEY" { if block == nil || block.Type != "PUBLIC KEY" {
err = fmt.Errorf("could not decode publicKeyPem to PUBLIC KEY pem block type") err = fmt.Errorf("could not decode publicKeyPem to PUBLIC KEY pem block type")
return 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) { 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", activitypub.ActivityStreamsContentType)
req.Header("Accept-Charset", "utf-8") req.Header("Accept-Charset", "utf-8")
clock, err := activitypub.NewClock() clock, err := activitypub.NewClock()
@ -99,8 +97,7 @@ func fetch(iri *url.URL) (b []byte, err error) {
return return
} }
req.Header("Date", fmt.Sprintf("%s GMT", clock.Now().UTC().Format(time.RFC1123))) 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 { if err != nil {
return return
} }
@ -118,20 +115,17 @@ func verifyHTTPSignatures(ctx *gitea_context.APIContext) (authenticated bool, er
r := ctx.Req r := ctx.Req
// 1. Figure out what key we need to verify // 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 { if err != nil {
return return
} }
ID := v.KeyId() ID := v.KeyId()
var idIRI *url.URL idIRI, err := url.Parse(ID)
idIRI, err = url.Parse(ID)
if err != nil { if err != nil {
return return
} }
// 2. Fetch the public key of the other actor // 2. Fetch the public key of the other actor
var b []byte b, err := fetch(idIRI)
b, err = fetch(idIRI)
if err != nil { if err != nil {
return return
} }