Skip to content

Commit

Permalink
Set secret with issuer (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatthm authored Mar 20, 2024
1 parent e161f78 commit b8364bf
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion keyring/totp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *TOTPSecretProvider) TOTPSecret(ctx context.Context) otp.TOTPSecret {
}

// SetTOTPSecret persists the TOTP secret to the keyring.
func (s *TOTPSecretProvider) SetTOTPSecret(ctx context.Context, secret otp.TOTPSecret) error {
func (s *TOTPSecretProvider) SetTOTPSecret(ctx context.Context, secret otp.TOTPSecret, _ string) error {
if s.account == "" {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion keyring/totp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestTOTPSecretProvider_SetTOTPSecret(t *testing.T) {
keyring.WithLogger(ctxd.NoOpLogger{}),
)

err := s.SetTOTPSecret(context.Background(), "secret")
err := s.SetTOTPSecret(context.Background(), "secret", "issuer")

if tc.expectedError == "" {
require.NoError(t, err)
Expand Down
10 changes: 5 additions & 5 deletions mock/totp_secret_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions mock/totp_secret_setter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions totp.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s TOTPSecret) TOTPSecret(context.Context) TOTPSecret {
}

// SetTOTPSecret sets the TOTP secret.
func (s TOTPSecret) SetTOTPSecret(context.Context, TOTPSecret) error {
func (s TOTPSecret) SetTOTPSecret(context.Context, TOTPSecret, string) error {
return ErrTOTPSecretReadOnly
}

Expand All @@ -64,7 +64,7 @@ func (s totpSecreteSurrogate) TOTPSecret(context.Context) TOTPSecret {
}

// SetTOTPSecret sets the TOTP secret.
func (s *totpSecreteSurrogate) SetTOTPSecret(_ context.Context, secret TOTPSecret) error {
func (s *totpSecreteSurrogate) SetTOTPSecret(_ context.Context, secret TOTPSecret, _ string) error {
s.secret = secret

return nil
Expand All @@ -91,7 +91,7 @@ type TOTPSecretGetter interface {

// TOTPSecretSetter is an interface that sets a TOTP secret.
type TOTPSecretSetter interface {
SetTOTPSecret(ctx context.Context, secret TOTPSecret) error
SetTOTPSecret(ctx context.Context, secret TOTPSecret, issuer string) error
}

// TOTPSecretDeleter is an interface that deletes a TOTP secret.
Expand Down Expand Up @@ -145,9 +145,9 @@ func (ps TOTPSecretProviders) TOTPSecret(ctx context.Context) TOTPSecret {
}

// SetTOTPSecret sets the TOTP secret.
func (ps TOTPSecretProviders) SetTOTPSecret(ctx context.Context, secret TOTPSecret) error {
func (ps TOTPSecretProviders) SetTOTPSecret(ctx context.Context, secret TOTPSecret, issuer string) error {
for _, p := range ps {
if err := p.SetTOTPSecret(ctx, secret); err != nil {
if err := p.SetTOTPSecret(ctx, secret, issuer); err != nil {
return err
}
}
Expand Down Expand Up @@ -195,7 +195,7 @@ func (e envTOTPSecret) TOTPSecret(_ context.Context) TOTPSecret {
}

// SetTOTPSecret sets the TOTP secret to the environment.
func (e envTOTPSecret) SetTOTPSecret(_ context.Context, secret TOTPSecret) error {
func (e envTOTPSecret) SetTOTPSecret(_ context.Context, secret TOTPSecret, _ string) error {
return os.Setenv(string(e), string(secret))
}

Expand Down
14 changes: 7 additions & 7 deletions totp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestTOTPSecret_TOTPSecret(t *testing.T) {

assert.Equal(t, "secret", string(p.TOTPSecret(ctx)))

err := p.SetTOTPSecret(ctx, "changed")
err := p.SetTOTPSecret(ctx, "changed", "")
require.ErrorIs(t, err, otp.ErrTOTPSecretReadOnly)

assert.Equal(t, "secret", string(p.TOTPSecret(ctx)))
Expand All @@ -72,7 +72,7 @@ func TestTOTPSecretFromEnv(t *testing.T) {

assert.Equal(t, "secret", string(p.TOTPSecret(ctx)))

err := p.SetTOTPSecret(ctx, "changed")
err := p.SetTOTPSecret(ctx, "changed", "")
require.NoError(t, err)

assert.Equal(t, "changed", string(p.TOTPSecret(ctx)))
Expand Down Expand Up @@ -140,23 +140,23 @@ func TestChainTOTPSecretProviders_SetSecret(t *testing.T) {
{
scenario: "failed",
mockProvider: mock.MockTOTPSecretProvider(func(p *mock.TOTPSecretProvider) {
p.On("SetTOTPSecret", mock.Anything, otp.TOTPSecret("secret")).
p.On("SetTOTPSecret", mock.Anything, otp.TOTPSecret("secret"), "issuer").
Return(assert.AnError)
}),
expectedError: `assert.AnError general error for testing`,
},
{
scenario: "readonly",
mockProvider: mock.MockTOTPSecretProvider(func(p *mock.TOTPSecretProvider) {
p.On("SetTOTPSecret", mock.Anything, otp.TOTPSecret("secret")).
p.On("SetTOTPSecret", mock.Anything, otp.TOTPSecret("secret"), "issuer").
Return(otp.ErrTOTPSecretReadOnly)
}),
expectedError: `totp secret is read-only`,
},
{
scenario: "success",
mockProvider: mock.MockTOTPSecretProvider(func(p *mock.TOTPSecretProvider) {
p.On("SetTOTPSecret", mock.Anything, otp.TOTPSecret("secret")).
p.On("SetTOTPSecret", mock.Anything, otp.TOTPSecret("secret"), "issuer").
Return(nil)
}),
},
Expand All @@ -169,7 +169,7 @@ func TestChainTOTPSecretProviders_SetSecret(t *testing.T) {

p := otp.ChainTOTPSecretProviders(tc.mockProvider(t))

err := p.SetTOTPSecret(context.Background(), "secret")
err := p.SetTOTPSecret(context.Background(), "secret", "issuer")

if tc.expectedError == "" {
require.NoError(t, err)
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestChainTOTPSecretProviders(t *testing.T) { //nolint: paralleltest

assert.Equal(t, "secret", string(p.TOTPSecret(ctx)))

err := p.SetTOTPSecret(ctx, "changed")
err := p.SetTOTPSecret(ctx, "changed", "")
require.NoError(t, err)

// Change secret.
Expand Down

0 comments on commit b8364bf

Please sign in to comment.