2022-03-19 13:55:58 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2021-10-27 01:59:14 +00:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-11-10 12:35:02 +00:00
|
|
|
"net/http/httptest"
|
2021-10-27 01:59:14 +00:00
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-10 12:35:02 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/activitypub"
|
2022-03-20 02:25:42 +00:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2021-10-27 01:59:14 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-03-19 13:55:58 +00:00
|
|
|
|
2021-10-29 03:13:24 +00:00
|
|
|
"github.com/go-fed/activity/pub"
|
2021-10-27 01:59:14 +00:00
|
|
|
"github.com/go-fed/activity/streams"
|
|
|
|
"github.com/go-fed/activity/streams/vocab"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestActivityPubPerson(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
|
|
|
setting.Federation.Enabled = true
|
2021-10-29 03:13:24 +00:00
|
|
|
setting.Database.LogSQL = true
|
2021-10-27 01:59:14 +00:00
|
|
|
defer func() {
|
|
|
|
setting.Federation.Enabled = false
|
2021-10-29 03:13:24 +00:00
|
|
|
setting.Database.LogSQL = false
|
2021-10-27 01:59:14 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
username := "user2"
|
|
|
|
req := NewRequestf(t, "GET", fmt.Sprintf("/api/v1/activitypub/user/%s", username))
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2021-11-10 12:35:02 +00:00
|
|
|
assert.Contains(t, resp.Body.String(), "@context")
|
2021-10-27 01:59:14 +00:00
|
|
|
var m map[string]interface{}
|
2022-03-19 21:36:44 +00:00
|
|
|
err := json.Unmarshal(resp.Body.Bytes(), &m)
|
2021-10-27 01:59:14 +00:00
|
|
|
|
|
|
|
var person vocab.ActivityStreamsPerson
|
|
|
|
resolver, _ := streams.NewJSONResolver(func(c context.Context, p vocab.ActivityStreamsPerson) error {
|
|
|
|
person = p
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
ctx := context.Background()
|
2022-03-20 02:25:42 +00:00
|
|
|
err = resolver.Resolve(ctx, m)
|
2021-10-27 01:59:14 +00:00
|
|
|
assert.Equal(t, err, nil)
|
2021-10-29 03:13:24 +00:00
|
|
|
assert.Equal(t, "Person", person.GetTypeName())
|
|
|
|
assert.Equal(t, username, person.GetActivityStreamsName().Begin().GetXMLSchemaString())
|
2021-11-10 12:35:02 +00:00
|
|
|
keyID := person.GetJSONLDId().GetIRI().String()
|
|
|
|
assert.Regexp(t, fmt.Sprintf("activitypub/user/%s$", username), keyID)
|
2021-10-27 01:59:14 +00:00
|
|
|
assert.Regexp(t, fmt.Sprintf("activitypub/user/%s/outbox$", username), person.GetActivityStreamsOutbox().GetIRI().String())
|
|
|
|
assert.Regexp(t, fmt.Sprintf("activitypub/user/%s/inbox$", username), person.GetActivityStreamsInbox().GetIRI().String())
|
2021-10-29 03:13:24 +00:00
|
|
|
|
|
|
|
pkp := person.GetW3IDSecurityV1PublicKey()
|
2022-03-19 21:36:44 +00:00
|
|
|
assert.NotNil(t, pkp)
|
2021-11-10 12:35:02 +00:00
|
|
|
publicKeyID := keyID + "/#main-key"
|
2021-10-29 03:13:24 +00:00
|
|
|
var pkpFound vocab.W3IDSecurityV1PublicKey
|
|
|
|
for pkpIter := pkp.Begin(); pkpIter != pkp.End(); pkpIter = pkpIter.Next() {
|
|
|
|
if !pkpIter.IsW3IDSecurityV1PublicKey() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pkValue := pkpIter.Get()
|
2021-11-10 12:35:02 +00:00
|
|
|
var pkID *url.URL
|
|
|
|
pkID, err = pub.GetId(pkValue)
|
2021-10-29 03:13:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-11-10 12:35:02 +00:00
|
|
|
assert.Equal(t, pkID.String(), publicKeyID)
|
|
|
|
if pkID.String() != publicKeyID {
|
2021-10-29 03:13:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
pkpFound = pkValue
|
|
|
|
break
|
|
|
|
}
|
|
|
|
assert.NotNil(t, pkpFound)
|
|
|
|
|
|
|
|
pkPemProp := pkpFound.GetW3IDSecurityV1PublicKeyPem()
|
|
|
|
assert.NotNil(t, pkPemProp)
|
|
|
|
assert.True(t, pkPemProp.IsXMLSchemaString())
|
|
|
|
|
|
|
|
pubKeyPem := pkPemProp.Get()
|
|
|
|
assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----", pubKeyPem)
|
2021-10-27 01:59:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActivityPubMissingPerson(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
|
|
|
setting.Federation.Enabled = true
|
|
|
|
defer func() {
|
|
|
|
setting.Federation.Enabled = false
|
|
|
|
}()
|
|
|
|
|
|
|
|
req := NewRequestf(t, "GET", "/api/v1/activitypub/user/nonexistentuser")
|
|
|
|
resp := MakeRequest(t, req, http.StatusNotFound)
|
2021-11-10 12:35:02 +00:00
|
|
|
assert.Contains(t, resp.Body.String(), "GetUserByName")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActivityPubPersonInbox(t *testing.T) {
|
|
|
|
srv := httptest.NewServer(c)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
|
|
|
appURL := setting.AppURL
|
|
|
|
setting.Federation.Enabled = true
|
|
|
|
setting.Database.LogSQL = true
|
|
|
|
setting.AppURL = srv.URL
|
|
|
|
defer func() {
|
|
|
|
setting.Federation.Enabled = false
|
|
|
|
setting.Database.LogSQL = false
|
|
|
|
setting.AppURL = appURL
|
|
|
|
}()
|
|
|
|
username1 := "user1"
|
|
|
|
user1, err := user_model.GetUserByName(username1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user1url := fmt.Sprintf("%s/api/v1/activitypub/user/%s/#main-key", srv.URL, username1)
|
|
|
|
c, err := activitypub.NewClient(user1, user1url)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
username2 := "user2"
|
|
|
|
user2inboxurl := fmt.Sprintf("%s/api/v1/activitypub/user/%s/inbox", srv.URL, username2)
|
|
|
|
|
|
|
|
// Signed request succeeds
|
|
|
|
resp, err := c.Post([]byte{}, user2inboxurl)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 204, resp.StatusCode)
|
|
|
|
|
|
|
|
// Unsigned request fails
|
|
|
|
req := NewRequest(t, "POST", user2inboxurl)
|
|
|
|
MakeRequest(t, req, 500)
|
2021-10-27 01:59:14 +00:00
|
|
|
})
|
|
|
|
}
|