Skip to content

Commit

Permalink
add generate function
Browse files Browse the repository at this point in the history
  • Loading branch information
juunini committed Oct 29, 2023
1 parent f00b8fd commit ef9649a
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 50 deletions.
48 changes: 20 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,37 @@ import (
signature_header "github.com/cloudmatelabs/go-activitypub-signature-header"
)

const privateKeyBytes = []byte("-----BEGIN RSA PRIVATE KEY-----...")
const message = []byte(`{
privateKeyBytes := []byte("-----BEGIN RSA PRIVATE KEY-----...")
message := []byte(`{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://snippet.cloudmt.co.kr/@juunini",
"id": "https://snippet.social/@juunini",
"type": "Follow",
"actor": "https://snippet.cloudmt.co.kr/@juunini",
"actor": "https://snippet.social/@juunini",
"object": "https://yodangang.express/users/9iffvxhojp"
}`)
const host := "yodangang.express"
const path := "/users/9iffvxhojp/inbox"
const keyID := "https://snippet.cloudmt.co.kr/@juunini#main-key"

privateKey, err := signature_header.PrivateKeyFromBytes(privateKeyBytes)
if err != nil {
// handle error
}

host := "yodangang.express"
path := "/users/9iffvxhojp/inbox"
keyID := "https://snippet.social/@juunini#main-key"
algorithm := crypto.SHA256
date := signature_header.Date()
digest := signature_header.Digest(algorithm, message)
signature, err := signature_header.Signature{
PrivateKey: privateKey,
Algorithm: algorithm,
Date: date,
Digest: digest,
Host: host,
Path: path,
KeyID: keyID,
}.String()

headers, err := signature_header.Generate(signature_header.GenerateInput{
PrivateKeyBytes: privateKeyBytes,
Algorithm: algorithm,
Host: host,
Path: path,
Body: message,
KeyID: keyID,
})
if err != nil {
// handle error
}

resty.New().R().
SetBody(message).
SetHeader("Date", date).
SetHeader("Digest", digest).
SetHeader("Host", host).
SetHeader("Signature", signature).
SetHeader("Date", headers.Date).
SetHeader("Digest", headers.Digest).
SetHeader("Host", headers.Host).
SetHeader("Signature", headers.Signature).
SetHeader("Content-Type", "application/activity+json").
Post("https://" + host + path)
```
Expand Down
71 changes: 71 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package signature_header

import "crypto"

/*
Example:
signature_header.GenerateInput{
PrivateKeyBytes: []byte("-----BEGIN RSA PRIVATE KEY-----..."),
Algorithm: crypto.SHA256,
Host: "example.com",
Path: "/inbox",
KeyID: "https://snippet.social/@juunini#main-key",
}
*/
type GenerateInput struct {
PrivateKeyBytes []byte
Algorithm crypto.Hash
Host string
Path string
Body []byte
KeyID string
}

type GenerateOutput struct {
Date string
Host string
Digest string
Signature string
}

/*
Example:
headers, err := signature_header.Generate(signature_header.GenerateInput{
PrivateKeyBytes: []byte("-----BEGIN RSA PRIVATE KEY-----..."),
Algorithm: crypto.SHA256,
Host: "example.com",
Path: "/inbox",
KeyID: "https://snippet.social/@juunini#main-key",
})
*/
func Generate(input GenerateInput) (*GenerateOutput, error) {
date := Date()
digest := Digest(input.Algorithm, input.Body)

privateKey, err := PrivateKeyFromBytes(input.PrivateKeyBytes)
if err != nil {
return nil, err
}

signature, err := Signature{
PrivateKey: privateKey,
Algorithm: input.Algorithm,
Date: date,
Digest: digest,
Host: input.Host,
Path: input.Path,
KeyID: input.KeyID,
}.String()
if err != nil {
return nil, err
}

return &GenerateOutput{
Date: date,
Host: input.Host,
Digest: digest,
Signature: signature,
}, nil
}
42 changes: 20 additions & 22 deletions test/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,28 @@ func Test_VerifyWithBody(t *testing.T) {

func createSignature() map[string]string {
message := []byte(requestMessage)
host := "snippet.social"
path := "/@juunini/inbox"
keyID := "http://localhost:8000/@juunini#main-key"

privateKey, _ := signature_header.PrivateKeyFromBytes([]byte(privateKeyStr))

algorithm := crypto.SHA256
date := signature_header.Date()
digest := signature_header.Digest(algorithm, message)
signature, _ := signature_header.Signature{
PrivateKey: privateKey,
Algorithm: algorithm,
Date: date,
Digest: digest,
Host: host,
Path: path,
KeyID: keyID,
}.String()
const host = "snippet.social"
const path = "/@juunini/inbox"
const keyID = "http://localhost:8000/@juunini#main-key"
const algorithm = crypto.SHA256

headers, err := signature_header.Generate(signature_header.GenerateInput{
PrivateKeyBytes: []byte(privateKeyStr),
Algorithm: algorithm,
Host: host,
Path: path,
Body: message,
KeyID: keyID,
})
if err != nil {
panic(err)
}

return map[string]string{
"Signature": signature,
"Date": date,
"Host": host,
"Digest": digest,
"Signature": headers.Signature,
"Date": headers.Date,
"Host": headers.Host,
"Digest": headers.Digest,
"Content-Type": "application/activity+json",
}
}
Expand Down

0 comments on commit ef9649a

Please sign in to comment.