forgejo/models/forgefed/actor.go

226 lines
5.8 KiB
Go
Raw Normal View History

2023-12-08 20:37:26 +01:00
// Copyright 2023 The forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2023-12-09 14:53:40 +01:00
package forgefed
import (
"fmt"
"net/url"
"strings"
2023-12-09 18:30:47 +01:00
"code.gitea.io/gitea/modules/setting"
2023-12-08 18:08:54 +01:00
"code.gitea.io/gitea/modules/validation"
)
2023-12-22 13:44:45 +01:00
type Validateables interface {
validation.Validateable
ActorId | PersonId | RepositoryId
}
2023-12-09 18:30:47 +01:00
type ActorId struct {
2023-12-22 11:48:24 +01:00
validation.Validateable
2023-12-09 18:30:47 +01:00
Id string
Source string
Schema string
Path string
Host string
Port string
UnvalidatedInput string
}
2023-12-08 19:43:49 +01:00
type PersonId struct {
2023-12-09 18:30:47 +01:00
ActorId
}
type RepositoryId struct {
ActorId
}
// newActorId receives already validated inputs
func newActorId(validatedUri *url.URL, source string) (ActorId, error) {
2023-12-09 18:30:47 +01: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 14:22:23 +01:00
result := ActorId{}
2023-12-09 18:30:47 +01: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 14:22:23 +01:00
2023-12-22 13:44:45 +01:00
if valid, err := IsValid(result); !valid {
2023-12-21 14:22:23 +01:00
return ActorId{}, err
2023-12-09 18:30:47 +01:00
}
return result, nil
}
2023-12-08 20:37:26 +01:00
2023-12-21 14:22:23 +01: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 14:22:23 +01:00
actorId, err := newActorId(validatedUri, source)
2023-12-21 14:22:23 +01:00
if err != nil {
return PersonId{}, err
}
// validate Person specific path
personId := PersonId{actorId}
2023-12-22 13:44:45 +01:00
if valid, outcome := IsValid(personId); !valid {
2023-12-21 14:22:23 +01:00
return PersonId{}, outcome
}
return personId, nil
}
2023-12-09 18:30:47 +01: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 20:37:26 +01: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 14:22:23 +01:00
if err != nil {
return RepositoryId{}, err
2023-12-08 20:37:26 +01:00
}
2023-12-09 18:30:47 +01:00
2023-12-21 14:22:23 +01:00
// validate Person specific path
repoId := RepositoryId{actorId}
2023-12-22 13:44:45 +01:00
if valid, outcome := IsValid(repoId); !valid {
2023-12-21 14:22:23 +01:00
return RepositoryId{}, outcome
2023-12-08 20:37:26 +01:00
}
2023-12-21 14:22:23 +01:00
return repoId, nil
2023-12-08 20:37:26 +01:00
}
2023-12-09 18:30:47 +01:00
func (id ActorId) AsUri() string {
2023-12-08 20:37:26 +01:00
result := ""
2023-12-09 18:30:47 +01:00
if id.Port == "" {
result = fmt.Sprintf("%s://%s/%s/%s", id.Schema, id.Host, id.Path, id.Id)
2023-12-08 20:37:26 +01:00
} else {
2023-12-09 18:30:47 +01:00
result = fmt.Sprintf("%s://%s:%s/%s/%s", id.Schema, id.Host, id.Port, id.Path, id.Id)
}
2023-12-08 20:37:26 +01:00
return result
}
2023-12-15 14:45:20 +01:00
func (id PersonId) AsWebfinger() string {
2023-12-09 18:30:47 +01:00
result := fmt.Sprintf("@%s@%s", strings.ToLower(id.Id), strings.ToLower(id.Host))
2023-12-08 20:37:26 +01:00
return result
}
2023-12-15 14:45:20 +01: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 11:48:24 +01:00
// Validate collects error strings in a slice and returns this
2023-12-21 14:22:23 +01:00
func (value ActorId) Validate() []string {
2023-12-08 19:41:22 +01:00
var result = []string{}
2023-12-09 18:30:47 +01: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 20:37:26 +01:00
}
2023-12-08 19:41:22 +01:00
return result
}
2023-12-22 11:48:24 +01:00
func (value PersonId) Validate() []string {
var result = value.ActorId.Validate()
switch value.Source {
2023-12-21 14:22:23 +01:00
case "forgejo", "gitea":
2023-12-22 11:48:24 +01:00
if strings.ToLower(value.Path) != "api/v1/activitypub/user-id" && strings.ToLower(value.Path) != "api/activitypub/user-id" {
2023-12-22 13:44:45 +01:00
result = append(result, fmt.Sprintf("path: %q has to be a person specific api path", value.Path))
2023-12-21 14:22:23 +01:00
}
}
2023-12-22 11:48:24 +01:00
return result
}
2023-12-22 11:48:24 +01:00
func (value RepositoryId) Validate() []string {
var result = value.ActorId.Validate()
switch value.Source {
2023-12-21 14:22:23 +01:00
case "forgejo", "gitea":
2023-12-22 11:48:24 +01:00
if strings.ToLower(value.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(value.Path) != "api/activitypub/repository-id" {
2023-12-22 13:44:45 +01:00
result = append(result, fmt.Sprintf("path: %q has to be a repo specific api path", value.Path))
2023-12-21 14:22:23 +01:00
}
}
2023-12-22 11:48:24 +01:00
return result
}
2023-12-06 13:06:30 +01: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 11:48:24 +01:00
2023-12-22 13:44:45 +01:00
func IsValid[T Validateables](value T) (bool, error) {
if err := value.Validate(); len(err) > 0 {
errString := strings.Join(err, "\n")
return false, fmt.Errorf(errString)
}
return true, nil
}
/*
2023-12-22 11:48:24 +01: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
}
2023-12-22 13:44:45 +01:00
*/