mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-31 14:36:47 +00:00
add port and schema to federation host (#7203)
## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [ ] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Co-authored-by: Michael Jerger <michael.jerger@meissa-gmbh.de> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7203 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: zam <mirco.zachmann@meissa.de> Co-committed-by: zam <mirco.zachmann@meissa.de>
This commit is contained in:
parent
23360ad415
commit
f6a5b783d2
20 changed files with 411 additions and 147 deletions
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2023, 2024 The Forgejo Authors. All rights reserved.
|
||||
// Copyright 2023, 2024, 2025 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package forgefed
|
||||
|
@ -6,6 +6,7 @@ package forgefed
|
|||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"forgejo.org/modules/validation"
|
||||
|
@ -15,13 +16,14 @@ import (
|
|||
|
||||
// ----------------------------- ActorID --------------------------------------------
|
||||
type ActorID struct {
|
||||
ID string
|
||||
Source string
|
||||
Schema string
|
||||
Path string
|
||||
Host string
|
||||
Port string
|
||||
UnvalidatedInput string
|
||||
ID string
|
||||
Source string
|
||||
HostSchema string
|
||||
Path string
|
||||
Host string
|
||||
HostPort uint16
|
||||
UnvalidatedInput string
|
||||
IsPortSupplemented bool
|
||||
}
|
||||
|
||||
// Factory function for ActorID. Created struct is asserted to be valid
|
||||
|
@ -40,20 +42,23 @@ func NewActorID(uri string) (ActorID, error) {
|
|||
|
||||
func (id ActorID) AsURI() string {
|
||||
var result string
|
||||
if id.Port == "" {
|
||||
result = fmt.Sprintf("%s://%s/%s/%s", id.Schema, id.Host, id.Path, id.ID)
|
||||
|
||||
if id.IsPortSupplemented {
|
||||
result = fmt.Sprintf("%s://%s/%s/%s", id.HostSchema, id.Host, id.Path, id.ID)
|
||||
} else {
|
||||
result = fmt.Sprintf("%s://%s:%s/%s/%s", id.Schema, id.Host, id.Port, id.Path, id.ID)
|
||||
result = fmt.Sprintf("%s://%s:%d/%s/%s", id.HostSchema, id.Host, id.HostPort, id.Path, id.ID)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (id ActorID) Validate() []string {
|
||||
var result []string
|
||||
result = append(result, validation.ValidateNotEmpty(id.ID, "userId")...)
|
||||
result = append(result, validation.ValidateNotEmpty(id.Schema, "schema")...)
|
||||
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.HostSchema, "hostSchema")...)
|
||||
result = append(result, validation.ValidateNotEmpty(id.UnvalidatedInput, "unvalidatedInput")...)
|
||||
|
||||
if id.UnvalidatedInput != id.AsURI() {
|
||||
|
@ -104,12 +109,14 @@ 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")...)
|
||||
|
||||
switch id.Source {
|
||||
case "forgejo", "gitea":
|
||||
if strings.ToLower(id.Path) != "api/v1/activitypub/user-id" && strings.ToLower(id.Path) != "api/activitypub/user-id" {
|
||||
result = append(result, fmt.Sprintf("path: %q has to be a person specific api path", id.Path))
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
@ -168,6 +175,8 @@ func removeEmptyStrings(ls []string) []string {
|
|||
return rs
|
||||
}
|
||||
|
||||
// ----------------------------- newActorID --------------------------------------------
|
||||
|
||||
func newActorID(uri string) (ActorID, error) {
|
||||
validatedURI, err := url.ParseRequestURI(uri)
|
||||
if err != nil {
|
||||
|
@ -179,15 +188,27 @@ func newActorID(uri string) (ActorID, error) {
|
|||
}
|
||||
length := len(pathWithActorID)
|
||||
pathWithoutActorID := strings.Join(pathWithActorID[0:length-1], "/")
|
||||
id := pathWithActorID[length-1]
|
||||
id := strings.ToLower(pathWithActorID[length-1])
|
||||
|
||||
result := ActorID{}
|
||||
result.ID = id
|
||||
result.Schema = validatedURI.Scheme
|
||||
result.Host = validatedURI.Hostname()
|
||||
result.Path = pathWithoutActorID
|
||||
result.Port = validatedURI.Port()
|
||||
result.UnvalidatedInput = uri
|
||||
result.HostSchema = strings.ToLower(validatedURI.Scheme)
|
||||
result.Host = strings.ToLower(validatedURI.Hostname())
|
||||
result.Path = strings.ToLower(pathWithoutActorID)
|
||||
|
||||
if validatedURI.Port() == "" && result.HostSchema == "https" {
|
||||
result.IsPortSupplemented = true
|
||||
result.HostPort = 443
|
||||
} else if validatedURI.Port() == "" && result.HostSchema == "http" {
|
||||
result.IsPortSupplemented = true
|
||||
result.HostPort = 80
|
||||
} else {
|
||||
numPort, _ := strconv.ParseUint(validatedURI.Port(), 10, 16)
|
||||
result.HostPort = uint16(numPort)
|
||||
}
|
||||
|
||||
result.UnvalidatedInput = strings.ToLower(uri)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue