Search in sources :

Example 1 with KapuaEid

use of org.eclipse.kapua.commons.model.id.KapuaEid in project kapua by eclipse.

the class AccountServiceTest method testCreate.

@Test
public void testCreate() throws Exception {
    // KapuaPeid accountPeid = KapuaEidGenerator.generate();//
    KapuaId scopeId = new KapuaEid(BigInteger.valueOf(1));
    KapuaLocator locator = KapuaLocator.getInstance();
    long accountSerial = (new Date()).getTime();
    AccountCreator accountCreator = this.getTestAccountCreator(scopeId, accountSerial);
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.create(accountCreator);
    // 
    // Account asserts
    assertNotNull(account);
    assertNotNull(account.getId());
    assertNotNull(account.getId().getId());
    assertTrue(account.getOptlock() >= 0);
    assertTrue(account.getScopeId().equals(scopeId));
    assertTrue(account.getName().equals(accountCreator.getOrganizationName()));
    assertNotNull(account.getOrganization());
    assertTrue(account.getOrganization().getEmail().equals(accountCreator.getOrganizationEmail()));
    assertNotNull(account.getCreatedOn());
    assertNotNull(account.getCreatedBy());
    assertNotNull(account.getModifiedOn());
    assertNotNull(account.getModifiedBy());
}
Also used : AccountCreator(org.eclipse.kapua.service.account.AccountCreator) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaEid(org.eclipse.kapua.commons.model.id.KapuaEid) AccountService(org.eclipse.kapua.service.account.AccountService) Date(java.util.Date) Test(org.junit.Test)

Example 2 with KapuaEid

use of org.eclipse.kapua.commons.model.id.KapuaEid in project kapua by eclipse.

the class AccountServiceTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    // KapuaPeid accountPeid = KapuaEidGenerator.generate();//
    KapuaId scopeId = new KapuaEid(BigInteger.valueOf(1));
    KapuaLocator locator = KapuaLocator.getInstance();
    long accountSerial = (new Date()).getTime();
    AccountCreator accountCreator = this.getTestAccountCreator(scopeId, accountSerial);
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.create(accountCreator);
    account = accountService.find(account.getScopeId(), account.getId());
    Organization org = account.getOrganization();
    org.setAddressLine1("5th evenue, NY");
    account.setOrganization(org);
    Account accountUpd = accountService.update(account);
    // 
    // Account asserts
    assertNotNull(accountUpd);
    assertNotNull(accountUpd.getId());
    assertNotNull(accountUpd.getId().getId());
    assertTrue(accountUpd.getOptlock() >= 0);
    assertTrue(accountUpd.getId().equals(account.getId()));
    assertTrue(accountUpd.getScopeId().equals(account.getScopeId()));
    assertTrue(accountUpd.getName().equals(account.getName()));
    assertNotNull(accountUpd.getOrganization());
    assertTrue(accountUpd.getOrganization().getAddressLine1().equals(org.getAddressLine1()));
    assertNotNull(accountUpd.getCreatedOn().equals(account.getCreatedOn()));
    assertNotNull(accountUpd.getCreatedBy().equals(account.getCreatedBy()));
    assertNotNull(account.getModifiedOn());
    assertNotNull(account.getModifiedBy());
}
Also used : AccountCreator(org.eclipse.kapua.service.account.AccountCreator) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) Organization(org.eclipse.kapua.service.account.Organization) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaEid(org.eclipse.kapua.commons.model.id.KapuaEid) AccountService(org.eclipse.kapua.service.account.AccountService) Date(java.util.Date) Test(org.junit.Test)

Example 3 with KapuaEid

use of org.eclipse.kapua.commons.model.id.KapuaEid in project kapua by eclipse.

the class AuthenticationServiceShiroImpl method login.

