Search in sources :

Example 1 with Subject

use of org.apache.shiro.subject.Subject in project zeppelin by apache.

the class LoginRestApi method logout.

@POST
@Path("logout")
@ZeppelinApi
public Response logout() {
    JsonResponse response;
    Subject currentUser = org.apache.shiro.SecurityUtils.getSubject();
    currentUser.logout();
    response = new JsonResponse(Response.Status.UNAUTHORIZED, "", "");
    LOG.warn(response.toString());
    return response.build();
}
Also used : JsonResponse(org.apache.zeppelin.server.JsonResponse) Subject(org.apache.shiro.subject.Subject) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 2 with Subject

use of org.apache.shiro.subject.Subject in project zeppelin by apache.

the class SecurityUtils method getRoles.

/**
   * Return the roles associated with the authenticated user if any otherwise returns empty set
   * TODO(prasadwagle) Find correct way to get user roles (see SHIRO-492)
   *
   * @return shiro roles
   */
public static HashSet<String> getRoles() {
    if (!isEnabled) {
        return EMPTY_HASHSET;
    }
    Subject subject = org.apache.shiro.SecurityUtils.getSubject();
    HashSet<String> roles = new HashSet<>();
    Map allRoles = null;
    if (subject.isAuthenticated()) {
        Collection realmsList = SecurityUtils.getRealmsList();
        for (Iterator<Realm> iterator = realmsList.iterator(); iterator.hasNext(); ) {
            Realm realm = iterator.next();
            String name = realm.getClass().getName();
            if (name.equals("org.apache.shiro.realm.text.IniRealm")) {
                allRoles = ((IniRealm) realm).getIni().get("roles");
                break;
            } else if (name.equals("org.apache.zeppelin.realm.LdapRealm")) {
                allRoles = ((LdapRealm) realm).getListRoles();
                break;
            }
        }
        if (allRoles != null) {
            Iterator it = allRoles.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                if (subject.hasRole((String) pair.getKey())) {
                    roles.add((String) pair.getKey());
                }
            }
        }
    }
    return roles;
}
Also used : IniRealm(org.apache.shiro.realm.text.IniRealm) Subject(org.apache.shiro.subject.Subject) Iterator(java.util.Iterator) Collection(java.util.Collection) LdapRealm(org.apache.zeppelin.realm.LdapRealm) Map(java.util.Map) IniRealm(org.apache.shiro.realm.text.IniRealm) LdapRealm(org.apache.zeppelin.realm.LdapRealm) Realm(org.apache.shiro.realm.Realm) HashSet(java.util.HashSet)

Example 3 with Subject

use of org.apache.shiro.subject.Subject in project qi4j-sdk by Qi4j.

the class PasswordDomainTest method test.

// END SNIPPET: assembly
@Test
public void test() throws UnitOfWorkCompletionException {
    UnitOfWork uow = module.newUnitOfWork();
    UserFactory userFactory = module.findService(UserFactory.class).get();
    // START SNIPPET: usage
    User user = userFactory.createNewUser("foo", "bar");
    // END SNIPPET: usage
    uow.complete();
    uow = module.newUnitOfWork();
    // START SNIPPET: usage
    Subject currentUser = SecurityUtils.getSubject();
    currentUser.login(new UsernamePasswordToken("foo", "bar"));
    // END SNIPPET: usage
    assertNotNull("Unable to authenticate against PasswordRealmService", currentUser.getPrincipal());
    assertFalse(currentUser.hasRole("role-one"));
    uow.discard();
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 4 with Subject

use of org.apache.shiro.subject.Subject in project qi4j-sdk by Qi4j.

the class PermissionsDomainTest method test.

@Test
public void test() throws UnitOfWorkCompletionException {
    // START SNIPPET: usage
    UnitOfWork uow = module.newUnitOfWork();
    User user = userFactory.createNewUser("foo", "bar");
    Role role = roleFactory.create("role-one", "permission-one", "permission-two");
    role.assignTo(user);
    uow.complete();
    // END SNIPPET: usage
    // START SNIPPET: usage
    uow = module.newUnitOfWork();
    Subject currentUser = SecurityUtils.getSubject();
    currentUser.login(new UsernamePasswordToken("foo", "bar"));
    if (!currentUser.hasRole("role-one")) {
        fail("User 'foo' must have 'role-one' role.");
    }
    if (!currentUser.isPermitted("permission-one")) {
        fail("User 'foo' must have 'permission-one' permission.");
    }
    // END SNIPPET: usage
    assertThat(currentUser.hasRole("role-one"), is(true));
    assertThat(currentUser.hasRole("role-two"), is(false));
    assertThat(currentUser.isPermitted("permission-one"), is(true));
    assertThat(currentUser.isPermitted("permission-two"), is(true));
    assertThat(currentUser.isPermitted("permission-three"), is(false));
    // START SNIPPET: usage
    uow.discard();
// END SNIPPET: usage
}
Also used : Role(org.qi4j.library.shiro.domain.permissions.Role) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 5 with Subject

use of org.apache.shiro.subject.Subject in project qi4j-sdk by Qi4j.

the class RealmServiceTest method test.

// END SNIPPET: realm-service
@Test
public void test() {
    Subject currentUser = SecurityUtils.getSubject();
    UsernamePasswordToken token = new UsernamePasswordToken("foo", "bar");
    currentUser.login(token);
    assertNotNull("Unable to authenticate against MyRealmService", currentUser.getPrincipal());
}
Also used : Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

Subject (org.apache.shiro.subject.Subject)78 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)11 Test (org.junit.Test)9 IOException (java.io.IOException)8 Map (java.util.Map)8 Path (javax.ws.rs.Path)8 StopProcessingException (ddf.catalog.plugin.StopProcessingException)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 AccountVO (com.netsteadfast.greenstep.vo.AccountVO)5 Attribute (ddf.catalog.data.Attribute)5 KeyValueCollectionPermission (ddf.security.permission.KeyValueCollectionPermission)5 GET (javax.ws.rs.GET)5 AuthenticationException (org.apache.shiro.authc.AuthenticationException)5 ServiceException (com.netsteadfast.greenstep.base.exception.ServiceException)4 Metacard (ddf.catalog.data.Metacard)4 ApiOperation (io.swagger.annotations.ApiOperation)4 POST (javax.ws.rs.POST)4 PersistenceException (org.codice.ddf.persistence.PersistenceException)4