-
Notifications
You must be signed in to change notification settings - Fork 4
/
webfinger.go
94 lines (86 loc) · 2.37 KB
/
webfinger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* MIT License
*
* Copyright (c) 2022 The hvxahv Authors.
*
*/
package activitypub
import (
"encoding/json"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/hvxahv/hvx/errors"
"github.com/hvxahv/hvx/mailer"
"github.com/spf13/viper"
)
//type WebFingerLinks struct {
// Rel string `json:"rel"`
// Type string `json:"type,omitempty"`
// Href string `json:"href,omitempty"`
// Template string `json:"template,omitempty"`
//}
type WebFinger struct {
Subject string `json:"subject"`
Aliases []string `json:"aliases"`
Links []struct {
Rel string `json:"rel"`
Type string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Template string `json:"template,omitempty"`
} `json:"links"`
}
// NewWebFingerAddress
// FOR EXAMPLE: https://mas.to/.well-known/webfinger?resource=acct:hvturingga@halfmemories.com
func NewWebFingerAddress(host, resource string) string {
return fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", host, resource)
}
// GetWebFingerHandler Obtain the WebFinger of the remote
// instance through the email address format.
func GetWebFingerHandler(address string) (*WebFinger, error) {
format, err := mailer.ParseEmailAddress(address)
if err != nil {
return nil, err
}
g, err := resty.New().R().Get(NewWebFingerAddress(format.Domain, address))
if err != nil {
return nil, err
}
if g.StatusCode() != 200 {
return nil, errors.New(errors.ErrWebfinger)
}
var w WebFinger
if err := json.Unmarshal(g.Body(), &w); err != nil {
return nil, err
}
return &w, nil
}
// NewWebFinger Receive the username and combine the object
// into the standard json data returned by webFinger. In order
// to return to the query that the instance owns this actor.
func NewWebFinger(name string, isChan bool) *WebFinger {
address := viper.GetString("localhost")
sub := fmt.Sprintf("acct:%s@%s", name, address)
var href string
if isChan {
href = fmt.Sprintf("https://%s/c/%s", address, name)
} else {
href = fmt.Sprintf("https://%s/u/%s", address, name)
}
return &WebFinger{
Subject: sub,
Aliases: nil,
Links: []struct {
Rel string `json:"rel"`
Type string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Template string `json:"template,omitempty"`
}{
{
Rel: "self",
Type: "application/activity+json",
Href: href,
Template: "",
},
},
}
}