2023-01-11 19:42:06 -07:00
|
|
|
package webfinger
|
|
|
|
|
|
|
|
import (
|
2023-01-15 17:00:25 -07:00
|
|
|
"bytes"
|
2023-01-11 19:42:06 -07:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"hash/fnv"
|
|
|
|
"sort"
|
|
|
|
|
2023-02-26 22:33:01 -07:00
|
|
|
"go.sour.is/ev/pkg/es/event"
|
|
|
|
"go.sour.is/ev/pkg/set"
|
|
|
|
"go.sour.is/ev/pkg/slice"
|
2023-01-15 17:00:25 -07:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-01-11 19:42:06 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func StreamID(subject string) string {
|
|
|
|
h := fnv.New128a()
|
|
|
|
h.Write([]byte(subject))
|
|
|
|
return "webfinger." + base64.RawURLEncoding.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
// JRD is a JSON Resource Descriptor, specifying properties and related links
|
|
|
|
// for a resource.
|
|
|
|
type JRD struct {
|
2023-04-19 17:53:00 -06:00
|
|
|
Subject string `json:"subject,omitempty" yaml:"subject,omitempty"`
|
|
|
|
Aliases []string `json:"aliases,omitempty" yaml:"aliases,omitempty"`
|
|
|
|
Properties map[string]*string `json:"properties,omitempty" yaml:"properties,omitempty"`
|
|
|
|
Links Links `json:"links,omitempty" yaml:"links,omitempty"`
|
2023-04-02 21:00:22 -06:00
|
|
|
deleted bool
|
2023-04-19 17:53:00 -06:00
|
|
|
event.IsAggregate `json:"-" yaml:"-"`
|
2023-01-11 19:42:06 -07:00
|
|
|
}
|
|
|
|
|
2023-01-25 10:35:09 -07:00
|
|
|
func (a *JRD) CloneValues() *JRD {
|
|
|
|
m := make(map[string]*string, len(a.Properties))
|
2023-02-26 22:33:01 -07:00
|
|
|
for k, v := range a.Properties {
|
2023-01-25 10:35:09 -07:00
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
return &JRD{
|
2023-02-26 22:33:01 -07:00
|
|
|
Subject: a.Subject,
|
|
|
|
Aliases: append([]string{}, a.Aliases...),
|
|
|
|
Properties: m,
|
|
|
|
Links: append([]*Link{}, a.Links...),
|
2023-01-25 10:35:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 19:42:06 -07:00
|
|
|
var _ event.Aggregate = (*JRD)(nil)
|
|
|
|
|
|
|
|
// Link is a link to a related resource.
|
|
|
|
type Link struct {
|
2023-04-19 17:53:00 -06:00
|
|
|
Index uint64 `json:"-" yaml:"-"`
|
2023-01-11 19:42:06 -07:00
|
|
|
Rel string `json:"rel,omitempty"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
HRef string `json:"href,omitempty"`
|
|
|
|
Titles map[string]string `json:"titles,omitempty"`
|
|
|
|
Properties map[string]*string `json:"properties,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Links []*Link
|
|
|
|
|
|
|
|
// Len is the number of elements in the collection.
|
|
|
|
func (l Links) Len() int {
|
|
|
|
if l == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less reports whether the element with index i
|
|
|
|
func (l Links) Less(i int, j int) bool {
|
|
|
|
if l[i] == nil || l[j] == nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-04-19 17:53:00 -06:00
|
|
|
if l[i].Rel == l[j].Rel {
|
|
|
|
return l[i].Type < l[j].Type
|
|
|
|
}
|
2023-01-11 19:42:06 -07:00
|
|
|
return l[i].Rel < l[j].Rel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap swaps the elements with indexes i and j.
|
|
|
|
func (l Links) Swap(i int, j int) {
|
|
|
|
if l == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l[i], l[j] = l[j], l[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseJRD parses the JRD using json.Unmarshal.
|
|
|
|
func ParseJRD(blob []byte) (*JRD, error) {
|
|
|
|
jrd := JRD{}
|
|
|
|
err := json.Unmarshal(blob, &jrd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-19 17:53:00 -06:00
|
|
|
for i := range jrd.Links {
|
|
|
|
jrd.Links[i].Index = uint64(i)
|
|
|
|
}
|
2023-01-11 19:42:06 -07:00
|
|
|
return &jrd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLinkByRel returns the first *Link with the specified rel value.
|
|
|
|
func (jrd *JRD) GetLinkByRel(rel string) *Link {
|
|
|
|
for _, link := range jrd.Links {
|
|
|
|
if link.Rel == rel {
|
|
|
|
return link
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-19 17:53:00 -06:00
|
|
|
// GetLinksByRel returns each *Link with the specified rel value.
|
2023-01-15 17:00:25 -07:00
|
|
|
func (jrd *JRD) GetLinksByRel(rel ...string) []*Link {
|
|
|
|
var lis []*Link
|
|
|
|
rels := set.New(rel...)
|
|
|
|
|
|
|
|
for _, link := range jrd.Links {
|
|
|
|
if rels.Has(link.Rel) {
|
|
|
|
lis = append(lis, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lis
|
|
|
|
}
|
|
|
|
|
2023-01-11 19:42:06 -07:00
|
|
|
// GetProperty Returns the property value as a string.
|
|
|
|
// Per spec a property value can be null, empty string is returned in this case.
|
|
|
|
func (jrd *JRD) GetProperty(uri string) string {
|
|
|
|
if jrd.Properties[uri] == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return *jrd.Properties[uri]
|
|
|
|
}
|
2023-01-11 21:25:20 -07:00
|
|
|
func (a *JRD) SetProperty(name string, value *string) {
|
|
|
|
if a.Properties == nil {
|
|
|
|
a.Properties = make(map[string]*string)
|
|
|
|
}
|
|
|
|
a.Properties[name] = value
|
|
|
|
}
|
2023-01-15 17:00:25 -07:00
|
|
|
func (a *JRD) DeleteProperty(name string) {
|
|
|
|
if a.Properties == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(a.Properties, name)
|
|
|
|
}
|
2023-01-11 21:25:20 -07:00
|
|
|
func (a *JRD) IsDeleted() bool {
|
|
|
|
return a.deleted
|
|
|
|
}
|
2023-01-11 19:42:06 -07:00
|
|
|
|
|
|
|
// GetProperty Returns the property value as a string.
|
|
|
|
// Per spec a property value can be null, empty string is returned in this case.
|
|
|
|
func (link *Link) GetProperty(uri string) string {
|
|
|
|
if link.Properties[uri] == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return *link.Properties[uri]
|
|
|
|
}
|
|
|
|
func (link *Link) SetProperty(name string, value *string) {
|
|
|
|
if link.Properties == nil {
|
|
|
|
link.Properties = make(map[string]*string)
|
|
|
|
}
|
|
|
|
link.Properties[name] = value
|
|
|
|
}
|
2023-01-15 17:00:25 -07:00
|
|
|
func (link *Link) DeleteProperty(name string) {
|
|
|
|
if link.Properties == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(link.Properties, name)
|
|
|
|
}
|
2023-01-11 19:42:06 -07:00
|
|
|
|
|
|
|
// ApplyEvent implements event.Aggregate
|
|
|
|
func (a *JRD) ApplyEvent(events ...event.Event) {
|
|
|
|
for _, e := range events {
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *SubjectSet:
|
2023-01-11 21:25:20 -07:00
|
|
|
a.deleted = false
|
|
|
|
|
2023-01-11 19:42:06 -07:00
|
|
|
a.Subject = e.Subject
|
|
|
|
a.Aliases = e.Aliases
|
|
|
|
a.Properties = e.Properties
|
|
|
|
|
|
|
|
case *SubjectDeleted:
|
|
|
|
a.deleted = true
|
|
|
|
|
2023-01-15 17:00:25 -07:00
|
|
|
a.Subject = e.Subject
|
2023-01-11 19:42:06 -07:00
|
|
|
a.Aliases = a.Aliases[:0]
|
|
|
|
a.Links = a.Links[:0]
|
|
|
|
a.Properties = map[string]*string{}
|
|
|
|
|
|
|
|
case *LinkSet:
|
2023-04-19 17:53:00 -06:00
|
|
|
link, ok := slice.FindFn(func(l *Link) bool { return l.Index == e.Index }, a.Links...)
|
2023-01-11 19:42:06 -07:00
|
|
|
if !ok {
|
|
|
|
link = &Link{}
|
2023-04-19 17:53:00 -06:00
|
|
|
link.Index = uint64(len(a.Links))
|
2023-01-11 19:42:06 -07:00
|
|
|
a.Links = append(a.Links, link)
|
|
|
|
}
|
|
|
|
|
2023-04-19 17:53:00 -06:00
|
|
|
link.Rel = e.Rel
|
2023-01-11 19:42:06 -07:00
|
|
|
link.HRef = e.HRef
|
|
|
|
link.Type = e.Type
|
|
|
|
link.Titles = e.Titles
|
|
|
|
link.Properties = e.Properties
|
|
|
|
|
|
|
|
case *LinkDeleted:
|
2023-04-19 17:53:00 -06:00
|
|
|
a.Links = slice.FilterFn(func(link *Link) bool { return link.Index != e.Index }, a.Links...)
|
2023-01-11 19:42:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 17:00:25 -07:00
|
|
|
const NSauth = "https://sour.is/ns/auth"
|
|
|
|
const NSpubkey = "https://sour.is/ns/pubkey"
|
|
|
|
const NSredirect = "https://sour.is/rel/redirect"
|
2023-01-11 19:42:06 -07:00
|
|
|
|
2023-01-15 17:00:25 -07:00
|
|
|
func (a *JRD) OnAuth(claim, auth *JRD) error {
|
|
|
|
pubkey := claim.Properties[NSpubkey]
|
2023-01-11 21:25:20 -07:00
|
|
|
|
2023-01-15 17:00:25 -07:00
|
|
|
if v, ok := auth.Properties[NSpubkey]; ok && v != nil && cmpPtr(v, pubkey) {
|
2023-01-11 21:25:20 -07:00
|
|
|
// pubkey matches!
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("pubkey does not match")
|
|
|
|
}
|
|
|
|
|
2023-01-15 17:00:25 -07:00
|
|
|
if a.Version() > 0 && !a.IsDeleted() && a.Subject != claim.Subject {
|
2023-01-11 21:25:20 -07:00
|
|
|
return fmt.Errorf("subject does not match")
|
|
|
|
}
|
|
|
|
|
2023-01-15 17:00:25 -07:00
|
|
|
if auth.Subject == claim.Subject {
|
|
|
|
claim.SetProperty(NSpubkey, pubkey)
|
|
|
|
} else {
|
|
|
|
claim.SetProperty(NSauth, &auth.Subject)
|
|
|
|
claim.DeleteProperty(NSpubkey)
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:25:20 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-15 17:00:25 -07:00
|
|
|
func (a *JRD) OnDelete(jrd *JRD) error {
|
|
|
|
if a.Version() == 0 || a.IsDeleted() {
|
|
|
|
return nil
|
2023-01-11 19:42:06 -07:00
|
|
|
}
|
|
|
|
|
2023-01-15 17:00:25 -07:00
|
|
|
event.Raise(a, &SubjectDeleted{Subject: jrd.Subject})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *JRD) OnClaims(jrd *JRD) error {
|
2023-01-11 19:42:06 -07:00
|
|
|
|
|
|
|
err := a.OnSubjectSet(jrd.Subject, jrd.Aliases, jrd.Properties)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, z := range slice.Align(
|
|
|
|
jrd.Links,
|
|
|
|
a.Links,
|
2023-04-19 17:53:00 -06:00
|
|
|
func(l, r *Link) bool { return l.Index < r.Index },
|
2023-01-11 19:42:06 -07:00
|
|
|
) {
|
|
|
|
// Not in new == delete
|
|
|
|
if z.Key == nil {
|
|
|
|
link := *z.Value
|
2023-04-19 17:53:00 -06:00
|
|
|
event.Raise(a, &LinkDeleted{Index: link.Index, Rel: link.Rel})
|
2023-01-11 19:42:06 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not in old == create
|
|
|
|
if z.Value == nil {
|
|
|
|
link := *z.Key
|
|
|
|
event.Raise(a, &LinkSet{
|
2023-04-19 17:53:00 -06:00
|
|
|
Index: link.Index,
|
2023-01-11 19:42:06 -07:00
|
|
|
Rel: link.Rel,
|
|
|
|
Type: link.Type,
|
|
|
|
HRef: link.HRef,
|
|
|
|
Titles: link.Titles,
|
|
|
|
Properties: link.Properties,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// in both == compare
|
|
|
|
a.OnLinkSet(*z.Key, *z.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *JRD) OnSubjectSet(subject string, aliases []string, props map[string]*string) error {
|
|
|
|
modified := false
|
|
|
|
e := &SubjectSet{
|
|
|
|
Subject: subject,
|
|
|
|
Aliases: aliases,
|
|
|
|
Properties: props,
|
|
|
|
}
|
|
|
|
|
|
|
|
if subject != a.Subject {
|
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(aliases)
|
|
|
|
sort.Strings(a.Aliases)
|
|
|
|
for _, z := range slice.Zip(aliases, a.Aliases) {
|
|
|
|
if z.Key != z.Value {
|
|
|
|
modified = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, z := range slice.Zip(
|
|
|
|
slice.Zip(slice.FromMap(props)),
|
|
|
|
slice.Zip(slice.FromMap(a.Properties)),
|
|
|
|
) {
|
2023-01-11 21:25:20 -07:00
|
|
|
newValue := z.Key
|
|
|
|
curValue := z.Value
|
|
|
|
|
|
|
|
if newValue.Key != curValue.Key {
|
|
|
|
modified = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cmpPtr(newValue.Value, curValue.Value) {
|
2023-01-11 19:42:06 -07:00
|
|
|
modified = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if modified {
|
|
|
|
event.Raise(a, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *JRD) OnLinkSet(o, n *Link) error {
|
|
|
|
modified := false
|
|
|
|
e := &LinkSet{
|
2023-04-19 17:53:00 -06:00
|
|
|
Index: n.Index,
|
2023-01-11 19:42:06 -07:00
|
|
|
Rel: n.Rel,
|
|
|
|
Type: n.Type,
|
|
|
|
HRef: n.HRef,
|
|
|
|
Titles: n.Titles,
|
|
|
|
Properties: n.Properties,
|
|
|
|
}
|
|
|
|
|
2023-04-19 17:53:00 -06:00
|
|
|
// if n.Index != o.Index {
|
|
|
|
// fmt.Println(342)
|
|
|
|
// modified = true
|
|
|
|
// }
|
2023-01-11 19:42:06 -07:00
|
|
|
if n.Rel != o.Rel {
|
2023-04-19 17:53:00 -06:00
|
|
|
fmt.Println(346)
|
2023-01-11 19:42:06 -07:00
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
if n.Type != o.Type {
|
2023-04-19 17:53:00 -06:00
|
|
|
fmt.Println(350)
|
|
|
|
|
2023-01-11 19:42:06 -07:00
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
if n.HRef != o.HRef {
|
2023-04-19 17:53:00 -06:00
|
|
|
fmt.Println(355)
|
|
|
|
|
2023-01-11 19:42:06 -07:00
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
|
2023-04-19 17:53:00 -06:00
|
|
|
nKeys := slice.FromMapKeys(n.Properties)
|
|
|
|
sort.Strings(nKeys)
|
|
|
|
|
|
|
|
oKeys := slice.FromMapKeys(o.Properties)
|
|
|
|
sort.Strings(oKeys)
|
|
|
|
|
2023-01-11 19:42:06 -07:00
|
|
|
for _, z := range slice.Zip(
|
2023-04-19 17:53:00 -06:00
|
|
|
slice.Zip(nKeys, slice.FromMapValues(n.Titles, nKeys)),
|
|
|
|
slice.Zip(oKeys, slice.FromMapValues(o.Titles, oKeys)),
|
2023-01-11 19:42:06 -07:00
|
|
|
) {
|
|
|
|
if z.Key != z.Value {
|
2023-04-19 17:53:00 -06:00
|
|
|
fmt.Println(365)
|
|
|
|
|
2023-01-11 19:42:06 -07:00
|
|
|
modified = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 17:53:00 -06:00
|
|
|
nKeys = slice.FromMapKeys(n.Properties)
|
|
|
|
sort.Strings(nKeys)
|
|
|
|
|
|
|
|
oKeys = slice.FromMapKeys(o.Properties)
|
|
|
|
sort.Strings(oKeys)
|
|
|
|
|
2023-01-11 19:42:06 -07:00
|
|
|
for _, z := range slice.Zip(
|
2023-04-19 17:53:00 -06:00
|
|
|
slice.Zip(nKeys, slice.FromMapValues(n.Properties, nKeys)),
|
|
|
|
slice.Zip(oKeys, slice.FromMapValues(o.Properties, oKeys)),
|
2023-01-11 19:42:06 -07:00
|
|
|
) {
|
2023-01-11 21:25:20 -07:00
|
|
|
newValue := z.Key
|
|
|
|
curValue := z.Value
|
|
|
|
|
|
|
|
if newValue.Key != curValue.Key {
|
2023-04-19 17:53:00 -06:00
|
|
|
fmt.Println(380, newValue.Key, curValue.Key)
|
|
|
|
|
2023-01-11 21:25:20 -07:00
|
|
|
modified = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cmpPtr(newValue.Value, curValue.Value) {
|
2023-04-19 17:53:00 -06:00
|
|
|
fmt.Println(387)
|
|
|
|
|
2023-01-11 19:42:06 -07:00
|
|
|
modified = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if modified {
|
|
|
|
event.Raise(a, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:25:20 -07:00
|
|
|
func cmpPtr[T comparable](l, r *T) bool {
|
|
|
|
if l == nil {
|
|
|
|
return r == nil
|
|
|
|
}
|
2023-01-11 19:42:06 -07:00
|
|
|
|
2023-01-11 21:25:20 -07:00
|
|
|
if r == nil {
|
|
|
|
return l == nil
|
2023-01-11 19:42:06 -07:00
|
|
|
}
|
2023-01-11 21:25:20 -07:00
|
|
|
|
|
|
|
return *l == *r
|
2023-01-11 19:42:06 -07:00
|
|
|
}
|
2023-01-15 17:00:25 -07:00
|
|
|
|
|
|
|
func (a *JRD) String() string {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
y := yaml.NewEncoder(b)
|
|
|
|
_ = y.Encode(a)
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|