dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 7e843eedf36481255f9d5ec7fc86ef6c8f45830c
parent 97c8d8f24ecec01e82cbf29a708010de0ebe9fbf
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue, 24 Jan 2023 17:35:21 -0800

add pgp fingerprint to user profile

Diffstat:
Mpkg/database/tableLinks.go | 18+++++-------------
Mpkg/template/fn.go | 4++++
Mpkg/template/tmp.go | 1+
Mpkg/utils/utils.go | 14++++++++++++++
Mpkg/web/public/views/pages/public-profile.gohtml | 4++--
5 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/pkg/database/tableLinks.go b/pkg/database/tableLinks.go @@ -2,15 +2,13 @@ package database import ( "bytes" - "encoding/hex" - "fmt" + "dkforest/pkg/utils" "github.com/ProtonMail/go-crypto/openpgp" "github.com/ProtonMail/go-crypto/openpgp/armor" "github.com/ProtonMail/go-crypto/openpgp/packet" "github.com/google/uuid" "github.com/sirupsen/logrus" "html" - "strings" "time" ) @@ -191,17 +189,11 @@ func (l LinksPgp) GetKeyID() string { } func (l LinksPgp) GetKeyFingerprint() string { - reader := bytes.NewReader([]byte(l.PgpPublicKey)) - if block, err := armor.Decode(reader); err == nil { - r := packet.NewReader(block.Body) - if e, err := openpgp.ReadEntity(r); err == nil { - fp := strings.ToUpper(hex.EncodeToString(e.PrimaryKey.Fingerprint)) - return fmt.Sprintf("%s %s %s %s %s %s %s %s %s %s", - fp[0:4], fp[4:8], fp[8:12], fp[12:16], fp[16:20], - fp[20:24], fp[24:28], fp[28:32], fp[32:36], fp[36:40]) - } + out := "n/a" + if fingerprint := utils.GetKeyFingerprint(l.PgpPublicKey); fingerprint != "" { + out = fingerprint } - return "n/a" + return out } func CreateLinkPgp(linkID int64, title, description, publicKey string) (out LinksPgp, err error) { diff --git a/pkg/template/fn.go b/pkg/template/fn.go @@ -18,6 +18,10 @@ import ( "github.com/nicksnyder/go-i18n/v2/i18n" ) +func getKeyFingerprint(pkey string) string { + return utils.GetKeyFingerprint(pkey) +} + func shortNum(n int64) string { if n < 1000 { return utils.FormatInt64(n) diff --git a/pkg/template/tmp.go b/pkg/template/tmp.go @@ -49,6 +49,7 @@ func GetRenderer(e *echo.Echo) *Templates { tmplBuilder.AddFn("DerefStr", derefStr) tmplBuilder.AddFn("N", n) tmplBuilder.AddFn("shortNum", shortNum) + tmplBuilder.AddFn("getKeyFingerprint", getKeyFingerprint) tmplBuilder.AddFn("shortNumPtr", shortNumPtr) tmplBuilder.AddFn("mod", mod) tmplBuilder.AddFn("ts", ts) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go @@ -392,6 +392,20 @@ func getGCMKeyBytes(keyBytes []byte) (cipher.AEAD, int, error) { return gcm, nonceSize, nil } +func GetKeyFingerprint(pkey string) string { + reader := bytes.NewReader([]byte(pkey)) + if block, err := armor.Decode(reader); err == nil { + r := packet.NewReader(block.Body) + if e, err := openpgp.ReadEntity(r); err == nil { + fp := strings.ToUpper(hex.EncodeToString(e.PrimaryKey.Fingerprint)) + return fmt.Sprintf("%s %s %s %s %s %s %s %s %s %s", + fp[0:4], fp[4:8], fp[8:12], fp[12:16], fp[16:20], + fp[20:24], fp[24:28], fp[28:32], fp[32:36], fp[36:40]) + } + } + return "" +} + func PgpCheckClearSignMessage(pkey, msg string) bool { keyring, err := openpgp.ReadArmoredKeyRing(strings.NewReader(pkey)) if err != nil { diff --git a/pkg/web/public/views/pages/public-profile.gohtml b/pkg/web/public/views/pages/public-profile.gohtml @@ -46,10 +46,10 @@ </table> <div class="mt-3"> <label>Age public key:</label> - {{ if .Data.User.AgePublicKey }}{{ .Data.User.AgePublicKey }}{{ else }}none{{ end }} + <span style="color: #c0ebff;">{{ if .Data.User.AgePublicKey }}{{ .Data.User.AgePublicKey }}{{ else }}none{{ end }}</span> </div> <div class="mt-3"> - <label for="pgpPublicKey">PGP key:</label><br /> + <label for="pgpPublicKey">PGP key: <span style="color: #82e17f;">{{ .Data.User.GPGPublicKey | getKeyFingerprint }}</span></label><br /> <textarea id="pgpPublicKey" rows="10" class="form-control mono" readonly>{{ .Data.User.GPGPublicKey }}</textarea> </div> <div class="mt-3">