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