1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-27 13:39:19 -05:00
forgejo/models/forgefed/actor.go

194 lines
5.7 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 {
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
}
2023-12-08 14:37:26 -05:00
func NewPersonId(uri string, source string) (PersonId, error) {
if !validation.IsValidExternalURL(uri) {
return PersonId{}, fmt.Errorf("uri %s is not a valid external url", uri)
}
validatedUri, _ := url.Parse(uri)
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]
result := PersonId{}
result.Id = id
result.Source = source
result.Schema = validatedUri.Scheme
result.Host = validatedUri.Hostname()
result.Path = pathWithoutActorID
result.Port = validatedUri.Port()
result.UnvalidatedInput = uri
if valid, err := result.IsValid(); !valid {
return PersonId{}, err
}
return result, nil
}
2023-12-08 14:37:26 -05:00
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
}
2023-12-09 12:30:47 -05:00
validatedUri, _ := url.Parse(uri)
pathWithActorID := strings.Split(validatedUri.Path, "/")
if containsEmptyString(pathWithActorID) {
pathWithActorID = removeEmptyStrings(pathWithActorID)
2023-12-08 14:37:26 -05:00
}
2023-12-09 12:30:47 -05:00
length := len(pathWithActorID)
pathWithoutActorID := strings.Join(pathWithActorID[0:length-1], "/")
id := pathWithActorID[length-1]
result := RepositoryId{}
result.Id = id
result.Source = source
result.Schema = validatedUri.Scheme
result.Host = validatedUri.Hostname()
result.Path = pathWithoutActorID
result.Port = validatedUri.Port()
result.UnvalidatedInput = uri
if valid, err := result.IsValid(); !valid {
return RepositoryId{}, err
2023-12-08 14:37:26 -05:00
}
2023-12-09 12:30:47 -05:00
return result, 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-09 12:30:47 -05:00
func (id ActorId) AsWebfinger() string {
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-06 05:24:42 -05:00
Validate collects error strings in a slice and returns this
*/
2023-12-08 13:43:49 -05:00
func (value PersonId) 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"})...)
switch value.Source {
case "forgejo", "gitea":
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 a api path", value.Path))
}
}
if value.UnvalidatedInput != value.AsUri() {
result = append(result, fmt.Sprintf("not all input: %q was parsed: %q", value.UnvalidatedInput, value.AsUri()))
}
return result
}
func (value RepositoryId) Validate() []string {
var result = []string{}
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"})...)
switch value.Source {
case "forgejo", "gitea":
2023-12-09 12:30:47 -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 a api path", value.Path))
}
}
2023-12-09 12:30:47 -05:00
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-09 08:53:40 -05:00
// TODO: Move valid-parts to valid package
2023-12-06 05:24:42 -05:00
/*
IsValid concatenates the error messages with newlines and returns them if there are any
*/
2023-12-08 13:43:49 -05:00
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-09 12:30:47 -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)
}
2023-12-09 12:30:47 -05:00
return true, nil
}
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
}