mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-11-22 09:54:24 +01:00
mv federated_user_creation to user package
This commit is contained in:
parent
e180467760
commit
edf7f61b83
|
@ -15,7 +15,7 @@ func init() {
|
||||||
db.RegisterModel(new(FederatedUser))
|
db.RegisterModel(new(FederatedUser))
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateFederationUser(ctx context.Context, user FederatedUser) error {
|
func CreateFederationUser(ctx context.Context, user *FederatedUser) error {
|
||||||
if res, err := validation.IsValid(user); !res {
|
if res, err := validation.IsValid(user); !res {
|
||||||
return fmt.Errorf("FederatedUser is not valid: %v", err)
|
return fmt.Errorf("FederatedUser is not valid: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,30 +4,25 @@
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/forgefed"
|
"code.gitea.io/gitea/models/forgefed"
|
||||||
"code.gitea.io/gitea/modules/context"
|
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
"code.gitea.io/gitea/modules/validation"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
pwd_gen "github.com/sethvargo/go-password/password"
|
pwd_gen "github.com/sethvargo/go-password/password"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateFederatedUserFromAP(ctx *context.APIContext, person forgefed.ForgePerson, personID forgefed.PersonID,
|
func CreateFederatedUserFromAP(ctx context.Context, person forgefed.ForgePerson,
|
||||||
federationHostID int64) (*User, error) {
|
personID forgefed.PersonID, federationHostID int64) (*User, *FederatedUser, error) {
|
||||||
if res, err := validation.IsValid(person); !res {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
log.Info("RepositoryInbox: validated person: %q", person)
|
|
||||||
|
|
||||||
localFqdn, err := url.ParseRequestURI(setting.AppURL)
|
localFqdn, err := url.ParseRequestURI(setting.AppURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
email := fmt.Sprintf("f%v@%v", uuid.New().String(), localFqdn.Hostname())
|
email := fmt.Sprintf("f%v@%v", uuid.New().String(), localFqdn.Hostname())
|
||||||
|
@ -41,10 +36,10 @@ func CreateFederatedUserFromAP(ctx *context.APIContext, person forgefed.ForgePer
|
||||||
|
|
||||||
password, err := pwd_gen.Generate(32, 10, 10, false, true)
|
password, err := pwd_gen.Generate(32, 10, 10, false, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
user := &User{
|
user := User{
|
||||||
LowerName: strings.ToLower(person.PreferredUsername.String()),
|
LowerName: strings.ToLower(person.PreferredUsername.String()),
|
||||||
Name: name,
|
Name: name,
|
||||||
FullName: fullName,
|
FullName: fullName,
|
||||||
|
@ -62,19 +57,20 @@ func CreateFederatedUserFromAP(ctx *context.APIContext, person forgefed.ForgePer
|
||||||
IsRestricted: util.OptionalBoolFalse,
|
IsRestricted: util.OptionalBoolFalse,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := CreateUser(ctx, user, overwrite); err != nil {
|
// TODO: Transaction around
|
||||||
return nil, err
|
if err := CreateUser(ctx, &user, overwrite); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
federatedUser, err := NewFederatedUser(user.ID, personID.ID, federationHostID)
|
federatedUser, err := NewFederatedUser(user.ID, personID.ID, federationHostID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = CreateFederationUser(ctx, federatedUser)
|
err = CreateFederationUser(ctx, &federatedUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return user, nil
|
return &user, &federatedUser, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ package activitypub
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
|
@ -20,10 +19,8 @@ import (
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
"code.gitea.io/gitea/modules/validation"
|
"code.gitea.io/gitea/modules/validation"
|
||||||
"code.gitea.io/gitea/modules/web"
|
"code.gitea.io/gitea/modules/web"
|
||||||
"github.com/google/uuid"
|
|
||||||
|
|
||||||
ap "github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"
|
||||||
pwd_gen "github.com/sethvargo/go-password/password"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Repository function returns the Repository actor for a repo
|
// Repository function returns the Repository actor for a repo
|
||||||
|
@ -97,28 +94,28 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
"RepositoryInbox: Validating ActorID", err)
|
"RepositoryInbox: Validating ActorID", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
federationInfo, err := forgefed.FindFederationHostByFqdn(ctx, rawActorID.Host)
|
federationHost, err := forgefed.FindFederationHostByFqdn(ctx, rawActorID.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError,
|
ctx.Error(http.StatusInternalServerError,
|
||||||
"RepositoryInbox: Error while loading FederationInfo", err)
|
"RepositoryInbox: Error while loading FederationInfo", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if federationInfo == nil {
|
if federationHost == nil {
|
||||||
result, err := createFederationInfo(ctx, rawActorID)
|
result, err := createFederationHost(ctx, rawActorID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate actorId", err)
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate actorId", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
federationInfo = &result
|
federationHost = &result
|
||||||
log.Info("RepositoryInbox: federationInfo validated: %v", federationInfo)
|
log.Info("RepositoryInbox: federationInfo validated: %v", federationHost)
|
||||||
}
|
}
|
||||||
if !activity.IsNewer(federationInfo.LatestActivity) {
|
if !activity.IsNewer(federationHost.LatestActivity) {
|
||||||
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate Activity",
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate Activity",
|
||||||
fmt.Errorf("Activity already processed"))
|
fmt.Errorf("Activity already processed"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
actorID, err := forgefed.NewPersonID(actorURI, string(federationInfo.NodeInfo.Source))
|
actorID, err := forgefed.NewPersonID(actorURI, string(federationHost.NodeInfo.Source))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate actorId", err)
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: Validate actorId", err)
|
||||||
return
|
return
|
||||||
|
@ -140,6 +137,8 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
log.Info("RepositoryInbox: remoteStargazer: %v", actorAsLoginID)
|
log.Info("RepositoryInbox: remoteStargazer: %v", actorAsLoginID)
|
||||||
|
|
||||||
// Check if user already exists
|
// Check if user already exists
|
||||||
|
// TODO: search for federation user instead
|
||||||
|
// users, _, err := SearchFederatedUser(actorID.ID, federationHost.ID)
|
||||||
users, err := SearchUsersByLoginName(actorAsLoginID)
|
users, err := SearchUsersByLoginName(actorAsLoginID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "RepositoryInbox: Searching for user failed", err)
|
ctx.Error(http.StatusInternalServerError, "RepositoryInbox: Searching for user failed", err)
|
||||||
|
@ -150,7 +149,7 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
switch len(users) {
|
switch len(users) {
|
||||||
case 0:
|
case 0:
|
||||||
{
|
{
|
||||||
user, err = createUserFromAP(ctx, actorID)
|
user, err = createUserFromAP(ctx, actorID, federationHost.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError,
|
ctx.Error(http.StatusInternalServerError,
|
||||||
"RepositoryInbox: Creating federated user failed", err)
|
"RepositoryInbox: Creating federated user failed", err)
|
||||||
|
@ -180,8 +179,8 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
federationInfo.LatestActivity = activity.StartTime
|
federationHost.LatestActivity = activity.StartTime
|
||||||
err = forgefed.UpdateFederationHost(ctx, *federationInfo)
|
err = forgefed.UpdateFederationHost(ctx, *federationHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: error updateing federateionInfo", err)
|
ctx.Error(http.StatusNotAcceptable, "RepositoryInbox: error updateing federateionInfo", err)
|
||||||
return
|
return
|
||||||
|
@ -212,7 +211,7 @@ func SearchUsersByLoginName(loginName string) ([]*user_model.User, error) {
|
||||||
return users, nil
|
return users, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createFederationInfo(ctx *context.APIContext, actorID forgefed.ActorID) (forgefed.FederationHost, error) {
|
func createFederationHost(ctx *context.APIContext, actorID forgefed.ActorID) (forgefed.FederationHost, error) {
|
||||||
actionsUser := user_model.NewActionsUser()
|
actionsUser := user_model.NewActionsUser()
|
||||||
client, err := api.NewClient(ctx, actionsUser, "no idea where to get key material.")
|
client, err := api.NewClient(ctx, actionsUser, "no idea where to get key material.")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -245,68 +244,32 @@ func createFederationInfo(ctx *context.APIContext, actorID forgefed.ActorID) (fo
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createUserFromAP(ctx *context.APIContext, personID forgefed.PersonID) (*user_model.User, error) {
|
func createUserFromAP(ctx *context.APIContext, personID forgefed.PersonID, federationHostID int64) (*user_model.User, error) {
|
||||||
// ToDo: Do we get a publicKeyId from server, repo or owner or repo?
|
// ToDo: Do we get a publicKeyId from server, repo or owner or repo?
|
||||||
actionsUser := user_model.NewActionsUser()
|
actionsUser := user_model.NewActionsUser()
|
||||||
client, err := api.NewClient(ctx, actionsUser, "no idea where to get key material.")
|
client, err := api.NewClient(ctx, actionsUser, "no idea where to get key material.")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &user_model.User{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := client.GetBody(personID.AsURI())
|
body, err := client.GetBody(personID.AsURI())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &user_model.User{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
person := forgefed.ForgePerson{}
|
person := forgefed.ForgePerson{}
|
||||||
err = person.UnmarshalJSON(body)
|
err = person.UnmarshalJSON(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &user_model.User{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if res, err := validation.IsValid(person); !res {
|
if res, err := validation.IsValid(person); !res {
|
||||||
return &user_model.User{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Info("RepositoryInbox: validated person: %q", person)
|
log.Info("RepositoryInbox: validated person: %q", person)
|
||||||
|
|
||||||
localFqdn, err := url.ParseRequestURI(setting.AppURL)
|
user, _, err := user_model.CreateFederatedUserFromAP(ctx, person, personID, federationHostID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &user_model.User{}, err
|
return nil, err
|
||||||
}
|
|
||||||
|
|
||||||
email := fmt.Sprintf("f%v@%v", uuid.New().String(), localFqdn.Hostname())
|
|
||||||
loginName := personID.AsLoginName()
|
|
||||||
name := fmt.Sprintf("%v%v", person.PreferredUsername.String(), personID.HostSuffix())
|
|
||||||
log.Info("RepositoryInbox: person.Name: %v", person.Name)
|
|
||||||
fullName := person.Name.String()
|
|
||||||
if len(person.Name) == 0 {
|
|
||||||
fullName = name
|
|
||||||
}
|
|
||||||
|
|
||||||
password, err := pwd_gen.Generate(32, 10, 10, false, true)
|
|
||||||
if err != nil {
|
|
||||||
return &user_model.User{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
user := &user_model.User{
|
|
||||||
LowerName: strings.ToLower(person.PreferredUsername.String()),
|
|
||||||
Name: name,
|
|
||||||
FullName: fullName,
|
|
||||||
Email: email,
|
|
||||||
EmailNotificationsPreference: "disabled",
|
|
||||||
Passwd: password,
|
|
||||||
MustChangePassword: false,
|
|
||||||
LoginName: loginName,
|
|
||||||
Type: user_model.UserTypeRemoteUser,
|
|
||||||
IsAdmin: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
overwrite := &user_model.CreateUserOverwriteOptions{
|
|
||||||
IsActive: util.OptionalBoolFalse,
|
|
||||||
IsRestricted: util.OptionalBoolFalse,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := user_model.CreateUser(ctx, user, overwrite); err != nil {
|
|
||||||
return &user_model.User{}, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
|
|
Loading…
Reference in a new issue