Set ap.ItemTyperFunc to correctly unmarshal JSON

This commit is contained in:
Anthony Wang 2022-07-25 15:43:20 -05:00
parent bffb682117
commit 30b431da49
Signed by: a
GPG key ID: BC96B00AEC5F2D76
6 changed files with 37 additions and 54 deletions

View file

@ -90,7 +90,7 @@ var (
"git-receive-pack": perm.AccessModeWrite,
lfsAuthenticateVerb: perm.AccessModeNone,
}
alphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`)
alphaDashDotPattern = regexp.MustCompile(`[^\w-\.@]`)
)
func fail(userMessage, logMessage string, args ...interface{}) error {

View file

@ -8,8 +8,8 @@ import (
"net/http"
"net/url"
"code.gitea.io/gitea/models/forgefed"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/json"
ap "github.com/go-ap/activitypub"
)
@ -26,37 +26,32 @@ func AuthorizeInteraction(c *context.Context) {
return
}
var object map[string]interface{}
err = json.Unmarshal(resp, &object)
ap.ItemTyperFunc = forgefed.GetItemByType
object, err := ap.UnmarshalJSON(resp)
if err != nil {
c.ServerError("Unmarshal", err)
c.ServerError("UnmarshalJSON", err)
return
}
switch object["type"] {
case "Person":
var person ap.Person
err = person.UnmarshalJSON(resp)
switch object.GetType() {
case ap.PersonType:
if err != nil {
c.ServerError("UnmarshalJSON", err)
return
}
err = FederatedUserNew(c, person)
err = FederatedUserNew(c, object.(ap.Person))
if err != nil {
c.ServerError("FederatedUserNew", err)
return
}
name, err := personIRIToName(person.GetLink())
name, err := personIRIToName(object.GetLink())
if err != nil {
c.ServerError("personIRIToName", err)
return
}
c.Redirect(name)
/*case "organization":
// Do something idk
case "repository":
FederatedRepoNew() // TODO
case "ticket":
// TODO*/
case forgefed.RepositoryType:
err = FederatedRepoNew(object.(forgefed.Repository))
}
c.Status(http.StatusOK)

View file

@ -5,17 +5,19 @@
package activitypub
import (
"code.gitea.io/gitea/models"
repo_model "code.gitea.io/gitea/models/repo"
//"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/forgefed"
/*repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/repository"
ap "github.com/go-ap/activitypub"
ap "github.com/go-ap/activitypub"*/
)
func FederatedRepoNew(user *user_model.User, name string, iri ap.IRI) (*repo_model.Repository, error) {
func FederatedRepoNew(repo forgefed.Repository) error {
// TODO: also handle forks
return repository.CreateRepository(user, user, models.CreateRepoOptions{
Name: name,
})
/*_, err := repository.CreateRepository(user, user, models.CreateRepoOptions{
Name: repo.Name.String(),
})*/
return nil
}

View file

@ -48,7 +48,7 @@ func FederatedUserNew(ctx context.Context, person ap.Person) error {
user := &user_model.User{
Name: name,
FullName: person.Name.String(),
FullName: person.Name.String(), // May not exist!!
Email: email,
Avatar: avatar,
LoginType: auth.Federated,

View file

@ -12,7 +12,6 @@ import (
"code.gitea.io/gitea/models/forgefed"
"code.gitea.io/gitea/modules/activitypub"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
@ -96,51 +95,40 @@ func RepoInbox(ctx *context.APIContext) {
return
}
var activity map[string]interface{}
err = json.Unmarshal(body, activity)
ap.ItemTyperFunc = forgefed.GetItemByType
var activity ap.Activity
err = activity.UnmarshalJSON(body)
if err != nil {
ctx.ServerError("Unmarshal", err)
ctx.ServerError("UnmarshalJSON", err)
return
}
// Make sure keyID matches the user doing the activity
_, keyID, _ := getKeyID(ctx.Req)
actor, ok := activity["actor"]
if ok && !strings.HasPrefix(keyID, actor.(string)) {
if activity.Actor != nil && !strings.HasPrefix(keyID, activity.Actor.GetID().String()) {
ctx.ServerError("Actor does not match HTTP signature keyID", nil)
return
}
attributedTo, ok := activity["attributedTo"]
if ok && !strings.HasPrefix(keyID, attributedTo.(string)) {
if activity.AttributedTo != nil && !strings.HasPrefix(keyID, activity.AttributedTo.GetID().String()) {
ctx.ServerError("AttributedTo does not match HTTP signature keyID", nil)
return
}
// Process activity
switch activity["type"].(ap.ActivityVocabularyType) {
switch activity.Type {
case ap.CreateType:
// Create activity, extract the object
object, ok := activity["object"].(map[string]interface{})
if ok {
ctx.ServerError("Create activity does not contain object", err)
return
}
objectBinary, err := json.Marshal(object)
if err != nil {
ctx.ServerError("Marshal", err)
if activity.Object == nil {
ctx.ServerError("Activity does not contain object", err)
return
}
switch object["type"].(ap.ActivityVocabularyType) {
switch activity.Object.(ap.Object).Type {
case forgefed.RepositoryType:
// Fork created by remote instance
var repository forgefed.Repository
repository.UnmarshalJSON(objectBinary)
activitypub.ForkFromCreate(ctx, repository)
activitypub.ForkFromCreate(ctx, activity.Object.(forgefed.Repository))
case forgefed.TicketType:
// New issue or pull request
var ticket forgefed.Ticket
ticket.UnmarshalJSON(objectBinary)
ticket := activity.Object.(forgefed.Ticket)
if ticket.Origin != nil {
// New pull request
activitypub.PullRequest(ctx, ticket)
@ -150,12 +138,10 @@ func RepoInbox(ctx *context.APIContext) {
}
case ap.NoteType:
// New comment
var note ap.Note
note.UnmarshalJSON(objectBinary)
activitypub.Comment(ctx, note)
activitypub.Comment(ctx, activity.Object.(ap.Note))
}
default:
log.Info("Incoming unsupported ActivityStreams type: %s", activity["type"])
log.Info("Incoming unsupported ActivityStreams type: %s", activity.Type)
ctx.PlainText(http.StatusNotImplemented, "ActivityStreams type not supported")
return
}

View file

@ -39,7 +39,7 @@ const (
)
var (
separatorAntiPattern = regexp.MustCompile(`[^\w-\.]`)
separatorAntiPattern = regexp.MustCompile(`[^\w-\.@]`)
langCodePattern = regexp.MustCompile(`^[a-z]{2}-[A-Z]{2}$`)
)