Merge branch 'addPortToFederationHost' of codeberg.org:meissa/forgejo into addPortToFederationHost

This commit is contained in:
Michael Jerger 2025-04-03 14:30:14 +02:00
commit 5f8fcf7a0d
4 changed files with 13 additions and 26 deletions

View file

@ -27,7 +27,7 @@ type FederationHost struct {
}
// Factory function for FederationHost. Created struct is asserted to be valid.
func NewFederationHost(hostFqdn string, nodeInfo NodeInfo, port string, schema string) (FederationHost, error) {
func NewFederationHost(hostFqdn string, nodeInfo NodeInfo, port, schema string) (FederationHost, error) {
result := FederationHost{
HostFqdn: strings.ToLower(hostFqdn),
NodeInfo: nodeInfo,

View file

@ -10,6 +10,7 @@ import (
"forgejo.org/models/migrations/base"
"forgejo.org/modules/forgefed"
"forgejo.org/modules/timeutil"
"xorm.io/xorm"
)

View file

@ -46,19 +46,18 @@ func (id ActorID) AsURI() string {
} else {
result = fmt.Sprintf("%s://%s:%s/%s/%s", id.HostSchema, id.Host, id.HostPort, id.Path, id.ID)
}
return result
return result
}
func (id ActorID) Validate() []string {
var result []string
result = append(result, validation.ValidateNotEmpty(id.ID, "userId")...)
result = append(result, validation.ValidateNotEmpty(id.HostSchema, "schema")...)
result = append(result, validation.ValidateNotEmpty(id.HostSchema, "HostSchema")...)
result = append(result, validation.ValidateNotEmpty(id.Path, "path")...)
result = append(result, validation.ValidateNotEmpty(id.Host, "host")...)
result = append(result, validation.ValidateNotEmpty(id.HostPort, "HostPort")...)
result = append(result, validation.ValidateNotEmpty(id.UnvalidatedInput, "unvalidatedInput")...)
//add or
if id.UnvalidatedInput != id.AsURI() {
result = append(result, fmt.Sprintf("not all input was parsed, \nUnvalidated Input:%q \nParsed URI: %q", id.UnvalidatedInput, id.AsURI()))
}
@ -106,7 +105,6 @@ func (id PersonID) Validate() []string {
result := id.ActorID.Validate()
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"}, "Source")...)
result = append(result, validation.ValidateOneOf(id.HostSchema, []any{"https", "http", "HTTPS", "HTTP"}, "Source")...)
switch id.Source {
case "forgejo", "gitea":
@ -114,16 +112,7 @@ func (id PersonID) Validate() []string {
result = append(result, fmt.Sprintf("path: %q has to be a person specific api path", id.Path))
}
}
/*
switch id.HostSchema {
case "HTTPS", "HTTP":
if strings.ToLower(id.HostSchema) == "https" {
result = append(result, fmt.Sprintf("-%s", strings.ToLower(id.HostSchema)))
} else if strings.ToLower(id.HostSchema) == "http" {
result = append(result, fmt.Sprintf("-%s", strings.ToLower(id.HostSchema)))
}
}
*/
return result
}
@ -144,7 +133,6 @@ func NewRepositoryID(uri, source string) (RepositoryID, error) {
// validate Person specific
repoID := RepositoryID{result}
if valid, err := validation.IsValid(repoID); !valid {
return RepositoryID{}, err
}
@ -185,7 +173,6 @@ func removeEmptyStrings(ls []string) []string {
func newActorID(uri string) (ActorID, error) {
validatedURI, err := url.ParseRequestURI(uri)
if err != nil {
return ActorID{}, err
}
@ -216,7 +203,7 @@ func newActorID(uri string) (ActorID, error) {
}
result.HostPort = validatedURI.Port()
result.UnvalidatedInput = uri
result.UnvalidatedInput = fmt.Sprintf("%s://%s:%s/%s/%s", result.HostSchema, result.Host, result.HostPort, result.Path, result.ID)
return result, nil
}

View file

@ -29,7 +29,7 @@ func TestNewPersonId(t *testing.T) {
if sut != expected {
t.Errorf("expected: %v\n but was: %v\n", expected, sut)
}
expected = PersonID{}
expected.ID = "1"
expected.Source = "forgejo"
@ -39,12 +39,12 @@ func TestNewPersonId(t *testing.T) {
expected.HostPort = "443"
expected.IsPortSupplemented = false
expected.UnvalidatedInput = "https://an.other.host:443/api/v1/activitypub/user-id/1"
sut, _ = NewPersonID("https://an.other.host:443/api/v1/activitypub/user-id/1", "forgejo")
if sut != expected {
t.Errorf("expected: %v\n but was: %v\n", expected, sut)
}
expected = PersonID{}
expected.ID = "1"
expected.Source = "forgejo"
@ -57,24 +57,23 @@ func TestNewPersonId(t *testing.T) {
sut, _ = NewPersonID("http://an.other.host:80/api/v1/activitypub/user-id/1", "forgejo")
if sut != expected {
t.Errorf("expected: %v\n but was: %v\n", expected, sut)
t.Errorf("expected: %v\n but was: %v\n", expected, sut)
}
/*
expected = PersonID{}
expected.ID = "1"
expected.Source = "forgejo"
expected.HostSchema = "HTTPS"
expected.HostSchema = "https"
expected.Path = "api/v1/activitypub/user-id"
expected.Host = "an.other.host"
expected.HostPort = "443"
expected.IsPortSupplemented = false
expected.UnvalidatedInput = "https://an.other.host:443/api/v1/activitypub/user-id/1"
sut, _ = NewPersonID("https://an.other.host:443/api/v1/activitypub/user-id/1", "forgejo")
sut, _ = NewPersonID("HTTPS://an.other.host:443/api/v1/activitypub/user-id/1", "forgejo")
if sut != expected {
t.Errorf("expected: %v\n but was: %v\n", expected, sut)
}
*/
}
func TestNewRepositoryId(t *testing.T) {