forked from tangled.org/core
this repo has no description

add publickeys to pdses

+22
cmd/gen.go
···
+
package main
+
+
import (
+
shbild "github.com/icyphox/bild/api/bild"
+
cbg "github.com/whyrusleeping/cbor-gen"
+
)
+
+
func main() {
+
+
genCfg := cbg.Gen{
+
MaxStringLength: 1_000_000,
+
}
+
+
if err := genCfg.WriteMapEncodersToFile(
+
"api/bild/cbor_gen.go",
+
"bild",
+
shbild.PublicKey{},
+
); err != nil {
+
panic(err)
+
}
+
+
}
+1
flake.nix
···
pkgs.gopls
pkgs.httpie
pkgs.indigo-lexgen
+
pkgs.litecli
];
};
});
+1 -1
go.mod
···
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
-
github.com/google/uuid v1.4.0 // indirect
+
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/sessions v1.4.0 // indirect
+2
go.sum
···
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0=
+9
lexicon-build-config.json
···
+
[
+
{
+
"package": "bild",
+
"prefix": "sh.bild",
+
"outdir": "api/bild",
+
"import": "github.com/icyphox/bild/api/bild",
+
"gen-server": true
+
}
+
]
+38
lexicons/publicKey.json
···
+
{
+
"lexicon": 1,
+
"id": "sh.bild.publicKey",
+
"needsCbor": true,
+
"needsType": true,
+
"defs": {
+
"main": {
+
"type": "record",
+
"key": "tid",
+
"record": {
+
"type": "object",
+
"required": [
+
"key",
+
"name",
+
"created"
+
],
+
"properties": {
+
"key": {
+
"type": "string",
+
"maxLength": 4096,
+
"maxGraphemes": 4096,
+
"description": "public key contents"
+
},
+
"name": {
+
"type": "string",
+
"format": "string",
+
"description": "human-readable name for this key"
+
},
+
"created": {
+
"type": "string",
+
"format": "datetime",
+
"description": "key upload timestamp"
+
}
+
}
+
}
+
}
+
}
+
}
+24
routes/auth/auth.go
···
return dir.Lookup(ctx, *id)
}
+
func (a *Auth) AuthorizedClient(r *http.Request) (*xrpc.Client, error) {
+
clientSession, err := a.s.Get(r, "bild-session")
+
+
if err != nil || clientSession.IsNew {
+
return nil, err
+
}
+
+
did := clientSession.Values["did"].(string)
+
pdsUrl := clientSession.Values["pds"].(string)
+
accessJwt := clientSession.Values["accessJwt"].(string)
+
refreshJwt := clientSession.Values["refreshJwt"].(string)
+
+
client := &xrpc.Client{
+
Host: pdsUrl,
+
Auth: &xrpc.AuthInfo{
+
AccessJwt: accessJwt,
+
RefreshJwt: refreshJwt,
+
Did: did,
+
},
+
}
+
+
return client, nil
+
}
+
func (a *Auth) CreateInitialSession(w http.ResponseWriter, r *http.Request, username, appPassword string) (AtSessionCreate, error) {
ctx := r.Context()
resolved, err := resolveIdent(ctx, username)
+26
routes/routes.go
···
"strings"
"time"
+
comatproto "github.com/bluesky-social/indigo/api/atproto"
+
lexutil "github.com/bluesky-social/indigo/lex/util"
"github.com/dustin/go-humanize"
"github.com/go-chi/chi/v5"
"github.com/go-git/go-git/v5/plumbing"
+
"github.com/google/uuid"
"github.com/gorilla/sessions"
+
shbild "github.com/icyphox/bild/api/bild"
"github.com/icyphox/bild/config"
"github.com/icyphox/bild/db"
"github.com/icyphox/bild/git"
···
case http.MethodPut:
key := r.FormValue("key")
name := r.FormValue("name")
+
client, _ := h.auth.AuthorizedClient(r)
_, _, _, _, err := ssh.ParseAuthorizedKey([]byte(key))
if err != nil {
···
log.Printf("adding public key: %s", err)
return
}
+
+
// store in pds too
+
resp, err := comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{
+
Collection: "sh.bild.publicKey",
+
Repo: did,
+
Rkey: uuid.New().String(),
+
Record: &lexutil.LexiconTypeDecoder{Val: &shbild.PublicKey{
+
Created: time.Now().String(),
+
Key: key,
+
Name: name,
+
}},
+
})
+
+
// invalid record
+
if err != nil {
+
h.WriteOOBNotice(w, "keys", "Invalid inputs. Check your formatting and try again.")
+
log.Printf("failed to create record: %s", err)
+
return
+
}
+
+
log.Println("created atproto record: ", resp.Uri)
h.WriteOOBNotice(w, "keys", "Key added!")
return