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": case "Person":
var person ap.Person var person ap.Person
person.UnmarshalJSON(resp) person.UnmarshalJSON(resp)
err = FederatedUserNew(person) err = FederatedUserNew(c, person)
if err != nil { /*if err != nil {
c.ServerError("Could not create new federated user", err) c.ServerError("Could not create new federated user", err)
return return
}*/
name, err := personIRIToName(person.GetLink())
if err != nil {
c.ServerError("personIRIToName", err)
return
} }
c.Redirect(name)
/*case "organization": /*case "organization":
// Do something idk // Do something idk
case "repository": case "repository":

View File

@ -47,7 +47,6 @@ func personIRIToUser(ctx context.Context, personIRI ap.IRI) (*user_model.User, e
return user, err return user, err
} }
//FederatedUserNew(personIRI)
return user_model.GetUserByName(ctx, name) return user_model.GetUserByName(ctx, name)
} }

View File

@ -5,31 +5,42 @@
package activitypub package activitypub
import ( import (
"context"
"strings" "strings"
"code.gitea.io/gitea/models/auth" "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/modules/setting"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
ap "github.com/go-ap/activitypub" 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()) name, err := personIRIToName(person.GetLink())
if err != nil { if err != nil {
return err return err
} }
exists, err := user_model.IsUserExist(ctx, 0, name)
if err != nil {
return err
}
if exists {
return nil
}
var email string var email string
if person.Location != nil { if person.Location != nil {
email = person.Location.GetLink().String() email = person.Location.GetLink().String()
} else { } else {
// This might not even work
email = strings.ReplaceAll(name, "@", "+") + "@" + setting.Service.NoReplyAddress email = strings.ReplaceAll(name, "@", "+") + "@" + setting.Service.NoReplyAddress
} }
var avatar string var avatar string
if person.Icon != nil { if person.Icon != nil {
icon := person.Icon.(*ap.Image) icon := person.Icon.(*ap.Image)
// Currently doesn't work
avatar = icon.URL.GetLink().String() avatar = icon.URL.GetLink().String()
} else { } else {
avatar = "" avatar = ""

View File

@ -74,7 +74,14 @@ func verifyHTTPSignatures(ctx *gitea_context.APIContext) (authenticated bool, er
// 3. Verify the other actor's key // 3. Verify the other actor's key
algo := httpsig.Algorithm(setting.Federation.Algorithms[0]) algo := httpsig.Algorithm(setting.Federation.Algorithms[0])
authenticated = v.Verify(pubKey, algo) == nil 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 // ReqHTTPSignature function

View File

@ -36,12 +36,6 @@ func Profile(ctx *context.Context) {
return 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() { if ctx.ContextUser.IsOrganization() {
org.Home(ctx) org.Home(ctx)
return return