@@ -20,58 +20,24 @@ import (
20
20
"github.com/supabase/cli/internal/utils"
21
21
"github.com/supabase/cli/internal/utils/flags"
22
22
"github.com/supabase/cli/pkg/cast"
23
+ "github.com/supabase/cli/pkg/config"
23
24
)
24
25
25
- type Algorithm string
26
-
27
- const (
28
- AlgRS256 Algorithm = "RS256"
29
- AlgES256 Algorithm = "ES256"
30
- )
31
-
32
- type JWK struct {
33
- KeyType string `json:"kty"`
34
- KeyID string `json:"kid,omitempty"`
35
- Use string `json:"use,omitempty"`
36
- KeyOps []string `json:"key_ops,omitempty"`
37
- Algorithm string `json:"alg,omitempty"`
38
- Extractable * bool `json:"ext,omitempty"`
39
- // RSA specific fields
40
- Modulus string `json:"n,omitempty"`
41
- Exponent string `json:"e,omitempty"`
42
- // RSA private key fields
43
- PrivateExponent string `json:"d,omitempty"`
44
- FirstPrimeFactor string `json:"p,omitempty"`
45
- SecondPrimeFactor string `json:"q,omitempty"`
46
- FirstFactorCRTExponent string `json:"dp,omitempty"`
47
- SecondFactorCRTExponent string `json:"dq,omitempty"`
48
- FirstCRTCoefficient string `json:"qi,omitempty"`
49
- // EC specific fields
50
- Curve string `json:"crv,omitempty"`
51
- X string `json:"x,omitempty"`
52
- Y string `json:"y,omitempty"`
53
- }
54
-
55
- type KeyPair struct {
56
- PublicKey JWK
57
- PrivateKey JWK
58
- }
59
-
60
- // GenerateKeyPair generates a new key pair for the specified algorithm
61
- func GenerateKeyPair (alg Algorithm ) (* KeyPair , error ) {
62
- keyID := uuid .New ().String ()
26
+ // GeneratePrivateKey generates a new private key for the specified algorithm
27
+ func GeneratePrivateKey (alg config.Algorithm ) (* config.JWK , error ) {
28
+ keyID := uuid .New ()
63
29
64
30
switch alg {
65
- case AlgRS256 :
31
+ case config . AlgRS256 :
66
32
return generateRSAKeyPair (keyID )
67
- case AlgES256 :
33
+ case config . AlgES256 :
68
34
return generateECDSAKeyPair (keyID )
69
35
default :
70
36
return nil , errors .Errorf ("unsupported algorithm: %s" , alg )
71
37
}
72
38
}
73
39
74
- func generateRSAKeyPair (keyID string ) (* KeyPair , error ) {
40
+ func generateRSAKeyPair (keyID uuid. UUID ) (* config. JWK , error ) {
75
41
// Generate RSA key pair (2048 bits for RS256)
76
42
privateKey , err := rsa .GenerateKey (rand .Reader , 2048 )
77
43
if err != nil {
@@ -84,7 +50,7 @@ func generateRSAKeyPair(keyID string) (*KeyPair, error) {
84
50
privateKey .Precompute ()
85
51
86
52
// Convert to JWK format
87
- privateJWK := JWK {
53
+ privateJWK := config. JWK {
88
54
KeyType : "RSA" ,
89
55
KeyID : keyID ,
90
56
Use : "sig" ,
@@ -101,24 +67,10 @@ func generateRSAKeyPair(keyID string) (*KeyPair, error) {
101
67
FirstCRTCoefficient : base64 .RawURLEncoding .EncodeToString (privateKey .Precomputed .Qinv .Bytes ()),
102
68
}
103
69
104
- publicJWK := JWK {
105
- KeyType : "RSA" ,
106
- KeyID : keyID ,
107
- Use : "sig" ,
108
- KeyOps : []string {"verify" },
109
- Algorithm : "RS256" ,
110
- Extractable : cast .Ptr (true ),
111
- Modulus : privateJWK .Modulus ,
112
- Exponent : privateJWK .Exponent ,
113
- }
114
-
115
- return & KeyPair {
116
- PublicKey : publicJWK ,
117
- PrivateKey : privateJWK ,
118
- }, nil
70
+ return & privateJWK , nil
119
71
}
120
72
121
- func generateECDSAKeyPair (keyID string ) (* KeyPair , error ) {
73
+ func generateECDSAKeyPair (keyID uuid. UUID ) (* config. JWK , error ) {
122
74
// Generate ECDSA key pair (P-256 curve for ES256)
123
75
privateKey , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
124
76
if err != nil {
@@ -128,7 +80,7 @@ func generateECDSAKeyPair(keyID string) (*KeyPair, error) {
128
80
publicKey := & privateKey .PublicKey
129
81
130
82
// Convert to JWK format
131
- privateJWK := JWK {
83
+ privateJWK := config. JWK {
132
84
KeyType : "EC" ,
133
85
KeyID : keyID ,
134
86
Use : "sig" ,
@@ -141,22 +93,7 @@ func generateECDSAKeyPair(keyID string) (*KeyPair, error) {
141
93
PrivateExponent : base64 .RawURLEncoding .EncodeToString (privateKey .D .Bytes ()),
142
94
}
143
95
144
- publicJWK := JWK {
145
- KeyType : "EC" ,
146
- KeyID : keyID ,
147
- Use : "sig" ,
148
- KeyOps : []string {"verify" },
149
- Algorithm : "ES256" ,
150
- Extractable : cast .Ptr (true ),
151
- Curve : "P-256" ,
152
- X : privateJWK .X ,
153
- Y : privateJWK .Y ,
154
- }
155
-
156
- return & KeyPair {
157
- PublicKey : publicJWK ,
158
- PrivateKey : privateJWK ,
159
- }, nil
96
+ return & privateJWK , nil
160
97
}
161
98
162
99
// Run generates a key pair and writes it to the specified file path
@@ -168,13 +105,13 @@ func Run(ctx context.Context, algorithm string, appendMode bool, fsys afero.Fs)
168
105
outputPath := utils .Config .Auth .SigningKeysPath
169
106
170
107
// Generate key pair
171
- keyPair , err := GenerateKeyPair ( Algorithm (algorithm ))
108
+ privateJWK , err := GeneratePrivateKey ( config . Algorithm (algorithm ))
172
109
if err != nil {
173
110
return err
174
111
}
175
112
176
113
out := io .Writer (os .Stdout )
177
- var jwkArray []JWK
114
+ var jwkArray []config. JWK
178
115
if len (outputPath ) > 0 {
179
116
if err := utils .MkdirIfNotExistFS (fsys , filepath .Dir (outputPath )); err != nil {
180
117
return err
@@ -210,7 +147,7 @@ func Run(ctx context.Context, algorithm string, appendMode bool, fsys afero.Fs)
210
147
}
211
148
out = f
212
149
}
213
- jwkArray = append (jwkArray , keyPair . PrivateKey )
150
+ jwkArray = append (jwkArray , * privateJWK )
214
151
215
152
// Write to file
216
153
enc := json .NewEncoder (out )
@@ -245,5 +182,5 @@ signing_keys_path = "./signing_key.json"
245
182
246
183
// GetSupportedAlgorithms returns a list of supported algorithms
247
184
func GetSupportedAlgorithms () []string {
248
- return []string {string (AlgRS256 ), string (AlgES256 )}
185
+ return []string {string (config . AlgRS256 ), string (config . AlgES256 )}
249
186
}
0 commit comments