account_mgmt_test.go 7.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
package integrationtest

import (
	"testing"

	as "git.autistici.org/ai3/accountserver"
)

// Verify that authentication on GetUser works as expected:
// - users can fetch their own data but not other users'
// - admins can read everything.
func TestIntegration_GetUser_Auth(t *testing.T) {
	stop, _, c := startService(t)
	defer stop()

	testdata := []struct {
		authUser   string
		authGroup  string
		expectedOk bool
	}{
		{"uno@investici.org", "", true},
		{"uno@investici.org", "users", true},
		{"due@investici.org", "users", false},
		{testAdminUser, testAdminGroup, true},
	}

	for _, td := range testdata {
		var user as.User
		var groups []string
		if td.authGroup != "" {
			groups = append(groups, td.authGroup)
		}
		err := c.request("/api/user/get", &as.GetUserRequest{
			UserRequestBase: as.UserRequestBase{
				RequestBase: as.RequestBase{
					SSO: c.ssoTicket(td.authUser, groups...),
				},
				Username: "uno@investici.org",
			},
		}, &user)
		if td.expectedOk && err != nil {
			t.Errorf("access error for user %s: expected ok, got error: %v", td.authUser, err)
		} else if !td.expectedOk && err == nil {
			t.Errorf("access error for user %s: expected error, got ok", td.authUser)
		}
	}
}

// Verify that a user can't change someone else's password.
func TestIntegration_ChangeUserPassword_AuthFail(t *testing.T) {
	stop, _, c := startService(t)
	defer stop()

	err := c.request("/api/user/change_password", &as.ChangeUserPasswordRequest{
		PrivilegedRequestBase: as.PrivilegedRequestBase{
			UserRequestBase: as.UserRequestBase{
				RequestBase: as.RequestBase{
					SSO: c.ssoTicket("due@investici.org"),
				},
				Username: "uno@investici.org",
			},
			CurPassword: "password",
		},
		Password: "new_password",
	}, nil)

	if err == nil {
		t.Fatal("ChangePassword for another user succeeded")
	}
}

// Verify various attempts at changing the password (user has no encryption keys).
func TestIntegration_ChangeUserPassword(t *testing.T) {
	runChangeUserPasswordTest(t, "uno@investici.org", as.Config{})
}

// Verify various attempts at changing the password (user has no encryption keys).
func TestIntegration_ChangeUserPassword_WithOpportunisticEncryption(t *testing.T) {
	user := runChangeUserPasswordTest(t, "uno@investici.org", as.Config{
		EnableOpportunisticEncryption: true,
	})
	if !user.HasEncryptionKeys {
		t.Fatal("encryption keys were not created on password change")
	}
}

// Verify various attempts at changing the password (user with encryption keys).
func TestIntegration_ChangeUserPassword_WithEncryptionKeys(t *testing.T) {
	runChangeUserPasswordTest(t, "due@investici.org", as.Config{})
}

func runChangeUserPasswordTest(t *testing.T, username string, cfg as.Config) *as.RawUser {
	stop, be, c := startServiceWithConfig(t, cfg)
	defer stop()

	testdata := []struct {
		password    string
		newPassword string
		expectedOk  bool
	}{
		// Ordering is important as it is meant to emulate
		// setting the password, failing to reset it, then
		// succeeding.
		{"password", "new_password", true},
		{"BADPASS", "new_password_2", false},
		{"new_password", "password", true},
	}
	for _, td := range testdata {
		err := c.request("/api/user/change_password", &as.ChangeUserPasswordRequest{
			PrivilegedRequestBase: as.PrivilegedRequestBase{
				UserRequestBase: as.UserRequestBase{
					RequestBase: as.RequestBase{
						SSO: c.ssoTicket(username),
					},
					Username: username,
				},
				CurPassword: td.password,
			},
			Password: td.newPassword,
		}, nil)
		if err == nil && !td.expectedOk {
			t.Fatalf("ChangeUserPassword(old=%s new=%s) should have failed but didn't", td.password, td.newPassword)
		} else if err != nil && td.expectedOk {
			t.Fatalf("ChangeUserPassword(old=%s new=%s) failed: %v", td.password, td.newPassword, err)
		}
	}

	// The password that should work at the end of the above
	// series of checks is still "password".
	return checkUserInvariants(t, be, username, "password")
}

func TestIntegration_AccountRecovery(t *testing.T) {
134
	runAccountRecoveryTest(t, "uno@investici.org", false, false)
135 136 137
}

