3 Commits

Author SHA1 Message Date
Jon Lundy
05df6253db fixes to gpg key and twt proofs 2020-12-02 09:27:23 -07:00
Jon Lundy
b294d4fdc8 add additional url point for twtxt 2020-12-01 12:28:56 -07:00
Jon Lundy
90bba0e527 adding twtxt proofs 2020-12-01 12:24:35 -07:00
3 changed files with 90 additions and 10 deletions

View File

@@ -14,7 +14,7 @@ version:
@echo $(VERSION)
tag:
git tag -a v$(VERSION) -m "Version: $(VERSION)"
git push --tag
git push --follow-tags
release:
@make tag BUMP=patch
run:

View File

@@ -146,24 +146,21 @@ func ReadKey(r io.Reader, useArmored bool) (e *Entity, err error) {
var buf bytes.Buffer
var w io.Writer = &buf
e = &Entity{}
defer func(){ if e != nil { e.ArmorText = buf.String() }}()
if !useArmored {
var aw io.WriteCloser
aw, err = armor.Encode(&buf, "PGP PUBLIC KEY BLOCK", nil)
if err != nil {
return e, fmt.Errorf("Read key: %w", err)
}
defer aw.Close()
defer aw.Close()
w = aw
}
defer func() {
if e != nil {
e.ArmorText = buf.String()
}
}()
r = io.TeeReader(r, w)

View File

@@ -2,6 +2,7 @@ package keyproofs
import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/json"
@@ -205,6 +206,17 @@ func NewProof(ctx context.Context, uri, fingerprint string) ProofResolver {
return &httpResolve{p, url, nil}
}
case strings.Contains(p.URI.Path, "/conv/"), strings.Contains(p.URI.Path, "/twt/"):
if sp := strings.SplitN(p.URI.Path, "/", 3); len(sp) == 3 {
p.Icon = "fas fa-comment-alt"
p.Service = "Twtxt"
p.Name = fmt.Sprintf("...@%s", p.URI.Host)
p.Link = fmt.Sprintf("https://%s", p.URI.Host)
url := fmt.Sprintf("https://%s/api/v1/conv", p.URI.Host)
return &twtxtResolve{p, url, sp[2], nil}
}
default:
if sp := strings.SplitN(p.URI.Path, "/", 3); len(sp) > 1 {
p.Icon = "fas fa-project-diagram"
@@ -294,8 +306,8 @@ func (r *gitlabResolve) Resolve(ctx context.Context) error {
return ErrNoFingerprint
}
func (p *gitlabResolve) Proof() *Proof {
return &p.proof
func (r *gitlabResolve) Proof() *Proof {
return &r.proof
}
func (p *Proof) Resolve(ctx context.Context) error {
@@ -305,6 +317,44 @@ func (p *Proof) Proof() *Proof {
return p
}
type twtxtResolve struct {
proof Proof `json:"-"`
url string `json:"-"`
Hash string `json:"hash"`
headers map[string]string `json:"-"`
}
func (t *twtxtResolve) Resolve(ctx context.Context) error {
t.proof.Status = ProofInvalid
twt := struct {
Twts []struct {
Text string `json:"text"`
Twter struct{ Nick string }
} `json:"twts"`
}{}
if err := postJSON(ctx, t.url, nil, t, &twt); err != nil {
return err
}
if len(twt.Twts) > 0 {
nick := twt.Twts[0].Twter.Nick
t.proof.Name = fmt.Sprintf("%s@%s", nick, t.proof.URI.Host)
t.proof.Link += "/user/" + nick
ck := fmt.Sprintf("[Verifying my OpenPGP key: openpgp4fpr:%s]", t.proof.Fingerprint)
if strings.Contains(twt.Twts[0].Text, ck) {
t.proof.Status = ProofVerified
return nil
}
}
return ErrNoFingerprint
}
func (t *twtxtResolve) Proof() *Proof {
return &t.proof
}
func checkHTTP(ctx context.Context, uri, fingerprint string, hdr map[string]string) error {
log := log.Ctx(ctx)
@@ -370,3 +420,36 @@ func httpJSON(ctx context.Context, uri string, hdr map[string]string, dst interf
return json.NewDecoder(res.Body).Decode(dst)
}
func postJSON(ctx context.Context, uri string, hdr map[string]string, payload, dst interface{}) error {
log := log.Ctx(ctx)
log.Info().Str("URI", uri).Msg("postJSON")
body, err := json.Marshal(payload)
if err != nil {
log.Err(err).Send()
return err
}
buf := bytes.NewBuffer(body)
req, err := http.NewRequestWithContext(ctx, "POST", uri, buf)
if err != nil {
log.Err(err).Send()
return err
}
req.Header.Set("Accept", "application/json")
for k, v := range hdr {
req.Header.Set(k, v)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Err(err)
return err
}
defer res.Body.Close()
return json.NewDecoder(res.Body).Decode(dst)
}