@Override
public AccessToken login(AuthenticationCredentials authenticationToken) throws KapuaException {
    Subject currentUser = SecurityUtils.getSubject();
    if (currentUser.isAuthenticated()) {
        logger.info("Thread already authenticated for thread '{}' - '{}' - '{}'", new Object[] { Thread.currentThread().getId(), Thread.currentThread().getName(), currentUser.toString() });
        throw new KapuaAuthenticationException(KapuaAuthenticationErrorCodes.SUBJECT_ALREADY_LOGGED);
    }
    // AccessToken accessToken = null;
    if (authenticationToken instanceof UsernamePasswordTokenImpl) {
        UsernamePasswordTokenImpl usernamePasswordToken = (UsernamePasswordTokenImpl) authenticationToken;
        MDC.put(KapuaSecurityUtils.MDC_USERNAME, usernamePasswordToken.getUsername());
        UsernamePasswordToken shiroToken = new UsernamePasswordToken(usernamePasswordToken.getUsername(), usernamePasswordToken.getPassword());
        try {
            currentUser.login(shiroToken);
            Subject shiroSubject = SecurityUtils.getSubject();
            Session shiroSession = shiroSubject.getSession();
            KapuaEid scopeId = (KapuaEid) shiroSession.getAttribute("scopeId");
            KapuaEid userScopeId = (KapuaEid) shiroSession.getAttribute("userScopeId");
            KapuaEid userId = (KapuaEid) shiroSession.getAttribute("userId");
            // create the access token
            String generatedTokenKey = generateToken();
            AccessToken accessToken = new AccessTokenImpl(userId, scopeId, userScopeId, generatedTokenKey);
            KapuaSession kapuaSession = new KapuaSession(accessToken, scopeId, userScopeId, userId, usernamePasswordToken.getUsername());
            KapuaSecurityUtils.setSession(kapuaSession);
            shiroSubject.getSession().setAttribute(KapuaSession.KAPUA_SESSION_KEY, kapuaSession);
            logger.info("Login for thread '{}' - '{}' - '{}'", new Object[] { Thread.currentThread().getId(), Thread.currentThread().getName(), shiroSubject.toString() });
            return kapuaSession.getAccessToken();
        } catch (ShiroException se) {
            KapuaAuthenticationException kae = null;
            if (se instanceof UnknownAccountException) {
                kae = new KapuaAuthenticationException(KapuaAuthenticationErrorCodes.INVALID_USERNAME, se, usernamePasswordToken.getUsername());
            } else if (se instanceof DisabledAccountException) {
                kae = new KapuaAuthenticationException(KapuaAuthenticationErrorCodes.DISABLED_USERNAME, se, usernamePasswordToken.getUsername());
            } else if (se instanceof LockedAccountException) {
                kae = new KapuaAuthenticationException(KapuaAuthenticationErrorCodes.LOCKED_USERNAME, se, usernamePasswordToken.getUsername());
            } else if (se instanceof IncorrectCredentialsException) {
                kae = new KapuaAuthenticationException(KapuaAuthenticationErrorCodes.INVALID_CREDENTIALS, se, usernamePasswordToken.getUsername());
            } else if (se instanceof ExpiredCredentialsException) {
                kae = new KapuaAuthenticationException(KapuaAuthenticationErrorCodes.EXPIRED_CREDENTIALS, se, usernamePasswordToken.getUsername());
            } else {
                throw KapuaAuthenticationException.internalError(se);
            }
            currentUser.logout();
            throw kae;
        }
    } else {
        throw new KapuaAuthenticationException(KapuaAuthenticationErrorCodes.INVALID_CREDENTIALS_TOKEN_PROVIDED);
    }
}
Also used : DisabledAccountException(org.apache.shiro.authc.DisabledAccountException) IncorrectCredentialsException(org.apache.shiro.authc.IncorrectCredentialsException) KapuaSession(org.eclipse.kapua.commons.security.KapuaSession) AccessTokenImpl(org.eclipse.kapua.service.authentication.AccessTokenImpl) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) KapuaEid(org.eclipse.kapua.commons.model.id.KapuaEid) Subject(org.apache.shiro.subject.Subject) ExpiredCredentialsException(org.apache.shiro.authc.ExpiredCredentialsException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) ShiroException(org.apache.shiro.ShiroException) AccessToken(org.eclipse.kapua.service.authentication.AccessToken) LockedAccountException(org.apache.shiro.authc.LockedAccountException) Session(org.apache.shiro.session.Session) KapuaSession(org.eclipse.kapua.commons.security.KapuaSession)

Example 4 with KapuaEid

use of org.eclipse.kapua.commons.model.id.KapuaEid in project kapua by eclipse.

the class PermissionFactoryImpl method parseString.

