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

227 lines
6.2 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"
2024-02-09 11:46:27 -05:00
ap "github.com/go-ap/activitypub"
)
2023-12-29 09:48:45 -05:00
// ----------------------------- ActorID --------------------------------------------
2023-12-22 08:52:10 -05:00
type ActorID struct {
2023-12-22 09:10:21 -05:00
ID string
2023-12-09 12:30:47 -05:00
Source string
Schema string
Path string
Host string
Port string
UnvalidatedInput string
}
2023-12-29 09:48:45 -05: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 12:30:47 -05:00
}
2023-12-21 08:22:23 -05:00
2023-12-29 06:10:07 -05:00
if valid, outcome := validation.IsValid(result); !valid {
return ActorID{}, outcome
}
return result, nil
}
2023-12-29 09:48:45 -05: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 12:30:47 -05:00
}
2023-12-29 09:48:45 -05:00
return result
}
2023-12-09 12:30:47 -05:00
2023-12-29 09:48:45 -05: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 06:10:07 -05:00
2023-12-29 09:48:45 -05: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 12:30:47 -05:00
}
2023-12-08 14:37:26 -05:00
2023-12-29 09:48:45 -05:00
// ----------------------------- PersonID --------------------------------------------
type PersonID struct {
ActorID
}
// Factory function for PersonID. Created struct is asserted to be valid
2023-12-29 03:43:10 -05:00
func NewPersonID(uri, source string) (PersonID, error) {
2023-12-21 08:22:23 -05: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 09:48:45 -05:00
result, err := newActorID(uri)
2023-12-21 08:22:23 -05:00
if err != nil {
2023-12-22 08:52:10 -05:00
return PersonID{}, err
2023-12-21 08:22:23 -05:00
}
2023-12-29 09:48:45 -05:00
result.Source = source
2023-12-21 08:22:23 -05:00
// validate Person specific path
2023-12-29 09:48:45 -05:00
personID := PersonID{result}
2023-12-22 08:52:10 -05:00
if valid, outcome := validation.IsValid(personID); !valid {
return PersonID{}, outcome
2023-12-21 08:22:23 -05:00
}
2023-12-22 08:52:10 -05:00
return personID, nil
2023-12-21 08:22:23 -05:00
}
2023-12-22 08:52:10 -05:00
func (id PersonID) AsWebfinger() string {
2023-12-22 09:10:21 -05:00
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-22 08:52:10 -05:00
func (id PersonID) AsLoginName() string {
2023-12-22 09:10:21 -05:00
result := fmt.Sprintf("%s%s", strings.ToLower(id.ID), id.HostSuffix())
2023-12-15 08:45:20 -05:00
return result
}
2023-12-22 08:52:10 -05:00
func (id PersonID) HostSuffix() string {
2023-12-15 08:45:20 -05:00
result := fmt.Sprintf("-%s", strings.ToLower(id.Host))
return result
}
2023-12-22 08:52:10 -05:00
func (id PersonID) Validate() []string {
2023-12-22 09:00:42 -05:00
result := id.ActorID.Validate()
2023-12-29 06:10:07 -05:00
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
2023-12-29 09:48:45 -05:00
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"})...)
2023-12-22 08:52:10 -05:00
switch id.Source {
2023-12-21 08:22:23 -05:00
case "forgejo", "gitea":
2023-12-22 08:52:10 -05: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 08:22:23 -05:00
}
}
2023-12-22 05:48:24 -05:00
return result
}
2023-12-29 09:48:45 -05: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 08:52:10 -05:00
func (id RepositoryID) Validate() []string {
2023-12-22 09:00:42 -05:00
result := id.ActorID.Validate()
2023-12-29 06:10:07 -05:00
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
2023-12-29 09:48:45 -05:00
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"})...)
2023-12-22 08:52:10 -05:00
switch id.Source {
2023-12-21 08:22:23 -05:00
case "forgejo", "gitea":
2023-12-22 08:52:10 -05: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 08:22:23 -05:00
}
}
2023-12-22 05:48:24 -05:00
return result
}
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
}
2023-12-29 09:48:45 -05: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 12:04:46 -05: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
}