Skip to content

Commit

Permalink
rebase/lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexCuse committed Oct 13, 2023
1 parent a55604c commit d5a5b83
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 44 deletions.
2 changes: 1 addition & 1 deletion app/data/redis/blob_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *BlobStore) WriteNX(name string, blob []byte) (bool, error) {
}

func (s *BlobStore) Write(name string, blob []byte) (bool, error) {
res, err := s.Client.Set(name, blob, s.TTL).Result()
res, err := s.Client.Set(context.TODO(), name, blob, s.TTL).Result()
if res != "OK" {
return false, err
}
Expand Down
14 changes: 9 additions & 5 deletions app/services/credentials_verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ func TestCredentialsVerifierWithTOTPSuccess(t *testing.T) {
username := "myname"
password := "mysecret"
dbEncryptionKey := []byte("DLz2TNDRdWWA5w8YNeCJ7uzcS4WDzQmB")
totpSecret := "JKK5AG4NDAWSZSR4ZFKZBWZ7OJGLB2JM"
totpSecretEnc := []byte("cli6azfL5i7PAnh8U/w3Zbglsm3XcdaGODy+Ga5QqT02c9hotDAR1Y28--3UihzsJhw/+EU3R6--qUw9L8DwN5XPVfOStshKzA==")

bcrypted := []byte("$2a$04$lzQPXlov4RFLxps1uUGq4e4wmVjLYz3WrqQw4bSdfIiJRyo3/fk3C")

cfg := app.Config{BcryptCost: 4, DBEncryptionKey: dbEncryptionKey}
store := mock.NewAccountStore()
account, _ := store.Create(username, bcrypted)
store.SetTOTPSecret(account.ID, totpSecretEnc)
set, err := store.SetTOTPSecret(account.ID, totpSecretEnc)

Check failure on line 41 in app/services/credentials_verifier_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: totpSecretEnc

Check failure on line 41 in app/services/credentials_verifier_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: totpSecretEnc
require.NoError(t, err)
require.True(t, set)

code, err := totp.GenerateCode(totpSecret, time.Now())

Check failure on line 45 in app/services/credentials_verifier_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: totpSecret

Check failure on line 45 in app/services/credentials_verifier_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: totpSecret
require.NoError(t, err)
Expand Down Expand Up @@ -91,8 +92,11 @@ func TestCredentialsVerifierWithTOTPFailure(t *testing.T) {

cfg := app.Config{BcryptCost: 4, DBEncryptionKey: dbEncryptionKey}
store := mock.NewAccountStore()
account, _ := store.Create(username, bcrypted)
store.SetTOTPSecret(account.ID, totpSecretEnc)
account, err := store.Create(username, bcrypted)
require.NoError(t, err)
set, err := store.SetTOTPSecret(account.ID, totpSecretEnc)
require.NoError(t, err)
require.True(t, set)

testCases := []struct {
code string
Expand Down
2 changes: 1 addition & 1 deletion app/services/password_changer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestPasswordChanger(t *testing.T) {
}

invoke := func(id int, currentPassword string, password string) error {
return services.PasswordChanger(accountStore, &ops.LogReporter{logrus.New()}, cfg, id, currentPassword, password)
return services.PasswordChanger(accountStore, &ops.LogReporter{FieldLogger: logrus.New()}, cfg, id, currentPassword, password)
}

factory := func(username string, password string) (*models.Account, error) {
Expand Down
11 changes: 5 additions & 6 deletions app/services/password_resetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ func TestPasswordResetter(t *testing.T) {
}

func TestPasswordResetterWithTOTP(t *testing.T) {
totpSecret := "JKK5AG4NDAWSZSR4ZFKZBWZ7OJGLB2JM"
totpSecretEnc := []byte("cli6azfL5i7PAnh8U/w3Zbglsm3XcdaGODy+Ga5QqT02c9hotDAR1Y28--3UihzsJhw/+EU3R6--qUw9L8DwN5XPVfOStshKzA==")

accountStore := mock.NewAccountStore()
cfg := &app.Config{
AuthNURL: &url.URL{Scheme: "http", Host: "authn.example.com"},
Expand All @@ -137,14 +134,15 @@ func TestPasswordResetterWithTOTP(t *testing.T) {
}

invoke := func(token string, password string, totpCode string) error {
_, err := services.PasswordResetter(accountStore, &ops.LogReporter{logrus.New()}, cfg, token, password, totpCode)
_, err := services.PasswordResetter(accountStore, &ops.LogReporter{FieldLogger: logrus.New()}, cfg, token, password, totpCode)
return err
}

t.Run("sets new password", func(t *testing.T) {
expired, err := accountStore.Create("[email protected]", []byte("old"))
require.NoError(t, err)
accountStore.SetTOTPSecret(expired.ID, totpSecretEnc)
_, err = accountStore.SetTOTPSecret(expired.ID, totpSecretEnc)

Check failure on line 144 in app/services/password_resetter_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: totpSecretEnc

Check failure on line 144 in app/services/password_resetter_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: totpSecretEnc
require.NoError(t, err)
_, err = accountStore.RequireNewPassword(expired.ID)
require.NoError(t, err)

Expand All @@ -163,7 +161,8 @@ func TestPasswordResetterWithTOTP(t *testing.T) {
t.Run("without totp code", func(t *testing.T) {
expired, err := accountStore.Create("[email protected]", []byte("old"))
require.NoError(t, err)
accountStore.SetTOTPSecret(expired.ID, totpSecretEnc)
_, err = accountStore.SetTOTPSecret(expired.ID, totpSecretEnc)

Check failure on line 164 in app/services/password_resetter_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: totpSecretEnc

Check failure on line 164 in app/services/password_resetter_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: totpSecretEnc
require.NoError(t, err)
_, err = accountStore.RequireNewPassword(expired.ID)
require.NoError(t, err)

Expand Down
15 changes: 9 additions & 6 deletions app/services/passwordless_token_verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ func TestPasswordlessTokenVerifier(t *testing.T) {
}

func TestPasswordlessTokenVerifierWithTOTP(t *testing.T) {
totpSecret := "JKK5AG4NDAWSZSR4ZFKZBWZ7OJGLB2JM"
totpSecretEnc := []byte("cli6azfL5i7PAnh8U/w3Zbglsm3XcdaGODy+Ga5QqT02c9hotDAR1Y28--3UihzsJhw/+EU3R6--qUw9L8DwN5XPVfOStshKzA==")

accountStore := mock.NewAccountStore()
cfg := &app.Config{
AuthNURL: &url.URL{Scheme: "http", Host: "authn.example.com"},
Expand All @@ -122,14 +119,17 @@ func TestPasswordlessTokenVerifierWithTOTP(t *testing.T) {
}

invoke := func(token string, totpCode string) error {
_, err := services.PasswordlessTokenVerifier(accountStore, &ops.LogReporter{logrus.New()}, cfg, token, totpCode)
_, err := services.PasswordlessTokenVerifier(accountStore, &ops.LogReporter{FieldLogger: logrus.New()}, cfg, token, totpCode)
return err
}

t.Run("with good code", func(t *testing.T) {
account, err := accountStore.Create("[email protected]", []byte("old"))
accountStore.SetTOTPSecret(account.ID, totpSecretEnc)
require.NoError(t, err)
set, err := accountStore.SetTOTPSecret(account.ID, totpSecretEnc)

Check failure on line 129 in app/services/passwordless_token_verifier_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: totpSecretEnc

Check failure on line 129 in app/services/passwordless_token_verifier_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: totpSecretEnc
require.NoError(t, err)
require.True(t, set)

token := newToken(account.ID)

code, err := totp.GenerateCode(totpSecret, time.Now())

Check failure on line 135 in app/services/passwordless_token_verifier_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: totpSecret

Check failure on line 135 in app/services/passwordless_token_verifier_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: totpSecret
Expand All @@ -141,8 +141,11 @@ func TestPasswordlessTokenVerifierWithTOTP(t *testing.T) {

t.Run("with bad code", func(t *testing.T) {
account, err := accountStore.Create("[email protected]", []byte("old"))
accountStore.SetTOTPSecret(account.ID, totpSecretEnc)
require.NoError(t, err)
set, err := accountStore.SetTOTPSecret(account.ID, totpSecretEnc)

Check failure on line 145 in app/services/passwordless_token_verifier_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: totpSecretEnc

Check failure on line 145 in app/services/passwordless_token_verifier_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: totpSecretEnc (typecheck)
require.NoError(t, err)
require.True(t, set)

token := newToken(account.ID)

err = invoke(token, "12345")
Expand Down
13 changes: 7 additions & 6 deletions server/handlers/post_password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,6 @@ func TestPostPassword(t *testing.T) {
}

func TestPostPasswordWithTOTP(t *testing.T) {
totpSecret := "JKK5AG4NDAWSZSR4ZFKZBWZ7OJGLB2JM"
totpSecretEnc := []byte("cli6azfL5i7PAnh8U/w3Zbglsm3XcdaGODy+Ga5QqT02c9hotDAR1Y28--3UihzsJhw/+EU3R6--qUw9L8DwN5XPVfOStshKzA==")

app := test.App()
server := test.Server(app)
defer server.Close()
Expand Down Expand Up @@ -278,8 +275,10 @@ func TestPostPasswordWithTOTP(t *testing.T) {
t.Run("valid totp code", func(t *testing.T) {
// given an account
account, err := factory("[email protected]", "oldpwd")
app.AccountStore.SetTOTPSecret(account.ID, totpSecretEnc)
require.NoError(t, err)
set, err := app.AccountStore.SetTOTPSecret(account.ID, totpSecretEnc)

Check failure on line 279 in server/handlers/post_password_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: totpSecretEnc
require.NoError(t, err)
require.True(t, set)

// given a reset token
token, err := resets.New(app.Config, account.ID, account.PasswordChangedAt)
Expand All @@ -306,8 +305,10 @@ func TestPostPasswordWithTOTP(t *testing.T) {
t.Run("invalid totp code", func(t *testing.T) {
// given an account
account, err := factory("[email protected]", "oldpwd")
app.AccountStore.SetTOTPSecret(account.ID, totpSecretEnc)
require.NoError(t, err)
set, err := app.AccountStore.SetTOTPSecret(account.ID, totpSecretEnc)
require.NoError(t, err)
require.True(t, set)

// given a reset token
token, err := resets.New(app.Config, account.ID, account.PasswordChangedAt)
Expand All @@ -324,6 +325,6 @@ func TestPostPasswordWithTOTP(t *testing.T) {
require.NoError(t, err)

assert.Equal(t, http.StatusUnprocessableEntity, res.StatusCode)
test.AssertErrors(t, res, services.FieldErrors{{"totp", "INVALID_OR_EXPIRED"}})
test.AssertErrors(t, res, services.FieldErrors{{Field: "totp", Message: "INVALID_OR_EXPIRED"}})
})
}
12 changes: 1 addition & 11 deletions server/handlers/post_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ import (

"golang.org/x/crypto/bcrypt"

"github.com/keratin/authn-server/app/services"
"github.com/keratin/authn-server/lib/route"
"github.com/keratin/authn-server/server/test"
"github.com/keratin/authn-server/app/services"
"github.com/keratin/authn-server/lib/route"
"github.com/keratin/authn-server/server/test"
"github.com/pquerna/otp/totp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/bcrypt"
)

func TestPostSessionSuccess(t *testing.T) {
Expand Down Expand Up @@ -97,9 +93,6 @@ func TestPostSessionFailure(t *testing.T) {
}

func TestPostSessionSuccessWithTOTP(t *testing.T) {
totpSecret := "JKK5AG4NDAWSZSR4ZFKZBWZ7OJGLB2JM"
totpSecretEnc := []byte("cli6azfL5i7PAnh8U/w3Zbglsm3XcdaGODy+Ga5QqT02c9hotDAR1Y28--3UihzsJhw/+EU3R6--qUw9L8DwN5XPVfOStshKzA==")

app := test.App()
server := test.Server(app)
defer server.Close()
Expand Down Expand Up @@ -128,9 +121,6 @@ func TestPostSessionSuccessWithTOTP(t *testing.T) {
}

func TestPostSessionSuccessWithSessionAndTOTP(t *testing.T) {
totpSecret := "JKK5AG4NDAWSZSR4ZFKZBWZ7OJGLB2JM"
totpSecretEnc := []byte("cli6azfL5i7PAnh8U/w3Zbglsm3XcdaGODy+Ga5QqT02c9hotDAR1Y28--3UihzsJhw/+EU3R6--qUw9L8DwN5XPVfOStshKzA==")

app := test.App()
server := test.Server(app)
defer server.Close()
Expand Down Expand Up @@ -188,7 +178,7 @@ func TestPostSessionFailureWithTOTP(t *testing.T) {
totpCode string
errors services.FieldErrors
}{
{"foo", "bar", "12345", services.FieldErrors{{"totp", "INVALID_OR_EXPIRED"}}},
{"foo", "bar", "12345", services.FieldErrors{{Field: "totp", Message: "INVALID_OR_EXPIRED"}}},
}

for _, tc := range testCases {
Expand Down
13 changes: 7 additions & 6 deletions server/handlers/post_session_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ func TestPostSessionToken(t *testing.T) {
}

func TestPostSessionTokenWithTOTP(t *testing.T) {
totpSecret := "JKK5AG4NDAWSZSR4ZFKZBWZ7OJGLB2JM"
totpSecretEnc := []byte("cli6azfL5i7PAnh8U/w3Zbglsm3XcdaGODy+Ga5QqT02c9hotDAR1Y28--3UihzsJhw/+EU3R6--qUw9L8DwN5XPVfOStshKzA==")

app := test.App()
server := test.Server(app)
defer server.Close()
Expand Down Expand Up @@ -143,7 +140,9 @@ func TestPostSessionTokenWithTOTP(t *testing.T) {
// given an account
account, err := factory("[email protected]", "oldpwd")
require.NoError(t, err)
app.AccountStore.SetTOTPSecret(account.ID, totpSecretEnc)
set, err := app.AccountStore.SetTOTPSecret(account.ID, totpSecretEnc)
require.NoError(t, err)
require.True(t, set)

// given a passwordless token
token, err := passwordless.New(app.Config, account.ID)
Expand All @@ -170,7 +169,9 @@ func TestPostSessionTokenWithTOTP(t *testing.T) {
// given an account
account, err := factory("[email protected]", "oldpwd")
require.NoError(t, err)
app.AccountStore.SetTOTPSecret(account.ID, totpSecretEnc)
set, err := app.AccountStore.SetTOTPSecret(account.ID, totpSecretEnc)
require.NoError(t, err)
require.True(t, set)

// given a passwordless token
token, err := passwordless.New(app.Config, account.ID)
Expand All @@ -186,6 +187,6 @@ func TestPostSessionTokenWithTOTP(t *testing.T) {
require.NoError(t, err)

assert.Equal(t, http.StatusUnprocessableEntity, res.StatusCode)
test.AssertErrors(t, res, services.FieldErrors{{"totp", "INVALID_OR_EXPIRED"}})
test.AssertErrors(t, res, services.FieldErrors{{Field: "totp", Message: "INVALID_OR_EXPIRED"}})
})
}
2 changes: 0 additions & 2 deletions server/handlers/post_totp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func TestPostTOTPSuccess(t *testing.T) {
server := test.Server(app)
defer server.Close()

totpSecret := "JKK5AG4NDAWSZSR4ZFKZBWZ7OJGLB2JM"
account, _ := app.AccountStore.Create("[email protected]", []byte("password"))
existingSession := test.CreateSession(app.RefreshTokenStore, app.Config, account.ID)
err := app.TOTPCache.CacheTOTPSecret(account.ID, []byte(totpSecret))
Expand All @@ -45,7 +44,6 @@ func TestPostTOTPFailure(t *testing.T) {
server := test.Server(app)
defer server.Close()

totpSecret := "JKK5AG4NDAWSZSR4ZFKZBWZ7OJGLB2JM"
account, _ := app.AccountStore.Create("[email protected]", []byte("password"))
existingSession := test.CreateSession(app.RefreshTokenStore, app.Config, account.ID)
err := app.TOTPCache.CacheTOTPSecret(account.ID, []byte(totpSecret))
Expand Down

0 comments on commit d5a5b83

Please sign in to comment.