forked from kevadesu/forgejo
Use go-ap instead of go-fed
This commit is contained in:
parent
501a39fb98
commit
becdf5e1f1
8 changed files with 38 additions and 173 deletions
|
@ -6,7 +6,6 @@ package activitypub
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/activitypub"
|
||||
|
@ -14,7 +13,7 @@ import (
|
|||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/routers/api/v1/user"
|
||||
|
||||
"github.com/go-fed/activity/streams"
|
||||
ap "github.com/go-ap/activitypub"
|
||||
)
|
||||
|
||||
// Person function
|
||||
|
@ -39,59 +38,34 @@ func Person(ctx *context.APIContext) {
|
|||
return
|
||||
}
|
||||
username := ctx.Params("username")
|
||||
|
||||
person := streams.NewActivityStreamsPerson()
|
||||
|
||||
id := streams.NewJSONLDIdProperty()
|
||||
|
||||
link := strings.TrimSuffix(setting.AppURL, "/") + strings.TrimSuffix(ctx.Req.URL.EscapedPath(), "/")
|
||||
idIRI, _ := url.Parse(link)
|
||||
id.SetIRI(idIRI)
|
||||
person.SetJSONLDId(id)
|
||||
person := ap.PersonNew(ap.IRI(link))
|
||||
|
||||
name := streams.NewActivityStreamsNameProperty()
|
||||
name.AppendXMLSchemaString(username)
|
||||
person.SetActivityStreamsName(name)
|
||||
name := ap.NaturalLanguageValuesNew()
|
||||
name.Set("en", ap.Content(username))
|
||||
person.Name = name
|
||||
|
||||
ibox := streams.NewActivityStreamsInboxProperty()
|
||||
urlObject, _ := url.Parse(link + "/inbox")
|
||||
ibox.SetIRI(urlObject)
|
||||
person.SetActivityStreamsInbox(ibox)
|
||||
person.Inbox = ap.Item(ap.IRI(link + "/inbox"))
|
||||
person.Outbox = ap.Item(ap.IRI(link + "/outbox"))
|
||||
|
||||
obox := streams.NewActivityStreamsOutboxProperty()
|
||||
urlObject, _ = url.Parse(link + "/outbox")
|
||||
obox.SetIRI(urlObject)
|
||||
person.SetActivityStreamsOutbox(obox)
|
||||
|
||||
publicKeyProp := streams.NewW3IDSecurityV1PublicKeyProperty()
|
||||
|
||||
publicKeyType := streams.NewW3IDSecurityV1PublicKey()
|
||||
|
||||
pubKeyIDProp := streams.NewJSONLDIdProperty()
|
||||
pubKeyIRI, _ := url.Parse(link + "#main-key")
|
||||
pubKeyIDProp.SetIRI(pubKeyIRI)
|
||||
publicKeyType.SetJSONLDId(pubKeyIDProp)
|
||||
|
||||
ownerProp := streams.NewW3IDSecurityV1OwnerProperty()
|
||||
ownerProp.SetIRI(idIRI)
|
||||
publicKeyType.SetW3IDSecurityV1Owner(ownerProp)
|
||||
|
||||
publicKeyPemProp := streams.NewW3IDSecurityV1PublicKeyPemProperty()
|
||||
person.PublicKey.ID = ap.IRI(link + "#main-key")
|
||||
person.PublicKey.Owner = ap.IRI(link)
|
||||
|
||||
publicKeyPem, err := activitypub.GetPublicKey(user)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetPublicKey", err)
|
||||
return
|
||||
}
|
||||
publicKeyPemProp.Set(publicKeyPem)
|
||||
publicKeyType.SetW3IDSecurityV1PublicKeyPem(publicKeyPemProp)
|
||||
person.PublicKey.PublicKeyPem = publicKeyPem
|
||||
|
||||
publicKeyProp.AppendW3IDSecurityV1PublicKey(publicKeyType)
|
||||
person.SetW3IDSecurityV1PublicKey(publicKeyProp)
|
||||
|
||||
jsonmap, err := streams.Serialize(person)
|
||||
binary, err := person.MarshalJSON()
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "Serialize", err)
|
||||
}
|
||||
ctx.JSON(http.StatusOK, jsonmap)
|
||||
ctx.Resp.Header().Set("Content-Type", "application/json;charset=utf-8")
|
||||
ctx.Write(binary)
|
||||
ctx.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
// PersonInbox function
|
||||
|
|
|
@ -18,67 +18,25 @@ import (
|
|||
"code.gitea.io/gitea/modules/activitypub"
|
||||
gitea_context "code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/httplib"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/go-fed/activity/pub"
|
||||
"github.com/go-fed/activity/streams"
|
||||
"github.com/go-fed/activity/streams/vocab"
|
||||
ap "github.com/go-ap/activitypub"
|
||||
"github.com/go-fed/httpsig"
|
||||
)
|
||||
|
||||
type publicKeyer interface {
|
||||
GetW3IDSecurityV1PublicKey() vocab.W3IDSecurityV1PublicKeyProperty
|
||||
}
|
||||
|
||||
func getPublicKeyFromResponse(ctx context.Context, b []byte, keyID *url.URL) (p crypto.PublicKey, err error) {
|
||||
m := make(map[string]interface{})
|
||||
err = json.Unmarshal(b, &m)
|
||||
person := ap.PersonNew(ap.IRI(keyID.String()))
|
||||
err = person.UnmarshalJSON(b)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("ActivityStreams type cannot be converted to one known to have publicKey property: %T", b)
|
||||
return
|
||||
}
|
||||
var t vocab.Type
|
||||
t, err = streams.ToType(ctx, m)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
pker, ok := t.(publicKeyer)
|
||||
if !ok {
|
||||
err = fmt.Errorf("ActivityStreams type cannot be converted to one known to have publicKey property: %T", t)
|
||||
return
|
||||
}
|
||||
pkp := pker.GetW3IDSecurityV1PublicKey()
|
||||
if pkp == nil {
|
||||
err = fmt.Errorf("publicKey property is not provided")
|
||||
return
|
||||
}
|
||||
var pkpFound vocab.W3IDSecurityV1PublicKey
|
||||
for pkpIter := pkp.Begin(); pkpIter != pkp.End(); pkpIter = pkpIter.Next() {
|
||||
if !pkpIter.IsW3IDSecurityV1PublicKey() {
|
||||
continue
|
||||
}
|
||||
pkValue := pkpIter.Get()
|
||||
var pkID *url.URL
|
||||
pkID, err = pub.GetId(pkValue)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if pkID.String() != keyID.String() {
|
||||
continue
|
||||
}
|
||||
pkpFound = pkValue
|
||||
break
|
||||
}
|
||||
if pkpFound == nil {
|
||||
pkey := person.PublicKey
|
||||
if pkey.ID.String() != keyID.String() {
|
||||
err = fmt.Errorf("cannot find publicKey with id: %s in %s", keyID, b)
|
||||
return
|
||||
}
|
||||
pkPemProp := pkpFound.GetW3IDSecurityV1PublicKeyPem()
|
||||
if pkPemProp == nil || !pkPemProp.IsXMLSchemaString() {
|
||||
err = fmt.Errorf("publicKeyPem property is not provided or it is not embedded as a value")
|
||||
return
|
||||
}
|
||||
pubKeyPem := pkPemProp.Get()
|
||||
pubKeyPem := pkey.PublicKeyPem
|
||||
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")
|
||||
|
@ -92,11 +50,7 @@ func fetch(iri *url.URL) (b []byte, err error) {
|
|||
req := httplib.NewRequest(iri.String(), http.MethodGet)
|
||||
req.Header("Accept", activitypub.ActivityStreamsContentType)
|
||||
req.Header("Accept-Charset", "utf-8")
|
||||
clock, err := activitypub.NewClock()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.Header("Date", fmt.Sprintf("%s GMT", clock.Now().UTC().Format(time.RFC1123)))
|
||||
req.Header("Date", fmt.Sprintf("%s GMT", time.Now().UTC().Format(time.RFC1123)))
|
||||
resp, err := req.Response()
|
||||
if err != nil {
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue