forgejo/models/forgefed/actor.go

199 lines
5.4 KiB
Go
Raw Normal View History

2023-12-08 22:37:26 +03:00
// Copyright 2023 The forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2023-12-09 16:53:40 +03:00
package forgefed
import (
"fmt"
"net/url"
"strings"
2023-12-09 20:30:47 +03:00
"code.gitea.io/gitea/modules/setting"
2023-12-08 20:08:54 +03:00
"code.gitea.io/gitea/modules/validation"
)
2023-12-29 17:48:45 +03:00
// ----------------------------- ActorID --------------------------------------------
2023-12-22 16:52:10 +03:00
type ActorID struct {
2023-12-22 17:10:21 +03:00
ID string
2023-12-09 20:30:47 +03:00
Source string
Schema string
Path string
Host string
Port string
UnvalidatedInput string
}
2023-12-29 17:48:45 +03: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 20:30:47 +03:00
}
2023-12-21 16:22:23 +03:00
2023-12-29 14:10:07 +03:00
if valid, outcome := validation.IsValid(result); !valid {
return ActorID{}, outcome
}
return result, nil
}
2023-12-29 17:48:45 +03: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 20:30:47 +03:00
}
2023-12-29 17:48:45 +03:00
return result
}
2023-12-09 20:30:47 +03:00
2023-12-29 17:48:45 +03: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 14:10:07 +03:00
2023-12-29 17:48:45 +03: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 20:30:47 +03:00
}
2023-12-08 22:37:26 +03:00
2023-12-29 17:48:45 +03:00
// ----------------------------- PersonID --------------------------------------------
type PersonID struct {
ActorID
}
// Factory function for PersonID. Created struct is asserted to be valid
2023-12-29 11:43:10 +03:00
func NewPersonID(uri, source string) (PersonID, error) {
2023-12-21 16:22:23 +03: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 17:48:45 +03:00
result, err := newActorID(uri)
2023-12-21 16:22:23 +03:00
if err != nil {
2023-12-22 16:52:10 +03:00
return PersonID{}, err
2023-12-21 16:22:23 +03:00
}
2023-12-29 17:48:45 +03:00
result.Source = source
2023-12-21 16:22:23 +03:00
// validate Person specific path
2023-12-29 17:48:45 +03:00
personID := PersonID{result}
2023-12-22 16:52:10 +03:00
if valid, outcome := validation.IsValid(personID); !valid {
return PersonID{}, outcome
2023-12-21 16:22:23 +03:00
}
2023-12-22 16:52:10 +03:00
return personID, nil
2023-12-21 16:22:23 +03:00
}
2023-12-22 16:52:10 +03:00
func (id PersonID) AsWebfinger() string {
2023-12-22 17:10:21 +03:00
result := fmt.Sprintf("@%s@%s", strings.ToLower(id.ID), strings.ToLower(id.Host))
2023-12-08 22:37:26 +03:00
return result
}
2023-12-22 16:52:10 +03:00
func (id PersonID) AsLoginName() string {
2023-12-22 17:10:21 +03:00
result := fmt.Sprintf("%s%s", strings.ToLower(id.ID), id.HostSuffix())
2023-12-15 16:45:20 +03:00
return result
}
2023-12-22 16:52:10 +03:00
func (id PersonID) HostSuffix() string {
2023-12-15 16:45:20 +03:00
result := fmt.Sprintf("-%s", strings.ToLower(id.Host))
return result
}
2023-12-22 16:52:10 +03:00
func (id PersonID) Validate() []string {
2023-12-22 17:00:42 +03:00
result := id.ActorID.Validate()
2023-12-29 14:10:07 +03:00
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
2023-12-29 17:48:45 +03:00
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"})...)
2023-12-22 16:52:10 +03:00
switch id.Source {
2023-12-21 16:22:23 +03:00
case "forgejo", "gitea":
2023-12-22 16:52:10 +03: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 16:22:23 +03:00
}
}
2023-12-22 13:48:24 +03:00
return result
}
2023-12-29 17:48:45 +03: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 16:52:10 +03:00
func (id RepositoryID) Validate() []string {
2023-12-22 17:00:42 +03:00
result := id.ActorID.Validate()
2023-12-29 14:10:07 +03:00
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
2023-12-29 17:48:45 +03:00
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"})...)
2023-12-22 16:52:10 +03:00
switch id.Source {
2023-12-21 16:22:23 +03:00
case "forgejo", "gitea":
2023-12-22 16:52:10 +03: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 16:22:23 +03:00
}
}
2023-12-22 13:48:24 +03:00
return result
}
2023-12-06 15:06:30 +03: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 17:48:45 +03: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
}