2024-01-13 10:08:12 -05:00
|
|
|
// Copyright 2023, 2024 The forgejo Authors. All rights reserved.
|
2023-10-20 09:16:04 -04:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
2023-10-27 14:13:51 -04:00
|
|
|
"fmt"
|
2023-10-20 09:16:04 -04:00
|
|
|
"net/http"
|
2023-10-27 14:13:51 -04:00
|
|
|
"strings"
|
2023-10-20 09:16:04 -04:00
|
|
|
|
2023-12-09 08:27:29 -05:00
|
|
|
"code.gitea.io/gitea/models/forgefed"
|
2023-12-07 07:17:51 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2023-11-28 07:03:45 -05:00
|
|
|
api "code.gitea.io/gitea/modules/activitypub"
|
2023-10-20 09:16:04 -04:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2023-10-27 14:13:51 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-01-03 12:52:41 -05:00
|
|
|
"code.gitea.io/gitea/modules/validation"
|
2023-11-10 08:26:13 -05:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2023-10-27 14:13:51 -04:00
|
|
|
|
|
|
|
ap "github.com/go-ap/activitypub"
|
2023-10-20 09:16:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Repository function returns the Repository actor for a repo
|
|
|
|
func Repository(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /activitypub/repository-id/{repository-id} activitypub activitypubRepository
|
|
|
|
// ---
|
|
|
|
// summary: Returns the Repository actor for a repo
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: repository-id
|
|
|
|
// in: path
|
|
|
|
// description: repository ID of the repo
|
|
|
|
// type: integer
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/ActivityPub"
|
|
|
|
|
2023-11-15 06:10:31 -05:00
|
|
|
link := fmt.Sprintf("%s/api/v1/activitypub/repository-id/%d", strings.TrimSuffix(setting.AppURL, "/"), ctx.Repo.Repository.ID)
|
2023-11-06 12:29:48 -05:00
|
|
|
repo := forgefed.RepositoryNew(ap.IRI(link))
|
2023-10-27 14:13:51 -04:00
|
|
|
|
2023-11-06 12:29:48 -05:00
|
|
|
repo.Name = ap.NaturalLanguageValuesNew()
|
|
|
|
err := repo.Name.Set("en", ap.Content(ctx.Repo.Repository.Name))
|
2023-10-27 14:13:51 -04:00
|
|
|
if err != nil {
|
2024-01-13 08:17:11 -05:00
|
|
|
ctx.Error(http.StatusInternalServerError, "Set Name", err)
|
2023-10-27 14:13:51 -04:00
|
|
|
return
|
|
|
|
}
|
2023-11-06 12:29:48 -05:00
|
|
|
|
|
|
|
response(ctx, repo)
|
2023-10-20 09:16:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PersonInbox function handles the incoming data for a repository inbox
|
|
|
|
func RepositoryInbox(ctx *context.APIContext) {
|
|
|
|
// swagger:operation POST /activitypub/repository-id/{repository-id}/inbox activitypub activitypubRepository
|
|
|
|
// ---
|
|
|
|
// summary: Send to the inbox
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: repository-id
|
|
|
|
// in: path
|
|
|
|
// description: repository ID of the repo
|
|
|
|
// type: integer
|
|
|
|
// required: true
|
2023-11-08 02:56:22 -05:00
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
2024-01-03 12:29:12 -05:00
|
|
|
// "$ref": "#/definitions/ForgeLike"
|
2023-10-20 09:16:04 -04:00
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
|
2023-12-08 12:09:22 -05:00
|
|
|
repository := ctx.Repo.Repository
|
2023-12-15 08:45:20 -05:00
|
|
|
log.Info("RepositoryInbox: repo: %v", repository)
|
2023-12-08 12:09:22 -05:00
|
|
|
|
2024-01-03 12:29:12 -05:00
|
|
|
activity := web.GetForm(ctx).(*forgefed.ForgeLike)
|
2024-01-03 12:52:41 -05:00
|
|
|
if res, err := validation.IsValid(activity); !res {
|
2024-01-13 08:17:11 -05:00
|
|
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate activity", err)
|
2024-01-03 12:52:41 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Info("RepositoryInbox: activity validated:%v", activity)
|
2023-11-03 12:45:53 -04:00
|
|
|
|
2023-12-22 08:52:10 -05:00
|
|
|
// parse actorID (person)
|
2024-01-13 11:06:40 -05:00
|
|
|
actorURI := activity.Actor.GetID().String()
|
|
|
|
rawActorID, err := forgefed.NewActorID(actorURI)
|
2024-01-14 07:03:51 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError,
|
|
|
|
"RepositoryInbox: Validating ActorID", err)
|
|
|
|
return
|
|
|
|
}
|
2024-02-07 11:11:43 -05:00
|
|
|
federationHost, err := forgefed.FindFederationHostByFqdn(ctx, rawActorID.Host)
|
2024-01-12 11:00:17 -05:00
|
|
|
if err != nil {
|
2024-01-13 08:17:11 -05:00
|
|
|
ctx.Error(http.StatusInternalServerError,
|
|
|
|
"RepositoryInbox: Error while loading FederationInfo", err)
|
2024-01-12 11:00:17 -05:00
|
|
|
return
|
|
|
|
}
|
2024-02-07 11:11:43 -05:00
|
|
|
if federationHost == nil {
|
|
|
|
result, err := createFederationHost(ctx, rawActorID)
|
2024-01-12 11:00:17 -05:00
|
|
|
if err != nil {
|
2024-01-13 08:17:11 -05:00
|
|
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate actorId", err)
|
2024-01-12 11:00:17 -05:00
|
|
|
return
|
|
|
|
}
|
2024-02-07 11:11:43 -05:00
|
|
|
federationHost = &result
|
|
|
|
log.Info("RepositoryInbox: federationInfo validated: %v", federationHost)
|
2024-01-12 11:00:17 -05:00
|
|
|
}
|
2024-02-07 11:11:43 -05:00
|
|
|
if !activity.IsNewer(federationHost.LatestActivity) {
|
2024-01-13 08:17:11 -05:00
|
|
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate Activity",
|
|
|
|
fmt.Errorf("Activity already processed"))
|
|
|
|
return
|
|
|
|
}
|
2023-12-29 06:10:07 -05:00
|
|
|
|
2024-02-07 11:11:43 -05:00
|
|
|
actorID, err := forgefed.NewPersonID(actorURI, string(federationHost.NodeInfo.Source))
|
2023-12-07 05:45:24 -05:00
|
|
|
if err != nil {
|
2024-01-13 08:17:11 -05:00
|
|
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate actorId", err)
|
2023-12-08 12:09:22 -05:00
|
|
|
return
|
2023-12-07 05:45:24 -05:00
|
|
|
}
|
2023-12-22 08:52:10 -05:00
|
|
|
log.Info("RepositoryInbox: actorId validated: %v", actorID)
|
|
|
|
// parse objectID (repository)
|
2024-01-12 11:00:17 -05:00
|
|
|
objectID, err := forgefed.NewRepositoryID(activity.Object.GetID().String(), string(forgefed.ForgejoSourceType))
|
2023-12-09 13:11:38 -05:00
|
|
|
if err != nil {
|
2024-01-13 08:17:11 -05:00
|
|
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate objectId", err)
|
2023-12-12 05:04:03 -05:00
|
|
|
return
|
|
|
|
}
|
2023-12-22 09:10:21 -05:00
|
|
|
if objectID.ID != fmt.Sprint(repository.ID) {
|
2024-01-13 08:17:11 -05:00
|
|
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate objectId", err)
|
2023-12-09 13:11:38 -05:00
|
|
|
return
|
|
|
|
}
|
2023-12-22 08:52:10 -05:00
|
|
|
log.Info("RepositoryInbox: objectId validated: %v", objectID)
|
2023-12-06 07:36:26 -05:00
|
|
|
|
2023-12-22 08:52:10 -05:00
|
|
|
actorAsLoginID := actorID.AsLoginName() // used as LoginName in newly created user
|
|
|
|
log.Info("RepositoryInbox: remoteStargazer: %v", actorAsLoginID)
|
2023-11-15 06:29:17 -05:00
|
|
|
|
2023-12-01 09:07:13 -05:00
|
|
|
// Check if user already exists
|
2024-02-08 05:09:55 -05:00
|
|
|
user, _, err := user_model.FindFederatedUser(ctx, actorID.ID, federationHost.ID)
|
2023-12-05 04:37:51 -05:00
|
|
|
if err != nil {
|
2024-01-13 08:17:11 -05:00
|
|
|
ctx.Error(http.StatusInternalServerError, "RepositoryInbox: Searching for user failed", err)
|
2023-12-12 05:08:57 -05:00
|
|
|
return
|
2023-12-05 04:37:51 -05:00
|
|
|
}
|
2024-02-08 05:09:55 -05:00
|
|
|
if user != nil {
|
|
|
|
log.Info("RepositoryInbox: found user: %v", user)
|
|
|
|
} else {
|
|
|
|
user, err = createUserFromAP(ctx, actorID, federationHost.ID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError,
|
|
|
|
"RepositoryInbox: Creating federated user failed", err)
|
2023-12-13 10:49:23 -05:00
|
|
|
return
|
2023-12-05 04:37:51 -05:00
|
|
|
}
|
2024-02-08 05:09:55 -05:00
|
|
|
log.Info("RepositoryInbox: created user from ap: %v", user)
|
2023-12-07 07:21:26 -05:00
|
|
|
}
|
2023-12-06 09:56:26 -05:00
|
|
|
|
2023-12-15 08:45:20 -05:00
|
|
|
// execute the activity if the repo was not stared already
|
|
|
|
alreadyStared := repo_model.IsStaring(ctx, user.ID, repository.ID)
|
|
|
|
if !alreadyStared {
|
|
|
|
err = repo_model.StarRepo(ctx, user.ID, repository.ID, true)
|
|
|
|
if err != nil {
|
2024-01-13 08:17:11 -05:00
|
|
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Star operation", err)
|
2023-12-15 08:45:20 -05:00
|
|
|
return
|
2023-12-07 07:24:01 -05:00
|
|
|
}
|
|
|
|
}
|
2024-02-07 11:11:43 -05:00
|
|
|
federationHost.LatestActivity = activity.StartTime
|
|
|
|
err = forgefed.UpdateFederationHost(ctx, *federationHost)
|
2024-01-13 08:17:11 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: error updateing federateionInfo", err)
|
|
|
|
return
|
|
|
|
}
|
2023-11-30 10:10:26 -05:00
|
|
|
|
2023-10-20 09:16:04 -04:00
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
2023-12-13 10:44:11 -05:00
|
|
|
|
2024-02-07 11:11:43 -05:00
|
|
|
func createFederationHost(ctx *context.APIContext, actorID forgefed.ActorID) (forgefed.FederationHost, error) {
|
2023-12-22 09:00:42 -05:00
|
|
|
actionsUser := user_model.NewActionsUser()
|
2023-12-19 04:19:35 -05:00
|
|
|
client, err := api.NewClient(ctx, actionsUser, "no idea where to get key material.")
|
2023-12-13 10:44:11 -05:00
|
|
|
if err != nil {
|
2024-01-19 10:26:16 -05:00
|
|
|
return forgefed.FederationHost{}, err
|
2023-12-13 10:44:11 -05:00
|
|
|
}
|
2024-01-13 11:06:40 -05:00
|
|
|
body, err := client.GetBody(actorID.AsWellKnownNodeInfoURI())
|
2023-12-13 10:44:11 -05:00
|
|
|
if err != nil {
|
2024-01-19 10:26:16 -05:00
|
|
|
return forgefed.FederationHost{}, err
|
2023-12-29 09:48:45 -05:00
|
|
|
}
|
|
|
|
nodeInfoWellKnown, err := forgefed.NewNodeInfoWellKnown(body)
|
|
|
|
if err != nil {
|
2024-01-19 10:26:16 -05:00
|
|
|
return forgefed.FederationHost{}, err
|
2023-12-13 10:44:11 -05:00
|
|
|
}
|
2023-12-29 09:48:45 -05:00
|
|
|
body, err = client.GetBody(nodeInfoWellKnown.Href)
|
|
|
|
if err != nil {
|
2024-01-19 10:26:16 -05:00
|
|
|
return forgefed.FederationHost{}, err
|
2024-01-12 11:00:17 -05:00
|
|
|
}
|
|
|
|
nodeInfo, err := forgefed.NewNodeInfo(body)
|
|
|
|
if err != nil {
|
2024-01-19 10:26:16 -05:00
|
|
|
return forgefed.FederationHost{}, err
|
2024-01-12 11:00:17 -05:00
|
|
|
}
|
2024-01-19 10:26:16 -05:00
|
|
|
result, err := forgefed.NewFederationHost(nodeInfo, actorID.Host)
|
2024-01-13 10:08:12 -05:00
|
|
|
if err != nil {
|
2024-01-19 10:26:16 -05:00
|
|
|
return forgefed.FederationHost{}, err
|
2023-12-29 09:48:45 -05:00
|
|
|
}
|
2024-01-19 10:26:16 -05:00
|
|
|
err = forgefed.CreateFederationHost(ctx, result)
|
2024-01-12 11:27:52 -05:00
|
|
|
if err != nil {
|
2024-01-19 10:26:16 -05:00
|
|
|
return forgefed.FederationHost{}, err
|
2024-01-12 11:27:52 -05:00
|
|
|
}
|
2024-01-12 11:00:17 -05:00
|
|
|
return result, nil
|
2023-12-29 09:48:45 -05:00
|
|
|
}
|
2023-12-20 06:22:03 -05:00
|
|
|
|
2024-02-07 11:11:43 -05:00
|
|
|
func createUserFromAP(ctx *context.APIContext, personID forgefed.PersonID, federationHostID int64) (*user_model.User, error) {
|
2023-12-29 09:48:45 -05:00
|
|
|
// ToDo: Do we get a publicKeyId from server, repo or owner or repo?
|
|
|
|
actionsUser := user_model.NewActionsUser()
|
|
|
|
client, err := api.NewClient(ctx, actionsUser, "no idea where to get key material.")
|
|
|
|
if err != nil {
|
2024-02-07 11:11:43 -05:00
|
|
|
return nil, err
|
2023-12-20 06:22:03 -05:00
|
|
|
}
|
|
|
|
|
2023-12-29 09:48:45 -05:00
|
|
|
body, err := client.GetBody(personID.AsURI())
|
2023-12-13 10:44:11 -05:00
|
|
|
if err != nil {
|
2024-02-07 11:11:43 -05:00
|
|
|
return nil, err
|
2023-12-13 10:44:11 -05:00
|
|
|
}
|
2023-12-20 06:23:13 -05:00
|
|
|
|
2024-01-04 12:04:46 -05:00
|
|
|
person := forgefed.ForgePerson{}
|
2023-12-20 06:23:13 -05:00
|
|
|
err = person.UnmarshalJSON(body)
|
2023-12-13 10:44:11 -05:00
|
|
|
if err != nil {
|
2024-02-07 11:11:43 -05:00
|
|
|
return nil, err
|
2023-12-13 10:44:11 -05:00
|
|
|
}
|
2024-01-04 12:04:46 -05:00
|
|
|
if res, err := validation.IsValid(person); !res {
|
2024-02-07 11:11:43 -05:00
|
|
|
return nil, err
|
2024-01-04 12:04:46 -05:00
|
|
|
}
|
|
|
|
log.Info("RepositoryInbox: validated person: %q", person)
|
2024-01-03 12:10:24 -05:00
|
|
|
|
2024-02-07 11:11:43 -05:00
|
|
|
user, _, err := user_model.CreateFederatedUserFromAP(ctx, person, personID, federationHostID)
|
2024-02-07 08:59:39 -05:00
|
|
|
if err != nil {
|
2024-02-07 11:11:43 -05:00
|
|
|
return nil, err
|
2023-12-13 10:44:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|