@Override
public Permission parseString(String stringPermission) throws KapuaException {
    StringTokenizer st = new StringTokenizer(stringPermission, ":");
    int iTokensCount = st.countTokens();
    if (iTokensCount < 1 || iTokensCount > 3) {
        throw new KapuaAuthorizationException(KapuaAuthorizationErrorCodes.INVALID_STRING_PERMISSION, null, stringPermission);
    }
    // 
    // Build the new Permission
    String domain = st.nextToken();
    Actions action = null;
    if (iTokensCount > 1) {
        action = Actions.valueOf(st.nextToken());
    }
    KapuaId scopeTargetId = null;
    if (iTokensCount > 2) {
        try {
            BigInteger kapuaId = new BigInteger(st.nextToken());
            scopeTargetId = new KapuaEid(kapuaId);
        } catch (IllegalArgumentException iae) {
            throw new KapuaAuthorizationException(KapuaAuthorizationErrorCodes.INVALID_STRING_PERMISSION, iae, stringPermission);
        }
    }
    return new PermissionImpl(domain, action, scopeTargetId);
}
Also used : StringTokenizer(java.util.StringTokenizer) KapuaAuthorizationException(org.eclipse.kapua.service.authorization.shiro.KapuaAuthorizationException) Actions(org.eclipse.kapua.service.authorization.permission.Actions) RolePermissionImpl(org.eclipse.kapua.service.authorization.role.shiro.RolePermissionImpl) BigInteger(java.math.BigInteger) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaEid(org.eclipse.kapua.commons.model.id.KapuaEid)

Example 5 with KapuaEid

use of org.eclipse.kapua.commons.model.id.KapuaEid in project kapua by eclipse.

the class MessageStoreServiceTest method testStore.

@Test
public void testStore() throws Exception {
    KapuaId scopeId = new KapuaEid(BigInteger.valueOf(1));
    long accountSerial = (new Date()).getTime();
    AccountCreator accountCreator = this.getTestAccountCreator(scopeId, accountSerial);
    MessageStoreService messageStoreService = locator.getService(MessageStoreService.class);
    DatastoreObjectFactory dsObjectFactory = locator.getFactory(DatastoreObjectFactory.class);
    MessageCreator messageCreator = dsObjectFactory.newMessageCreator();
    Payload messagePayload = dsObjectFactory.newPayload();
    Position messagePosition = dsObjectFactory.newPosition();
    Map<String, Object> metrics = new HashMap<String, Object>();
    Date now = new Date();
    messageCreator.setTimestamp(now);
    messageCreator.setReceivedOn(now);
    String topicName = String.format("%s/CLIENT001/APP01", accountCreator.getName());
    messageCreator.setTopic(topicName);
    metrics.put("metric_long", 1L);
    metrics.put("metric_string", "pippo");
    messagePayload.setMetrics(metrics);
    messagePayload.setCollectedOn(now);
    messagePosition.setAltitude(1.0);
    messagePosition.setTimestamp(now);
    messagePayload.setPosition(messagePosition);
    messagePayload.setMetrics(metrics);
    messageCreator.setPayload(messagePayload);
// FIXME store fails fix it !!
// StorableId messageId = messageStoreService.store(scopeId, messageCreator);
// 
// Message asserts
// assertNotNull(messageId);
// assertTrue(!messageId.toString().isEmpty());
}
Also used : AccountCreator(org.eclipse.kapua.service.account.AccountCreator) DatastoreObjectFactory(org.eclipse.kapua.service.datastore.DatastoreObjectFactory) Position(org.eclipse.kapua.service.datastore.model.Position) HashMap(java.util.HashMap) MessageStoreService(org.eclipse.kapua.service.datastore.MessageStoreService) KapuaEid(org.eclipse.kapua.commons.model.id.KapuaEid) Date(java.util.Date) MessageCreator(org.eclipse.kapua.service.datastore.model.MessageCreator) Payload(org.eclipse.kapua.service.datastore.model.Payload) KapuaId(org.eclipse.kapua.model.id.KapuaId) Test(org.junit.Test)

Aggregations

KapuaEid (org.eclipse.kapua.commons.model.id.KapuaEid)16 Test (org.junit.Test)10 KapuaId (org.eclipse.kapua.model.id.KapuaId)7 Date (java.util.Date)6 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)6 AccountCreator (org.eclipse.kapua.service.account.AccountCreator)5 AccountService (org.eclipse.kapua.service.account.AccountService)5 Account (org.eclipse.kapua.service.account.Account)4 BigInteger (java.math.BigInteger)3 DeviceCreator (org.eclipse.kapua.service.device.registry.DeviceCreator)3 Session (org.apache.shiro.session.Session)2 Subject (org.apache.shiro.subject.Subject)2 KapuaException (org.eclipse.kapua.KapuaException)2 KapuaSession (org.eclipse.kapua.commons.security.KapuaSession)2 AccessToken (org.eclipse.kapua.service.authentication.AccessToken)2 AccessTokenImpl (org.eclipse.kapua.service.authentication.AccessTokenImpl)2 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1 PrePersist (javax.persistence.PrePersist)1 Query (javax.persistence.Query)1