Skip to content

Commit 0e2fcc2

Browse files
authored
Keys (#42)
* WIP: keys * WIP: create keys along with user * WIP: encryption * WIP: marker bytes * WIP: hybrid encryption * WIP: setup keys with password * WIP: frontend to provide key setup+password * WIP: wide logout button * linter * linter * workflows * workflows fix
1 parent 73063b9 commit 0e2fcc2

37 files changed

+1407
-208
lines changed

.github/workflows/pr.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ concurrency:
1111

1212
jobs:
1313
tests:
14-
runs-on: ubuntu-24.04
14+
runs-on: ubuntu-latest
1515
permissions:
1616
contents: read
1717
steps:
@@ -20,7 +20,7 @@ jobs:
2020
- name: setup golang
2121
uses: actions/setup-go@v5
2222
with:
23-
go-version: '^1.24.4'
23+
go-version: '^1.25.1'
2424
- name: Set up gotestfmt
2525
uses: gotesttools/gotestfmt-action@v2
2626
with:
@@ -37,7 +37,7 @@ jobs:
3737
path: /tmp/gotest.log
3838
if-no-files-found: error
3939
linter:
40-
runs-on: ubuntu-24.04
40+
runs-on: ubuntu-latest
4141
permissions:
4242
contents: read
4343
steps:
@@ -46,7 +46,7 @@ jobs:
4646
- name: setup golang
4747
uses: actions/setup-go@v5
4848
with:
49-
go-version: '^1.24.4'
49+
go-version: '^1.25.1'
5050
- name: run linter
51-
uses: golangci/golangci-lint-action@v6
51+
uses: golangci/golangci-lint-action@v8
5252

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ COPY frontend .
1111
RUN ["npm", "run", "build"]
1212
RUN ["npm", "prune", "--production"]
1313

14-
FROM golang:1.24.4 AS builder
14+
FROM golang:1.25.1 AS builder
1515

1616
WORKDIR /opt/synod
1717

backend/convert/fromdb/fromdb.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
sqlc "github.com/torfstack/synod/sql/gen"
88
)
99

