2022-06-19 15:39:22 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
2022-07-24 02:27:20 +00:00
|
|
|
"context"
|
2022-08-15 16:14:48 +00:00
|
|
|
"errors"
|
2022-07-23 21:34:06 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-06-19 15:39:22 +00:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-07-24 02:27:20 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-08-15 17:00:14 +00:00
|
|
|
user_service "code.gitea.io/gitea/services/user"
|
2022-06-19 15:39:22 +00:00
|
|
|
|
2022-07-11 22:27:57 +00:00
|
|
|
ap "github.com/go-ap/activitypub"
|
2022-06-19 15:39:22 +00:00
|
|
|
)
|
|
|
|
|
2022-07-27 19:43:01 +00:00
|
|
|
// Create a new federated user from a Person object
|
2022-08-15 16:14:48 +00:00
|
|
|
func FederatedUserNew(ctx context.Context, person *ap.Person) error {
|
2022-07-23 21:34:06 +00:00
|
|
|
name, err := personIRIToName(person.GetLink())
|
2022-07-14 03:10:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-24 02:27:20 +00:00
|
|
|
exists, err := user_model.IsUserExist(ctx, 0, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-23 21:34:06 +00:00
|
|
|
var email string
|
|
|
|
if person.Location != nil {
|
|
|
|
email = person.Location.GetLink().String()
|
|
|
|
} else {
|
2022-07-24 02:27:20 +00:00
|
|
|
// This might not even work
|
2022-07-23 21:34:06 +00:00
|
|
|
email = strings.ReplaceAll(name, "@", "+") + "@" + setting.Service.NoReplyAddress
|
|
|
|
}
|
|
|
|
|
2022-08-15 16:14:48 +00:00
|
|
|
if person.PublicKey.PublicKeyPem == "" {
|
|
|
|
return errors.New("person public key not found")
|
|
|
|
}
|
|
|
|
|
2022-06-19 15:39:22 +00:00
|
|
|
user := &user_model.User{
|
|
|
|
Name: name,
|
2022-07-25 20:43:20 +00:00
|
|
|
FullName: person.Name.String(), // May not exist!!
|
2022-07-23 21:34:06 +00:00
|
|
|
Email: email,
|
2022-06-19 15:39:22 +00:00
|
|
|
LoginType: auth.Federated,
|
2022-07-23 21:34:06 +00:00
|
|
|
LoginName: person.GetLink().String(),
|
2022-06-19 15:39:22 +00:00
|
|
|
}
|
2022-08-15 16:14:48 +00:00
|
|
|
err = user_model.CreateUser(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-15 17:00:14 +00:00
|
|
|
if person.Icon != nil {
|
|
|
|
icon := person.Icon.(*ap.Image)
|
|
|
|
iconURL, err := icon.URL.GetLink().URL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := Fetch(iconURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = user_service.UploadAvatar(user, body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 16:14:48 +00:00
|
|
|
err = user_model.SetUserSetting(user.ID, user_model.UserActivityPubPrivPem, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return user_model.SetUserSetting(user.ID, user_model.UserActivityPubPubPem, person.PublicKey.PublicKeyPem)
|
2022-06-19 15:39:22 +00:00
|
|
|
}
|