Search in sources :

Example 1 with UsernamePasswordTokenFactory

use of org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory in project kapua by eclipse.

the class KapuaTest method setUp.

@Before
public void setUp() {
    LOG.debug("Setting up test...");
    if (!isInitialized) {
        LOG.debug("Kapua test context is not initialized. Initializing...");
        try {
            // 
            // Login
            String username = "kapua-sys";
            String password = "kapua-password";
            AuthenticationService authenticationService = locator.getService(AuthenticationService.class);
            UsernamePasswordTokenFactory credentialsFactory = locator.getFactory(UsernamePasswordTokenFactory.class);
            authenticationService.login(credentialsFactory.newInstance(username, password.toCharArray()));
            // 
            // Get current user Id
            adminUserId = KapuaSecurityUtils.getSession().getUserId();
            adminScopeId = KapuaSecurityUtils.getSession().getScopeId();
        } catch (KapuaException exc) {
            exc.printStackTrace();
        }
        isInitialized = true;
    }
}
Also used : KapuaException(org.eclipse.kapua.KapuaException) UsernamePasswordTokenFactory(org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory) AuthenticationService(org.eclipse.kapua.service.authentication.AuthenticationService) Before(org.junit.Before)

Example 2 with UsernamePasswordTokenFactory

use of org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory in project kapua by eclipse.

the class AuthenticationServiceTest method testLoginAndLogout.

/**
 * We should ignore this test until schema loading feature is provided.
 */
@Ignore
@Test
public void testLoginAndLogout() throws Exception {
    String username = "kapua-sys";
    char[] password = "kapua-password".toCharArray();
    KapuaLocator locator = KapuaLocator.getInstance();
    UsernamePasswordTokenFactory usernamePasswordTokenFactory = locator.getFactory(UsernamePasswordTokenFactory.class);
    UsernamePasswordToken credentialsToken = usernamePasswordTokenFactory.newInstance(username, password);
    AuthenticationService authenticationService = locator.getService(AuthenticationService.class);
    authenticationService.login(credentialsToken);
    Subject currentSubject = SecurityUtils.getSubject();
    assertTrue(currentSubject.isAuthenticated());
    assertEquals(username, currentSubject.getPrincipal());
    authenticationService.logout();
    assertFalse(currentSubject.isAuthenticated());
    assertNull(currentSubject.getPrincipal());
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) UsernamePasswordTokenFactory(org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory) AuthenticationService(org.eclipse.kapua.service.authentication.AuthenticationService) Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.eclipse.kapua.service.authentication.UsernamePasswordToken) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with UsernamePasswordTokenFactory

use of org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory in project kapua by eclipse.

the class DeviceCommandManagementServiceTest method setUpClass.

// @BeforeClass
public static void setUpClass() {
    try {
        // 
        // Login
        String username = "kapua-sys";
        String password = "kapua-password";
        AuthenticationService authenticationService = locator.getService(AuthenticationService.class);
        UsernamePasswordTokenFactory credentialsFactory = locator.getFactory(UsernamePasswordTokenFactory.class);
        authenticationService.login(credentialsFactory.newInstance(username, password.toCharArray()));
        // 
        // Get current user Id
        adminUserId = KapuaSecurityUtils.getSession().getUserId();
        adminScopeId = KapuaSecurityUtils.getSession().getScopeId();
        // 
        // Create test account
        KapuaLocator locator = KapuaLocator.getInstance();
        // 
        // Account creation
        String accountName = String.format("test-acct-%d", (new Date()).getTime());
        AccountService accountService = locator.getService(AccountService.class);
        AccountFactory accountFactory = locator.getFactory(AccountFactory.class);
        AccountCreator accountCreator = accountFactory.newAccountCreator(adminScopeId, accountName);
        accountCreator.setAccountPassword("!bV0123456789");
        accountCreator.setOrganizationName(accountName);
        accountCreator.setOrganizationEmail(accountName + "@m.com");
        account = accountService.create(accountCreator);
    } catch (KapuaException exc) {
        exc.printStackTrace();
    }
}
Also used : AccountCreator(org.eclipse.kapua.service.account.AccountCreator) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) AccountFactory(org.eclipse.kapua.service.account.AccountFactory) KapuaException(org.eclipse.kapua.KapuaException) UsernamePasswordTokenFactory(org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory) AuthenticationService(org.eclipse.kapua.service.authentication.AuthenticationService) AccountService(org.eclipse.kapua.service.account.AccountService) Date(java.util.Date)

Example 4 with UsernamePasswordTokenFactory

use of org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory in project kapua by eclipse.

the class GwtAuthorizationServiceImpl method login.

/**
 * Login call in response to the login dialog.
 */
public GwtSession login(GwtUser tmpUser) throws GwtKapuaException {
    // VIP
    // keep this here to make sure we initialize the logger.
    // Without the following, console logger may not log anything when deployed into tomcat.
    s_logger.info(">>> THIS IS INFO <<<");
    s_logger.warn(">>> THIS IS WARN <<<");
    s_logger.debug(">>> THIS IS DEBUG <<<");
    GwtSession gwtSession = null;
    try {
        // Get the user
        KapuaLocator locator = KapuaLocator.getInstance();
        AuthenticationService authenticationService = locator.getService(AuthenticationService.class);
        UsernamePasswordTokenFactory credentialsFactory = locator.getFactory(UsernamePasswordTokenFactory.class);
        AuthenticationCredentials credentials = credentialsFactory.newInstance(tmpUser.getUsername(), tmpUser.getPassword().toCharArray());
        // Login
        authenticationService.login(credentials);
        // Get the session infos
        gwtSession = establishSession();
    } catch (Throwable t) {
        logout();
        KapuaExceptionHandler.handle(t);
    }
    return gwtSession;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) AuthenticationCredentials(org.eclipse.kapua.service.authentication.AuthenticationCredentials) UsernamePasswordTokenFactory(org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory) GwtSession(org.eclipse.kapua.app.console.shared.model.GwtSession) AuthenticationService(org.eclipse.kapua.service.authentication.AuthenticationService)

Aggregations

AuthenticationService (org.eclipse.kapua.service.authentication.AuthenticationService)4 UsernamePasswordTokenFactory (org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory)4 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)3 KapuaException (org.eclipse.kapua.KapuaException)2 Date (java.util.Date)1 Subject (org.apache.shiro.subject.Subject)1 GwtSession (org.eclipse.kapua.app.console.shared.model.GwtSession)1 AccountCreator (org.eclipse.kapua.service.account.AccountCreator)1 AccountFactory (org.eclipse.kapua.service.account.AccountFactory)1 AccountService (org.eclipse.kapua.service.account.AccountService)1 AuthenticationCredentials (org.eclipse.kapua.service.authentication.AuthenticationCredentials)1 UsernamePasswordToken (org.eclipse.kapua.service.authentication.UsernamePasswordToken)1 Before (org.junit.Before)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1