2023-10-27 14:13:51 -04:00
|
|
|
// Copyright 2023 The forgejo Authors. All rights reserved.
|
2023-10-20 09:16:04 -04:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package activitypub
|
|
|
|
|
2023-12-20 05:27:44 -05:00
|
|
|
// ToDo: Fix linting
|
|
|
|
// ToDo: Maybe do a request for the node info
|
|
|
|
// Then maybe save the node info in a DB table - this could be useful for validation
|
2023-10-20 09:16:04 -04:00
|
|
|
import (
|
2023-10-27 14:13:51 -04:00
|
|
|
"fmt"
|
2023-11-29 09:34:02 -05:00
|
|
|
"io"
|
2023-10-20 09:16:04 -04:00
|
|
|
"net/http"
|
2023-10-27 14:13:51 -04:00
|
|
|
"strings"
|
2023-12-07 07:54:07 -05:00
|
|
|
"time"
|
2023-10-20 09:16:04 -04:00
|
|
|
|
2023-12-01 11:06:39 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
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"
|
2023-12-05 04:37:51 -05:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-11-10 08:26:13 -05:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2023-12-21 08:21:47 -05:00
|
|
|
"code.gitea.io/gitea/routers/utils"
|
2023-12-05 04:37:51 -05:00
|
|
|
"github.com/google/uuid"
|
2023-10-27 14:13:51 -04:00
|
|
|
|
|
|
|
ap "github.com/go-ap/activitypub"
|
2023-12-05 05:38:36 -05:00
|
|
|
pwd_gen "github.com/sethvargo/go-password/password"
|
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 {
|
2023-11-06 12:29:48 -05:00
|
|
|
ctx.ServerError("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:
|
|
|
|
// "$ref": "#/definitions/Star"
|
2023-10-20 09:16:04 -04:00
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
|
2023-12-07 07:21:26 -05:00
|
|
|
var user *user_model.User
|
|
|
|
|
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
|
|
|
|
2023-12-01 11:06:39 -05:00
|
|
|
activity := web.GetForm(ctx).(*forgefed.Star)
|
2023-12-15 08:45:20 -05:00
|
|
|
log.Info("RepositoryInbox: activity:%v", activity)
|
2023-11-03 12:45:53 -04:00
|
|
|
|
2023-12-22 08:52:10 -05:00
|
|
|
// parse actorID (person)
|
2023-12-29 06:10:07 -05:00
|
|
|
// rawActorID, err := forgefed.NewActorID(activity.Actor.GetID().String())
|
|
|
|
|
|
|
|
// nodeInfo, err := createNodeInfo(rawActorID)
|
|
|
|
|
2023-12-22 08:52:10 -05:00
|
|
|
actorID, err := forgefed.NewPersonID(activity.Actor.GetID().String(), string(activity.Source))
|
2023-12-07 05:45:24 -05:00
|
|
|
if err != nil {
|
2023-12-08 13:52:09 -05:00
|
|
|
ctx.ServerError("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)
|
2023-12-22 09:00:42 -05:00
|
|
|
objectID, err := forgefed.NewRepositoryID(activity.Object.GetID().String(), string(activity.Source))
|
2023-12-09 13:11:38 -05:00
|
|
|
if err != nil {
|
2023-12-12 05:04:03 -05:00
|
|
|
ctx.ServerError("Validate objectId", err)
|
|
|
|
return
|
|
|
|
}
|
2023-12-22 09:10:21 -05:00
|
|
|
if objectID.ID != fmt.Sprint(repository.ID) {
|
2023-12-12 05:04:03 -05:00
|
|
|
ctx.ServerError("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
|
2023-12-22 08:52:10 -05:00
|
|
|
users, err := SearchUsersByLoginName(actorAsLoginID)
|
2023-12-05 04:37:51 -05:00
|
|
|
if err != nil {
|
2023-12-15 10:02:50 -05:00
|
|
|
ctx.ServerError("Searching for user failed", err)
|
2023-12-12 05:08:57 -05:00
|
|
|
return
|
2023-12-05 04:37:51 -05:00
|
|
|
}
|
2023-12-15 08:45:20 -05:00
|
|
|
log.Info("RepositoryInbox: local found users: %v", len(users))
|
2023-12-06 03:07:09 -05:00
|
|
|
|
2023-12-07 07:21:26 -05:00
|
|
|
switch len(users) {
|
|
|
|
case 0:
|
|
|
|
{
|
2023-12-22 08:52:10 -05:00
|
|
|
user, err = createUserFromAP(ctx, actorID)
|
2023-12-07 07:21:26 -05:00
|
|
|
if err != nil {
|
2023-12-19 06:26:55 -05:00
|
|
|
ctx.ServerError("Creating user failed", err)
|
2023-12-13 10:44:11 -05:00
|
|
|
return
|
2023-12-07 07:21:26 -05:00
|
|
|
}
|
2023-12-15 08:45:20 -05:00
|
|
|
log.Info("RepositoryInbox: created user from ap: %v", user)
|
2023-12-07 04:44:35 -05:00
|
|
|
}
|
2023-12-07 07:21:26 -05:00
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
user = users[0]
|
2023-12-15 08:45:20 -05:00
|
|
|
log.Info("RepositoryInbox: found user: %v", user)
|
2023-12-06 09:37:58 -05:00
|
|
|
}
|
2023-12-07 07:21:26 -05:00
|
|
|
default:
|
|
|
|
{
|
2023-12-15 08:45:20 -05:00
|
|
|
ctx.Error(http.StatusInternalServerError, "StarRepo",
|
|
|
|
fmt.Errorf("found more than one matches for federated users"))
|
2023-12-13 10:49:23 -05:00
|
|
|
return
|
2023-12-05 04:37:51 -05:00
|
|
|
}
|
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 {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "StarRepo", err)
|
|
|
|
return
|
2023-12-07 07:24:01 -05:00
|
|
|
}
|
|
|
|
}
|
2023-11-30 10:10:26 -05:00
|
|
|
|
2023-12-07 07:54:07 -05:00
|
|
|
// wait 5 sec.
|
|
|
|
time.Sleep(5 * time.Second)
|
2023-11-10 08:10:23 -05:00
|
|
|
|
2023-10-20 09:16:04 -04:00
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
2023-12-13 10:44:11 -05:00
|
|
|
|
2023-12-15 08:45:20 -05:00
|
|
|
// TODO: Move this to model.user.search ? or to model.user.externalLoginUser ?
|
|
|
|
func SearchUsersByLoginName(loginName string) ([]*user_model.User, error) {
|
2023-12-22 09:00:42 -05:00
|
|
|
actionsUser := user_model.NewActionsUser()
|
2023-12-15 08:45:20 -05:00
|
|
|
actionsUser.IsAdmin = true
|
|
|
|
|
|
|
|
options := &user_model.SearchUserOptions{
|
|
|
|
LoginName: loginName,
|
|
|
|
Actor: actionsUser,
|
|
|
|
Type: user_model.UserTypeRemoteUser,
|
|
|
|
OrderBy: db.SearchOrderByAlphabetically,
|
|
|
|
ListOptions: db.ListOptions{PageSize: 1},
|
|
|
|
IsActive: util.OptionalBoolFalse,
|
|
|
|
IncludeReserved: true,
|
|
|
|
}
|
|
|
|
users, _, err := user_model.SearchUsers(db.DefaultContext, options)
|
|
|
|
if err != nil {
|
|
|
|
return []*user_model.User{}, fmt.Errorf("search failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2023-12-20 05:27:44 -05:00
|
|
|
// ToDo: Maybe use externalLoginUser
|
2023-12-22 08:52:10 -05:00
|
|
|
func createUserFromAP(ctx *context.APIContext, personID forgefed.PersonID) (*user_model.User, error) {
|
2023-12-13 10:44:11 -05:00
|
|
|
// ToDo: Do we get a publicKeyId from server, repo or owner or repo?
|
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 {
|
|
|
|
return &user_model.User{}, err
|
|
|
|
}
|
2023-12-20 06:22:03 -05:00
|
|
|
|
2023-12-22 08:52:10 -05:00
|
|
|
response, err := client.Get(personID.AsURI())
|
2023-12-13 10:44:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return &user_model.User{}, err
|
|
|
|
}
|
2023-12-20 06:23:13 -05:00
|
|
|
log.Info("RepositoryInbox: got status: %v", response.Status)
|
2023-12-20 06:22:03 -05:00
|
|
|
|
|
|
|
// validate response; ToDo: Should we widen the restrictions here?
|
|
|
|
if response.StatusCode != 200 {
|
2023-12-22 09:10:21 -05:00
|
|
|
err = fmt.Errorf("got non 200 status code for id: %v", personID.ID)
|
2023-12-20 06:22:03 -05:00
|
|
|
return &user_model.User{}, err
|
|
|
|
}
|
|
|
|
|
2023-12-13 10:44:11 -05:00
|
|
|
defer response.Body.Close()
|
|
|
|
body, err := io.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return &user_model.User{}, err
|
|
|
|
}
|
2023-12-20 06:30:22 -05:00
|
|
|
log.Info("RepositoryInbox: got body: %v", utils.CharLimiter(string(body), 120))
|
2023-12-20 06:23:13 -05:00
|
|
|
|
2023-12-13 10:44:11 -05:00
|
|
|
person := ap.Person{}
|
2023-12-20 06:23:13 -05:00
|
|
|
err = person.UnmarshalJSON(body)
|
2023-12-13 10:44:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return &user_model.User{}, err
|
|
|
|
}
|
2023-12-15 08:45:20 -05:00
|
|
|
log.Info("RepositoryInbox: got person by ap: %v", person)
|
2023-12-21 03:30:07 -05:00
|
|
|
|
2023-12-22 08:52:10 -05:00
|
|
|
email := fmt.Sprintf("%v@%v", uuid.New().String(), personID.Host)
|
|
|
|
loginName := personID.AsLoginName()
|
|
|
|
name := fmt.Sprintf("%v%v", person.PreferredUsername.String(), personID.HostSuffix())
|
2023-12-15 08:45:20 -05:00
|
|
|
log.Info("RepositoryInbox: person.Name: %v", person.Name)
|
|
|
|
fullName := person.Name.String()
|
|
|
|
if len(person.Name) == 0 {
|
|
|
|
fullName = name
|
|
|
|
}
|
2023-12-21 03:30:07 -05:00
|
|
|
|
2023-12-13 10:44:11 -05:00
|
|
|
password, err := pwd_gen.Generate(32, 10, 10, false, true)
|
|
|
|
if err != nil {
|
|
|
|
return &user_model.User{}, err
|
|
|
|
}
|
2023-12-21 03:30:07 -05:00
|
|
|
|
2023-12-13 10:44:11 -05:00
|
|
|
user := &user_model.User{
|
2023-12-15 08:45:20 -05:00
|
|
|
LowerName: strings.ToLower(person.PreferredUsername.String()),
|
|
|
|
Name: name,
|
|
|
|
FullName: fullName,
|
2023-12-13 10:44:11 -05:00
|
|
|
Email: email,
|
|
|
|
EmailNotificationsPreference: "disabled",
|
|
|
|
Passwd: password,
|
|
|
|
MustChangePassword: false,
|
|
|
|
LoginName: loginName,
|
|
|
|
Type: user_model.UserTypeRemoteUser,
|
|
|
|
IsAdmin: false,
|
|
|
|
}
|
2023-12-21 03:30:07 -05:00
|
|
|
|
2023-12-13 10:44:11 -05:00
|
|
|
overwrite := &user_model.CreateUserOverwriteOptions{
|
|
|
|
IsActive: util.OptionalBoolFalse,
|
|
|
|
IsRestricted: util.OptionalBoolFalse,
|
|
|
|
}
|
2023-12-21 03:30:07 -05:00
|
|
|
|
2023-12-13 10:44:11 -05:00
|
|
|
if err := user_model.CreateUser(ctx, user, overwrite); err != nil {
|
|
|
|
return &user_model.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|