forgejo/models/forgefed/actor.go

230 lines
6.4 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"
2024-03-26 16:56:06 +01:00
"code.gitea.io/gitea/modules/log"
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"
2024-02-09 17:46:27 +01:00
ap "github.com/go-ap/activitypub"
)
2023-12-29 15:48:45 +01:00
// ----------------------------- ActorID --------------------------------------------
2023-12-22 14:52:10 +01:00
type ActorID struct {
2023-12-22 15:10:21 +01:00
ID string
2023-12-09 18:30:47 +01:00
Source string
Schema string
Path string
Host string
Port string
UnvalidatedInput string
}
2023-12-29 15:48:45 +01: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 18:30:47 +01:00
}
2023-12-21 14:22:23 +01:00
2023-12-29 12:10:07 +01:00
if valid, outcome := validation.IsValid(result); !valid {
return ActorID{}, outcome
}
return result, nil
}
2023-12-29 15:48:45 +01: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 18:30:47 +01:00
}
2023-12-29 15:48:45 +01:00
return result
}
2023-12-09 18:30:47 +01:00
2023-12-29 15:48:45 +01: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 12:10:07 +01:00
2023-12-29 15:48:45 +01:00
if id.UnvalidatedInput != id.AsURI() {
2024-03-26 16:56:06 +01:00
result = append(result, fmt.Sprintf("not all input was parsed, \nUnvalidated Input:%q \nParsed URI: %q", id.UnvalidatedInput, id.AsURI()))
2023-12-29 15:48:45 +01:00
}
return result
2023-12-09 18:30:47 +01:00
}
2023-12-08 20:37:26 +01:00
2023-12-29 15:48:45 +01:00
// ----------------------------- PersonID --------------------------------------------
type PersonID struct {
ActorID
}
// Factory function for PersonID. Created struct is asserted to be valid
2023-12-29 09:43:10 +01:00
func NewPersonID(uri, 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-29 15:48:45 +01:00
result, err := newActorID(uri)
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
}
2023-12-29 15:48:45 +01:00
result.Source = source
2023-12-21 14:22:23 +01:00
// validate Person specific path
2023-12-29 15:48:45 +01:00
personID := PersonID{result}
2023-12-22 14:52:10 +01:00
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 (id PersonID) AsWebfinger() string {
2023-12-22 15:10:21 +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-22 14:52:10 +01:00
func (id PersonID) AsLoginName() string {
2023-12-22 15:10:21 +01:00
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 14:52:10 +01:00
func (id PersonID) Validate() []string {
2023-12-22 15:00:42 +01:00
result := id.ActorID.Validate()
2023-12-29 12:10:07 +01:00
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
2023-12-29 15:48:45 +01:00
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"})...)
2023-12-22 14:52:10 +01:00
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-12-29 15:48:45 +01: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 14:52:10 +01:00
func (id RepositoryID) Validate() []string {
2023-12-22 15:00:42 +01:00
result := id.ActorID.Validate()
2023-12-29 12:10:07 +01:00
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
2023-12-29 15:48:45 +01:00
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"})...)
2023-12-22 14:52:10 +01:00
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-12-22 11:48:24 +01:00
return result
}
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-29 15:48:45 +01:00
func newActorID(uri string) (ActorID, error) {
validatedURI, err := url.ParseRequestURI(uri)
2024-03-26 16:56:06 +01:00
log.Info("Validated URI is: %v", validatedURI)
log.Info("Input is: %v", validatedURI)
2023-12-29 15:48:45 +01:00
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()
2024-03-26 16:56:06 +01:00
result.UnvalidatedInput = validatedURI.String() // ToDo: Whats happening here?
2023-12-29 15:48:45 +01:00
return result, nil
}
2024-01-04 18:04:46 +01: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
}