upload files

This commit is contained in:
doesnm 2024-05-31 20:41:29 +03:00
parent 7bed027302
commit 07cd0a20c8
3 changed files with 121 additions and 18 deletions

View file

@ -3,6 +3,7 @@ package activityserve
import (
"bytes"
"encoding/json"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
@ -20,7 +21,7 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"crypto/ed25519"
"github.com/dchest/uniuri"
"github.com/go-fed/httpsig"
)
@ -41,6 +42,9 @@ type Actor struct {
privateKey crypto.PrivateKey
publicKeyPem string
privateKeyPem string
ed25519PublicKey ed25519.PublicKey
ed25519PrivateKey ed25519.PrivateKey
public string
publicKeyID string
OnFollow func(map[string]interface{})
OnReceiveContent func(map[string]interface{})
@ -56,8 +60,8 @@ type ActorToSave struct {
Name, Summary, ActorType, IRI, PublicKey, PrivateKey string
Followers, Following, Rejected, Requested map[string]interface{}
Attachment []interface {}
Ed25519PrivateKey,Ed25519PublicKey string
}
// MakeActor creates and returns a new local actor we can act
// on behalf of. It also creates its files on disk
func MakeActor(name, summary, actorType string) (Actor, error) {
@ -96,8 +100,14 @@ func MakeActor(name, summary, actorType string) (Actor, error) {
// create actor's keypair
rng := rand.Reader
privateKey, err := rsa.GenerateKey(rng, 2048)
e2_pub, e2_priv,err := ed25519.GenerateKey(rng)
if err != nil {
log.Info("Can't create ed25519 keys")
log.Info(err)
}
actor.ed25519PrivateKey = e2_priv
actor.ed25519PublicKey = e2_pub
publicKey := privateKey.PublicKey
actor.publicKey = publicKey
actor.privateKey = privateKey
@ -190,7 +200,8 @@ func LoadActor(name string) (Actor, error) {
log.Info(err)
return Actor{}, err
}
decodeEd2PublicKey,err := base64.StdEncoding.DecodeString(jsonData["Ed25519PublicKey"].(string))
decodeEd2PrivateKey,err := base64.StdEncoding.DecodeString(jsonData["Ed25519PrivateKey"].(string))
actor := Actor{
Name: name,
summary: jsonData["Summary"].(string),
@ -208,6 +219,8 @@ func LoadActor(name string) (Actor, error) {
followersIRI: baseURL + name + "/followers",
publicKeyID: baseURL + name + "#main-key",
attachment: jsonData["Attachment"].([]interface{}),
ed25519PublicKey: decodeEd2PublicKey,
ed25519PrivateKey: decodeEd2PrivateKey,
}
actor.OnFollow = func(activity map[string]interface{}) { actor.Accept(activity) }
@ -262,7 +275,8 @@ func (a *Actor) save() error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, 0755)
}
encodedEd25519pub := base64.StdEncoding.EncodeToString(a.ed25519PublicKey)
encodedEd25519priv := base64.StdEncoding.EncodeToString(a.ed25519PrivateKey)
actorToSave := ActorToSave{
Name: a.Name,
Summary: a.summary,
@ -275,8 +289,9 @@ func (a *Actor) save() error {
PublicKey: a.publicKeyPem,
PrivateKey: a.privateKeyPem,
Attachment: a.attachment,
Ed25519PublicKey: encodedEd25519pub,
Ed25519PrivateKey: encodedEd25519priv,
}
actorJSON, err := json.MarshalIndent(actorToSave, "", "\t")
if err != nil {
log.Info("error Marshalling actor json")
@ -294,22 +309,22 @@ func (a *Actor) save() error {
}
func (a *Actor) whoAmI() string {
encoded := base64.StdEncoding.EncodeToString(a.ed25519PublicKey)
self := make(map[string]interface{})
self["@context"] = context()
self["type"] = a.actorType
self["id"] = baseURL + a.Name
self["name"] = a.Name
self["id"] = baseURL + ".well-known/apgateway/" + encoded + "/actor"
self["name"] = nil
self["preferredUsername"] = a.Name
self["summary"] = a.summary
self["inbox"] = baseURL + a.Name + "/inbox"
self["outbox"] = baseURL + a.Name + "/outbox"
self["followers"] = baseURL + a.Name + "/peers/followers"
self["following"] = baseURL + a.Name + "/peers/following"
self["inbox"] = baseURL + ".well-known/apgateway/" + encoded + "/inbox"
self["outbox"] = baseURL + ".well-known/apgateway/" + encoded + "/outbox"
self["followers"] = baseURL + ".well-known/apgateway/" + encoded + "/peers/followers"
self["following"] = baseURL + ".well-known/apgateway/" + encoded + "/peers/following"
self["attachment"] = a.attachment
self["publicKey"] = map[string]string{
"id": baseURL + a.Name + "#main-key",
"owner": baseURL + a.Name,
"id": baseURL + ".well-known/apgateway/" + encoded + "#main-key",
"owner": baseURL + ".well-known/apgateway/" + encoded + "/actor",
"publicKeyPem": a.publicKeyPem,
}
selfString, _ := json.Marshal(self)

View file

@ -364,7 +364,7 @@ func Serve(actors map[string]Actor) {
gorilla.HandleFunc("/{actor}/inbox", inboxHandler)
gorilla.HandleFunc("/{actor}/inbox/", inboxHandler)
gorilla.HandleFunc("/{actor}/", actorHandler)
gorilla.HandleFunc("/{actor}", actorHandler)
gorilla.HandleFunc("/.well-known/apgateway/{actor}/actor", actorHandler)
gorilla.HandleFunc("/{actor}/item/{hash}", postHandler)
http.Handle("/", gorilla)

88
silverpill.json Normal file
View file

@ -0,0 +1,88 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://www.w3.org/ns/did/v1",
"https://w3id.org/security/v1",
"https://w3id.org/security/data-integrity/v1",
"https://w3id.org/security/multikey/v1",
{
"MitraJcsEip191Signature2022": "mitra:MitraJcsEip191Signature2022",
"PropertyValue": "schema:PropertyValue",
"VerifiableIdentityStatement": "mitra:VerifiableIdentityStatement",
"featured": "toot:featured",
"gateways": "mitra:gateways",
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"mitra": "http://jsonld.mitra.social#",
"proofPurpose": "sec:proofPurpose",
"proofValue": "sec:proofValue",
"sameAs": "schema:sameAs",
"schema": "http://schema.org/",
"subscribers": "mitra:subscribers",
"toot": "http://joinmastodon.org/ns#",
"value": "schema:value"
}
],
"alsoKnownAs": [
"https://mitra.social/users/followbot"
],
"assertionMethod": [
{
"controller": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor",
"id": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor#main-key",
"publicKeyMultibase": "z4MXj1wBzi9jUstyPArehoipP2vta2h8poW9baePvKQHTtZp8cdQKNkx3qSpQYPwzo9CoD75xaiaPLaHYFybdsTKLJAf8if2GiXCTf96BALKxyy9bLgSUUaxX9VsvV5Z7FtHNCBnvuuF1sqtbSBq8Ex9KgZFgZjXXTdFBwA6ad3F2okUXQYY9wjUpdCouchnUHxKrBnTTPz8cAJKXMdR6DpCUCFfdyjW7oDdaWqrUHBYZisFTouFWuogUkRb2MjWnjps7FtqqKSszpiSF9W7UxB4DdnQ3jvgqdZ7avWKPSJDVTeTRLP6iuv6XvSShLk8tEDJfornKFczuv2MuDiQTTYR9xAQM7htD31HPN1vAcPzEQ7krYRkU",
"type": "Multikey"
},
{
"controller": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor",
"id": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor#ed25519-key",
"publicKeyMultibase": "z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf",
"type": "Multikey"
}
],
"authentication": [
{
"controller": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor",
"id": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor#main-key",
"publicKeyMultibase": "z4MXj1wBzi9jUstyPArehoipP2vta2h8poW9baePvKQHTtZp8cdQKNkx3qSpQYPwzo9CoD75xaiaPLaHYFybdsTKLJAf8if2GiXCTf96BALKxyy9bLgSUUaxX9VsvV5Z7FtHNCBnvuuF1sqtbSBq8Ex9KgZFgZjXXTdFBwA6ad3F2okUXQYY9wjUpdCouchnUHxKrBnTTPz8cAJKXMdR6DpCUCFfdyjW7oDdaWqrUHBYZisFTouFWuogUkRb2MjWnjps7FtqqKSszpiSF9W7UxB4DdnQ3jvgqdZ7avWKPSJDVTeTRLP6iuv6XvSShLk8tEDJfornKFczuv2MuDiQTTYR9xAQM7htD31HPN1vAcPzEQ7krYRkU",
"type": "Multikey"
},
{
"controller": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor",
"id": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor#ed25519-key",
"publicKeyMultibase": "z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf",
"type": "Multikey"
}
],
"featured": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor/collections/featured",
"followers": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor/followers",
"following": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor/following",
"gateways": [
"https://public.mitra.social"
],
"id": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor",
"inbox": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor/inbox",
"manuallyApprovesFollowers": false,
"name": null,
"outbox": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor/outbox",
"preferredUsername": "silverpill",
"proof": {
"created": "2024-05-29T08:08:06.974875078Z",
"cryptosuite": "eddsa-jcs-2022",
"proofPurpose": "assertionMethod",
"proofValue": "z2GrXezAbsUh3jms2aer3MfRSLyKqqiei52t7Gie49YvWkmzfxK1Vqg3aNVD9hXMgCQdg1KLfRKZV7SLywbSzGnfJ",
"type": "DataIntegrityProof",
"verificationMethod": "did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf"
},
"publicKey": {
"id": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor#main-key",
"owner": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor",
"publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqcnHfgupRhkrzywzMnik\ncEg2WkfLiCvAxTpAlaDOPTdkd9MPPVhceNCeRpLzlsNeerU/A/9G/Oug7y9/0Aln\n7EL0NQvR47y6p0jegGguvsWMWwtWKcSg5uFAgEP93RFnqr/+S3KKZe4LrluYIVMu\n4tzsvZtJ7VxLsbDoXH59KtpspDQxk5fFKFk7Hsi3ba0zguxWibO5WhfqruFD1Fj+\nghuZtjMXaMcGpYctcz+R+uK3AHDI7Iqdzl8rjBB2exBS/VFXf4duFuiylZLLrCe7\n/1er0xICocaOxUhqTBcD5xI8MR3W5uFnEafWZLAgFqE1Xm2mNawHh33Nlq57Jn/e\nqQIDAQAB\n-----END PUBLIC KEY-----\n"
},
"sameAs": [
"https://public.mitra.social/users/silverpill?fep_ef61=true",
"ap://did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor"
],
"subscribers": "https://public.mitra.social/.well-known/apgateway/did:key:z6MkuESiSXv58qijkRDtV262UsFpptPWcAXNpfarp4b1WjRf/actor/subscribers",
"type": "Person",
"url": "https://public.mitra.social/users/silverpill"
}