Skip to content

Commit c726567

Browse files
committed
fix: generate default signing key
1 parent 5368b3f commit c726567

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

internal/start/start.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
_ "embed"
7+
"encoding/json"
78
"fmt"
89
"io"
910
"net"
@@ -621,8 +622,8 @@ EOF
621622
fmt.Sprintf("GOTRUE_RATE_LIMIT_WEB3=%v", utils.Config.Auth.RateLimit.Web3),
622623
}
623624

624-
// Since signing key is validated by ResolveJWKS, simply read the key file.
625-
if keys, err := afero.ReadFile(fsys, utils.Config.Auth.SigningKeysPath); err == nil && len(keys) > 0 {
625+
// Serialise default or custom signing keys
626+
if keys, err := json.Marshal(utils.Config.Auth.SigningKeys); err == nil {
626627
env = append(env, "GOTRUE_JWT_KEYS="+string(keys))
627628
// TODO: deprecate HS256 when it's no longer supported
628629
env = append(env, "GOTRUE_JWT_VALID_METHODS=HS256,RS256,ES256")

pkg/config/apikeys.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"crypto"
55
"crypto/ecdsa"
66
"crypto/elliptic"
7+
"crypto/rand"
78
"crypto/rsa"
89
"encoding/base64"
910
"math/big"
1011
"time"
1112

1213
"github.com/go-errors/errors"
1314
"github.com/golang-jwt/jwt/v5"
15+
"github.com/google/uuid"
16+
"github.com/supabase/cli/pkg/cast"
1417
)
1518

1619
const (
@@ -46,6 +49,25 @@ func (a *auth) generateAPIKeys() error {
4649
} else if len(a.JwtSecret.Value) < 16 {
4750
return errors.Errorf("Invalid config for auth.jwt_secret. Must be at least 16 characters")
4851
}
52+
// Generate default signing key (P-256 curve for ES256)
53+
if len(a.SigningKeysPath) == 0 {
54+
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
55+
if err != nil {
56+
return errors.Errorf("failed to generate ECDSA key: %w", err)
57+
}
58+
a.SigningKeys = append(a.SigningKeys, JWK{
59+
KeyType: "EC",
60+
KeyID: uuid.New().String(),
61+
Use: "sig",
62+
KeyOps: []string{"sign", "verify"},
63+
Algorithm: "ES256",
64+
Extractable: cast.Ptr(true),
65+
Curve: "P-256",
66+
X: base64.RawURLEncoding.EncodeToString(privateKey.PublicKey.X.Bytes()),
67+
Y: base64.RawURLEncoding.EncodeToString(privateKey.PublicKey.Y.Bytes()),
68+
PrivateExponent: base64.RawURLEncoding.EncodeToString(privateKey.D.Bytes()),
69+
})
70+
}
4971
// Generate anon key if not provided
5072
if len(a.AnonKey.Value) == 0 {
5173
signed, err := a.generateJWT("anon")

0 commit comments

Comments
 (0)