1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-10 11:25:56 -05:00
forgejo/models/forgefed/nodeinfo.go

135 lines
3.2 KiB
Go
Raw Normal View History

2023-12-29 06:10:07 -05:00
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package forgefed
import (
2023-12-29 09:48:45 -05:00
"fmt"
2023-12-29 06:10:07 -05:00
"net/url"
"code.gitea.io/gitea/modules/validation"
2024-01-12 08:33:52 -05:00
2023-12-29 06:10:07 -05:00
"github.com/valyala/fastjson"
)
type (
SourceType string
)
const (
2023-12-29 09:48:45 -05:00
ForgejoSourceType SourceType = "forgejo"
GiteaSourceType SourceType = "gitea"
2023-12-29 06:10:07 -05:00
)
2023-12-29 09:48:45 -05:00
var KnownSourceTypes = []any{
ForgejoSourceType, GiteaSourceType,
2023-12-29 06:10:07 -05:00
}
2023-12-29 09:48:45 -05:00
// ------------------------------------------------ NodeInfoWellKnown ------------------------------------------------
2023-12-29 06:10:07 -05:00
// NodeInfo data type
// swagger:model
type NodeInfoWellKnown struct {
Href string
}
2023-12-29 09:48:45 -05:00
// Factory function for PersonID. Created struct is asserted to be valid
2023-12-29 06:10:07 -05:00
func NewNodeInfoWellKnown(body []byte) (NodeInfoWellKnown, error) {
result, err := NodeInfoWellKnownUnmarshalJSON(body)
if err != nil {
return NodeInfoWellKnown{}, err
}
2023-12-29 09:48:45 -05:00
if valid, err := validation.IsValid(result); !valid {
return NodeInfoWellKnown{}, err
2023-12-29 06:10:07 -05:00
}
2023-12-29 09:48:45 -05: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 06:10:07 -05: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")...)
parsedUrl, err := url.Parse(node.Href)
if err != nil {
result = append(result, err.Error())
return result
}
if parsedUrl.Host == "" {
result = append(result, "Href has to be absolute")
}
2023-12-29 09:48:45 -05:00
result = append(result, validation.ValidateOneOf(parsedUrl.Scheme, []any{"http", "https"})...)
2023-12-29 06:10:07 -05:00
if parsedUrl.RawQuery != "" {
result = append(result, "Href may not contain query")
}
return result
}
2023-12-29 09:48:45 -05:00
func (id ActorID) AsWellKnownNodeInfoUri() string {
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 {
Source SourceType
}
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.Source = SourceType(source)
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.Source), "source")...)
result = append(result, validation.ValidateOneOf(node.Source, KnownSourceTypes)...)
return result
}