2022-11-06 09:53:38 +00:00
|
|
|
package wellknown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-11-11 21:15:46 +00:00
|
|
|
"github.com/go-ap/jsonld"
|
2022-11-06 09:53:38 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.sebtobie.de/httpserver"
|
|
|
|
"go.sebtobie.de/httpserver/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ middleware.Middleware = &Middleware{}
|
2022-11-07 17:45:04 +00:00
|
|
|
var _ middleware.PreSetupMiddleware = &Middleware{}
|
2022-11-06 09:53:38 +00:00
|
|
|
var _ httpserver.Site = &Middleware{}
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
|
|
|
// Link is the reference where the application can find the object
|
|
|
|
Link struct {
|
|
|
|
Rel string
|
|
|
|
Type string
|
|
|
|
Href *url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
// Middleware is an middleware for sites to register themselfs to provide wellknown urls
|
|
|
|
Middleware struct {
|
|
|
|
// webfinger is an map consisting of protocol as key and an array of WebfingerAccount as value
|
|
|
|
webfinger map[string][]Webfinger
|
|
|
|
}
|
|
|
|
|
|
|
|
// Webfinger is an function that accepts the account part(after ?resource=<protocol>:) and returns the aliases and the links.
|
|
|
|
// The bool value if for faster detection if the account was found.
|
|
|
|
Webfinger func(resource string) ([]string, []*Link, bool)
|
|
|
|
)
|
|
|
|
|
|
|
|
// New creates a new initialized Middleware
|
|
|
|
func New() *Middleware {
|
|
|
|
return &Middleware{
|
|
|
|
webfinger: map[string][]Webfinger{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 10:05:16 +00:00
|
|
|
// Defaults returns nothing
|
2022-11-06 09:53:38 +00:00
|
|
|
func (*Middleware) Defaults() middleware.Config {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gin does nothing.
|
|
|
|
func (*Middleware) Gin(c *gin.Context) {
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup does nothing
|
|
|
|
func (*Middleware) Setup(middleware.Config) {}
|
|
|
|
|
|
|
|
// Teardown does nothing
|
|
|
|
func (*Middleware) Teardown() {}
|
|
|
|
|
2022-11-07 17:45:04 +00:00
|
|
|
// PreSetup goes through all sites and collects all registrations for wellknown uris
|
|
|
|
func (m *Middleware) PreSetup(s []any) error {
|
2022-11-06 09:53:38 +00:00
|
|
|
for _, site := range s {
|
|
|
|
if wfs, ok := site.(FingerSite); ok {
|
|
|
|
for _, finger := range wfs.RegisterFingers() {
|
|
|
|
if _, ok := m.webfinger[finger.Protocol]; !ok {
|
|
|
|
m.webfinger[finger.Protocol] = []Webfinger{}
|
|
|
|
}
|
|
|
|
m.webfinger[finger.Protocol] = append(m.webfinger[finger.Protocol], finger.Webfinger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes the routergroup. It must always run on /
|
|
|
|
func (m *Middleware) Init(rg *gin.RouterGroup) {
|
|
|
|
if len(m.webfinger) > 0 {
|
|
|
|
rg.GET(".well-known/webfinger", m.webfingerhf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Middleware) webfingerhf(c *gin.Context) {
|
|
|
|
resource := c.Query("resource")
|
|
|
|
if resource == "" {
|
|
|
|
c.AbortWithStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
uri, err := url.Parse(resource)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Str("uri", resource).Msg("Failed to parse uri")
|
|
|
|
c.AbortWithStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var wf []Webfinger
|
|
|
|
var found bool
|
|
|
|
if wf, found = m.webfinger[uri.Scheme]; !found {
|
|
|
|
c.AbortWithStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var output = struct {
|
|
|
|
Subject string `json:"subject,omitempty"`
|
|
|
|
Aliases []string `json:"aliases,omitempty"`
|
|
|
|
Links []*Link `json:"links,omitempty"`
|
|
|
|
}{Subject: resource, Aliases: []string{}, Links: []*Link{}}
|
|
|
|
var aliases = []string{}
|
|
|
|
var links = []*Link{}
|
|
|
|
var input = uri.Opaque
|
|
|
|
if uri.Opaque == "" {
|
|
|
|
input = resource
|
|
|
|
}
|
|
|
|
outputok := false
|
|
|
|
for _, finger := range wf {
|
|
|
|
aliases, links, found = finger(input)
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
output.Aliases = append(output.Aliases, aliases...)
|
|
|
|
output.Links = append(output.Links, links...)
|
|
|
|
outputok = true
|
|
|
|
}
|
|
|
|
if outputok {
|
|
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
2022-11-11 21:15:46 +00:00
|
|
|
json, err := jsonld.Marshal(output)
|
2022-11-06 09:53:38 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(500)
|
|
|
|
return
|
|
|
|
}
|
2022-11-11 21:15:46 +00:00
|
|
|
c.Data(200, jsonld.ContentType, json)
|
2022-11-06 09:53:38 +00:00
|
|
|
}
|
|
|
|
c.AbortWithStatus(404)
|
|
|
|
}
|