signature_header

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 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"

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

const privateKeyBytes = []byte("-----BEGIN RSA PRIVATE KEY-----...")
const message = []byte(`{
  "@context": "https://www.w3.org/ns/activitystreams",
  "id": "https://snippet.cloudmt.co.kr/@juunini",
  "type": "Follow",
  "actor": "https://snippet.cloudmt.co.kr/@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
}

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()
if err != nil {
  // handle error
}

resty.New().R().
  SetBody(message).
  SetHeader("Date", date).
  SetHeader("Digest", digest).
  SetHeader("Host", host).
  SetHeader("Signature", signature).
  SetHeader("Content-Type", "application/activity+json").
  Post("https://" + host + path)
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 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