Added the PublicKey property from the auth package

This commit is contained in:
Marius Orcsik 2019-12-03 20:59:57 +01:00
parent 79e39d3c2b
commit bf3906d5fb
No known key found for this signature in database
GPG key ID: 77618B618F79EB72
2 changed files with 47 additions and 4 deletions

View file

@ -2,6 +2,7 @@ package activitypub
import (
"errors"
"github.com/buger/jsonparser"
"time"
"unsafe"
)
@ -149,8 +150,8 @@ type Actor struct {
// to a JSON-LD document with these properties.
Endpoints *Endpoints `jsonld:"endpoints,omitempty"`
// A list of supplementary Collections which may be of interest.
Streams []ItemCollection `jsonld:"streams,omitempty"`
Streams []ItemCollection `jsonld:"streams,omitempty"`
PublicKey PublicKey `jsonld:"publicKey,omitempty"`
}
// GetID returns the ObjectID corresponding to the current Actor
@ -183,6 +184,31 @@ func (a Actor) IsCollection() bool {
return false
}
// PublicKey holds the ActivityPub compatible public key data
type PublicKey struct {
ID ObjectID `jsonld:"id,omitempty"`
Owner ObjectOrLink `jsonld:"owner,omitempty"`
PublicKeyPem string `jsonld:"publicKeyPem,omitempty"`
}
func (p *PublicKey) UnmarshalJSON(data []byte) error {
if id, err := jsonparser.GetString(data, "id"); err == nil {
p.ID = ObjectID(id)
} else {
return err
}
if o, err := jsonparser.GetString(data, "owner"); err == nil {
p.Owner = IRI(o)
} else {
return err
}
if pub, err := jsonparser.GetString(data, "publicKeyPem"); err == nil {
p.PublicKeyPem = pub
} else {
return err
}
return nil
}
type (
// Application describes a software application.
Application = Actor
@ -323,8 +349,8 @@ func (a *Actor) UnmarshalJSON(data []byte) error {
a.Outbox = JSONGetItem(data, "outbox")
a.Liked = JSONGetItem(data, "liked")
a.Endpoints = JSONGetActorEndpoints(data, "endpoints")
// TODO(marius): Streams needs custom unmarshalling
//a.Streams = JSONGetItems(data, "streams")
a.Streams = JSONGetStreams(data, "streams")
a.PublicKey = JSONGetPublicKey(data, "publicKey")
return nil
}

View file

@ -71,6 +71,13 @@ func JSONGetString(data []byte, prop string) string {
return val
}
func JSONGetBytes(data []byte, prop string) []byte {
val, _, _, err := jsonparser.Get(data, prop)
if err != nil {
}
return val
}
func JSONGetBoolean(data []byte, prop string) bool {
val, err := jsonparser.GetBoolean(data, prop)
if err != nil {
@ -120,6 +127,16 @@ func JSONGetDuration(data []byte, prop string) time.Duration {
return d
}
func JSONGetPublicKey(data []byte, prop string) PublicKey {
key := PublicKey{}
key.UnmarshalJSON(JSONGetBytes(data, prop))
return key
}
func JSONGetStreams(data []byte, prop string) []ItemCollection {
return nil
}
func itemFn(data []byte) (Item, error) {
if len(data) == 0 {
return nil, nil