@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { if (!(token instanceof UsernamePasswordToken)) { throw new UnsupportedTokenException(String.format("Token of type %s is not supported. A %s is required.", token.getClass().getName(), UsernamePasswordToken.class.getName())); } UsernamePasswordToken t = (UsernamePasswordToken) token; LOGGER.info("doGetAuthenticationInfo for {}", ((UsernamePasswordToken) token).getUsername()); GithubPrincipal authenticatedPrincipal; try { authenticatedPrincipal = githubClient.authz(t.getUsername(), t.getPassword()); LOGGER.info("Successfully authenticated {}",t.getUsername()); } catch (GithubAuthenticationException e) { LOGGER.warn("Failed authentication", e); return null; } return createSimpleAuthInfo(authenticatedPrincipal, t); }
/** * Method description * * * @param token * * @param authToken * * @return * * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken authToken) throws AuthenticationException { if (!(authToken instanceof PublicKeyToken)) { throw new UnsupportedTokenException("PublicKeyToken is required"); } PublicKeyToken token = (PublicKeyToken) authToken; AuthenticationInfo info = null; AuthenticationResult result = authenticator.authenticate( token.getUsername(), token.getPublicKey()); if ((result != null) && (AuthenticationState.SUCCESS == result.getState())) { info = createAuthenticationInfo(token, result); } else if ((result != null) && (AuthenticationState.NOT_FOUND == result.getState())) { throw new UnknownAccountException("unknown account ".concat(token .getUsername())); } else { throw new AccountException("authentication failed"); } return info; }
/** * Method description * * * @param token * * @param authToken * * @return * * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken authToken) throws AuthenticationException { if (!(authToken instanceof UsernamePasswordToken)) { throw new UnsupportedTokenException( "ScmAuthenticationToken is required"); } UsernamePasswordToken token = (UsernamePasswordToken) authToken; AuthenticationInfo info = null; AuthenticationResult result = authenticator.authenticate(null, null, token.getUsername(), new String(token.getPassword())); if ((result != null) && (AuthenticationState.SUCCESS == result.getState())) { info = createAuthenticationInfo(token, result); } else if ((result != null) && (AuthenticationState.NOT_FOUND == result.getState())) { throw new UnknownAccountException("unknown account ".concat(token .getUsername())); } else { throw new AccountException("authentication failed"); } return info; }
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) { if(!(token instanceof ServletContainerToken)) { throw new UnsupportedTokenException("Token not supported: " + token); } //On GAE, if the user was logged by the container, it is also known to the UserService UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); if(user == null) { throw new UnknownAccountException("User is authenticated to the container, but is not known to the UserService"); } //TODO verifica utilizzo User come principal direttamente return new SimpleAuthenticationInfo(user, token.getCredentials(), getName()); }
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { if (!(authenticationToken instanceof UsernamePasswordToken)) { throw new UnsupportedTokenException("Token of type " + authenticationToken.getClass().getName() + " is not supported. A " + UsernamePasswordToken.class.getName() + " is required."); } UsernamePasswordToken userPass = (UsernamePasswordToken) authenticationToken; String token = new String(userPass.getPassword()); if (token.isEmpty()) { LOGGER.debug(GITLAB_MSG + "token for {} is empty", userPass.getUsername()); return null; } try { LOGGER.debug(GITLAB_MSG + "authenticating {}", userPass.getUsername()); LOGGER.debug(GITLAB_MSG + "null? " + (gitlab == null)); LOGGER.debug(GITLAB_MSG + "null? " + (gitlab.getRestClient() == null)); GitlabUser gitlabUser = gitlab.getRestClient().getUser(userPass.getUsername(), token); User user = gitlabUser.toUser(); if (user.getUserId() == null || user.getUserId().isEmpty()) { LOGGER.debug(GITLAB_MSG + "authentication failed {}", user); throw new AuthenticationException(DEFAULT_MESSAGE + " for " + userPass.getUsername()); } LOGGER.debug(GITLAB_MSG + "successfully authenticated {}", userPass.getUsername()); return new SimpleAuthenticationInfo(gitlabUser /*userPass.getPrincipal()*/, userPass.getCredentials(), getName()); } catch (Exception e) { LOGGER.debug(GITLAB_MSG + "authentication failed {}", userPass.getUsername()); throw new AuthenticationException(DEFAULT_MESSAGE, e); } }
/** * 获取认证信息 * * @param token * 令牌 * @return 认证信息 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken token) { AuthenticationToken authenticationToken = (AuthenticationToken) token; String username = authenticationToken.getUsername(); String password = new String(authenticationToken.getPassword()); String captchaId = authenticationToken.getCaptchaId(); String captcha = authenticationToken.getCaptcha(); String ip = authenticationToken.getHost(); if (!captchaService.isValid(CaptchaType.adminLogin, captchaId, captcha)) { throw new UnsupportedTokenException(); } if (username != null && password != null) { Admin admin = adminService.findByUsername(username); if (admin == null) { throw new UnknownAccountException(); } if (!admin.getIsEnabled()) { throw new DisabledAccountException(); } Setting setting = SettingUtils.get(); if (admin.getIsLocked()) { if (ArrayUtils.contains(setting.getAccountLockTypes(), AccountLockType.admin)) { int loginFailureLockTime = setting.getAccountLockTime(); if (loginFailureLockTime == 0) { throw new LockedAccountException(); } Date lockedDate = admin.getLockedDate(); Date unlockDate = DateUtils.addMinutes(lockedDate, loginFailureLockTime); if (new Date().after(unlockDate)) { admin.setLoginFailureCount(0); admin.setIsLocked(false); admin.setLockedDate(null); adminService.update(admin); } else { throw new LockedAccountException(); } } else { admin.setLoginFailureCount(0); admin.setIsLocked(false); admin.setLockedDate(null); adminService.update(admin); } } if (!DigestUtils.md5Hex(password).equals(admin.getPassword())) { int loginFailureCount = admin.getLoginFailureCount() + 1; if (loginFailureCount >= setting.getAccountLockCount()) { admin.setIsLocked(true); admin.setLockedDate(new Date()); } admin.setLoginFailureCount(loginFailureCount); adminService.update(admin); throw new IncorrectCredentialsException(); } admin.setLoginIp(ip); admin.setLoginDate(new Date()); admin.setLoginFailureCount(0); adminService.update(admin); return new SimpleAuthenticationInfo(new Principal(admin.getId(), username), password, getName()); } throw new UnknownAccountException(); }