-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_info.go
64 lines (55 loc) · 1.89 KB
/
server_info.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
package profilefed
import (
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"net/http"
)
// ServerInfoHandler handles the server info endpoint
// defined by ProfileFed.
type ServerInfoHandler struct {
// ServerName is the current name of the server. This
// should be the same as the domain used to access it.
ServerName string
// PreviousNames should contain any previous names this server used.
PreviousNames []string
// PublicKey should contain the server's public Ed25519 key.
PublicKey ed25519.PublicKey
// PrivateKey should contain the server's private Ed25519 key.
PrivateKey ed25519.PrivateKey
// PreviousKeys should contain any previously-used private keys.
// If this is not provided when the key changes, servers will not
// trust the new key and all responses will be rejected.
PreviousKeys []ed25519.PrivateKey
// ErrorHandler is called whenever an error is encountered.
ErrorHandler func(err error, res http.ResponseWriter)
}
type serverInfoData struct {
ServerName string `json:"server_name"`
PreviousNames []string `json:"previous_names"`
PublicKey string `json:"pubkey"`
}
// ServeHTTP implements the http.Handler interface
func (sih ServerInfoHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
data, err := json.Marshal(serverInfoData{
ServerName: sih.ServerName,
PreviousNames: sih.PreviousNames,
PublicKey: base64.StdEncoding.EncodeToString(sih.PublicKey),
})
if err != nil {
sih.ErrorHandler(err, res)
return
}
for _, key := range sih.PreviousKeys {
sig := ed25519.Sign(key, data)
res.Header().Add("X-ProfileFed-Previous", base64.StdEncoding.EncodeToString(sig))
}
sig := ed25519.Sign(sih.PrivateKey, data)
res.Header().Set("X-ProfileFed-Sig", base64.StdEncoding.EncodeToString(sig))
res.Header().Set("Content-Type", "application/json")
_, err = res.Write(data)
if err != nil {
sih.ErrorHandler(err, res)
return
}
}