From ab540d07be377eed704398218beca4cb028b5790 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Sat, 23 Jul 2022 21:27:20 -0500 Subject: [PATCH] Create new federated users in reqsignature.go --- modules/activitypub/authorize_interaction.go | 10 ++++++++-- modules/activitypub/iri.go | 1 - modules/activitypub/user.go | 15 +++++++++++++-- routers/api/v1/activitypub/reqsignature.go | 9 ++++++++- routers/web/user/profile.go | 6 ------ 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/modules/activitypub/authorize_interaction.go b/modules/activitypub/authorize_interaction.go index 4b670e8223..9ee8c0f74a 100644 --- a/modules/activitypub/authorize_interaction.go +++ b/modules/activitypub/authorize_interaction.go @@ -36,11 +36,17 @@ func AuthorizeInteraction(c *context.Context) { case "Person": var person ap.Person person.UnmarshalJSON(resp) - err = FederatedUserNew(person) - if err != nil { + err = FederatedUserNew(c, person) + /*if err != nil { c.ServerError("Could not create new federated user", err) return + }*/ + name, err := personIRIToName(person.GetLink()) + if err != nil { + c.ServerError("personIRIToName", err) + return } + c.Redirect(name) /*case "organization": // Do something idk case "repository": diff --git a/modules/activitypub/iri.go b/modules/activitypub/iri.go index 70f80f0097..1ef12239ac 100644 --- a/modules/activitypub/iri.go +++ b/modules/activitypub/iri.go @@ -47,7 +47,6 @@ func personIRIToUser(ctx context.Context, personIRI ap.IRI) (*user_model.User, e return user, err } - //FederatedUserNew(personIRI) return user_model.GetUserByName(ctx, name) } diff --git a/modules/activitypub/user.go b/modules/activitypub/user.go index 66e5997145..5e1a8583ba 100644 --- a/modules/activitypub/user.go +++ b/modules/activitypub/user.go @@ -5,31 +5,42 @@ package activitypub import ( + "context" "strings" "code.gitea.io/gitea/models/auth" - "code.gitea.io/gitea/modules/setting" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/setting" ap "github.com/go-ap/activitypub" ) -func FederatedUserNew(person ap.Person) error { +func FederatedUserNew(ctx context.Context, person ap.Person) error { name, err := personIRIToName(person.GetLink()) if err != nil { return err } + exists, err := user_model.IsUserExist(ctx, 0, name) + if err != nil { + return err + } + if exists { + return nil + } + var email string if person.Location != nil { email = person.Location.GetLink().String() } else { + // This might not even work email = strings.ReplaceAll(name, "@", "+") + "@" + setting.Service.NoReplyAddress } var avatar string if person.Icon != nil { icon := person.Icon.(*ap.Image) + // Currently doesn't work avatar = icon.URL.GetLink().String() } else { avatar = "" diff --git a/routers/api/v1/activitypub/reqsignature.go b/routers/api/v1/activitypub/reqsignature.go index 79cd394587..f8a6360621 100644 --- a/routers/api/v1/activitypub/reqsignature.go +++ b/routers/api/v1/activitypub/reqsignature.go @@ -74,7 +74,14 @@ func verifyHTTPSignatures(ctx *gitea_context.APIContext) (authenticated bool, er // 3. Verify the other actor's key algo := httpsig.Algorithm(setting.Federation.Algorithms[0]) authenticated = v.Verify(pubKey, algo) == nil - return authenticated, err + if !authenticated { + return + } + // 4. Create a federated user for the actor + var person ap.Person + person.UnmarshalJSON(b) + err = activitypub.FederatedUserNew(ctx, person) + return } // ReqHTTPSignature function diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 792adadd03..6f23d239e2 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -36,12 +36,6 @@ func Profile(ctx *context.Context) { return } - if strings.Contains(ctx.ContextUser.Name, "@") { - ctx.Resp.Header().Add("Location", ctx.ContextUser.Website) - ctx.Resp.WriteHeader(http.StatusTemporaryRedirect) - return - } - if ctx.ContextUser.IsOrganization() { org.Home(ctx) return