Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-25 11:16:11 +01:00

integrate federation info in api call

This commit is contained in:
Michael Jerger 2024-01-12 17:00:17 +01:00
parent 52400f7978
commit 380d3db0bf
3 changed files with 32 additions and 13 deletions

View file

@ -24,7 +24,6 @@ func (info FederationInfo) Validate() []string {
var result []string var result []string
result = append(result, validation.ValidateNotEmpty(string(info.HostFqdn), "HostFqdn")...) result = append(result, validation.ValidateNotEmpty(string(info.HostFqdn), "HostFqdn")...)
result = append(result, validation.ValidateMaxLen(string(info.HostFqdn), 255, "HostFqdn")...) result = append(result, validation.ValidateMaxLen(string(info.HostFqdn), 255, "HostFqdn")...)
result = append(result, validation.ValidateNotEmpty(info.LatestActivity, "LatestActivity")...)
result = append(result, info.NodeInfo.Validate()...) result = append(result, info.NodeInfo.Validate()...)
return result return result

View file

@ -29,13 +29,13 @@ func GetFederationInfo(ctx context.Context, ID int64) (*FederationInfo, error) {
return info, nil return info, nil
} }
func GetFederationInfoByHostFqdn(ctx context.Context, fqdn string) (*FederationInfo, error) { func FindFederationInfoByHostFqdn(ctx context.Context, fqdn string) (*FederationInfo, error) {
info := new(FederationInfo) info := new(FederationInfo)
has, err := db.GetEngine(ctx).Where("host_fqdn=?", fqdn).Get(info) has, err := db.GetEngine(ctx).Where("host_fqdn=?", fqdn).Get(info)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, fmt.Errorf("FederationInfo record %v does not exist", fqdn) return nil, nil
} }
if res, err := validation.IsValid(info); !res { if res, err := validation.IsValid(info); !res {
return nil, fmt.Errorf("FederationInfo is not valid: %v", err) return nil, fmt.Errorf("FederationInfo is not valid: %v", err)

View file

@ -94,17 +94,29 @@ func RepositoryInbox(ctx *context.APIContext) {
// parse actorID (person) // parse actorID (person)
actorUri := activity.Actor.GetID().String() actorUri := activity.Actor.GetID().String()
rawActorID, err := forgefed.NewActorID(actorUri) rawActorID, err := forgefed.NewActorID(actorUri)
nodeInfo, err := createNodeInfo(ctx, rawActorID) federationInfo, err := forgefed.FindFederationInfoByHostFqdn(ctx, rawActorID.Host)
log.Info("RepositoryInbox: nodeInfo validated: %v", nodeInfo) if err != nil {
ctx.ServerError("Error while loading FederationInfo: %v", err)
return
}
if federationInfo == nil {
result, err := createFederationInfo(ctx, rawActorID)
if err != nil {
ctx.ServerError("Validate actorId", err)
return
}
federationInfo = &result
log.Info("RepositoryInbox: nodeInfo validated: %v", federationInfo)
}
actorID, err := forgefed.NewPersonID(actorUri, string(nodeInfo.Source)) actorID, err := forgefed.NewPersonID(actorUri, string(federationInfo.NodeInfo.Source))
if err != nil { if err != nil {
ctx.ServerError("Validate actorId", err) ctx.ServerError("Validate actorId", err)
return return
} }
log.Info("RepositoryInbox: actorId validated: %v", actorID) log.Info("RepositoryInbox: actorId validated: %v", actorID)
// parse objectID (repository) // parse objectID (repository)
objectID, err := forgefed.NewRepositoryID(activity.Object.GetID().String(), string(nodeInfo.Source)) objectID, err := forgefed.NewRepositoryID(activity.Object.GetID().String(), string(forgefed.ForgejoSourceType))
if err != nil { if err != nil {
ctx.ServerError("Validate objectId", err) ctx.ServerError("Validate objectId", err)
return return
@ -184,25 +196,33 @@ func SearchUsersByLoginName(loginName string) ([]*user_model.User, error) {
return users, nil return users, nil
} }
func createNodeInfo(ctx *context.APIContext, actorID forgefed.ActorID) (forgefed.NodeInfo, error) { func createFederationInfo(ctx *context.APIContext, actorID forgefed.ActorID) (forgefed.FederationInfo, 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 {
return forgefed.NodeInfo{}, err return forgefed.FederationInfo{}, err
} }
body, err := client.GetBody(actorID.AsWellKnownNodeInfoUri()) body, err := client.GetBody(actorID.AsWellKnownNodeInfoUri())
if err != nil { if err != nil {
return forgefed.NodeInfo{}, err return forgefed.FederationInfo{}, err
} }
nodeInfoWellKnown, err := forgefed.NewNodeInfoWellKnown(body) nodeInfoWellKnown, err := forgefed.NewNodeInfoWellKnown(body)
if err != nil { if err != nil {
return forgefed.NodeInfo{}, err return forgefed.FederationInfo{}, err
} }
body, err = client.GetBody(nodeInfoWellKnown.Href) body, err = client.GetBody(nodeInfoWellKnown.Href)
if err != nil { if err != nil {
return forgefed.NodeInfo{}, err return forgefed.FederationInfo{}, err
} }
return forgefed.NewNodeInfo(body) nodeInfo, err := forgefed.NewNodeInfo(body)
if err != nil {
return forgefed.FederationInfo{}, err
}
result := forgefed.FederationInfo{
HostFqdn: actorID.Host,
NodeInfo: nodeInfo,
}
return result, nil
} }
// ToDo: Maybe use externalLoginUser // ToDo: Maybe use externalLoginUser