Skip to content

Commit

Permalink
add ParseSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
juunini committed Oct 26, 2023
1 parent 5ddf267 commit 9201397
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ err := verifier.VerifyWithActor("https://yodangang.express/@juunini")
err := verifier.VerifyWithBody([]byte("{...}"))
```

### Parse `Signature` header

```go
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](LICENSE)
Expand Down
17 changes: 17 additions & 0 deletions signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ digest: %s`,
fmt.Sprintf(`signature="%s"`, signature),
}, ","), nil
}

func ParseSignature(signatureStr string) map[string]string {
parsedSignature := map[string]string{}

for _, param := range strings.Split(strings.Replace(signatureStr, "Signature ", "", 1), ",") {
split := strings.Split(param, "=")
key := split[0]
value := strings.ReplaceAll(
strings.Join(split[1:], "="),
`"`, "",
)

parsedSignature[key] = value
}

return parsedSignature
}
63 changes: 63 additions & 0 deletions test/signature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package signature_header_test

import (
"fmt"
"strings"
"testing"

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

var (
keyId = `https://snippet.social/@juunini#main-key`
algorithm = `rsa-sha256`
_headers = `(request-target) date host digest`
signatureHash = "g99dh20dygasd=="
)

var signature = strings.Join([]string{
fmt.Sprintf(`keyId="%s"`, keyId),
fmt.Sprintf(`algorithm="%s"`, algorithm),
fmt.Sprintf(`headers="%s"`, headers),
fmt.Sprintf(`signature="%s"`, signatureHash),
}, ",")

func Test_ParseSignature(t *testing.T) {
params := signature_header.ParseSignature(signature)

if params["algorithm"] != algorithm {
t.Errorf("Algorithm is not correct: %s", params["algorithm"])
}

if params["keyId"] != keyId {
t.Errorf("KeyID is not correct: %s", params["keyId"])
}

if params["signature"] != signatureHash {
t.Errorf("Signature is not correct: %s", params["signature"])
}

if params["headers"] != _headers {
t.Errorf("Headers is not correct: %s", params["headers"])
}
}

func Test_ParseSignature_WithAuthorization(t *testing.T) {
params := signature_header.ParseSignature("Signature " + signature)

if params["algorithm"] != algorithm {
t.Errorf("Algorithm is not correct: %s", params["algorithm"])
}

if params["keyId"] != keyId {
t.Errorf("KeyID is not correct: %s", params["keyId"])
}

if params["signature"] != signatureHash {
t.Errorf("Signature is not correct: %s", params["signature"])
}

if params["headers"] != _headers {
t.Errorf("Headers is not correct: %s", params["headers"])
}
}

0 comments on commit 9201397

Please sign in to comment.