Search in sources :

Example 1 with KapuaRuntimeException

use of org.eclipse.kapua.KapuaRuntimeException in project kapua by eclipse.

the class KapuaAuthenticatingRealm method doGetAuthenticationInfo.

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    // 
    // Extract credentials
    UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
    String tokenUsername = token.getUsername();
    // char[] tokenPassword = token.getPassword();
    // 
    // Get Services
    KapuaLocator locator;
    UserService userService;
    AccountService accountService;
    CredentialService credentialService;
    try {
        locator = KapuaLocator.getInstance();
        userService = locator.getService(UserService.class);
        accountService = locator.getService(AccountService.class);
        credentialService = locator.getService(CredentialService.class);
    } catch (KapuaRuntimeException kre) {
        throw new ShiroException("Error while getting services!", kre);
    }
    // 
    // Get the associated user by name
    final User user;
    try {
        user = KapuaSecurityUtils.doPriviledge(new Callable<User>() {

            @Override
            public User call() throws Exception {
                return userService.findByName(tokenUsername);
            }
        });
    } catch (Exception e) {
        // to preserve the original exception message (if possible)
        if (e instanceof AuthenticationException) {
            throw (AuthenticationException) e;
        } else {
            throw new ShiroException("Error while find user!", e);
        }
    }
    // Check existence
    if (user == null) {
        throw new UnknownAccountException();
    }
    // Check disabled
    if (UserStatus.DISABLED.equals(user.getStatus())) {
        throw new DisabledAccountException();
    }
    // 
    // Find account
    final Account account;
    try {
        account = KapuaSecurityUtils.doPriviledge(new Callable<Account>() {

            @Override
            public Account call() throws Exception {
                return accountService.find(user.getScopeId());
            }
        });
    } catch (Exception e) {
        // to preserve the original exception message (if possible)
        if (e instanceof AuthenticationException) {
            throw (AuthenticationException) e;
        } else {
            throw new ShiroException("Error while find account!", e);
        }
    }
    // Check existence
    if (account == null) {
        throw new UnknownAccountException();
    }
    // 
    // Find credentials
    // FIXME: manage multiple credentials and multiple credentials type
    Credential credential = null;
    try {
        credential = KapuaSecurityUtils.doPriviledge(new Callable<Credential>() {

            @Override
            public Credential call() throws Exception {
                CredentialListResult credentialList = credentialService.findByUserId(user.getScopeId(), user.getId());
                // TODO may be better to filter by credential type?
                if (credentialList != null && !credentialList.isEmpty()) {
                    return credentialList.getItem(0);
                } else {
                    throw new UnknownAccountException();
                }
            }
        });
    } catch (Exception e) {
        if (e instanceof AuthenticationException) {
            throw (AuthenticationException) e;
        } else {
            throw new ShiroException("Error while find credentials!", e);
        }
    }
    // 
    // BuildAuthenticationInfo8
    KapuaSimpleAuthenticationInfo info = new KapuaSimpleAuthenticationInfo(user, credential, account, getName());
    return info;
}
Also used : DisabledAccountException(org.apache.shiro.authc.DisabledAccountException) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) Credential(org.eclipse.kapua.service.authentication.credential.Credential) User(org.eclipse.kapua.service.user.User) UserService(org.eclipse.kapua.service.user.UserService) AuthenticationException(org.apache.shiro.authc.AuthenticationException) KapuaRuntimeException(org.eclipse.kapua.KapuaRuntimeException) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) Callable(java.util.concurrent.Callable) ShiroException(org.apache.shiro.ShiroException) DisabledAccountException(org.apache.shiro.authc.DisabledAccountException) KapuaRuntimeException(org.eclipse.kapua.KapuaRuntimeException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) KapuaException(org.eclipse.kapua.KapuaException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) ShiroException(org.apache.shiro.ShiroException) KapuaSimpleAuthenticationInfo(org.eclipse.kapua.service.authentication.shiro.credential.KapuaSimpleAuthenticationInfo) CredentialService(org.eclipse.kapua.service.authentication.credential.CredentialService) CredentialListResult(org.eclipse.kapua.service.authentication.credential.CredentialListResult) AccountService(org.eclipse.kapua.service.account.AccountService)

Example 2 with KapuaRuntimeException

use of org.eclipse.kapua.KapuaRuntimeException in project kapua by eclipse.

