-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
package models | ||
|
||
import "sample/db" | ||
|
||
type UserKeyPair struct { | ||
ID string `gorm:"primaryKey"` | ||
PublicKey string `gorm:"not null;type:text"` | ||
PrivateKey string `gorm:"not null;type:text"` | ||
} | ||
|
||
func (u *UserKeyPair) GetByID() error { | ||
return db.DB.First(&u, "id = ?", u.ID).Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package inbox | ||
|
||
import ( | ||
"crypto" | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
"sample/constants" | ||
"sample/models" | ||
|
||
signature_header "github.com/cloudmatelabs/go-activitypub-signature-header" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func followAccept(body []byte, id string, actor string) error { | ||
var followRequest map[string]interface{} | ||
json.Unmarshal(body, &followRequest) | ||
delete(followRequest, "@context") | ||
|
||
userURL := fmt.Sprintf(constants.USER_JSON_URL_FORMAT, constants.APP_ADDRESS, id) | ||
|
||
acceptBody, _ := json.Marshal(fiber.Map{ | ||
"@context": "https://www.w3.org/ns/activitystreams", | ||
"object": followRequest, | ||
"type": "Accept", | ||
"id": userURL, | ||
"actor": userURL, | ||
}) | ||
|
||
actorURL, _ := url.Parse(actor) | ||
path := actorURL.Path + "/inbox" | ||
keyID := userURL + "#main-key" | ||
|
||
keyPair := models.UserKeyPair{ID: id} | ||
keyPair.GetByID() | ||
|
||
privateKey, err := signature_header.PrivateKeyFromBytes([]byte(keyPair.PrivateKey)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
algorithm := crypto.SHA256 | ||
date := signature_header.Date() | ||
digest := signature_header.Digest(algorithm, acceptBody) | ||
signature, err := signature_header.Signature{ | ||
PrivateKey: privateKey, | ||
Algorithm: algorithm, | ||
Date: date, | ||
Digest: digest, | ||
Host: actorURL.Host, | ||
Path: path, | ||
KeyID: keyID, | ||
}.String() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
agent := fiber.Post(actor + "/inbox") | ||
agent.Request().Header.Set("Date", date) | ||
agent.Request().Header.Set("Digest", digest) | ||
agent.Request().Header.Set("Host", actorURL.Host) | ||
agent.Request().Header.Set("Signature", signature) | ||
agent.ContentType("application/activity+json") | ||
agent.Body(acceptBody) | ||
|
||
_, _, errs := agent.Bytes() | ||
if len(errs) > 0 { | ||
return errs[0] | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters