aboutsummaryrefslogtreecommitdiff
path: root/database.go
diff options
context:
space:
mode:
authorFChannel0 <77419041+FChannel0@users.noreply.github.com>2021-07-03 16:17:46 -0700
committerGitHub <noreply@github.com>2021-07-03 16:17:46 -0700
commitab6edbbe8044d5a3c7d9456bd15e3a74af35111a (patch)
treeb45a5860c9a8e89071324ab980316054e1030695 /database.go
parenta3f45e6a5cb85a43364756bd28a14936d1d95ae7 (diff)
parentd9cce000fdc8d804e73ff2fa4bfd2f1a151d90d9 (diff)
Merge pull request #35 from the-sageman/pem-failsafe
In case the PEM key is missing, do not fail too hard.
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 6a871ba..baae16a 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)