A vibe coded tangled fork which supports pijul.
1package models
2
3import (
4 "encoding/json"
5 "fmt"
6 "time"
7
8 "github.com/bluesky-social/indigo/atproto/syntax"
9 "github.com/gliderlabs/ssh"
10 "tangled.org/core/api/tangled"
11)
12
13type PublicKey struct {
14 Did string `json:"did"`
15 Key string `json:"key"`
16 Name string `json:"name"`
17 Rkey string `json:"rkey"`
18 Created *time.Time
19}
20
21func (p PublicKey) MarshalJSON() ([]byte, error) {
22 type Alias PublicKey
23 return json.Marshal(&struct {
24 Created string `json:"created"`
25 *Alias
26 }{
27 Created: p.Created.Format(time.RFC3339),
28 Alias: (*Alias)(&p),
29 })
30}
31
32func (p *PublicKey) AsRecord() tangled.PublicKey {
33 return tangled.PublicKey{
34 Name: p.Name,
35 Key: p.Key,
36 CreatedAt: p.Created.Format(time.RFC3339),
37 }
38}
39
40var _ Validator = new(PublicKey)
41
42func (p *PublicKey) Validate() error {
43 if _, _, _, _, err := ssh.ParseAuthorizedKey([]byte(p.Key)); err != nil {
44 return fmt.Errorf("invalid ssh key format: %w", err)
45 }
46
47 return nil
48}
49
50func PublicKeyFromRecord(did syntax.DID, rkey syntax.RecordKey, record tangled.PublicKey) (PublicKey, error) {
51 created, err := time.Parse(time.RFC3339, record.CreatedAt)
52 if err != nil {
53 return PublicKey{}, fmt.Errorf("invalid time format '%s'", record.CreatedAt)
54 }
55
56 return PublicKey{
57 Did: did.String(),
58 Rkey: rkey.String(),
59 Name: record.Name,
60 Key: record.Key,
61 Created: &created,
62 }, nil
63}