forgejo/models/forgefed/actor.go

227 lines
6.2 KiB
Go
Raw Normal View History

2023-12-08 19:37:26 +00:00
// Copyright 2023 The forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2023-12-09 13:53:40 +00:00
package forgefed
import (
"fmt"
"net/url"
"strings"
2023-12-09 17:30:47 +00:00
"code.gitea.io/gitea/modules/setting"
2023-12-08 17:08:54 +00:00
"code.gitea.io/gitea/modules/validation"
2024-02-09 16:46:27 +00:00
ap "github.com/go-ap/activitypub"
)
2023-12-29 14:48:45 +00:00
// ----------------------------- ActorID --------------------------------------------
2023-12-22 13:52:10 +00:00
type ActorID struct {
2023-12-22 14:10:21 +00:00
ID string
2023-12-09 17:30:47 +00:00
Source string
Schema string
Path string
Host string
Port string
UnvalidatedInput string
}
2023-12-29 14:48:45 +00:00
// Factory function for ActorID. Created struct is asserted to be valid
func NewActorID(uri string) (ActorID, error) {
result, err := newActorID(uri)
if err != nil {
return ActorID{}, err
2023-12-09 17:30:47 +00:00
}
2023-12-21 13:22:23 +00:00
2023-12-29 11:10:07 +00:00
if valid, outcome := validation.IsValid(result); !valid {
return ActorID{}, outcome
}
return result, nil
}
2023-12-29 14:48:45 +00:00
func (id ActorID) AsURI() string {
var result string
if id.Port == "" {
result = fmt.Sprintf("%s://%s/%s/%s", id.Schema, id.Host, id.Path, id.ID)
} else {
result = fmt.Sprintf("%s://%s:%s/%s/%s", id.Schema, id.Host, id.Port, id.Path, id.ID)
2023-12-09 17:30:47 +00:00
}
2023-12-29 14:48:45 +00:00
return result
}
2023-12-09 17:30:47 +00:00
2023-12-29 14:48:45 +00:00
func (id ActorID) Validate() []string {
var result []string
result = append(result, validation.ValidateNotEmpty(id.ID, "userId")...)
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")...)
2023-12-29 11:10:07 +00:00
2023-12-29 14:48:45 +00:00
if id.UnvalidatedInput != id.AsURI() {
result = append(result, fmt.Sprintf("not all input: %q was parsed: %q", id.UnvalidatedInput, id.AsURI()))
}
return result
2023-12-09 17:30:47 +00:00
}
2023-12-08 19:37:26 +00:00
2023-12-29 14:48:45 +00:00
// ----------------------------- PersonID --------------------------------------------
type PersonID struct {
ActorID
}
// Factory function for PersonID. Created struct is asserted to be valid
2023-12-29 08:43:10 +00:00
func NewPersonID(uri, source string) (PersonID, error) {
2023-12-21 13:22:23 +00:00
// TODO: remove after test
//if !validation.IsValidExternalURL(uri) {
// return PersonId{}, fmt.Errorf("uri %s is not a valid external url", uri)
//}
2023-12-29 14:48:45 +00:00
result, err := newActorID(uri)
2023-12-21 13:22:23 +00:00
if err != nil {
2023-12-22 13:52:10 +00:00
return PersonID{}, err
2023-12-21 13:22:23 +00:00
}
2023-12-29 14:48:45 +00:00
result.Source = source
2023-12-21 13:22:23 +00:00
// validate Person specific path
2023-12-29 14:48:45 +00:00
personID := PersonID{result}
2023-12-22 13:52:10 +00:00
if valid, outcome := validation.IsValid(personID); !valid {
return PersonID{}, outcome
2023-12-21 13:22:23 +00:00
}
2023-12-22 13:52:10 +00:00
return personID, nil
2023-12-21 13:22:23 +00:00
}
2023-12-22 13:52:10 +00:00
func (id PersonID) AsWebfinger() string {
2023-12-22 14:10:21 +00:00
result := fmt.Sprintf("@%s@%s", strings.ToLower(id.ID), strings.ToLower(id.Host))
2023-12-08 19:37:26 +00:00
return result
}
2023-12-22 13:52:10 +00:00
func (id PersonID) AsLoginName() string {
2023-12-22 14:10:21 +00:00
result := fmt.Sprintf("%s%s", strings.ToLower(id.ID), id.HostSuffix())
2023-12-15 13:45:20 +00:00
return result
}
2023-12-22 13:52:10 +00:00
func (id PersonID) HostSuffix() string {
2023-12-15 13:45:20 +00:00
result := fmt.Sprintf("-%s", strings.ToLower(id.Host))
return result
}
2023-12-22 13:52:10 +00:00
func (id PersonID) Validate() []string {
2023-12-22 14:00:42 +00:00
result := id.ActorID.Validate()
2023-12-29 11:10:07 +00:00
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
2023-12-29 14:48:45 +00:00
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"})...)
2023-12-22 13:52:10 +00:00
switch id.Source {
2023-12-21 13:22:23 +00:00
case "forgejo", "gitea":
2023-12-22 13:52:10 +00: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 13:22:23 +00:00
}
}
2023-12-22 10:48:24 +00:00
return result
}
2023-12-29 14:48:45 +00:00
// ----------------------------- RepositoryID --------------------------------------------
type RepositoryID struct {
ActorID
}
// Factory function for RepositoryID. Created struct is asserted to be valid.
func NewRepositoryID(uri, 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")
}
result, err := newActorID(uri)
if err != nil {
return RepositoryID{}, err
}
result.Source = source
// validate Person specific path
repoID := RepositoryID{result}
if valid, outcome := validation.IsValid(repoID); !valid {
return RepositoryID{}, outcome
}
return repoID, nil
}
2023-12-22 13:52:10 +00:00
func (id RepositoryID) Validate() []string {
2023-12-22 14:00:42 +00:00
result := id.ActorID.Validate()
2023-12-29 11:10:07 +00:00
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
2023-12-29 14:48:45 +00:00
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"})...)
2023-12-22 13:52:10 +00:00
switch id.Source {
2023-12-21 13:22:23 +00:00
case "forgejo", "gitea":
2023-12-22 13:52:10 +00: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 13:22:23 +00:00
}
}
2023-12-22 10:48:24 +00:00
return result
}
2023-12-06 12:06:30 +00: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-29 14:48:45 +00:00
func newActorID(uri string) (ActorID, error) {
validatedURI, err := url.ParseRequestURI(uri)
if err != nil {
return ActorID{}, err
}
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 := ActorID{}
result.ID = id
result.Schema = validatedURI.Scheme
result.Host = validatedURI.Hostname()
result.Path = pathWithoutActorID
result.Port = validatedURI.Port()
result.UnvalidatedInput = validatedURI.String()
return result, nil
}
2024-01-04 17:04:46 +00:00
// ----------------------------- ForgePerson -------------------------------------
// ForgePerson activity data type
// swagger:model
type ForgePerson struct {
// swagger:ignore
ap.Actor
}
func (s ForgePerson) MarshalJSON() ([]byte, error) {
return s.Actor.MarshalJSON()
}
func (s *ForgePerson) UnmarshalJSON(data []byte) error {
return s.Actor.UnmarshalJSON(data)
}
func (s ForgePerson) Validate() []string {
var result []string
result = append(result, validation.ValidateNotEmpty(string(s.Type), "type")...)
result = append(result, validation.ValidateOneOf(string(s.Type), []any{string(ap.PersonType)})...)
result = append(result, validation.ValidateNotEmpty(s.PreferredUsername.String(), "preferredUsername")...)
return result
}