the class KapuaModule method configure.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void configure() {
    BufferedReader br = null;
    try {
        List<URL> servicesDefinitions = Arrays.asList(ResourceUtils.getResource(SERVICE_RESOURCE), ResourceUtils.getResource(SERVICE_TEST_RESOURCE));
        for (URL servicesUrl : servicesDefinitions) {
            if (servicesUrl != null) {
                String services = ResourceUtils.readResource(servicesUrl);
                br = new BufferedReader(new StringReader(services));
                String trimmedServiceLine = null;
                for (String serviceName = br.readLine(); serviceName != null; serviceName = br.readLine()) {
                    trimmedServiceLine = serviceName.trim();
                    if (trimmedServiceLine.length() == 0 || trimmedServiceLine.startsWith(COMMENT_PREFIX)) {
                        continue;
                    }
                    try {
                        Class<?> kapuaObject = Class.forName(trimmedServiceLine);
                        if (KapuaService.class.isAssignableFrom(kapuaObject)) {
                            bind(kapuaObject).toProvider(new KapuaServiceLoaderProvider(kapuaObject));
                            logger.info("Bound Kapua service {}", trimmedServiceLine);
                        } else if (KapuaObjectFactory.class.isAssignableFrom(kapuaObject)) {
                            bind(kapuaObject).toProvider(new KapuaFactoryLoaderProvider(kapuaObject));
                            logger.info("Bound Kapua factory {}", trimmedServiceLine);
                        }
                    } catch (Exception e) {
                        logger.error("Cannot load Kapua service/factory " + trimmedServiceLine, e);
                    } catch (Throwable e) {
                        logger.error("Cannot load Kapua service/factory " + trimmedServiceLine, e);
                        throw e;
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error("Exeption configuring module: {}", e.getMessage(), e);
        throw new KapuaRuntimeException(KapuaErrorCodes.INTERNAL_ERROR, "Cannot load " + SERVICE_RESOURCE, e);
    } finally {
        if (br != null)
            try {
                br.close();
            } catch (Exception e) {
            }
    }
}
Also used : KapuaRuntimeException(org.eclipse.kapua.KapuaRuntimeException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) URL(java.net.URL) KapuaObjectFactory(org.eclipse.kapua.model.KapuaObjectFactory) KapuaRuntimeException(org.eclipse.kapua.KapuaRuntimeException)

Example 3 with KapuaRuntimeException

use of org.eclipse.kapua.KapuaRuntimeException in project kapua by eclipse.

the class AuthenticationUtils method cryptCredential.

/**
 * Encrypts and return the plain credential value (unencrypted value).
 *
 * @param plainValue
 * @return
 * @throws KapuaException
 */
public static String cryptCredential(String plainValue) throws KapuaException {
    // 
    // Argument validator
    ArgumentValidator.notEmptyOrNull(plainValue, "plainValue");
    // 
    // Do crypt
    String cryptedValue = null;
    try {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        String salt = BCrypt.gensalt(12, random);
        cryptedValue = BCrypt.hashpw(plainValue, salt);
    } catch (NoSuchAlgorithmException e) {
        throw new KapuaRuntimeException(KapuaAuthenticationErrorCodes.CREDENTIAL_CRYPT_ERROR, e, (Object[]) null);
    }
    return cryptedValue;
}
Also used : KapuaRuntimeException(org.eclipse.kapua.KapuaRuntimeException) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 4 with KapuaRuntimeException

use of org.eclipse.kapua.KapuaRuntimeException in project kapua by eclipse.

the class IdGeneratorServiceImpl method generate.

@Override
public KapuaId generate() throws KapuaException {
    KapuaEid id = null;
    EntityManager em = null;
    try {
        em = CommonsEntityManagerFactory.getEntityManager();
        Query q = em.createNativeQuery(QUERY_SELECT_UUID_SHORT);
        BigInteger bi = (BigInteger) q.getSingleResult();
        id = new KapuaEid(bi);
    } catch (Exception pe) {
        throw new KapuaRuntimeException(KapuaCommonsErrorCodes.ID_GENERATION_ERROR, pe);
    } finally {
        if (em != null) {
            em.close();
        }
    }
    return id;
}
Also used : EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) Query(javax.persistence.Query) KapuaRuntimeException(org.eclipse.kapua.KapuaRuntimeException) BigInteger(java.math.BigInteger) KapuaEid(org.eclipse.kapua.commons.model.id.KapuaEid) KapuaRuntimeException(org.eclipse.kapua.KapuaRuntimeException) KapuaException(org.eclipse.kapua.KapuaException)

Aggregations

KapuaRuntimeException (org.eclipse.kapua.KapuaRuntimeException)4 KapuaException (org.eclipse.kapua.KapuaException)2 BufferedReader (java.io.BufferedReader)1 StringReader (java.io.StringReader)1 BigInteger (java.math.BigInteger)1 URL (java.net.URL)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SecureRandom (java.security.SecureRandom)1 Callable (java.util.concurrent.Callable)1 Query (javax.persistence.Query)1 ShiroException (org.apache.shiro.ShiroException)1 AuthenticationException (org.apache.shiro.authc.AuthenticationException)1 DisabledAccountException (org.apache.shiro.authc.DisabledAccountException)1 UnknownAccountException (org.apache.shiro.authc.UnknownAccountException)1 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)1 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)1 KapuaEid (org.eclipse.kapua.commons.model.id.KapuaEid)1 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)1 KapuaObjectFactory (org.eclipse.kapua.model.KapuaObjectFactory)1 Account (org.eclipse.kapua.service.account.Account)1