Skip to content

Commit cd013f5

Browse files
committed
chore: simplify key generation
1 parent a26a8a8 commit cd013f5

File tree

2 files changed

+28
-61
lines changed

2 files changed

+28
-61
lines changed

internal/gen/signingkeys/signingkeys.go

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,8 @@ import (
2323
"github.com/supabase/cli/pkg/config"
2424
)
2525

26-
type KeyPair struct {
27-
PublicKey config.JWK
28-
PrivateKey config.JWK
29-
}
30-
31-
// GenerateKeyPair generates a new key pair for the specified algorithm
32-
func GenerateKeyPair(alg config.Algorithm) (*KeyPair, error) {
26+
// GeneratePrivateKey generates a new private key for the specified algorithm
27+
func GeneratePrivateKey(alg config.Algorithm) (*config.JWK, error) {
3328
keyID := uuid.New()
3429

3530
switch alg {
@@ -42,7 +37,7 @@ func GenerateKeyPair(alg config.Algorithm) (*KeyPair, error) {
4237
}
4338
}
4439

45-
func generateRSAKeyPair(keyID uuid.UUID) (*KeyPair, error) {
40+
func generateRSAKeyPair(keyID uuid.UUID) (*config.JWK, error) {
4641
// Generate RSA key pair (2048 bits for RS256)
4742
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
4843
if err != nil {
@@ -72,24 +67,10 @@ func generateRSAKeyPair(keyID uuid.UUID) (*KeyPair, error) {
7267
FirstCRTCoefficient: base64.RawURLEncoding.EncodeToString(privateKey.Precomputed.Qinv.Bytes()),
7368
}
7469

75-
publicJWK := config.JWK{
76-
KeyType: "RSA",
77-
KeyID: keyID,
78-
Use: "sig",
79-
KeyOps: []string{"verify"},
80-
Algorithm: "RS256",
81-
Extractable: cast.Ptr(true),
82-
Modulus: privateJWK.Modulus,
83-
Exponent: privateJWK.Exponent,
84-
}
85-
86-
return &KeyPair{
87-
PublicKey: publicJWK,
88-
PrivateKey: privateJWK,
89-
}, nil
70+
return &privateJWK, nil
9071
}
9172

92-
func generateECDSAKeyPair(keyID uuid.UUID) (*KeyPair, error) {
73+
func generateECDSAKeyPair(keyID uuid.UUID) (*config.JWK, error) {
9374
// Generate ECDSA key pair (P-256 curve for ES256)
9475
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
9576
if err != nil {
@@ -112,22 +93,7 @@ func generateECDSAKeyPair(keyID uuid.UUID) (*KeyPair, error) {
11293
PrivateExponent: base64.RawURLEncoding.EncodeToString(privateKey.D.Bytes()),
11394
}
11495

115-
publicJWK := config.JWK{
116-
KeyType: "EC",
117-
KeyID: keyID,
118-
Use: "sig",
119-
KeyOps: []string{"verify"},
120-
Algorithm: "ES256",
121-
Extractable: cast.Ptr(true),
122-
Curve: "P-256",
123-
X: privateJWK.X,
124-
Y: privateJWK.Y,
125-
}
126-
127-
return &KeyPair{
128-
PublicKey: publicJWK,
129-
PrivateKey: privateJWK,
130-
}, nil
96+
return &privateJWK, nil
13197
}
13298

13399
// Run generates a key pair and writes it to the specified file path
@@ -139,7 +105,7 @@ func Run(ctx context.Context, algorithm string, appendMode bool, fsys afero.Fs)
139105
outputPath := utils.Config.Auth.SigningKeysPath
140106

141107
// Generate key pair
142-
keyPair, err := GenerateKeyPair(config.Algorithm(algorithm))
108+
privateJWK, err := GeneratePrivateKey(config.Algorithm(algorithm))
143109
if err != nil {
144110
return err
145111
}
@@ -181,7 +147,7 @@ func Run(ctx context.Context, algorithm string, appendMode bool, fsys afero.Fs)
181147
}
182148
out = f
183149
}
184-
jwkArray = append(jwkArray, keyPair.PrivateKey)
150+
jwkArray = append(jwkArray, *privateJWK)
185151

186152
// Write to file
187153
enc := json.NewEncoder(out)

internal/gen/signingkeys/signingkeys_test.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,61 +31,62 @@ func TestGenerateKeyPair(t *testing.T) {
3131

3232
for _, tt := range tests {
3333
t.Run(tt.name, func(t *testing.T) {
34-
keyPair, err := GenerateKeyPair(tt.algorithm)
34+
privateJWK, err := GeneratePrivateKey(tt.algorithm)
3535
if (err != nil) != tt.wantErr {
3636
t.Errorf("GenerateKeyPair(%s) error = %v, wantErr %v", tt.algorithm, err, tt.wantErr)
3737
return
3838
}
3939
if !tt.wantErr {
40-
if keyPair == nil {
40+
if privateJWK == nil {
4141
t.Error("GenerateKeyPair() returned nil key pair")
4242
return
4343
}
4444

4545
// Check that both public and private keys are generated
46-
if keyPair.PublicKey.KeyType == "" {
46+
publicJWK := privateJWK.ToPublicJWK()
47+
if publicJWK.KeyType == "" {
4748
t.Error("Public key type is empty")
4849
}
49-
if keyPair.PrivateKey.KeyType == "" {
50+
if privateJWK.KeyType == "" {
5051
t.Error("Private key type is empty")
5152
}
5253

5354
// Check that key IDs match
54-
if keyPair.PublicKey.KeyID != keyPair.PrivateKey.KeyID {
55+
if publicJWK.KeyID != privateJWK.KeyID {
5556
t.Error("Public and private key IDs don't match")
5657
}
5758

5859
// Algorithm-specific checks
5960
switch tt.algorithm {
6061
case config.AlgRS256:
61-
if keyPair.PublicKey.KeyType != "RSA" {
62-
t.Errorf("Expected RSA key type, got %s", keyPair.PublicKey.KeyType)
62+
if publicJWK.KeyType != "RSA" {
63+
t.Errorf("Expected RSA key type, got %s", publicJWK.KeyType)
6364
}
64-
if keyPair.PrivateKey.Algorithm != "RS256" {
65-
t.Errorf("Expected RS256 algorithm, got %s", keyPair.PrivateKey.Algorithm)
65+
if privateJWK.Algorithm != "RS256" {
66+
t.Errorf("Expected RS256 algorithm, got %s", privateJWK.Algorithm)
6667
}
6768
// Check that RSA-specific fields are present
68-
if keyPair.PrivateKey.Modulus == "" {
69+
if privateJWK.Modulus == "" {
6970
t.Error("RSA private key missing modulus")
7071
}
71-
if keyPair.PrivateKey.PrivateExponent == "" {
72+
if privateJWK.PrivateExponent == "" {
7273
t.Error("RSA private key missing private exponent")
7374
}
7475
case config.AlgES256:
75-
if keyPair.PublicKey.KeyType != "EC" {
76-
t.Errorf("Expected EC key type, got %s", keyPair.PublicKey.KeyType)
76+
if publicJWK.KeyType != "EC" {
77+
t.Errorf("Expected EC key type, got %s", publicJWK.KeyType)
7778
}
78-
if keyPair.PrivateKey.Algorithm != "ES256" {
79-
t.Errorf("Expected ES256 algorithm, got %s", keyPair.PrivateKey.Algorithm)
79+
if privateJWK.Algorithm != "ES256" {
80+
t.Errorf("Expected ES256 algorithm, got %s", privateJWK.Algorithm)
8081
}
8182
// Check that EC-specific fields are present
82-
if keyPair.PrivateKey.Curve != "P-256" {
83-
t.Errorf("Expected P-256 curve, got %s", keyPair.PrivateKey.Curve)
83+
if privateJWK.Curve != "P-256" {
84+
t.Errorf("Expected P-256 curve, got %s", privateJWK.Curve)
8485
}
85-
if keyPair.PrivateKey.X == "" {
86+
if privateJWK.X == "" {
8687
t.Error("EC private key missing X coordinate")
8788
}
88-
if keyPair.PrivateKey.Y == "" {
89+
if privateJWK.Y == "" {
8990
t.Error("EC private key missing Y coordinate")
9091
}
9192
}

0 commit comments

Comments
 (0)