From ef9649a400ce641d41e6ba5dac2dc0dc11d0f3bb Mon Sep 17 00:00:00 2001 From: Juunini Date: Sun, 29 Oct 2023 23:06:52 +0900 Subject: [PATCH] add generate function --- README.md | 48 +++++++++++++----------------- generate.go | 71 +++++++++++++++++++++++++++++++++++++++++++++ test/verify_test.go | 42 +++++++++++++-------------- 3 files changed, 111 insertions(+), 50 deletions(-) create mode 100644 generate.go diff --git a/README.md b/README.md index 101b579..e67bd25 100644 --- a/README.md +++ b/README.md @@ -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) ``` diff --git a/generate.go b/generate.go new file mode 100644 index 0000000..f4d9c51 --- /dev/null +++ b/generate.go @@ -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 +} diff --git a/test/verify_test.go b/test/verify_test.go index 576db75..f89b772 100644 --- a/test/verify_test.go +++ b/test/verify_test.go @@ -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", } }