satellite/{console,db}: allow passphrasepromt toggling

This change adds the user's passphrase prompt setting to the
/account/settings endpoints.

Issue: https://github.com/storj/storj/issues/5616

Change-Id: I48d470d49e82096fd090b74da323b279e342546e
This commit is contained in:
Wilfred Asomani 2023-04-28 17:23:56 +00:00 committed by Storj Robot
parent d8081169cd
commit bf05040dd6
5 changed files with 87 additions and 66 deletions

View File

@ -971,10 +971,11 @@ func (a *Auth) SetUserSettings(w http.ResponseWriter, r *http.Request) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
var updateInfo struct { var updateInfo struct {
OnboardingStart *bool `json:"onboardingStart"` OnboardingStart *bool `json:"onboardingStart"`
OnboardingEnd *bool `json:"onboardingEnd"` OnboardingEnd *bool `json:"onboardingEnd"`
OnboardingStep *string `json:"onboardingStep"` PassphrasePrompt *bool `json:"passphrasePrompt"`
SessionDuration *int64 `json:"sessionDuration"` OnboardingStep *string `json:"onboardingStep"`
SessionDuration *int64 `json:"sessionDuration"`
} }
err = json.NewDecoder(r.Body).Decode(&updateInfo) err = json.NewDecoder(r.Body).Decode(&updateInfo)
@ -993,10 +994,11 @@ func (a *Auth) SetUserSettings(w http.ResponseWriter, r *http.Request) {
} }
settings, err := a.service.SetUserSettings(ctx, console.UpsertUserSettingsRequest{ settings, err := a.service.SetUserSettings(ctx, console.UpsertUserSettingsRequest{
OnboardingStart: updateInfo.OnboardingStart, OnboardingStart: updateInfo.OnboardingStart,
OnboardingEnd: updateInfo.OnboardingEnd, OnboardingEnd: updateInfo.OnboardingEnd,
OnboardingStep: updateInfo.OnboardingStep, OnboardingStep: updateInfo.OnboardingStep,
SessionDuration: newDuration, PassphrasePrompt: updateInfo.PassphrasePrompt,
SessionDuration: newDuration,
}) })
if err != nil { if err != nil {
a.serveJSONError(w, err) a.serveJSONError(w, err)

View File

@ -103,61 +103,58 @@ func TestAuth(t *testing.T) {
} }
{ // Test_UserSettings { // Test_UserSettings
testGetSettings := func(expected struct { type expectedSettings struct {
SessionDuration *time.Duration SessionDuration *time.Duration
OnboardingStart bool OnboardingStart bool
OnboardingEnd bool OnboardingEnd bool
OnboardingStep *string PassphrasePrompt bool
}) { OnboardingStep *string
}
testGetSettings := func(expected expectedSettings) {
resp, body := test.request(http.MethodGet, "/auth/account/settings", nil) resp, body := test.request(http.MethodGet, "/auth/account/settings", nil)
var settings struct { var settings struct {
SessionDuration *time.Duration SessionDuration *time.Duration
OnboardingStart bool OnboardingStart bool
OnboardingEnd bool OnboardingEnd bool
OnboardingStep *string PassphrasePrompt bool
OnboardingStep *string
} }
require.Equal(t, http.StatusOK, resp.StatusCode) require.Equal(t, http.StatusOK, resp.StatusCode)
require.NoError(test.t, json.Unmarshal([]byte(body), &settings)) require.NoError(test.t, json.Unmarshal([]byte(body), &settings))
require.Equal(test.t, expected.OnboardingStart, settings.OnboardingStart) require.Equal(test.t, expected.OnboardingStart, settings.OnboardingStart)
require.Equal(test.t, expected.OnboardingEnd, settings.OnboardingEnd) require.Equal(test.t, expected.OnboardingEnd, settings.OnboardingEnd)
require.Equal(test.t, expected.PassphrasePrompt, settings.PassphrasePrompt)
require.Equal(test.t, expected.OnboardingStep, settings.OnboardingStep) require.Equal(test.t, expected.OnboardingStep, settings.OnboardingStep)
require.Equal(test.t, expected.SessionDuration, settings.SessionDuration) require.Equal(test.t, expected.SessionDuration, settings.SessionDuration)
} }
testGetSettings(struct { testGetSettings(expectedSettings{
SessionDuration *time.Duration SessionDuration: nil,
OnboardingStart bool OnboardingStart: true,
OnboardingEnd bool OnboardingEnd: true,
OnboardingStep *string PassphrasePrompt: true,
}{ OnboardingStep: nil,
SessionDuration: nil,
OnboardingStart: true,
OnboardingEnd: true,
OnboardingStep: nil,
}) })
step := "cli" step := "cli"
duration := time.Duration(15) * time.Minute duration := time.Duration(15) * time.Minute
resp, _ := test.request(http.MethodPatch, "/auth/account/settings", resp, _ := test.request(http.MethodPatch, "/auth/account/settings",
test.toJSON(map[string]interface{}{ test.toJSON(map[string]interface{}{
"sessionDuration": duration, "sessionDuration": duration,
"onboardingStart": true, "onboardingStart": true,
"onboardingEnd": false, "onboardingEnd": false,
"onboardingStep": step, "passphrasePrompt": false,
"onboardingStep": step,
})) }))
require.Equal(t, http.StatusOK, resp.StatusCode) require.Equal(t, http.StatusOK, resp.StatusCode)
testGetSettings(struct { testGetSettings(expectedSettings{
SessionDuration *time.Duration SessionDuration: &duration,
OnboardingStart bool OnboardingStart: true,
OnboardingEnd bool OnboardingEnd: false,
OnboardingStep *string PassphrasePrompt: false,
}{ OnboardingStep: &step,
SessionDuration: &duration,
OnboardingStart: true,
OnboardingEnd: false,
OnboardingStep: &step,
}) })
resp, _ = test.request(http.MethodPatch, "/auth/account/settings", resp, _ = test.request(http.MethodPatch, "/auth/account/settings",
@ -170,16 +167,12 @@ func TestAuth(t *testing.T) {
require.Equal(t, http.StatusOK, resp.StatusCode) require.Equal(t, http.StatusOK, resp.StatusCode)
// having passed nil to /auth/account/settings shouldn't have changed existing values. // having passed nil to /auth/account/settings shouldn't have changed existing values.
testGetSettings(struct { testGetSettings(expectedSettings{
SessionDuration *time.Duration SessionDuration: &duration,
OnboardingStart bool OnboardingStart: true,
OnboardingEnd bool OnboardingEnd: false,
OnboardingStep *string PassphrasePrompt: false,
}{ OnboardingStep: &step,
SessionDuration: &duration,
OnboardingStart: true,
OnboardingEnd: false,
OnboardingStep: &step,
}) })
// having passed 0 as sessionDuration to /auth/account/settings should nullify it. // having passed 0 as sessionDuration to /auth/account/settings should nullify it.
@ -189,12 +182,7 @@ func TestAuth(t *testing.T) {
})) }))
require.Equal(t, http.StatusOK, resp.StatusCode) require.Equal(t, http.StatusOK, resp.StatusCode)
testGetSettings(struct { testGetSettings(expectedSettings{
SessionDuration *time.Duration
OnboardingStart bool
OnboardingEnd bool
OnboardingStep *string
}{
SessionDuration: nil, SessionDuration: nil,
OnboardingStart: true, OnboardingStart: true,
OnboardingEnd: false, OnboardingEnd: false,

View File

@ -251,17 +251,19 @@ type UpdateUserRequest struct {
// UserSettings contains configurations for a user. // UserSettings contains configurations for a user.
type UserSettings struct { type UserSettings struct {
SessionDuration *time.Duration `json:"sessionDuration"` SessionDuration *time.Duration `json:"sessionDuration"`
OnboardingStart bool `json:"onboardingStart"` OnboardingStart bool `json:"onboardingStart"`
OnboardingEnd bool `json:"onboardingEnd"` OnboardingEnd bool `json:"onboardingEnd"`
OnboardingStep *string `json:"onboardingStep"` PassphrasePrompt bool `json:"passphrasePrompt"`
OnboardingStep *string `json:"onboardingStep"`
} }
// UpsertUserSettingsRequest contains all user settings which are configurable via Users.UpsertSettings. // UpsertUserSettingsRequest contains all user settings which are configurable via Users.UpsertSettings.
type UpsertUserSettingsRequest struct { type UpsertUserSettingsRequest struct {
// The DB stores this value with minute granularity. Finer time units are ignored. // The DB stores this value with minute granularity. Finer time units are ignored.
SessionDuration **time.Duration SessionDuration **time.Duration
OnboardingStart *bool OnboardingStart *bool
OnboardingEnd *bool OnboardingEnd *bool
OnboardingStep *string PassphrasePrompt *bool
OnboardingStep *string
} }

View File

@ -302,6 +302,10 @@ func (users *users) GetSettings(ctx context.Context, userID uuid.UUID) (settings
settings.OnboardingStart = row.OnboardingStart settings.OnboardingStart = row.OnboardingStart
settings.OnboardingEnd = row.OnboardingEnd settings.OnboardingEnd = row.OnboardingEnd
settings.OnboardingStep = row.OnboardingStep settings.OnboardingStep = row.OnboardingStep
settings.PassphrasePrompt = true
if row.PassphrasePrompt != nil {
settings.PassphrasePrompt = *row.PassphrasePrompt
}
if row.SessionMinutes != nil { if row.SessionMinutes != nil {
dur := time.Duration(*row.SessionMinutes) * time.Minute dur := time.Duration(*row.SessionMinutes) * time.Minute
settings.SessionDuration = &dur settings.SessionDuration = &dur
@ -334,6 +338,10 @@ func (users *users) UpsertSettings(ctx context.Context, userID uuid.UUID, settin
update.OnboardingEnd = dbx.UserSettings_OnboardingEnd(*settings.OnboardingEnd) update.OnboardingEnd = dbx.UserSettings_OnboardingEnd(*settings.OnboardingEnd)
fieldCount++ fieldCount++
} }
if settings.PassphrasePrompt != nil {
update.PassphrasePrompt = dbx.UserSettings_PassphrasePrompt(*settings.PassphrasePrompt)
fieldCount++
}
if settings.OnboardingStep != nil { if settings.OnboardingStep != nil {
update.OnboardingStep = dbx.UserSettings_OnboardingStep(*settings.OnboardingStep) update.OnboardingStep = dbx.UserSettings_OnboardingStep(*settings.OnboardingStep)
fieldCount++ fieldCount++

View File

@ -363,5 +363,26 @@ func TestUserSettings(t *testing.T) {
require.Equal(t, newBool, settings.OnboardingEnd) require.Equal(t, newBool, settings.OnboardingEnd)
require.Equal(t, &newStep, settings.OnboardingStep) require.Equal(t, &newStep, settings.OnboardingStep)
}) })
t.Run("test passphrase prompt", func(t *testing.T) {
id = testrand.UUID()
require.NoError(t, users.UpsertSettings(ctx, id, console.UpsertUserSettingsRequest{}))
settings, err := users.GetSettings(ctx, id)
require.NoError(t, err)
require.True(t, settings.PassphrasePrompt)
newBool := false
require.NoError(t, users.UpsertSettings(ctx, id, console.UpsertUserSettingsRequest{
PassphrasePrompt: &newBool,
}))
settings, err = users.GetSettings(ctx, id)
require.NoError(t, err)
require.Equal(t, newBool, settings.PassphrasePrompt)
require.NoError(t, users.UpsertSettings(ctx, id, console.UpsertUserSettingsRequest{}))
settings, err = users.GetSettings(ctx, id)
require.NoError(t, err)
require.Equal(t, newBool, settings.PassphrasePrompt)
})
}) })
} }