@ -1,5 +1,5 @@
/ *
/ *
* Copyright © 2018 - 2020 A Bunch Tell LLC .
* Copyright © 2018 - 2021 A Bunch Tell LLC .
*
*
* This file is part of WriteFreely .
* This file is part of WriteFreely .
*
*
@ -17,10 +17,12 @@ import (
"encoding/base64"
"encoding/base64"
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"github.com/writeas/writefreely/config"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
"net/http/httputil"
"net/http/httputil"
"net/url"
"net/url"
"path/filepath"
"strconv"
"strconv"
"time"
"time"
@ -41,6 +43,17 @@ const (
apCacheTime = time . Minute
apCacheTime = time . Minute
)
)
var instanceColl * Collection
func initActivityPub ( cfg * config . Config ) {
ur , _ := url . Parse ( cfg . App . Host )
instanceColl = & Collection {
ID : 0 ,
Alias : ur . Host ,
Title : ur . Host ,
}
}
type RemoteUser struct {
type RemoteUser struct {
ID int64
ID int64
ActorID string
ActorID string
@ -76,12 +89,17 @@ func handleFetchCollectionActivities(app *App, w http.ResponseWriter, r *http.Re
vars := mux . Vars ( r )
vars := mux . Vars ( r )
alias := vars [ "alias" ]
alias := vars [ "alias" ]
if alias == "" {
alias = filepath . Base ( r . RequestURI )
}
// TODO: enforce visibility
// TODO: enforce visibility
// Get base Collection data
// Get base Collection data
var c * Collection
var c * Collection
var err error
var err error
if app . cfg . App . SingleUser {
if alias == r . Host {
c = instanceColl
} else if app . cfg . App . SingleUser {
c , err = app . db . GetCollectionByID ( 1 )
c , err = app . db . GetCollectionByID ( 1 )
} else {
} else {
c , err = app . db . GetCollection ( alias )
c , err = app . db . GetCollection ( alias )
@ -546,6 +564,22 @@ func resolveIRI(hostName, url string) ([]byte, error) {
r . Header . Add ( "Accept" , "application/activity+json" )
r . Header . Add ( "Accept" , "application/activity+json" )
r . Header . Set ( "User-Agent" , ServerUserAgent ( hostName ) )
r . Header . Set ( "User-Agent" , ServerUserAgent ( hostName ) )
p := instanceColl . PersonObject ( )
h := sha256 . New ( )
h . Write ( [ ] byte { } )
r . Header . Add ( "Digest" , "SHA-256=" + base64 . StdEncoding . EncodeToString ( h . Sum ( nil ) ) )
// Sign using the 'Signature' header
privKey , err := activitypub . DecodePrivateKey ( p . GetPrivKey ( ) )
if err != nil {
return nil , err
}
signer := httpsig . NewSigner ( p . PublicKey . ID , privKey , httpsig . RSASHA256 , [ ] string { "(request-target)" , "date" , "host" , "digest" } )
err = signer . SignSigHeader ( r )
if err != nil {
log . Error ( "Can't sign: %v" , err )
}
if debugging {
if debugging {
dump , err := httputil . DumpRequestOut ( r , true )
dump , err := httputil . DumpRequestOut ( r , true )
if err != nil {
if err != nil {