forgejo/models/forgefed/nodeinfo.go

136 lines
3.3 KiB
Go
Raw Normal View History

2023-12-29 11:10:07 +00:00
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package forgefed
import (
2023-12-29 14:48:45 +00:00
"fmt"
2023-12-29 11:10:07 +00:00
"net/url"
"code.gitea.io/gitea/modules/validation"
2024-01-12 13:33:52 +00:00
2023-12-29 11:10:07 +00:00
"github.com/valyala/fastjson"
)
// ToDo: Search for full text SourceType and Source, also in .md files
2023-12-29 11:10:07 +00:00
type (
SoftwareNameType string
2023-12-29 11:10:07 +00:00
)
const (
ForgejoSourceType SoftwareNameType = "forgejo"
GiteaSourceType SoftwareNameType = "gitea"
2023-12-29 11:10:07 +00:00
)
2023-12-29 14:48:45 +00:00
var KnownSourceTypes = []any{
ForgejoSourceType, GiteaSourceType,
2023-12-29 11:10:07 +00:00
}
2023-12-29 14:48:45 +00:00
// ------------------------------------------------ NodeInfoWellKnown ------------------------------------------------
2023-12-29 11:10:07 +00:00
// NodeInfo data type
// swagger:model
type NodeInfoWellKnown struct {
Href string
}
2023-12-29 14:48:45 +00:00
// Factory function for PersonID. Created struct is asserted to be valid
2023-12-29 11:10:07 +00:00
func NewNodeInfoWellKnown(body []byte) (NodeInfoWellKnown, error) {
result, err := NodeInfoWellKnownUnmarshalJSON(body)
if err != nil {
return NodeInfoWellKnown{}, err
}
2023-12-29 14:48:45 +00:00
if valid, err := validation.IsValid(result); !valid {
return NodeInfoWellKnown{}, err
2023-12-29 11:10:07 +00:00
}
2023-12-29 14:48:45 +00:00
return result, nil
}
func NodeInfoWellKnownUnmarshalJSON(data []byte) (NodeInfoWellKnown, error) {
p := fastjson.Parser{}
val, err := p.ParseBytes(data)
if err != nil {
return NodeInfoWellKnown{}, err
}
href := string(val.GetStringBytes("links", "0", "href"))
return NodeInfoWellKnown{Href: href}, nil
2023-12-29 11:10:07 +00:00
}
// Validate collects error strings in a slice and returns this
func (node NodeInfoWellKnown) Validate() []string {
var result []string
result = append(result, validation.ValidateNotEmpty(node.Href, "Href")...)
2024-01-13 15:22:49 +00:00
parsedURL, err := url.Parse(node.Href)
2023-12-29 11:10:07 +00:00
if err != nil {
result = append(result, err.Error())
return result
}
2024-01-13 15:22:49 +00:00
if parsedURL.Host == "" {
2023-12-29 11:10:07 +00:00
result = append(result, "Href has to be absolute")
}
2024-01-13 15:22:49 +00:00
result = append(result, validation.ValidateOneOf(parsedURL.Scheme, []any{"http", "https"})...)
2023-12-29 11:10:07 +00:00
2024-01-13 15:22:49 +00:00
if parsedURL.RawQuery != "" {
2023-12-29 11:10:07 +00:00
result = append(result, "Href may not contain query")
}
return result
}
2023-12-29 14:48:45 +00:00
2024-01-13 16:06:40 +00:00
func (id ActorID) AsWellKnownNodeInfoURI() string {
2023-12-29 14:48:45 +00:00
wellKnownPath := ".well-known/nodeinfo"
var result string
if id.Port == "" {
result = fmt.Sprintf("%s://%s/%s", id.Schema, id.Host, wellKnownPath)
} else {
result = fmt.Sprintf("%s://%s:%s/%s", id.Schema, id.Host, id.Port, wellKnownPath)
}
return result
}
// ------------------------------------------------ NodeInfo ------------------------------------------------
// NodeInfo data type
// swagger:model
type NodeInfo struct {
SoftwareName SoftwareNameType
2023-12-29 14:48:45 +00:00
}
func NodeInfoUnmarshalJSON(data []byte) (NodeInfo, error) {
p := fastjson.Parser{}
val, err := p.ParseBytes(data)
if err != nil {
return NodeInfo{}, err
}
source := string(val.GetStringBytes("software", "name"))
result := NodeInfo{}
result.SoftwareName = SoftwareNameType(source)
2023-12-29 14:48:45 +00:00
return result, nil
}
func NewNodeInfo(body []byte) (NodeInfo, error) {
result, err := NodeInfoUnmarshalJSON(body)
if err != nil {
return NodeInfo{}, err
}
if valid, err := validation.IsValid(result); !valid {
return NodeInfo{}, err
}
return result, nil
}
// Validate collects error strings in a slice and returns this
func (node NodeInfo) Validate() []string {
var result []string
result = append(result, validation.ValidateNotEmpty(string(node.SoftwareName), "source")...)
result = append(result, validation.ValidateOneOf(node.SoftwareName, KnownSourceTypes)...)
2023-12-29 14:48:45 +00:00
return result
}