Refactoring-1811

This commit is contained in:
Naoki Kosaka
2018-11-25 23:40:21 +09:00
parent 4c84636b29
commit 2e883d9aed
16 changed files with 406 additions and 200 deletions

View File

@ -5,18 +5,13 @@ import (
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"
"github.com/Songmu/go-httpdate"
"github.com/satori/go.uuid"
"github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/httpsig"
)
@ -60,112 +55,3 @@ func SendActivity(inboxURL string, KeyID string, refBytes []byte, pKey *rsa.Priv
return nil
}
// RetrieveActor : Retrieve Remote actor
func RetrieveActor(url string) (*Actor, error) {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/activity+json, application/ld+json")
req.Header.Set("User-Agent", UA_STRING)
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
var actor Actor
err = json.Unmarshal(data, &actor)
if err != nil {
return nil, err
}
return &actor, nil
}
// DescribeNestedActivity : Descrive Nested Activity Series
func DescribeNestedActivity(nestedActivity interface{}) (*Activity, error) {
mappedObject := nestedActivity.(map[string]interface{})
if id, ok := mappedObject["id"].(string); ok {
if nestedType, ok := mappedObject["type"].(string); ok {
actor, ok := mappedObject["actor"].(string)
if !ok {
actor = ""
}
switch object := mappedObject["object"].(type) {
case string:
return &Activity{
ID: id,
Type: nestedType,
Actor: actor,
Object: object,
}, nil
default:
return &Activity{
ID: id,
Type: nestedType,
Actor: actor,
Object: mappedObject["object"],
}, nil
}
}
return nil, errors.New("Can't assart type")
}
return nil, errors.New("Can't assart id")
}
// GenerateActor : Generate Actor by hostname and publickey
func GenerateActor(hostname *url.URL, publickey *rsa.PublicKey) Actor {
return Actor{
[]string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"},
hostname.String() + "/actor",
"Service",
"relay",
hostname.String() + "/inbox",
nil,
PublicKey{
hostname.String() + "/actor#main-key",
hostname.String() + "/actor",
keyloader.GeneratePublicKeyPEMString(publickey),
},
}
}
// GenerateWebfingerResource : Generate Webfinger Resource
func GenerateWebfingerResource(hostname *url.URL, actor *Actor) WebfingerResource {
return WebfingerResource{
"acct:" + actor.PreferredUsername + "@" + hostname.Host,
[]WebfingerLink{
WebfingerLink{
"self",
"application/activity+json",
actor.ID,
},
},
}
}
// GenerateActivityResponse : Generate Responce Activity to Activity
func GenerateActivityResponse(host *url.URL, to *url.URL, responseType string, activity Activity) Activity {
return Activity{
[]string{"https://www.w3.org/ns/activitystreams"},
host.String() + "/activities/" + uuid.NewV4().String(),
host.String() + "/actor",
responseType,
&activity,
nil,
nil,
}
}
// GenerateActivityAnnounce : Generate Announce Activity to Activity
func GenerateActivityAnnounce(host *url.URL, to *url.URL, actiivtyID string) Activity {
return Activity{
[]string{"https://www.w3.org/ns/activitystreams"},
host.String() + "/activities/" + uuid.NewV4().String(),
host.String() + "/actor",
"Announce",
actiivtyID,
[]string{host.String() + "/actor/followers"},
nil,
}
}

View File

@ -1,5 +1,17 @@
package activitypub
import (
"crypto/rsa"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"github.com/satori/go.uuid"
"github.com/yukimochi/Activity-Relay/KeyLoader"
)
// PublicKey : Activity Certificate.
type PublicKey struct {
ID string `json:"id"`
@ -23,6 +35,38 @@ type Actor struct {
PublicKey PublicKey `json:"publicKey"`
}
func (a *Actor) GenerateSelfKey(hostname *url.URL, publickey *rsa.PublicKey) {
a.Context = []string{"https://www.w3.org/ns/activitystreams"}
a.ID = hostname.String() + "/actor"
a.Type = "Service"
a.PreferredUsername = "relay"
a.Inbox = hostname.String() + "/inbox"
a.PublicKey = PublicKey{
hostname.String() + "/actor#main-key",
hostname.String() + "/actor",
keyloader.GeneratePublicKeyPEMString(publickey),
}
}
func (a *Actor) RetrieveRemoteActor(url string) error {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/activity+json, application/ld+json")
req.Header.Set("User-Agent", UA_STRING)
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(data, &a)
if err != nil {
return err
}
return nil
}
// Activity : ActivityPub Activity.
type Activity struct {
Context interface{} `json:"@context"`
@ -34,6 +78,60 @@ type Activity struct {
Cc []string `json:"cc"`
}
func (a *Activity) GenerateResponse(host *url.URL, responseType string) Activity {
return Activity{
[]string{"https://www.w3.org/ns/activitystreams"},
host.String() + "/activities/" + uuid.NewV4().String(),
host.String() + "/actor",
responseType,
&a,
nil,
nil,
}
}
func (a *Activity) GenerateAnnounce(host *url.URL) Activity {
return Activity{
[]string{"https://www.w3.org/ns/activitystreams"},
host.String() + "/activities/" + uuid.NewV4().String(),
host.String() + "/actor",
"Announce",
a.ID,
[]string{host.String() + "/actor/followers"},
nil,
}
}
func (a *Activity) NestedActivity() (*Activity, error) {
mappedObject := a.Object.(map[string]interface{})
if id, ok := mappedObject["id"].(string); ok {
if nestedType, ok := mappedObject["type"].(string); ok {
actor, ok := mappedObject["actor"].(string)
if !ok {
actor = ""
}
switch object := mappedObject["object"].(type) {
case string:
return &Activity{
ID: id,
Type: nestedType,
Actor: actor,
Object: object,
}, nil
default:
return &Activity{
ID: id,
Type: nestedType,
Actor: actor,
Object: mappedObject["object"],
}, nil
}
}
return nil, errors.New("Can't assart type")
}
return nil, errors.New("Can't assart id")
}
// Signature : ActivityPub Header Signature.
type Signature struct {
Type string `json:"type"`
@ -42,15 +140,26 @@ type Signature struct {
SignatureValue string `json:"signatureValue"`
}
// WebfingerResource : Webfinger Resource.
type WebfingerResource struct {
Subject string `json:"subject"`
Links []WebfingerLink `json:"links"`
}
// WebfingerLink : Webfinger Link Resource.
type WebfingerLink struct {
Rel string `json:"rel"`
Type string `json:"type"`
Href string `json:"href"`
}
// WebfingerResource : Webfinger Resource.
type WebfingerResource struct {
Subject string `json:"subject"`
Links []WebfingerLink `json:"links"`
}
func (a *WebfingerResource) GenerateFromActor(hostname *url.URL, actor *Actor) {
a.Subject = "acct:" + actor.PreferredUsername + "@" + hostname.Host
a.Links = []WebfingerLink{
WebfingerLink{
"self",
"application/activity+json",
actor.ID,
},
}
}