Search in sources :

Example 31 with AuthRequest

use of com.google.gerrit.server.account.AuthRequest in project gerrit by GerritCodeReview.

the class AccountManagerIT method authenticateNewAccountWithEmail.

@Test
public void authenticateNewAccountWithEmail() throws Exception {
    String email = "foo@example.com";
    ExternalId.Key mailtoExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_MAILTO, email);
    assertNoSuchExternalIds(mailtoExtIdKey);
    AuthRequest who = authRequestFactory.createForEmail(email);
    AuthResult authResult = accountManager.authenticate(who);
    assertAuthResultForNewAccount(authResult, mailtoExtIdKey);
    assertExternalId(mailtoExtIdKey, email);
}
Also used : AuthRequest(com.google.gerrit.server.account.AuthRequest) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AuthResult(com.google.gerrit.server.account.AuthResult) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 32 with AuthRequest

use of com.google.gerrit.server.account.AuthRequest in project gerrit by GerritCodeReview.

the class AccountManagerIT method authenticateNewAccountWithUsernameAndEmail.

@Test
public void authenticateNewAccountWithUsernameAndEmail() throws Exception {
    String username = "foo";
    ExternalId.Key gerritExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_GERRIT, username);
    ExternalId.Key usernameExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_USERNAME, username);
    assertNoSuchExternalIds(gerritExtIdKey, usernameExtIdKey);
    AuthRequest who = authRequestFactory.createForUser(username);
    String email = "foo@example.com";
    who.setEmailAddress(email);
    AuthResult authResult = accountManager.authenticate(who);
    assertAuthResultForNewAccount(authResult, gerritExtIdKey);
    assertExternalId(gerritExtIdKey, email);
    assertExternalIdsWithoutEmail(usernameExtIdKey);
}
Also used : AuthRequest(com.google.gerrit.server.account.AuthRequest) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AuthResult(com.google.gerrit.server.account.AuthResult) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 33 with AuthRequest

use of com.google.gerrit.server.account.AuthRequest in project gerrit by GerritCodeReview.

the class AccountManagerIT method cannotAuthenticateNewAccountWithUsernameAndEmailThatIsAlreadyUsed.

@Test
public void cannotAuthenticateNewAccountWithUsernameAndEmailThatIsAlreadyUsed() throws Exception {
    String email = "foo@example.com";
    // Create an account with an SCHEME_EXTERNAL external ID that occupies the email.
    String username = "foo";
    Account.Id accountId = Account.id(seq.nextAccountId());
    ExternalId.Key externalExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_EXTERNAL, username);
    accountsUpdate.insert("Create Test Account", accountId, u -> u.addExternalId(externalIdFactory.createWithEmail(externalExtIdKey, accountId, email)));
    // Try to authenticate with a new username and claim the same email.
    // Expect that this fails because the email is already assigned to the other account.
    AuthRequest who = authRequestFactory.createForUser("bar");
    who.setEmailAddress(email);
    AccountException thrown = assertThrows(AccountException.class, () -> accountManager.authenticate(who));
    assertThat(thrown).hasMessageThat().contains("Email 'foo@example.com' in use by another account");
}
Also used : Account(com.google.gerrit.entities.Account) AuthRequest(com.google.gerrit.server.account.AuthRequest) AccountException(com.google.gerrit.server.account.AccountException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 34 with AuthRequest

use of com.google.gerrit.server.account.AuthRequest in project gerrit by GerritCodeReview.

the class AccountManagerIT method deactivateAccountOnAuthenticationWhenAutoUpdateAccountActiveStatusIsEnabled.

@Test
@GerritConfig(name = "auth.autoUpdateAccountActiveStatus", value = "true")
public void deactivateAccountOnAuthenticationWhenAutoUpdateAccountActiveStatusIsEnabled() throws Exception {
    String username = "foo";
    Account.Id accountId = Account.id(seq.nextAccountId());
    ExternalId.Key gerritExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_GERRIT, username);
    accountsUpdate.insert("Create Test Account", accountId, u -> u.addExternalId(externalIdFactory.create(gerritExtIdKey, accountId)));
    AuthRequest who = authRequestFactory.createForUser(username);
    who.setActive(false);
    who.setAuthProvidesAccountActiveStatus(true);
    AccountException thrown = assertThrows(AccountException.class, () -> accountManager.authenticate(who));
    assertThat(thrown).hasMessageThat().isEqualTo("Authentication error, account inactive");
    Optional<AccountState> accountState = accounts.get(accountId);
    assertThat(accountState).isPresent();
    assertThat(accountState.get().account().isActive()).isFalse();
}
Also used : Account(com.google.gerrit.entities.Account) AuthRequest(com.google.gerrit.server.account.AuthRequest) AccountException(com.google.gerrit.server.account.AccountException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AccountState(com.google.gerrit.server.account.AccountState) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 35 with AuthRequest

use of com.google.gerrit.server.account.AuthRequest in project gerrit by GerritCodeReview.

the class AccountManagerIT method linkNewExternalId.

@Test
public void linkNewExternalId() throws Exception {
    // Create an account with a SCHEME_GERRIT external ID and no email
    String username = "foo";
    Account.Id accountId = Account.id(seq.nextAccountId());
    ExternalId.Key gerritExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_GERRIT, username);
    accountsUpdate.insert("Create Test Account", accountId, u -> u.addExternalId(externalIdFactory.create(gerritExtIdKey, accountId)));
    // Check that email is not used yet.
    String email = "foo@example.com";
    ExternalId.Key mailtoExtIdKey = externalIdKeyFactory.create(ExternalId.SCHEME_MAILTO, email);
    assertNoSuchExternalIds(mailtoExtIdKey);
    // Link the email to the account.
    // Expect that a MAILTO external ID is created.
    AuthRequest who = authRequestFactory.createForEmail(email);
    AuthResult authResult = accountManager.link(accountId, who);
    assertAuthResultForExistingAccount(authResult, accountId, mailtoExtIdKey);
    assertExternalId(mailtoExtIdKey, accountId, email);
}
Also used : Account(com.google.gerrit.entities.Account) AuthRequest(com.google.gerrit.server.account.AuthRequest) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AuthResult(com.google.gerrit.server.account.AuthResult) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

AuthRequest (com.google.gerrit.server.account.AuthRequest)36 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)25 Test (org.junit.Test)25 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)24 Account (com.google.gerrit.entities.Account)23 AuthResult (com.google.gerrit.server.account.AuthResult)22 AccountException (com.google.gerrit.server.account.AccountException)18 AccountState (com.google.gerrit.server.account.AccountState)10 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)2 AuthenticationFailedException (com.google.gerrit.server.account.AuthenticationFailedException)2 AuthenticationUnavailableException (com.google.gerrit.server.auth.AuthenticationUnavailableException)2 Change (com.google.gerrit.entities.Change)1 GitBasicAuthPolicy (com.google.gerrit.extensions.client.GitBasicAuthPolicy)1 UnresolvableAccountException (com.google.gerrit.server.account.AccountResolver.UnresolvableAccountException)1 AccountUserNameException (com.google.gerrit.server.account.AccountUserNameException)1 ExternalIdNotes (com.google.gerrit.server.account.externalids.ExternalIdNotes)1 NoSuchUserException (com.google.gerrit.server.auth.NoSuchUserException)1 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)1 ManualRequestContext (com.google.gerrit.server.util.ManualRequestContext)1 Repo (com.google.gerrit.testing.InMemoryRepositoryManager.Repo)1