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
|
2023-11-16 14:49:05 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2023-11-24 12:48:14 +01:00
|
|
|
|
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-11-16 14:49:05 +01:00
|
|
|
)
|
|
|
|
|
2023-12-22 13:44:45 +01:00
|
|
|
type Validateables interface {
|
|
|
|
validation.Validateable
|
2023-12-22 14:52:10 +01:00
|
|
|
ActorID | PersonID | RepositoryID
|
2023-12-22 13:44:45 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
type ActorID struct {
|
|
|
|
ID string
|
2023-12-09 18:30:47 +01:00
|
|
|
Source string
|
|
|
|
Schema string
|
|
|
|
Path string
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
UnvalidatedInput string
|
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
type PersonID struct {
|
|
|
|
ActorID
|
2023-12-09 18:30:47 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
type RepositoryID struct {
|
|
|
|
ActorID
|
2023-11-16 14:49:05 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
// newActorID receives already validated inputs
|
|
|
|
func newActorID(validatedURI *url.URL, source string) (ActorID, error) {
|
|
|
|
pathWithActorID := strings.Split(validatedURI.Path, "/")
|
2023-12-09 18:30:47 +01:00
|
|
|
if containsEmptyString(pathWithActorID) {
|
|
|
|
pathWithActorID = removeEmptyStrings(pathWithActorID)
|
|
|
|
}
|
|
|
|
length := len(pathWithActorID)
|
|
|
|
pathWithoutActorID := strings.Join(pathWithActorID[0:length-1], "/")
|
|
|
|
id := pathWithActorID[length-1]
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
result := ActorID{}
|
|
|
|
result.ID = id
|
2023-12-09 18:30:47 +01:00
|
|
|
result.Source = source
|
2023-12-22 14:52:10 +01:00
|
|
|
result.Schema = validatedURI.Scheme
|
|
|
|
result.Host = validatedURI.Hostname()
|
2023-12-09 18:30:47 +01:00
|
|
|
result.Path = pathWithoutActorID
|
2023-12-22 14:52:10 +01:00
|
|
|
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-22 14:52:10 +01:00
|
|
|
func NewPersonID(uri string, source string) (PersonID, error) {
|
2023-12-21 14:22:23 +01:00
|
|
|
// TODO: remove after test
|
|
|
|
//if !validation.IsValidExternalURL(uri) {
|
|
|
|
// return PersonId{}, fmt.Errorf("uri %s is not a valid external url", uri)
|
|
|
|
//}
|
2023-12-22 14:52:10 +01:00
|
|
|
validatedURI, err := url.ParseRequestURI(uri)
|
2023-12-22 13:42:07 +01:00
|
|
|
if err != nil {
|
2023-12-22 14:52:10 +01:00
|
|
|
return PersonID{}, err
|
2023-12-22 13:42:07 +01:00
|
|
|
}
|
2023-12-21 14:22:23 +01:00
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
actorID, err := newActorID(validatedURI, source)
|
2023-12-21 14:22:23 +01:00
|
|
|
if err != nil {
|
2023-12-22 14:52:10 +01:00
|
|
|
return PersonID{}, err
|
2023-12-21 14:22:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate Person specific path
|
2023-12-22 14:52:10 +01:00
|
|
|
personID := PersonID{actorID}
|
|
|
|
if valid, outcome := validation.IsValid(personID); !valid {
|
|
|
|
return PersonID{}, outcome
|
2023-12-21 14:22:23 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
return personID, nil
|
2023-12-21 14:22:23 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
func NewRepositoryId(uri string, source string) (RepositoryID, error) {
|
|
|
|
|
2023-12-09 18:30:47 +01:00
|
|
|
if !validation.IsAPIURL(uri) {
|
2023-12-22 14:52:10 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
validatedURI, err := url.ParseRequestURI(uri)
|
2023-12-22 13:42:07 +01:00
|
|
|
if err != nil {
|
2023-12-22 14:52:10 +01:00
|
|
|
return RepositoryID{}, err
|
2023-12-22 13:42:07 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
actorID, err := newActorID(validatedURI, source)
|
2023-12-21 14:22:23 +01:00
|
|
|
if err != nil {
|
2023-12-22 14:52:10 +01:00
|
|
|
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
|
2023-12-22 14:52:10 +01:00
|
|
|
repoID := RepositoryID{actorID}
|
|
|
|
if valid, outcome := validation.IsValid(repoID); !valid {
|
|
|
|
return RepositoryID{}, outcome
|
2023-12-08 20:37:26 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
return repoID, nil
|
2023-12-08 20:37:26 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
func (id ActorID) AsURI() string {
|
|
|
|
var result string
|
2023-12-09 18:30:47 +01:00
|
|
|
if id.Port == "" {
|
2023-12-22 14:52:10 +01:00
|
|
|
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-22 14:52:10 +01:00
|
|
|
result = fmt.Sprintf("%s://%s:%s/%s/%s", id.Schema, id.Host, id.Port, id.Path, id.ID)
|
2023-11-24 11:37:29 +01:00
|
|
|
}
|
2023-12-08 20:37:26 +01:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
func (id PersonID) AsWebfinger() string {
|
|
|
|
result := fmt.Sprintf("@%s@%s", strings.ToLower(id.ID), strings.ToLower(id.Host))
|
2023-12-08 20:37:26 +01:00
|
|
|
return result
|
2023-11-24 11:37:29 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
func (id PersonID) AsLoginName() string {
|
|
|
|
result := fmt.Sprintf("%s%s", strings.ToLower(id.ID), id.HostSuffix())
|
2023-12-15 14:45:20 +01:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
func (id PersonID) HostSuffix() string {
|
2023-12-15 14:45:20 +01:00
|
|
|
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-22 14:52:10 +01:00
|
|
|
func (id ActorID) Validate() []string {
|
2023-12-08 19:41:22 +01:00
|
|
|
var result = []string{}
|
2023-12-22 14:52:10 +01:00
|
|
|
result = append(result, validation.ValidateNotEmpty(id.ID, "userId")...)
|
|
|
|
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
|
|
|
|
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.UnvalidatedInput, "unvalidatedInput")...)
|
|
|
|
result = append(result, validation.ValidateOneOf(id.Source, []string{"forgejo", "gitea"})...)
|
2023-12-09 18:30:47 +01:00
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
if id.UnvalidatedInput != id.AsURI() {
|
|
|
|
result = append(result, fmt.Sprintf("not all input: %q was parsed: %q", id.UnvalidatedInput, id.AsURI()))
|
2023-12-08 20:37:26 +01:00
|
|
|
}
|
2023-11-16 14:49:05 +01:00
|
|
|
|
2023-12-08 19:41:22 +01:00
|
|
|
return result
|
2023-11-16 14:49:05 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
func (id PersonID) Validate() []string {
|
|
|
|
var result = id.ActorID.Validate()
|
|
|
|
switch id.Source {
|
2023-12-21 14:22:23 +01:00
|
|
|
case "forgejo", "gitea":
|
2023-12-22 14:52:10 +01:00
|
|
|
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))
|
2023-12-21 14:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-22 11:48:24 +01:00
|
|
|
return result
|
2023-11-24 11:37:29 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:52:10 +01:00
|
|
|
func (id RepositoryID) Validate() []string {
|
|
|
|
var result = id.ActorID.Validate()
|
|
|
|
switch id.Source {
|
2023-12-21 14:22:23 +01:00
|
|
|
case "forgejo", "gitea":
|
2023-12-22 14:52:10 +01:00
|
|
|
if strings.ToLower(id.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(id.Path) != "api/activitypub/repository-id" {
|
|
|
|
result = append(result, fmt.Sprintf("path: %q has to be a repo specific api path", id.Path))
|
2023-12-21 14:22:23 +01:00
|
|
|
}
|
2023-11-24 11:37:29 +01:00
|
|
|
}
|
2023-12-22 11:48:24 +01:00
|
|
|
return result
|
2023-11-24 11:37:29 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|