aboutsummaryrefslogtreecommitdiff
path: root/database.go
diff options
context:
space:
mode:
authorsageman_ <sageman@anomine.net>2021-07-03 20:43:41 +0200
committersageman_ <sageman@anomine.net>2021-07-03 20:43:41 +0200
commitd9cce000fdc8d804e73ff2fa4bfd2f1a151d90d9 (patch)
treef03c80f35d43c0a5eb305a58de4fb9dc06a99533 /database.go
parent97ce6d91545ec10f35d692ccbf60536d83f5debf (diff)
In case the PEM key is missing, do not fail too hard.
Tell the administrator in a friendly matter that they are an idiot and they should seek assistance in generating a new keypair and telling everyone about it.
Diffstat (limited to 'database.go')
-rw-r--r--database.go46
1 files changed, 38 insertions, 8 deletions
diff --git a/database.go b/database.go
index 9d5d721..70b7572 100644
--- a/database.go
+++ b/database.go
@@ -1,12 +1,15 @@
package main
-import "fmt"
-import "database/sql"
-import _ "github.com/lib/pq"
-import "time"
-import "os"
-import "strings"
-import "sort"
+import (
+ "database/sql"
+ "fmt"
+ "os"
+ "sort"
+ "strings"
+ "time"
+
+ _ "github.com/lib/pq"
+)
func GetActorFromDB(db *sql.DB, id string) Actor {
var nActor Actor
@@ -27,6 +30,10 @@ func GetActorFromDB(db *sql.DB, id string) Actor {
}
nActor.PublicKey = GetActorPemFromDB(db, publicKeyPem)
+ if nActor.PublicKey.PublicKeyPem == ""{
+ err = CreatePublicKeyFromPrivate(db, &nActor, publicKeyPem)
+ CheckError(err, "error creating public key from private")
+ }
return nActor
}
@@ -50,6 +57,10 @@ func GetActorByNameFromDB(db *sql.DB, name string) Actor {
}
nActor.PublicKey = GetActorPemFromDB(db, publicKeyPem)
+ if nActor.PublicKey.PublicKeyPem == ""{
+ err = CreatePublicKeyFromPrivate(db, &nActor, publicKeyPem)
+ CheckError(err, "error creating public key from private")
+ }
return nActor
}
@@ -1470,13 +1481,32 @@ func GetActorPemFromDB(db *sql.DB, pemID string) PublicKeyPem {
defer rows.Close()
rows.Next()
rows.Scan(&pem.Id, &pem.Owner, &pem.PublicKeyPem)
- f, _ := os.ReadFile(pem.PublicKeyPem)
+ f, err := os.ReadFile(pem.PublicKeyPem)
+ if err != nil{
+ pem.PublicKeyPem = ""
+ return pem
+ }
pem.PublicKeyPem = strings.ReplaceAll(string(f), "\r\n", `\n`)
return pem
}
+func GetActorPemFileFromDB(db *sql.DB, pemID string) string{
+ query := `select file from publickeypem where id=$1`
+ rows, err := db.Query(query, pemID)
+
+ CheckError(err, "could not get public key filename from database")
+
+ var file string
+
+ defer rows.Close()
+ rows.Next()
+ rows.Scan(&file)
+
+ return file
+}
+
func MarkObjectSensitive(db *sql.DB, id string, sensitive bool) {
var query = `update activitystream set sensitive=$1 where id=$2`
_, err := db.Exec(query, sensitive, id)