Search in sources :

Example 31 with UsernamePasswordToken

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

the class JDBCRealmTest method testPermissionNotPresent.

@Test
public void testPermissionNotPresent() throws Exception {
    String testMethodName = name.getMethodName();
    JdbcRealm realm = realmMap.get(testMethodName);
    createDefaultSchema(testMethodName, false);
    realm.setSaltStyle(JdbcRealm.SaltStyle.NO_SALT);
    realm.setPermissionsLookupEnabled(true);
    Subject.Builder builder = new Subject.Builder(securityManager);
    Subject currentUser = builder.buildSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);
    currentUser.login(token);
    Assert.assertFalse(currentUser.isPermitted("testDomain:testTarget:specialAction"));
}
Also used : Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 32 with UsernamePasswordToken

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

the class JDBCRealmTest method testPermissionPresent.

@Test
public void testPermissionPresent() throws Exception {
    String testMethodName = name.getMethodName();
    JdbcRealm realm = realmMap.get(testMethodName);
    createDefaultSchema(testMethodName, false);
    realm.setSaltStyle(JdbcRealm.SaltStyle.NO_SALT);
    realm.setPermissionsLookupEnabled(true);
    Subject.Builder builder = new Subject.Builder(securityManager);
    Subject currentUser = builder.buildSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);
    currentUser.login(token);
    Assert.assertTrue(currentUser.isPermitted(testPermissionString));
}
Also used : Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 33 with UsernamePasswordToken

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

the class DefaultLdapRealmTest method testGetAuthenticationInfoNamingException.

@Test(expected = AuthenticationException.class)
public void testGetAuthenticationInfoNamingException() throws NamingException {
    realm.setUserDnTemplate("uid={0},ou=users,dc=mycompany,dc=com");
    LdapContextFactory factory = createMock(LdapContextFactory.class);
    realm.setContextFactory(factory);
    expect(factory.getLdapContext(isA(Object.class), isA(Object.class))).andThrow(new NamingException("Communication error."));
    replay(factory);
    realm.getAuthenticationInfo(new UsernamePasswordToken("jsmith", "secret"));
}
Also used : NamingException(javax.naming.NamingException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) Test(org.junit.Test)

Example 34 with UsernamePasswordToken

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

the class DefaultLdapRealmTest method testUserDnTemplateSubstitution.

@Test
public void testUserDnTemplateSubstitution() throws NamingException {
    realm.setUserDnTemplate("uid={0},ou=users,dc=mycompany,dc=com");
    LdapContextFactory factory = createMock(LdapContextFactory.class);
    realm.setContextFactory(factory);
    Object expectedPrincipal = "uid=jsmith,ou=users,dc=mycompany,dc=com";
    expect(factory.getLdapContext(eq(expectedPrincipal), isA(Object.class))).andReturn(createNiceMock(LdapContext.class));
    replay(factory);
    realm.getAuthenticationInfo(new UsernamePasswordToken("jsmith", "secret"));
    verify(factory);
}
Also used : LdapContext(javax.naming.ldap.LdapContext) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) Test(org.junit.Test)

Example 35 with UsernamePasswordToken

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

the class IniRealmTest method testIniFile.

@Test
public void testIniFile() {
    IniRealm realm = new IniRealm();
    realm.setResourcePath("classpath:org/apache/shiro/realm/text/IniRealmTest.simple.ini");
    realm.init();
    assertTrue(realm.roleExists("admin"));
    UsernamePasswordToken token = new UsernamePasswordToken("user1", "user1");
    AuthenticationInfo info = realm.getAuthenticationInfo(token);
    assertNotNull(info);
    assertTrue(realm.hasRole(info.getPrincipals(), "admin"));
}
Also used : AuthenticationInfo(org.apache.shiro.authc.AuthenticationInfo) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) Test(org.junit.Test)

Aggregations

UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)114 Subject (org.apache.shiro.subject.Subject)50 Test (org.junit.Test)30 AuthenticationException (org.apache.shiro.authc.AuthenticationException)28 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)27 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)17 AuthenticationInfo (org.apache.shiro.authc.AuthenticationInfo)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Test (org.testng.annotations.Test)11 LockedAccountException (org.apache.shiro.authc.LockedAccountException)10 IncorrectCredentialsException (org.apache.shiro.authc.IncorrectCredentialsException)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 UnknownAccountException (org.apache.shiro.authc.UnknownAccountException)7 DelegatingSubject (org.apache.shiro.subject.support.DelegatingSubject)7 Session (org.apache.shiro.session.Session)6 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4