graphvent/user.go

121 lines
2.2 KiB
Go

2023-07-20 22:08:28 -06:00
package graphvent
import (
"time"
"fmt"
"encoding/json"
"crypto/ecdsa"
"crypto/x509"
)
2023-07-26 00:18:11 -06:00
type ECDHExt struct {
2023-07-20 22:08:28 -06:00
Granted time.Time
Pubkey *ecdsa.PublicKey
Shared []byte
}
2023-07-26 00:18:11 -06:00
type ECDHExtJSON struct {
2023-07-20 22:08:28 -06:00
Granted time.Time `json:"granted"`
Pubkey []byte `json:"pubkey"`
Shared []byte `json:"shared"`
}
func (ext *ECDHExt) Process(context *StateContext, node *Node, signal Signal) error {
2023-07-26 00:18:11 -06:00
return nil
2023-07-20 22:08:28 -06:00
}
2023-07-26 00:18:11 -06:00
const ECDHExtType = ExtType("ECDH")
func (ext *ECDHExt) Type() ExtType {
return ECDHExtType
}
func (ext *ECDHExt) Serialize() ([]byte, error) {
pubkey, err := x509.MarshalPKIXPublicKey(ext.Pubkey)
2023-07-20 22:08:28 -06:00
if err != nil {
return nil, err
}
2023-07-26 00:18:11 -06:00
return json.MarshalIndent(&ECDHExtJSON{
Granted: ext.Granted,
2023-07-20 22:08:28 -06:00
Pubkey: pubkey,
2023-07-26 00:18:11 -06:00
Shared: ext.Shared,
2023-07-20 22:08:28 -06:00
}, "", " ")
}
2023-07-26 00:18:11 -06:00
func LoadECDHExt(ctx *Context, data []byte) (Extension, error) {
var j ECDHExtJSON
err := json.Unmarshal(data, &j)
if err != nil {
return nil, err
}
2023-07-20 22:08:28 -06:00
pub, err := x509.ParsePKIXPublicKey(j.Pubkey)
if err != nil {
return nil, err
}
var pubkey *ecdsa.PublicKey
switch pub.(type) {
case *ecdsa.PublicKey:
pubkey = pub.(*ecdsa.PublicKey)
default:
2023-07-26 00:18:11 -06:00
return nil, fmt.Errorf("Invalid key type: %+v", pub)
2023-07-20 22:08:28 -06:00
}
2023-07-26 00:18:11 -06:00
extension := ECDHExt{
Granted: j.Granted,
2023-07-20 22:08:28 -06:00
Pubkey: pubkey,
2023-07-26 00:18:11 -06:00
Shared: j.Shared,
2023-07-20 22:08:28 -06:00
}
2023-07-25 09:51:55 -06:00
2023-07-26 00:18:11 -06:00
return &extension, nil
2023-07-25 09:51:55 -06:00
}
2023-07-26 00:18:11 -06:00
type GroupExt struct {
Members NodeMap
2023-07-25 09:51:55 -06:00
}
2023-07-26 00:18:11 -06:00
const GroupExtType = ExtType("GROUP")
func (ext *GroupExt) Type() ExtType {
return GroupExtType
2023-07-25 09:51:55 -06:00
}
2023-07-26 00:18:11 -06:00
func (ext *GroupExt) Serialize() ([]byte, error) {
return json.MarshalIndent(&struct{
Members []string `json:"members"`
}{
Members: SaveNodeList(ext.Members),
}, "", " ")
2023-07-25 09:51:55 -06:00
}
2023-07-26 11:56:10 -06:00
func NewGroupExt(members NodeMap) *GroupExt {
if members == nil {
members = NodeMap{}
}
return &GroupExt{
Members: members,
}
}
2023-07-26 00:18:11 -06:00
func LoadGroupExt(ctx *Context, data []byte) (Extension, error) {
var j struct {
Members []string `json:"members"`
2023-07-25 09:51:55 -06:00
}
2023-07-26 00:18:11 -06:00
err := json.Unmarshal(data, &j)
if err != nil {
return nil, err
}
2023-07-25 09:51:55 -06:00
2023-07-26 00:18:11 -06:00
members, err := RestoreNodeList(ctx, j.Members)
if err != nil {
return nil, err
}
2023-07-26 11:56:10 -06:00
return NewGroupExt(members), nil
2023-07-26 00:18:11 -06:00
}
2023-07-25 09:51:55 -06:00
func (ext *GroupExt) Process(context *StateContext, node *Node, signal Signal) error {
2023-07-26 00:18:11 -06:00
return nil
}