Package org.jboss.seam.security

Examples of org.jboss.seam.security.Identity


  
   public boolean hasPermission(Object target, String action)
   {     
      if (permissionStore == null) return false;
     
      Identity identity = Identity.instance();
     
      if (!identity.isLoggedIn()) return false;     
     
      List<Permission> permissions = permissionStore.listPermissions(target, action);
     
      String username = identity.getPrincipal() != null ? identity.getPrincipal().getName() : null;
     
      for (Permission permission : permissions)
      {
         if (username != null && permission.getRecipient() instanceof SimplePrincipal &&
               username.equals(permission.getRecipient().getName()))
         {
            return true;
         }
        
         if (permission.getRecipient() instanceof Role)
         {
            Role role = (Role) permission.getRecipient();
           
            if (role.isConditional())
            {
               RuleBasedPermissionResolver resolver = RuleBasedPermissionResolver.instance();
               if (resolver.checkConditionalRole(role.getName(), target, action)) return true;              
            }
            else if (identity.hasRole(role.getName()))
            {
               return true;
            }
         }
      }     
View Full Code Here


  
   public void filterSetByAction(Set<Object> targets, String action)
   {
      if (permissionStore == null) return;
     
      Identity identity = Identity.instance();
      if (!identity.isLoggedIn()) return;
     
      List<Permission> permissions = permissionStore.listPermissions(targets, action);
     
      String username = identity.getPrincipal().getName();
     
      Iterator iter = targets.iterator();
      while (iter.hasNext())
      {
         Object target = iter.next();
        
         for (Permission permission : permissions)
         {
            if (permission.getTarget().equals(target))
            {
               if (permission.getRecipient() instanceof SimplePrincipal &&
                     username.equals(permission.getRecipient().getName()))
               {
                  iter.remove();
                  break;
               }
              
               if (permission.getRecipient() instanceof Role)
               {
                  Role role = (Role) permission.getRecipient();
                 
                  if (role.isConditional())
                  {
                     RuleBasedPermissionResolver resolver = RuleBasedPermissionResolver.instance();
                     if (resolver.checkConditionalRole(role.getName(), target, action))
                     {
                        iter.remove();
                        break;
                     }
                  }
                  else if (identity.hasRole(role.getName()))
                  {
                     iter.remove();
                     break;
                  }
               }              
View Full Code Here

     
      // Otherwise if identity management is enabled, use it.
      IdentityManager identityManager = IdentityManager.instance();
      if (identityManager != null && identityManager.isEnabled())
      {
         Identity identity = Identity.instance();
        
         try
         {
            boolean success = identityManager.authenticate(username, identity.getCredentials().getPassword());
           
            if (success)
            {
               for (String role : identityManager.getImpliedRoles(username))
               {
                  identity.addRole(role);
               }
            }
           
            return success;
         }
View Full Code Here

  
   public boolean hasPermission(Object target, String action)
   {     
      if (permissionStore == null) return false;
     
      Identity identity = Identity.instance();
     
      if (!identity.isLoggedIn()) return false;     
     
      List<Permission> permissions = permissionStore.listPermissions(target, action);
     
      String username = identity.getPrincipal().getName();
     
      for (Permission permission : permissions)
      {
         if (permission.getRecipient() instanceof SimplePrincipal &&
               username.equals(permission.getRecipient().getName()))
         {
            return true;
         }
        
         if (permission.getRecipient() instanceof Role)
         {
            Role role = (Role) permission.getRecipient();
           
            if (role.isConditional())
            {
               RuleBasedPermissionResolver resolver = RuleBasedPermissionResolver.instance();
               if (resolver.checkConditionalRole(role.getName(), target, action)) return true;              
            }
            else if (identity.hasRole(role.getName()))
            {
               return true;
            }
         }
      }     
View Full Code Here

  
   public void filterSetByAction(Set<Object> targets, String action)
   {
      if (permissionStore == null) return;
     
      Identity identity = Identity.instance();
      if (!identity.isLoggedIn()) return;
     
      List<Permission> permissions = permissionStore.listPermissions(targets, action);
     
      String username = identity.getPrincipal().getName();
     
      Iterator iter = targets.iterator();
      while (iter.hasNext())
      {
         Object target = iter.next();
        
         for (Permission permission : permissions)
         {
            if (permission.getTarget().equals(target))
            {
               if (permission.getRecipient() instanceof SimplePrincipal &&
                     username.equals(permission.getRecipient().getName()))
               {
                  iter.remove();
                  break;
               }
              
               if (permission.getRecipient() instanceof Role)
               {
                  Role role = (Role) permission.getRecipient();
                 
                  if (role.isConditional())
                  {
                     RuleBasedPermissionResolver resolver = RuleBasedPermissionResolver.instance();
                     if (resolver.checkConditionalRole(role.getName(), target, action))
                     {
                        iter.remove();
                        break;
                     }
                  }
                  else if (identity.hasRole(role.getName()))
                  {
                     iter.remove();
                     break;
                  }
               }              
View Full Code Here

  
   private void processBasicAuth(HttpServletRequest request,
            HttpServletResponse response, FilterChain chain)
      throws IOException, ServletException
   {
      Identity identity = Identity.instance();

      if (identity == null)
      {
         throw new ServletException("Identity not found - please ensure that the Identity component is created on startup.");
      }
     
      Credentials credentials = identity.getCredentials();
     
      boolean requireAuth = false;
     
      String header = request.getHeader("Authorization");
      if (header != null && header.startsWith("Basic "))
      {
         String base64Token = header.substring(6);
         String token = new String(Base64.decode(base64Token));

         String username = "";
         String password = "";
         int delim = token.indexOf(":");

         if (delim != -1)
         {
             username = token.substring(0, delim);
             password = token.substring(delim + 1);
         }

         // Only reauthenticate if username doesn't match Identity.username and user isn't authenticated
         if (!username.equals(credentials.getUsername()) || !identity.isLoggedIn())
         {
            try
            {
               credentials.setPassword(password);
               authenticate( request, username );
            }        
            catch (Exception ex)
            {
               log.warn("Error authenticating: " + ex.getMessage());
               requireAuth = true;
           
         }
      }
     
      if (!identity.isLoggedIn() && !credentials.isSet())
      {
         requireAuth = true;
      }
     
      try
      {
         if (!requireAuth)
         {
            chain.doFilter(request, response);
            return;
         }
      }
      catch (NotLoggedInException ex)
      {
         requireAuth = true;
      }
     
      if ((requireAuth && !identity.isLoggedIn()))
      {
         response.addHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
         response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Not authorized");        
      }              
   }
View Full Code Here

   private void processDigestAuth(HttpServletRequest request,
            HttpServletResponse response, FilterChain chain)
      throws IOException, ServletException
   {
      Identity identity = Identity.instance();
     
      if (identity == null)
      {
         throw new ServletException("Identity not found - please ensure that the Identity component is created on startup.");
      }     
     
      Credentials credentials = identity.getCredentials();
     
      boolean requireAuth = false;   
      boolean nonceExpired = false;
     
      String header = request.getHeader("Authorization");     
      if (header != null && header.startsWith("Digest "))
      {       
         String section212response = header.substring(7);

         String[] headerEntries = section212response.split(",");
         Map<String,String> headerMap = new HashMap<String,String>();
         for (String entry : headerEntries)
         {
            String[] vals = split(entry, "=");
            headerMap.put(vals[0].trim(), vals[1].replace("\"", "").trim());
         }
        

         DigestRequest digestRequest = new DigestRequest();
         digestRequest.setHttpMethod(request.getMethod());
         digestRequest.setSystemRealm(realm);
         digestRequest.setRealm(headerMap.get("realm"));        
         digestRequest.setKey(key);
         digestRequest.setNonce(headerMap.get("nonce"));
         digestRequest.setUri(headerMap.get("uri"));
         digestRequest.setClientDigest(headerMap.get("response"));
         digestRequest.setQop(headerMap.get("qop"));
         digestRequest.setNonceCount(headerMap.get("nc"));
         digestRequest.setClientNonce(headerMap.get("cnonce"));
                 
         try
         {
            digestRequest.validate();
            request.getSession().setAttribute(DigestRequest.DIGEST_REQUEST, digestRequest);
            authenticate( request, headerMap.get("username") );
         }
         catch (DigestValidationException ex)
         {
            log.warn(String.format("Digest validation failed, header [%s]: %s",
                     section212response, ex.getMessage()));
            requireAuth = true;
           
            if (ex.isNonceExpired()) nonceExpired = true;
         }           
         catch (Exception ex)
         {
            log.warn("Error authenticating: " + ex.getMessage());
            requireAuth = true;
         }
      }  

      if (!identity.isLoggedIn() && !credentials.isSet())
      {
         requireAuth = true;
      }
     
      try
      {
         if (!requireAuth)
         {
            chain.doFilter(request, response);
            return;
         }
      }
      catch (NotLoggedInException ex)
      {
         requireAuth = true;
      }
     
      if ((requireAuth && !identity.isLoggedIn()))
      {     
         long expiryTime = System.currentTimeMillis() + (nonceValiditySeconds * 1000);
        
         String signatureValue = DigestUtils.md5Hex(expiryTime + ":" + key);
         String nonceValue = expiryTime + ":" + signatureValue;
View Full Code Here

      }            
   }
  
   private void authenticate(HttpServletRequest request, final String username) throws ServletException, IOException, LoginException
   {
      Identity identity = Identity.instance();
      identity.getCredentials().setUsername(username);
      identity.authenticate();
   }
View Full Code Here

        String[] a = unpack(auth);
        String usr = a[0];
        String pwd = a[1];
        if ( Contexts.isApplicationContextActive() ) {
           // return (FileManagerUtils) Component.getInstance( "fileManager" );
            Identity ids = Identity.instance();
            ids.getCredentials().setUsername(usr);
            ids.getCredentials().setPassword(pwd);
            try {
                ids.authenticate();
                log.info(usr + " authenticated for rest api");
              
                return true;
            } catch (LoginException e) {
                log.warn("Unable to authenticate for rest api: " + usr);
View Full Code Here

     * Autologin means that its not really logged in, but a generic username will be used.
     * Basically means security is bypassed.
     *
     */
    private String checkAutoLogin() {
        Identity id = Identity.instance();
        id.getCredentials().setUsername( GUEST_LOGIN );
        try {
            id.authenticate();
        } catch ( LoginException e ) {
            return null;
        }
        if ( id.isLoggedIn() ) {
            return id.getCredentials().getUsername();
        } else {
            return null;
        }

    }
View Full Code Here

TOP

Related Classes of org.jboss.seam.security.Identity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.