Search in sources :

Example 6 with SimpleAccount

use of org.apache.shiro.authc.SimpleAccount in project ART-TIME by Artezio.

the class ActiveDirectoryRealmTest method testDoGetAuthorizationInfo_ifPrincipalsCollectionNotEmpty.

@Test
public void testDoGetAuthorizationInfo_ifPrincipalsCollectionNotEmpty() throws Exception {
    activeDirectoryRealm = PowerMock.createPartialMock(ActiveDirectoryRealm.class, "queryForAuthorizationInfo");
    setField(activeDirectoryRealm, "projectRepository", projectRepository);
    List<Project> projects = new ArrayList<Project>();
    PrincipalCollection principals = new SimplePrincipalCollection("principal", "ldapRealm");
    AuthorizationInfo info = new SimpleAccount(principals, "credential", new HashSet<String>());
    PowerMock.expectPrivate(activeDirectoryRealm, "queryForAuthorizationInfo", anyObject(PrincipalCollection.class), anyObject(LdapContextFactory.class)).andReturn(info);
    expect(projectRepository.getProjectsByManager("principal")).andReturn(projects);
    PowerMock.replayAll(projectRepository, activeDirectoryRealm);
    AuthorizationInfo actual = activeDirectoryRealm.doGetAuthorizationInfo(principals);
    PowerMock.verifyAll();
    assertTrue(actual.getRoles().contains(UserRoles.EMPLOYEE_ROLE));
}
Also used : SimpleAccount(org.apache.shiro.authc.SimpleAccount) Project(com.artezio.arttime.datamodel.Project) ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) AuthorizationInfo(org.apache.shiro.authz.AuthorizationInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with SimpleAccount

use of org.apache.shiro.authc.SimpleAccount in project ART-TIME by Artezio.

the class ActiveDirectoryRealmTest method testDoGetAuthorizationInfo_ifExistManagedProjects.

@Test
public void testDoGetAuthorizationInfo_ifExistManagedProjects() throws Exception {
    activeDirectoryRealm = PowerMock.createPartialMock(ActiveDirectoryRealm.class, "queryForAuthorizationInfo");
    setField(activeDirectoryRealm, "projectRepository", projectRepository);
    List<Project> projects = Arrays.asList(new Project());
    PrincipalCollection principals = new SimplePrincipalCollection("principal", "ldapRealm");
    AuthorizationInfo info = new SimpleAccount(principals, "credential", new HashSet<String>());
    PowerMock.expectPrivate(activeDirectoryRealm, "queryForAuthorizationInfo", anyObject(PrincipalCollection.class), anyObject(LdapContextFactory.class)).andReturn(info);
    expect(projectRepository.getProjectsByManager("principal")).andReturn(projects);
    PowerMock.replayAll(projectRepository, activeDirectoryRealm);
    AuthorizationInfo actual = activeDirectoryRealm.doGetAuthorizationInfo(principals);
    PowerMock.verifyAll();
    assertTrue(actual.getRoles().contains(UserRoles.EMPLOYEE_ROLE));
    assertTrue(actual.getRoles().contains(UserRoles.PM_ROLE));
}
Also used : SimpleAccount(org.apache.shiro.authc.SimpleAccount) Project(com.artezio.arttime.datamodel.Project) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) AuthorizationInfo(org.apache.shiro.authz.AuthorizationInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with SimpleAccount

use of org.apache.shiro.authc.SimpleAccount in project ART-TIME by Artezio.

the class AdminRealmTest method testGetAccount_ifLoggedUserNotAdmin.

@Test
public void testGetAccount_ifLoggedUserNotAdmin() {
    Settings settings = new Settings(new EnumMap<>(Setting.Name.class));
    settings.setAdminUsername("admin");
    SimpleAccount actual = adminRealm.getAccount("user", settings);
    assertNull(actual);
}
Also used : SimpleAccount(org.apache.shiro.authc.SimpleAccount) Settings(com.artezio.arttime.config.Settings) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with SimpleAccount

use of org.apache.shiro.authc.SimpleAccount in project shiro by apache.

the class SimpleAccountRealm method addAccount.

public void addAccount(String username, String password, String... roles) {
    Set<String> roleNames = CollectionUtils.asSet(roles);
    SimpleAccount account = new SimpleAccount(username, password, getName(), roleNames, null);
    add(account);
}
Also used : SimpleAccount(org.apache.shiro.authc.SimpleAccount)

Example 10 with SimpleAccount

use of org.apache.shiro.authc.SimpleAccount in project shiro by apache.

the class SimpleAccountRealm method doGetAuthenticationInfo.

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    SimpleAccount account = getUser(upToken.getUsername());
    if (account != null) {
        if (account.isLocked()) {
            throw new LockedAccountException("Account [" + account + "] is locked.");
        }
        if (account.isCredentialsExpired()) {
            String msg = "The credentials for account [" + account + "] are expired";
            throw new ExpiredCredentialsException(msg);
        }
    }
    return account;
}
Also used : SimpleAccount(org.apache.shiro.authc.SimpleAccount) LockedAccountException(org.apache.shiro.authc.LockedAccountException) ExpiredCredentialsException(org.apache.shiro.authc.ExpiredCredentialsException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Aggregations

SimpleAccount (org.apache.shiro.authc.SimpleAccount)18 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)5 Settings (com.artezio.arttime.config.Settings)4 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)3 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)3 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)3 User (org.graylog2.plugin.database.users.User)3 Project (com.artezio.arttime.datamodel.Project)2 AuthenticationInfo (org.apache.shiro.authc.AuthenticationInfo)2 ValidationException (org.graylog2.plugin.database.ValidationException)2 User (io.nutz.demo.simple.bean.User)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)1 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)1 LdapConnectionConfig (org.apache.directory.ldap.client.api.LdapConnectionConfig)1 LdapNetworkConnection (org.apache.directory.ldap.client.api.LdapNetworkConnection)1 AuthenticationException (org.apache.shiro.authc.AuthenticationException)1