1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-29 09:31:11 -05:00
forgejo/models/forgefed/actor.go

211 lines
5.5 KiB
Go
Raw Normal View History

2023-12-08 14:37:26 -05:00
// Copyright 2023 The forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2023-12-09 08:53:40 -05:00
package forgefed
import (
"fmt"
"net/url"
"strings"
2023-12-09 12:30:47 -05:00
"code.gitea.io/gitea/modules/setting"
2023-12-08 12:08:54 -05:00
"code.gitea.io/gitea/modules/validation"
)
2023-12-09 12:30:47 -05:00
type ActorId struct {
2023-12-22 05:48:24 -05:00
validation.Validateable
2023-12-09 12:30:47 -05:00
Id string
Source string
Schema string
Path string
Host string
Port string
UnvalidatedInput string
}
2023-12-08 13:43:49 -05:00
type PersonId struct {
2023-12-09 12:30:47 -05:00
ActorId
}
type RepositoryId struct {
ActorId
}
// newActorId receives already validated inputs
func newActorId(validatedUri *url.URL, source string) (ActorId, error) {
2023-12-09 12:30:47 -05:00
pathWithActorID := strings.Split(validatedUri.Path, "/")
if containsEmptyString(pathWithActorID) {
pathWithActorID = removeEmptyStrings(pathWithActorID)
}
length := len(pathWithActorID)
pathWithoutActorID := strings.Join(pathWithActorID[0:length-1], "/")
id := pathWithActorID[length-1]
2023-12-21 08:22:23 -05:00
result := ActorId{}
2023-12-09 12:30:47 -05:00
result.Id = id
result.Source = source
result.Schema = validatedUri.Scheme
result.Host = validatedUri.Hostname()
result.Path = pathWithoutActorID
result.Port = validatedUri.Port()
result.UnvalidatedInput = validatedUri.String()
2023-12-21 08:22:23 -05:00
2023-12-09 12:30:47 -05:00
if valid, err := result.IsValid(); !valid {
2023-12-21 08:22:23 -05:00
return ActorId{}, err
2023-12-09 12:30:47 -05:00
}
return result, nil
}
2023-12-08 14:37:26 -05:00
2023-12-21 08:22:23 -05:00
func NewPersonId(uri string, source string) (PersonId, error) {
// TODO: remove after test
//if !validation.IsValidExternalURL(uri) {
// return PersonId{}, fmt.Errorf("uri %s is not a valid external url", uri)
//}
validatedUri, err := url.ParseRequestURI(uri) // ToDo: Why no err treatment at this place?
if err != nil {
return PersonId{}, err
}
2023-12-21 08:22:23 -05:00
actorId, err := newActorId(validatedUri, source)
2023-12-21 08:22:23 -05:00
if err != nil {
return PersonId{}, err
}
// validate Person specific path
personId := PersonId{actorId}
if valid, outcome := personId.IsValid(); !valid {
return PersonId{}, outcome
}
return personId, nil
}
2023-12-09 12:30:47 -05:00
// TODO: tbd how an which parts can be generalized
func NewRepositoryId(uri string, source string) (RepositoryId, error) {
if !validation.IsAPIURL(uri) {
return RepositoryId{}, fmt.Errorf("uri %s is not a valid repo url on this host %s", uri, setting.AppURL+"api")
2023-12-08 14:37:26 -05:00
}
validatedUri, err := url.ParseRequestURI(uri) // ToDo: Why no err treatment at this place?
if err != nil {
return RepositoryId{}, err
}
actorId, err := newActorId(validatedUri, source)
2023-12-21 08:22:23 -05:00
if err != nil {
return RepositoryId{}, err
2023-12-08 14:37:26 -05:00
}
2023-12-09 12:30:47 -05:00
2023-12-21 08:22:23 -05:00
// validate Person specific path
repoId := RepositoryId{actorId}
if valid, outcome := repoId.IsValid(); !valid {
return RepositoryId{}, outcome
2023-12-08 14:37:26 -05:00
}
2023-12-21 08:22:23 -05:00
return repoId, nil
2023-12-08 14:37:26 -05:00
}
2023-12-09 12:30:47 -05:00
func (id ActorId) AsUri() string {
2023-12-08 14:37:26 -05:00
result := ""
2023-12-09 12:30:47 -05:00
if id.Port == "" {
result = fmt.Sprintf("%s://%s/%s/%s", id.Schema, id.Host, id.Path, id.Id)
2023-12-08 14:37:26 -05:00
} else {
2023-12-09 12:30:47 -05:00
result = fmt.Sprintf("%s://%s:%s/%s/%s", id.Schema, id.Host, id.Port, id.Path, id.Id)
}
2023-12-08 14:37:26 -05:00
return result
}
2023-12-15 08:45:20 -05:00
func (id PersonId) AsWebfinger() string {
2023-12-09 12:30:47 -05:00
result := fmt.Sprintf("@%s@%s", strings.ToLower(id.Id), strings.ToLower(id.Host))
2023-12-08 14:37:26 -05:00
return result
}
2023-12-15 08:45:20 -05:00
func (id PersonId) AsLoginName() string {
result := fmt.Sprintf("%s%s", strings.ToLower(id.Id), id.HostSuffix())
return result
}
func (id PersonId) HostSuffix() string {
result := fmt.Sprintf("-%s", strings.ToLower(id.Host))
return result
}
2023-12-22 05:48:24 -05:00
// Validate collects error strings in a slice and returns this
2023-12-21 08:22:23 -05:00
func (value ActorId) Validate() []string {
2023-12-08 13:41:22 -05:00
var result = []string{}
2023-12-09 12:30:47 -05:00
result = append(result, validation.ValidateNotEmpty(value.Id, "userId")...)
result = append(result, validation.ValidateNotEmpty(value.Source, "source")...)
result = append(result, validation.ValidateNotEmpty(value.Schema, "schema")...)
result = append(result, validation.ValidateNotEmpty(value.Path, "path")...)
result = append(result, validation.ValidateNotEmpty(value.Host, "host")...)
result = append(result, validation.ValidateNotEmpty(value.UnvalidatedInput, "unvalidatedInput")...)
result = append(result, validation.ValidateOneOf(value.Source, []string{"forgejo", "gitea"})...)
if value.UnvalidatedInput != value.AsUri() {
result = append(result, fmt.Sprintf("not all input: %q was parsed: %q", value.UnvalidatedInput, value.AsUri()))
2023-12-08 14:37:26 -05:00
}
2023-12-08 13:41:22 -05:00
return result
}
2023-12-22 05:48:24 -05:00
func (value PersonId) Validate() []string {
var result = value.ActorId.Validate()
switch value.Source {
2023-12-21 08:22:23 -05:00
case "forgejo", "gitea":
2023-12-22 05:48:24 -05:00
if strings.ToLower(value.Path) != "api/v1/activitypub/user-id" && strings.ToLower(value.Path) != "api/activitypub/user-id" {
result = append(result, fmt.Sprintf("path: %q has to be an api path", value.Path))
2023-12-21 08:22:23 -05:00
}
}
2023-12-22 05:48:24 -05:00
return result
}
2023-12-22 05:48:24 -05:00
func (value RepositoryId) Validate() []string {
var result = value.ActorId.Validate()
switch value.Source {
2023-12-21 08:22:23 -05:00
case "forgejo", "gitea":
2023-12-22 05:48:24 -05:00
if strings.ToLower(value.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(value.Path) != "api/activitypub/repository-id" {
result = append(result, fmt.Sprintf("path: %q has to be an api path", value.Path))
2023-12-21 08:22:23 -05:00
}
}
2023-12-22 05:48:24 -05:00
return result
}
2023-12-06 07:06:30 -05:00
func containsEmptyString(ar []string) bool {
for _, elem := range ar {
if elem == "" {
return true
}
}
return false
}
func removeEmptyStrings(ls []string) []string {
var rs []string
for _, str := range ls {
if str != "" {
rs = append(rs, str)
}
}
return rs
}
2023-12-22 05:48:24 -05:00
func (a RepositoryId) IsValid() (bool, error) {
if err := a.Validate(); len(err) > 0 {
errString := strings.Join(err, "\n")
return false, fmt.Errorf(errString)
}
return true, nil
}
func (a PersonId) IsValid() (bool, error) {
if err := a.Validate(); len(err) > 0 {
errString := strings.Join(err, "\n")
return false, fmt.Errorf(errString)
}
return true, nil
}