10-
func Secret(in sqlc.Secret) models.Secret {
11-
return models.Secret{
10+
func Secret(in sqlc.Secret) models.EncryptedSecret {
11+
return models.EncryptedSecret{
1212
ID: &in.ID,
1313
Value: string(in.Value),
1414
Key: in.Key,
@@ -17,20 +17,45 @@ func Secret(in sqlc.Secret) models.Secret {
1717
}
1818
}
1919

20-
func Secrets(in []sqlc.Secret) models.Secrets {
21-
out := make([]models.Secret, len(in))
20+
func Secrets(in []sqlc.Secret) []models.EncryptedSecret {
21+
out := make([]models.EncryptedSecret, len(in))
2222
for i, s := range in {
2323
out[i] = Secret(s)
2424
}
2525
return out
2626
}
2727

28-
func User(in sqlc.User) models.User {
29-
return models.User{
30-
ID: &in.ID,
31-
Subject: in.Subject,
32-
Email: in.Email,
33-
FullName: in.FullName,
28+
func User(in sqlc.User) models.ExistingUser {
29+
return models.ExistingUser{
30+
ID: in.ID,
31+
User: models.User{
32+
Subject: in.Subject,
33+
Email: in.Email,
34+
FullName: in.FullName,
35+
},
36+
}
37+
}
38+
39+
func KeyPair(in sqlc.Key) models.UserKeyPair {
40+
userKeyPair := models.UserKeyPair{
41+
ID: &in.ID,
42+
Type: models.KeyType(in.Type),
43+
UserID: in.UserID,
44+
Public: in.Public,
45+
Private: in.Private,
46+
}
47+
if in.PasswordID.Valid {
48+
userKeyPair.PasswordID = &in.PasswordID.Int64
49+
}
50+
return userKeyPair
51+
}
52+
53+
func HashedPassword(in sqlc.Password) models.HashedPassword {
54+
return models.HashedPassword{
55+
ID: &in.ID,
56+
Hash: in.Hash,
57+
Salt: in.Salt,
58+
Iterations: in.Iterations,
3459
}
3560
}
3661

backend/convert/todb/todb.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package todb
22

33
import (
4+
"github.com/jackc/pgx/v5/pgtype"
45
"github.com/torfstack/synod/backend/models"
56
sqlc "github.com/torfstack/synod/sql/gen"
67
)
@@ -15,7 +16,7 @@ func Secret(in models.Secret) sqlc.Secret {
1516
}
1617
}
1718

18-
func InsertSecretParams(in models.Secret, userID int64) sqlc.InsertSecretParams {
19+
func InsertSecretParams(in models.EncryptedSecret, userID int64) sqlc.InsertSecretParams {
1920
return sqlc.InsertSecretParams{
2021
Value: []byte(in.Value),
2122
Key: in.Key,
@@ -25,7 +26,7 @@ func InsertSecretParams(in models.Secret, userID int64) sqlc.InsertSecretParams
2526
}
2627
}
2728

28-
func UpdateSecretParams(in models.Secret, userID int64) sqlc.UpdateSecretParams {
29+
func UpdateSecretParams(in models.EncryptedSecret, userID int64) sqlc.UpdateSecretParams {
2930
return sqlc.UpdateSecretParams{
3031
ID: *in.ID,
3132
Value: []byte(in.Value),
@@ -44,6 +45,30 @@ func InsertUserParams(in models.User) sqlc.InsertUserParams {
4445
}
4546
}
4647

48+
func InsertKeysParams(in models.UserKeyPair) sqlc.InsertKeysParams {
49+
params := sqlc.InsertKeysParams{
50+
UserID: in.UserID,
51+
Type: int32(in.Type),
52+
Public: in.Public,
53+
Private: in.Private,
54+
}
55+
if in.PasswordID != nil {
56+
params.PasswordID = pgtype.Int8{
57+
Int64: *in.PasswordID,
58+
Valid: true,
59+
}
60+
}
61+
return params
62+
}
63+
64+
func InsertPasswordParams(in models.HashedPassword) sqlc.InsertPasswordParams {
65+
return sqlc.InsertPasswordParams{
66+
Hash: in.Hash,
67+
Salt: in.Salt,
68+
Iterations: in.Iterations,
69+
}
70+
}
71+
4772
func tagsString(tags []string) string {
4873
if len(tags) == 0 {
4974
return ""

backend/db/interfaces.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ import (
77
)
88

99
type Database interface {
10-
WithTx(ctx context.Context) (Database, Transaction)
10+
WithTx(ctx context.Context, withTx func(Database) error) error
1111

1212
DoesUserExist(ctx context.Context, username string) (bool, error)
13-
InsertUser(ctx context.Context, params models.User) error
14-
SelectUserByName(ctx context.Context, username string) (models.User, error)
15-
UpsertSecret(ctx context.Context, secret models.Secret, userID int64) error
16-
SelectSecrets(ctx context.Context, userID int64) ([]models.Secret, error)
13+
InsertUser(ctx context.Context, user models.User) (models.ExistingUser, error)
14+
SelectUserByName(ctx context.Context, username string) (models.ExistingUser, error)
15+
16+
UpsertSecret(ctx context.Context, secret models.EncryptedSecret, userID int64) (models.EncryptedSecret, error)
17+
SelectSecrets(ctx context.Context, userID int64) ([]models.EncryptedSecret, error)
18+
19+
InsertKeys(ctx context.Context, pair models.UserKeyPair) (models.UserKeyPair, error)
20+
SelectKeys(ctx context.Context, userID int64) (models.UserKeyPair, error)
21+
HasKeys(ctx context.Context, userID int64) (bool, error)
22+
23+
InsertPassword(ctx context.Context, password models.HashedPassword) (models.HashedPassword, error)
24+
SelectPassword(ctx context.Context, passwordID int64) (models.HashedPassword, error)
1725
}
1826

1927
type Transaction interface {

0 commit comments

Comments
 (0)