Create new federated users in reqsignature.go

This commit is contained in:
Anthony Wang 2022-07-23 21:27:20 -05:00
parent 5da6b4fd84
commit ab540d07be
No known key found for this signature in database
GPG Key ID: BC96B00AEC5F2D76
5 changed files with 29 additions and 12 deletions

View File

@ -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":

View File

@ -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)
}

View File

@ -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 = ""

View File

@ -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

View File

@ -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