2019-09-10 11:21:39 +03:00
|
|
|
package activityserve
|
|
|
|
|
|
|
|
import (
|
2019-11-08 11:41:06 +03:00
|
|
|
"encoding/json"
|
2024-05-16 22:31:02 +03:00
|
|
|
"encoding/pem"
|
2019-11-08 11:41:06 +03:00
|
|
|
"github.com/gologme/log"
|
2024-05-16 22:31:02 +03:00
|
|
|
"crypto/x509"
|
|
|
|
"crypto/rsa"
|
2019-09-10 11:21:39 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// RemoteActor is a type that holds an actor
|
2024-05-16 22:31:02 +03:00
|
|
|
// that we want to interact with
|
2019-09-10 11:21:39 +03:00
|
|
|
type RemoteActor struct {
|
2019-10-10 19:56:21 +03:00
|
|
|
iri, outbox, inbox, sharedInbox string
|
2023-04-28 21:02:47 +03:00
|
|
|
url string
|
2019-10-10 19:56:21 +03:00
|
|
|
info map[string]interface{}
|
2024-05-16 22:31:02 +03:00
|
|
|
publicKey *rsa.PublicKey
|
2024-06-10 12:54:10 +03:00
|
|
|
la Actor
|
2019-09-10 11:21:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRemoteActor returns a remoteActor which holds
|
|
|
|
// all the info required for an actor we want to
|
|
|
|
// interact with (not essentially sitting in our instance)
|
2024-06-10 12:54:10 +03:00
|
|
|
func NewRemoteActor(a Actor,iri string) (RemoteActor, error) {
|
|
|
|
info, err := get(a,iri)
|
2019-09-10 11:21:39 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Info("Couldn't get remote actor information")
|
|
|
|
log.Info(err)
|
|
|
|
return RemoteActor{}, err
|
|
|
|
}
|
2024-05-16 22:31:02 +03:00
|
|
|
publicKeyBlock := info["publicKey"].(map[string]interface {})
|
|
|
|
publicKeyPem := publicKeyBlock["publicKeyPem"].(string)
|
|
|
|
spkiBlock, _ := pem.Decode([]byte(publicKeyPem))
|
|
|
|
var spkiKey *rsa.PublicKey
|
|
|
|
pubInterface, _ := x509.ParsePKIXPublicKey(spkiBlock.Bytes)
|
|
|
|
spkiKey = pubInterface.(*rsa.PublicKey)
|
2020-04-09 17:58:33 +03:00
|
|
|
outbox, _ := info["outbox"].(string)
|
|
|
|
inbox, _ := info["inbox"].(string)
|
2023-04-28 21:02:47 +03:00
|
|
|
url, _ := info["url"].(string)
|
2019-10-11 10:08:24 +03:00
|
|
|
var endpoints map[string]interface{}
|
|
|
|
var sharedInbox string
|
|
|
|
if info["endpoints"] != nil {
|
|
|
|
endpoints = info["endpoints"].(map[string]interface{})
|
2019-10-14 12:01:07 +03:00
|
|
|
if val, ok := endpoints["sharedInbox"]; ok {
|
|
|
|
sharedInbox = val.(string)
|
2019-11-08 11:41:06 +03:00
|
|
|
}
|
2019-10-11 10:08:24 +03:00
|
|
|
}
|
2019-09-10 11:21:39 +03:00
|
|
|
|
|
|
|
return RemoteActor{
|
2019-10-10 19:56:21 +03:00
|
|
|
iri: iri,
|
|
|
|
outbox: outbox,
|
|
|
|
inbox: inbox,
|
|
|
|
sharedInbox: sharedInbox,
|
2023-04-28 21:02:47 +03:00
|
|
|
url: url,
|
2024-05-16 22:31:02 +03:00
|
|
|
publicKey: spkiKey,
|
2024-06-10 12:54:10 +03:00
|
|
|
la: a,
|
2019-09-10 11:21:39 +03:00
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ra RemoteActor) getLatestPosts(number int) (map[string]interface{}, error) {
|
2024-06-10 12:54:10 +03:00
|
|
|
return get(ra.la,ra.outbox)
|
2019-09-10 11:21:39 +03:00
|
|
|
}
|
|
|
|
|
2024-06-10 12:54:10 +03:00
|
|
|
func get(a Actor,iri string) (info map[string]interface{}, err error) {
|
2019-09-10 11:21:39 +03:00
|
|
|
|
2024-06-10 12:54:10 +03:00
|
|
|
// sactor,err := GetActor("server","Internal service actor","Service")
|
2019-09-10 11:21:39 +03:00
|
|
|
if err != nil {
|
2024-05-16 22:31:02 +03:00
|
|
|
log.Info("Failed to get service actor")
|
2019-09-10 11:21:39 +03:00
|
|
|
log.Info(err)
|
|
|
|
return
|
|
|
|
}
|
2024-06-10 12:54:10 +03:00
|
|
|
responseData,err := a.signedHTTPGet(iri)
|
2019-09-10 11:21:39 +03:00
|
|
|
if err != nil {
|
2024-05-16 22:31:02 +03:00
|
|
|
log.Info("Failed to make signed http get request")
|
2019-09-10 11:21:39 +03:00
|
|
|
log.Info(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var e interface{}
|
2024-05-16 22:31:02 +03:00
|
|
|
err = json.Unmarshal([]byte(responseData), &e)
|
2019-09-10 11:21:39 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Info("something went wrong when unmarshalling the json")
|
|
|
|
log.Info(err)
|
2019-09-20 16:21:21 +03:00
|
|
|
return
|
2019-09-10 11:21:39 +03:00
|
|
|
}
|
|
|
|
info = e.(map[string]interface{})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-10-08 15:22:59 +03:00
|
|
|
|
|
|
|
// GetInbox returns the inbox url of the actor
|
|
|
|
func (ra RemoteActor) GetInbox() string {
|
|
|
|
return ra.inbox
|
|
|
|
}
|
2019-10-10 19:56:21 +03:00
|
|
|
|
|
|
|
// GetSharedInbox returns the inbox url of the actor
|
|
|
|
func (ra RemoteActor) GetSharedInbox() string {
|
2019-10-11 10:08:24 +03:00
|
|
|
if ra.sharedInbox == "" {
|
|
|
|
return ra.inbox
|
|
|
|
}
|
2019-10-10 19:56:21 +03:00
|
|
|
return ra.sharedInbox
|
|
|
|
}
|
2023-04-28 21:02:47 +03:00
|
|
|
|
|
|
|
func (ra RemoteActor) URL() string {
|
|
|
|
return ra.url
|
|
|
|
}
|