func TestIntegration_AccountRecovery_WithEncryptionKeys(t *testing.T) {
138
	user := runAccountRecoveryTest(t, "due@investici.org", false, false)
139
	if !user.HasEncryptionKeys {
140 141 142 143 144 145 146 147
		t.Fatal("encryption keys not enabled after account recovery")
	}
}

func TestIntegration_AccountRecovery_WithOpportunisticEncryption(t *testing.T) {
	user := runAccountRecoveryTest(t, "uno@investici.org", false, true)
	if !user.HasEncryptionKeys {
		t.Fatal("encryption keys not enabled after account recovery")
148 149 150
	}
}

ale's avatar
ale committed
151
func TestIntegration_AccountRecovery_WithCache(t *testing.T) {
152
	runAccountRecoveryTest(t, "uno@investici.org", true, false)
ale's avatar
ale committed
153 154 155
}

func TestIntegration_AccountRecovery_WithEncryptionKeysAndCache(t *testing.T) {
156
	user := runAccountRecoveryTest(t, "due@investici.org", true, false)
ale's avatar
ale committed
157
	if !user.HasEncryptionKeys {
158
		t.Fatal("encryption keys not enabled after account recovery")
ale's avatar
ale committed
159 160 161
	}
}

162 163 164 165 166
func runAccountRecoveryTest(t *testing.T, username string, enableCache, enableOpportunisticEncryption bool) *as.RawUser {
	cfg := as.Config{
		EnableOpportunisticEncryption: enableOpportunisticEncryption,
	}
	stop, be, c := startServiceWithConfig(t, cfg)
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	defer stop()

	hint := "secret code?"
	secondaryPw := "open sesame!"
	err := c.request("/api/user/set_account_recovery_hint", &as.SetAccountRecoveryHintRequest{
		PrivilegedRequestBase: as.PrivilegedRequestBase{
			UserRequestBase: as.UserRequestBase{
				RequestBase: as.RequestBase{
					SSO: c.ssoTicket(username),
				},
				Username: username,
			},
			CurPassword: "password",
		},
		Hint:     hint,
		Response: secondaryPw,
	}, nil)
	if err != nil {
		t.Fatalf("SetAccountRecoveryHint failed: %v", err)
	}

	// The first request just fetches the recovery hint.
	var resp as.AccountRecoveryResponse
	err = c.request("/api/recover_account", &as.AccountRecoveryRequest{
		Username: username,
	}, &resp)
	if err != nil {
		t.Fatalf("AccountRecovery (hint only) failed: %v", err)
	}
	if resp.Hint != hint {
		t.Fatalf("bad AccountRecovery hint, got '%s' expected '%s'", resp.Hint, hint)
	}

	// Now recover the account and set a new password.
	newPw := "new password"
	err = c.request("/api/recover_account", &as.AccountRecoveryRequest{
		Username:         username,
		RecoveryPassword: secondaryPw,
		Password:         newPw,
	}, &resp)
	if err != nil {
		t.Fatalf("AccountRecovery failed: %v", err)
	}

	return checkUserInvariants(t, be, username, newPw)
}
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

func TestIntegration_AppSpecificPassword(t *testing.T) {
	stop, _, c := startService(t)
	defer stop()

	username := "tre@investici.org"

	var resp as.CreateApplicationSpecificPasswordResponse
	err := c.request("/api/user/create_app_specific_password", &as.CreateApplicationSpecificPasswordRequest{
		PrivilegedRequestBase: as.PrivilegedRequestBase{
			UserRequestBase: as.UserRequestBase{
				RequestBase: as.RequestBase{
					SSO: c.ssoTicket(username),
				},
				Username: username,
			},
			CurPassword: "password",
		},
		Service: "service",
		Notes:   "notes",
	}, &resp)
	if err != nil {
		t.Fatalf("CreateApplicationSpecificPassword failed: %v", err)
	}

	var user as.User
	err = c.request("/api/user/get", &as.GetUserRequest{
		UserRequestBase: as.UserRequestBase{
			RequestBase: as.RequestBase{
				SSO: c.ssoTicket(testAdminUser, testAdminGroup),
			},
			Username: username,
		},
	}, &user)
	if err != nil {
		t.Fatalf("GetUser error: %v", err)
	}
	if len(user.AppSpecificPasswords) == 0 {
		t.Errorf("no ASPs were retrieved: %+v", user)
	}
}