From a20f535211ecf168ef7bc65d721515741e7ce3b3 Mon Sep 17 00:00:00 2001 From: Mirco Date: Tue, 5 Dec 2023 11:38:36 +0100 Subject: [PATCH] WIP: pwdgen, username --- go.mod | 1 + routers/api/v1/activitypub/repository.go | 42 +++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 73b9506e6c..240be0ed43 100644 --- a/go.mod +++ b/go.mod @@ -257,6 +257,7 @@ require ( github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/segmentio/asm v1.2.0 // indirect + github.com/sethvargo/go-password v0.2.0 // indirect github.com/shopspring/decimal v1.3.1 // indirect github.com/shurcooL/httpfs v0.0.0-20230704072500-f1e31cf0ba5c // indirect github.com/sirupsen/logrus v1.9.3 // indirect diff --git a/routers/api/v1/activitypub/repository.go b/routers/api/v1/activitypub/repository.go index fdcd5a9a81..a489ac0f6f 100644 --- a/routers/api/v1/activitypub/repository.go +++ b/routers/api/v1/activitypub/repository.go @@ -23,6 +23,7 @@ import ( user_model "code.gitea.io/gitea/models/user" ap "github.com/go-ap/activitypub" + pwd_gen "github.com/sethvargo/go-password/password" //f3 "lab.forgefriends.org/friendlyforgeformat/gof3" ) @@ -189,17 +190,29 @@ func RepositoryInbox(ctx *context.APIContext) { And depending on implementation check if the person already exists in federated user db. */ email, err := generateUUIDMail(person) - username := getUserName(person) + if err != nil { + fmt.Errorf("Generate user failed: %v", err) + } + + username, err := getUserName(person) + if err != nil { + fmt.Errorf("Generate user failed: %v", err) + } + + password, err := generateRandomPassword() + if err != nil { + fmt.Errorf("Generate password failed: %v", err) + } user := &user_model.User{ - LowerName: username.ToLower(), + LowerName: strings.ToLower(username), Name: username, Email: email, EmailNotificationsPreference: "disabled", - Passwd: generateRandomPassword(), + Passwd: password, MustChangePassword: false, LoginName: target, - Type: UserType.UserTypeRemoteUser, + Type: user_model.UserTypeRemoteUser, IsAdmin: false, } @@ -219,7 +232,6 @@ func RepositoryInbox(ctx *context.APIContext) { } // TODO: handle case of count > 1 - // execute star action // wait 15 sec. @@ -238,3 +250,23 @@ func generateUUIDMail(person ap.Actor) (string, error) { return strings.Join([]string{id, host}, "@"), err } + +func getUserName(person ap.Actor) (string, error) { + if name := person.PreferredUsername.String(); name != "" { + return name, nil + } + if name := person.Name.String(); name != "" { + return name, nil + } + return "", fmt.Errorf("Empty name, preferredUsername field") +} + +func generateRandomPassword() (string, error) { + // Generate a password that is 64 characters long with 10 digits, 10 symbols, + // allowing upper and lower case letters, disallowing repeat characters. + res, err := pwd_gen.Generate(32, 10, 10, false, false) + if err != nil { + return "", err + } + return res, err +}