signature_header

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2023 License: MIT Imports: 13 Imported by: 0

README

cloudmate logo

ActivityPub Signature header

by Cloudmate


Golang

Go Test

Install

go get -u github.com/cloudmatelabs/go-activitypub-signature-header

Introduce

This library is generate Signature header for the connect with ActivityPub federations.
And verify the Signature header.

Usage

Sign Signature header
import (
  "crypto"
  "net/url"

  "github.com/go-resty/resty/v2"
  signature_header "github.com/cloudmatelabs/go-activitypub-signature-header"
)

requestURL, _ := url.Parse("https://yodangang.express/users/9iffvxhojp/inbox")
message := []byte(`{
  "@context": "https://www.w3.org/ns/activitystreams",
  "id": "https://snippet.social/@juunini",
  "type": "Follow",
  "actor": "https://snippet.social/@juunini",
  "object": "https://yodangang.express/users/9iffvxhojp"
}`)

headers, err := signature_header.Generate(signature_header.GenerateInput{
  PrivateKeyBytes: []byte("-----BEGIN RSA PRIVATE KEY-----..."),
  // Algorithm:       crypto.SHA256, // optional. if not set, default is crypto.SHA256
  Host:            requestURL.Host,
  Path:            requestURL.Path,
  Body:            message,
  KeyID:           "https://snippet.social/@juunini#main-key",
})
if err != nil {
  // handle error
}

resty.New().R().
  SetBody(message).
  SetHeader("Date", headers.Date).
  SetHeader("Digest", headers.Digest).
  SetHeader("Host", headers.Host).
  SetHeader("Signature", headers.Signature).
  SetHeader("Content-Type", "application/activity+json").
  Post(requestURL.String())
Verify Signature header
import (
  signature_header "github.com/cloudmatelabs/go-activitypub-signature-header"
)

verifier := signature_header.Verifier{
  Method: "POST",
  URL: "https://snippet.social/@juunini/inbox",
  Headers: map[string]string{
    "Signature": "...",
    "Host": "...",
    "Date": "...",
    "Digest": "...",
    "Authorization": "...",
    "...": "...",
  },
}

// Recommended
err := verifier.VerifyWithPublicKey(publicKey)
err := verifier.VerifyWithPublicKeyStr(publicKeyStr)

// You can use, but not recommended
err := verifier.VerifyWithActor("https://yodangang.express/@juunini")
err := verifier.VerifyWithBody([]byte("{...}"))
Parse Signature header
import (
  signature_header "github.com/cloudmatelabs/go-activitypub-signature-header"
)

// map[string]string
params := signature_header.ParseSignature(signature)
// or given Signature authorization header
// params := signature_header.ParseSignature(authorization)

params["keyId"]
params["algorithm"]
params["headers"]
params["signature"]

License

MIT

But, this library use httpsig.
httpsig is licensed under the BSD 3-Clause License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Date

func Date() string

func Digest

func Digest(hash crypto.Hash, message []byte) string

Example: signature_header.Digest(crypto.SHA256, []byte(`{"@context": [...], ...}`))

Output: SHA-256=7Uq...Q==

func GenerateKey

func GenerateKey(bits int) (privateKey *rsa.PrivateKey, privateKeyBytes []byte, publicKeyBytes []byte)

func ParseSignature added in v0.1.1

func ParseSignature(signatureStr string) map[string]string

func PrivateKeyFromBytes

func PrivateKeyFromBytes(privateKeyBytes []byte) (*rsa.PrivateKey, error)

Types

type GenerateInput added in v0.2.0

type GenerateInput struct {
	PrivateKeyBytes []byte
	Algorithm       crypto.Hash
	Host            string
	Path            string
	Body            []byte
	KeyID           string
}

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 GenerateOutput added in v0.2.0

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

func Generate added in v0.2.0

func Generate(input GenerateInput) (*GenerateOutput, error)

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",
})

type Signature

type Signature struct {
	PrivateKey *rsa.PrivateKey
	Algorithm  crypto.Hash
	Date       string
	Digest     string
	Host       string
	Path       string
	KeyID      string
}

Example:

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

func (Signature) String

func (s Signature) String() (string, error)

type Verifier added in v0.1.0

type Verifier struct {
	Method  string
	URL     string
	Headers map[string]string
	// contains filtered or unexported fields
}

func (*Verifier) VerifyWithActor added in v0.1.0

func (v *Verifier) VerifyWithActor(actor string) error

func (*Verifier) VerifyWithBody added in v0.1.0

func (v *Verifier) VerifyWithBody(body []byte) error

func (*Verifier) VerifyWithPublicKey added in v0.1.0

func (v *Verifier) VerifyWithPublicKey(publicKey crypto.PublicKey) error

func (*Verifier) VerifyWithPublicKeyStr added in v0.1.0

func (v *Verifier) VerifyWithPublicKeyStr(publicKeyStr string) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL