Search in sources :

Example 1 with KapuaSimpleAuthenticationInfo

use of org.eclipse.kapua.service.authentication.shiro.credential.KapuaSimpleAuthenticationInfo in project kapua by eclipse.

the class KapuaAuthenticatingRealm method assertCredentialsMatch.

@Override
protected void assertCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) throws AuthenticationException {
    KapuaSimpleAuthenticationInfo kapuaInfo = (KapuaSimpleAuthenticationInfo) info;
    super.assertCredentialsMatch(authcToken, info);
    Subject currentSubject = SecurityUtils.getSubject();
    Session session = currentSubject.getSession();
    session.setAttribute("scopeId", kapuaInfo.getUser().getScopeId());
    session.setAttribute("userScopeId", kapuaInfo.getUser().getScopeId());
    session.setAttribute("userId", kapuaInfo.getUser().getId());
}
Also used : KapuaSimpleAuthenticationInfo(org.eclipse.kapua.service.authentication.shiro.credential.KapuaSimpleAuthenticationInfo) Subject(org.apache.shiro.subject.Subject) Session(org.apache.shiro.session.Session)

Example 2 with KapuaSimpleAuthenticationInfo

use of org.eclipse.kapua.service.authentication.shiro.credential.KapuaSimpleAuthenticationInfo 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)

Aggregations

KapuaSimpleAuthenticationInfo (org.eclipse.kapua.service.authentication.shiro.credential.KapuaSimpleAuthenticationInfo)2 Callable (java.util.concurrent.Callable)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 Session (org.apache.shiro.session.Session)1 Subject (org.apache.shiro.subject.Subject)1 KapuaException (org.eclipse.kapua.KapuaException)1 KapuaRuntimeException (org.eclipse.kapua.KapuaRuntimeException)1 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)1 Account (org.eclipse.kapua.service.account.Account)1 AccountService (org.eclipse.kapua.service.account.AccountService)1 Credential (org.eclipse.kapua.service.authentication.credential.Credential)1 CredentialListResult (org.eclipse.kapua.service.authentication.credential.CredentialListResult)1 CredentialService (org.eclipse.kapua.service.authentication.credential.CredentialService)1 User (org.eclipse.kapua.service.user.User)1 UserService (org.eclipse.kapua.service.user.UserService)1