mirror of
https://github.com/versia-pub/versia-go.git
synced 2026-03-13 04:29:15 +01:00
chore: init
This commit is contained in:
commit
320715f3e7
174 changed files with 42083 additions and 0 deletions
51
pkg/webfinger/host_meta.go
Normal file
51
pkg/webfinger/host_meta.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package webfinger
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type HostMeta struct {
|
||||
JSON []byte
|
||||
XML []byte
|
||||
}
|
||||
|
||||
func NewHostMeta(baseURL *url.URL) HostMeta {
|
||||
template := &url.URL{Path: "/.well-known/webfinger?resource={uri}"}
|
||||
template = baseURL.ResolveReference(template)
|
||||
|
||||
return HostMeta{
|
||||
JSON: generateJSONHostMeta(template),
|
||||
XML: generateXMLHostMeta(template),
|
||||
}
|
||||
}
|
||||
|
||||
func generateXMLHostMeta(template *url.URL) []byte {
|
||||
return []byte(`<?xml version="1.0"?>
|
||||
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
|
||||
<Link rel="lrdd" template="` + template.String() + `" />
|
||||
</XRD>`)
|
||||
}
|
||||
|
||||
func generateJSONHostMeta(template *url.URL) []byte {
|
||||
b, err := json.Marshal(hostMetaStruct{
|
||||
Links: []hostMetaLink{{
|
||||
Rel: "lrdd",
|
||||
Template: template.String(),
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
type hostMetaStruct struct {
|
||||
Links []hostMetaLink `json:"links"`
|
||||
}
|
||||
|
||||
type hostMetaLink struct {
|
||||
Rel string `json:"rel"`
|
||||
Template string `json:"template"`
|
||||
}
|
||||
72
pkg/webfinger/webfinger.go
Normal file
72
pkg/webfinger/webfinger.go
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package webfinger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidSyntax = errors.New("must follow the format \"acct:<ID|Username>@<DOMAIN>\"")
|
||||
)
|
||||
|
||||
func ParseResource(res string) (*UserID, error) {
|
||||
if !strings.HasPrefix(res, "acct:") {
|
||||
return nil, ErrInvalidSyntax
|
||||
}
|
||||
|
||||
if !strings.Contains(res, "@") {
|
||||
return nil, ErrInvalidSyntax
|
||||
}
|
||||
|
||||
spl := strings.Split(res, "@")
|
||||
if len(spl) != 2 {
|
||||
return nil, ErrInvalidSyntax
|
||||
}
|
||||
|
||||
userID := strings.TrimPrefix(spl[0], "acct:")
|
||||
domain := spl[1]
|
||||
|
||||
return &UserID{userID, domain}, nil
|
||||
}
|
||||
|
||||
type UserID struct {
|
||||
ID string
|
||||
Domain string
|
||||
}
|
||||
|
||||
func (u UserID) String() string {
|
||||
return u.ID + "@" + u.Domain
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Subject string `json:"subject,omitempty"`
|
||||
Links []Link `json:"links,omitempty"`
|
||||
|
||||
Error *string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type Link struct {
|
||||
Relation string `json:"rel"`
|
||||
Type any `json:"type"`
|
||||
Link string `json:"href"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
UserID
|
||||
|
||||
URI *url.URL
|
||||
|
||||
Avatar *url.URL
|
||||
AvatarMIMEType string
|
||||
}
|
||||
|
||||
func (u User) WebFingerResource() Response {
|
||||
return Response{
|
||||
Subject: "acct:" + u.String(),
|
||||
Links: []Link{
|
||||
{"self", "application/json", u.URI.String()},
|
||||
{"avatar", u.AvatarMIMEType, u.Avatar.String()},
|
||||
},
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue