Skip to content

Commit

Permalink
add follow accept code
Browse files Browse the repository at this point in the history
  • Loading branch information
juunini committed Oct 25, 2023
1 parent 6cb0b3f commit 4de7b57
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
6 changes: 6 additions & 0 deletions models/user_keypair.go
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
}
72 changes: 72 additions & 0 deletions router/user/inbox/follow_accept.go
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
}
4 changes: 4 additions & 0 deletions router/user/inbox/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func Route(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusBadRequest)
}

if messageType == "Follow" {
followAccept(originBody, id, actor)
}

db.DB.Save(&models.UserInbox{
ID: uuid.New(),
From: actor,
Expand Down
2 changes: 1 addition & 1 deletion router/user/inbox/verify_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func fetchActorPublicKey(actor string) (string, error) {
agent.Request().Header.Set("Accept", "application/ld+json")

statusCode, response, errs := agent.Bytes()
if errs != nil {
if len(errs) > 0 {
return "", errs[0]
}

Expand Down

0 comments on commit 4de7b57

Please sign in to comment.