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;
}
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) {
}
}
}
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;
}
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;
}
Aggregations