Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-29 21:26:10 +01:00
forgejo/models/forgefed/actor.go

202 lines
5.1 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-21 14:22:23 +01:00
type Validateable interface {
Validate() []string
}
2023-12-09 18:30:47 +01:00
type ActorId struct {
2023-12-21 14:22:23 +01:00
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
}
2023-12-21 14:22:23 +01:00
func newActorId(uri, source string) (ActorId, error) {
2023-12-19 10:55:30 +01:00
validatedUri, _ := url.Parse(uri) // ToDo: Why no err treatment at this place?
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 = uri
2023-12-21 14:22:23 +01:00
2023-12-09 18:30:47 +01:00
if valid, err := result.IsValid(); !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)
//}
actorId, err := newActorId(uri, source)
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 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
}
2023-12-21 14:22:23 +01:00
actorId, err := newActorId(uri, source)
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}
if valid, outcome := repoId.IsValid(); !valid {
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-06 11:24:42 +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-09 14:53:40 +01:00
// TODO: Move valid-parts to valid package
2023-12-06 11:24:42 +01:00
/*
IsValid concatenates the error messages with newlines and returns them if there are any
*/
2023-12-21 14:22:23 +01:00
func (a ActorId) IsValid() (bool, error) {
if err := a.Validate(); len(err) > 0 {
errString := strings.Join(err, "\n")
return false, fmt.Errorf(errString)
}
2023-12-21 14:22:23 +01:00
return true, nil
}
func (a PersonId) IsValid() (bool, error) {
switch a.Source {
case "forgejo", "gitea":
if strings.ToLower(a.Path) != "api/v1/activitypub/user-id" && strings.ToLower(a.Path) != "api/activitypub/user-id" {
err := fmt.Errorf("path: %q has to be an api path", a.Path)
return false, err
}
}
return true, nil
}
2023-12-09 18:30:47 +01:00
func (a RepositoryId) IsValid() (bool, error) {
2023-12-21 14:22:23 +01:00
switch a.Source {
case "forgejo", "gitea":
if strings.ToLower(a.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(a.Path) != "api/activitypub/repository-id" {
err := fmt.Errorf("path: %q has to be an api path", a.Path)
return false, err
}
}
2023-12-09 18:30:47 +01:00
return true, nil
}
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
}