Skip to content

Commit f2ad852

Browse files
committed
Use typed SDK fields for CLI TOTP settings
1 parent 5d0acf0 commit f2ad852

4 files changed

Lines changed: 58 additions & 33 deletions

File tree

‎cmd/credentials.go‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -182,35 +182,27 @@ func (c CredentialsCmd) Get(ctx context.Context, in CredentialsGetInput) error {
182182
return nil
183183
}
184184

185-
func totpExtraFields(secret, algorithm string, digits, period *int) (map[string]any, error) {
185+
func validateTotpSettings(secret, algorithm string, digits, period *int) error {
186186
if algorithm == "" && digits == nil && period == nil {
187-
return nil, nil
187+
return nil
188188
}
189189
if secret == "" {
190-
return nil, fmt.Errorf("TOTP settings require --totp-secret")
190+
return fmt.Errorf("TOTP settings require --totp-secret")
191191
}
192-
fields := make(map[string]any)
193192
if algorithm != "" {
194193
switch strings.ToUpper(algorithm) {
195194
case "SHA1", "SHA256", "SHA512":
196-
fields["totp_algorithm"] = strings.ToUpper(algorithm)
197195
default:
198-
return nil, fmt.Errorf("--totp-algorithm must be SHA1, SHA256, or SHA512")
196+
return fmt.Errorf("--totp-algorithm must be SHA1, SHA256, or SHA512")
199197
}
200198
}
201-
if digits != nil {
202-
if *digits < 6 || *digits > 9 {
203-
return nil, fmt.Errorf("--totp-digits must be between 6 and 9")
204-
}
205-
fields["totp_digits"] = *digits
199+
if digits != nil && (*digits < 6 || *digits > 9) {
200+
return fmt.Errorf("--totp-digits must be between 6 and 9")
206201
}
207-
if period != nil {
208-
if *period < 15 || *period > 300 {
209-
return nil, fmt.Errorf("--totp-period must be between 15 and 300")
210-
}
211-
fields["totp_period"] = *period
202+
if period != nil && (*period < 15 || *period > 300) {
203+
return fmt.Errorf("--totp-period must be between 15 and 300")
212204
}
213-
return fields, nil
205+
return nil
214206
}
215207

216208
func (c CredentialsCmd) Create(ctx context.Context, in CredentialsCreateInput) error {
@@ -228,8 +220,7 @@ func (c CredentialsCmd) Create(ctx context.Context, in CredentialsCreateInput) e
228220
return fmt.Errorf("at least one --value is required")
229221
}
230222

231-
extra, err := totpExtraFields(in.TotpSecret, in.TotpAlgorithm, in.TotpDigits, in.TotpPeriod)
232-
if err != nil {
223+
if err := validateTotpSettings(in.TotpSecret, in.TotpAlgorithm, in.TotpDigits, in.TotpPeriod); err != nil {
233224
return err
234225
}
235226
params := kernel.CredentialNewParams{
@@ -245,7 +236,15 @@ func (c CredentialsCmd) Create(ctx context.Context, in CredentialsCreateInput) e
245236
if in.TotpSecret != "" {
246237
params.CreateCredentialRequest.TotpSecret = kernel.Opt(in.TotpSecret)
247238
}
248-
params.CreateCredentialRequest.SetExtraFields(extra)
239+
if in.TotpAlgorithm != "" {
240+
params.CreateCredentialRequest.TotpAlgorithm = kernel.CreateCredentialRequestTotpAlgorithm(strings.ToUpper(in.TotpAlgorithm))
241+
}
242+
if in.TotpDigits != nil {
243+
params.CreateCredentialRequest.TotpDigits = kernel.Int(int64(*in.TotpDigits))
244+
}
245+
if in.TotpPeriod != nil {
246+
params.CreateCredentialRequest.TotpPeriod = kernel.Int(int64(*in.TotpPeriod))
247+
}
249248

250249
if in.Output != "json" {
251250
pterm.Info.Printf("Creating credential '%s'...\n", in.Name)
@@ -294,8 +293,7 @@ func (c CredentialsCmd) Update(ctx context.Context, in CredentialsUpdateInput) e
294293
if err := validateJSONOutput(in.Output); err != nil {
295294
return err
296295
}
297-
extra, err := totpExtraFields(in.TotpSecret, in.TotpAlgorithm, in.TotpDigits, in.TotpPeriod)
298-
if err != nil {
296+
if err := validateTotpSettings(in.TotpSecret, in.TotpAlgorithm, in.TotpDigits, in.TotpPeriod); err != nil {
299297
return err
300298
}
301299

@@ -311,7 +309,15 @@ func (c CredentialsCmd) Update(ctx context.Context, in CredentialsUpdateInput) e
311309
if in.TotpSecret != "" {
312310
params.UpdateCredentialRequest.TotpSecret = kernel.Opt(in.TotpSecret)
313311
}
314-
params.UpdateCredentialRequest.SetExtraFields(extra)
312+
if in.TotpAlgorithm != "" {
313+
params.UpdateCredentialRequest.TotpAlgorithm = kernel.UpdateCredentialRequestTotpAlgorithm(strings.ToUpper(in.TotpAlgorithm))
314+
}
315+
if in.TotpDigits != nil {
316+
params.UpdateCredentialRequest.TotpDigits = kernel.Int(int64(*in.TotpDigits))
317+
}
318+
if in.TotpPeriod != nil {
319+
params.UpdateCredentialRequest.TotpPeriod = kernel.Int(int64(*in.TotpPeriod))
320+
}
315321
if len(in.Values) > 0 {
316322
params.UpdateCredentialRequest.Values = in.Values
317323
}

‎cmd/credentials_totp_test.go‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11-
func TestTotpExtraFields(t *testing.T) {
11+
func TestTypedTotpFields(t *testing.T) {
1212
digits, period := 8, 60
13-
extra, err := totpExtraFields("A234567A234567A2", "sha512", &digits, &period)
14-
require.NoError(t, err)
15-
params := kernel.CreateCredentialRequestParam{Domain: "example.com", Name: "test", Values: map[string]string{"username": "test"}}
16-
params.SetExtraFields(extra)
13+
require.NoError(t, validateTotpSettings("A234567A234567A2", "sha512", &digits, &period))
14+
params := kernel.CreateCredentialRequestParam{
15+
Domain: "example.com",
16+
Name: "test",
17+
Values: map[string]string{"username": "test"},
18+
TotpSecret: kernel.String("A234567A234567A2"),
19+
TotpAlgorithm: kernel.CreateCredentialRequestTotpAlgorithmSha512,
20+
TotpDigits: kernel.Int(8),
21+
TotpPeriod: kernel.Int(60),
22+
}
1723
encoded, err := json.Marshal(params)
1824
require.NoError(t, err)
1925
var body map[string]any
@@ -22,6 +28,20 @@ func TestTotpExtraFields(t *testing.T) {
2228
require.Equal(t, float64(8), body["totp_digits"])
2329
require.Equal(t, float64(60), body["totp_period"])
2430

31+
update := kernel.UpdateCredentialRequestParam{
32+
TotpSecret: kernel.String("A234567A234567A2"),
33+
TotpAlgorithm: kernel.UpdateCredentialRequestTotpAlgorithmSha512,
34+
TotpDigits: kernel.Int(8),
35+
TotpPeriod: kernel.Int(60),
36+
}
37+
encoded, err = json.Marshal(update)
38+
require.NoError(t, err)
39+
body = make(map[string]any)
40+
require.NoError(t, json.Unmarshal(encoded, &body))
41+
require.Equal(t, "SHA512", body["totp_algorithm"])
42+
require.Equal(t, float64(8), body["totp_digits"])
43+
require.Equal(t, float64(60), body["totp_period"])
44+
2545
for _, test := range []struct {
2646
secret string
2747
algorithm string
@@ -33,7 +53,6 @@ func TestTotpExtraFields(t *testing.T) {
3353
{secret: "A234567A234567A2", digits: new(int)},
3454
{secret: "A234567A234567A2", period: new(int)},
3555
} {
36-
_, err := totpExtraFields(test.secret, test.algorithm, test.digits, test.period)
37-
require.Error(t, err)
56+
require.Error(t, validateTotpSettings(test.secret, test.algorithm, test.digits, test.period))
3857
}
3958
}

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1
1010
github.com/golang-jwt/jwt/v5 v5.2.2
1111
github.com/joho/godotenv v1.5.1
12-
github.com/kernel/kernel-go-sdk v0.114.1-0.20260930182635-e746d9980b83
12+
github.com/kernel/kernel-go-sdk v0.116.0
1313
github.com/klauspost/compress v1.18.5
1414
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
1515
github.com/pterm/pterm v0.12.80

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
6666
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
6767
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
6868
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
69-
github.com/kernel/kernel-go-sdk v0.114.1-0.20260930182635-e746d9980b83 h1:S0qtghU55P043VAtKzRzoPM9Ix8OcsEbTQNGsnoWN3U=
70-
github.com/kernel/kernel-go-sdk v0.114.1-0.20260930182635-e746d9980b83/go.mod h1:EeZzSuHZVeHKxKCPUzxou2bovNGhXaz0RXrSqKNf1AQ=
69+
github.com/kernel/kernel-go-sdk v0.116.0 h1:NwZwl40sJ11lI8aHYSmMa0b6eXIXoZQVuH6UfPUaZX0=
70+
github.com/kernel/kernel-go-sdk v0.116.0/go.mod h1:EeZzSuHZVeHKxKCPUzxou2bovNGhXaz0RXrSqKNf1AQ=
7171
github.com/klauspost/compress v1.18.5 h1:/h1gH5Ce+VWNLSWqPzOVn6XBO+vJbCNGvjoaGBFW2IE=
7272
github.com/klauspost/compress v1.18.5/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
7373
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=

0 commit comments

Comments
 (0)