use of org.springframework.security.core.AuthenticationException in project the-app by devops-dojo.
the class AuthenticationServiceImpl method authenticate.
@Override
public boolean authenticate(LoginInfo loginInfo) {
try {
Authentication usernamePasswordAuthentication = new UsernamePasswordAuthenticationToken(loginInfo.getUsername(), loginInfo.getPassword());
Authentication authenticateResult = authenticationManager.authenticate(usernamePasswordAuthentication);
SecurityContextHolder.getContext().setAuthentication(authenticateResult);
logger.info(String.format("Authentication of '%s' was %ssuccessful", loginInfo.getUsername(), (authenticateResult.isAuthenticated() ? "" : "not ")));
return authenticateResult.isAuthenticated();
} catch (AuthenticationException e) {
String msg = String.format("User '%s' could not authenticated correct:", loginInfo.getUsername());
logger.info(msg, e);
}
return false;
}
use of org.springframework.security.core.AuthenticationException in project ranger by apache.
the class RangerAuthenticationProvider method getJDBCAuthentication.
private Authentication getJDBCAuthentication(Authentication authentication, String encoder) throws AuthenticationException {
try {
DaoAuthenticationProvider authenticator = new DaoAuthenticationProvider();
authenticator.setUserDetailsService(userDetailsService);
if (this.isFipsEnabled) {
if (authentication != null && authentication.getCredentials() != null && !authentication.isAuthenticated()) {
Pbkdf2PasswordEncoderCust passwordEncoder = new Pbkdf2PasswordEncoderCust(authentication.getName());
passwordEncoder.setEncodeHashAsBase64(true);
authenticator.setPasswordEncoder(passwordEncoder);
}
} else {
if (encoder != null && "SHA256".equalsIgnoreCase(encoder) && authentication != null) {
authenticator.setPasswordEncoder(new RangerCustomPasswordEncoder(authentication.getName(), "SHA-256"));
} else if (encoder != null && "MD5".equalsIgnoreCase(encoder) && authentication != null) {
authenticator.setPasswordEncoder(new RangerCustomPasswordEncoder(authentication.getName(), "MD5"));
}
}
String userName = "";
String userPassword = "";
if (authentication != null) {
userName = authentication.getName();
if (authentication.getCredentials() != null) {
userPassword = authentication.getCredentials().toString();
}
}
String rangerLdapDefaultRole = PropertiesUtil.getProperty("ranger.ldap.default.role", "ROLE_USER");
if (userName != null && userPassword != null && !userName.trim().isEmpty() && !userPassword.trim().isEmpty()) {
final List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority(rangerLdapDefaultRole));
final UserDetails principal = new User(userName, userPassword, grantedAuths);
final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(principal, userPassword, grantedAuths);
authentication = authenticator.authenticate(finalAuthentication);
return authentication;
} else {
if (authentication != null && !authentication.isAuthenticated()) {
throw new BadCredentialsException("Bad credentials");
}
}
} catch (BadCredentialsException e) {
throw e;
} catch (AuthenticationServiceException e) {
throw e;
} catch (AuthenticationException e) {
throw e;
} catch (Exception e) {
throw e;
} catch (Throwable t) {
throw new BadCredentialsException("Bad credentials", t);
}
return authentication;
}
use of org.springframework.security.core.AuthenticationException in project service-authorization by reportportal.
the class GitHubTokenServices method loadAuthentication.
@Override
public OAuth2Authentication loadAuthentication(String accessToken) throws AuthenticationException, InvalidTokenException {
GitHubClient gitHubClient = GitHubClient.withAccessToken(accessToken);
UserResource gitHubUser = gitHubClient.getUser();
OAuthRegistrationResource oAuthRegistrationResource = oAuthRegistrationSupplier.get();
List<String> allowedOrganizations = ofNullable(oAuthRegistrationResource.getRestrictions()).flatMap(restrictions -> ofNullable(restrictions.get("organizations"))).map(it -> Splitter.on(",").omitEmptyStrings().splitToList(it)).orElse(emptyList());
if (!allowedOrganizations.isEmpty()) {
boolean assignedToOrganization = gitHubClient.getUserOrganizations(gitHubUser.getLogin()).stream().map(OrganizationResource::getLogin).anyMatch(allowedOrganizations::contains);
if (!assignedToOrganization) {
throw new InsufficientOrganizationException("User '" + gitHubUser.getLogin() + "' does not belong to allowed GitHUB organization");
}
}
ReportPortalUser user = replicator.replicateUser(gitHubUser, gitHubClient);
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, "N/A", user.getAuthorities());
Map<String, Serializable> extensionProperties = Collections.singletonMap(UPSTREAM_TOKEN, accessToken);
OAuth2Request request = new OAuth2Request(null, oAuthRegistrationResource.getClientId(), null, true, null, null, null, null, extensionProperties);
return new OAuth2Authentication(request, token);
}
use of org.springframework.security.core.AuthenticationException in project pentaho-platform by pentaho.
the class SpringSecurityLoginModule method getAuthentication.
/**
* {@inheritDoc}
*
* Creates a {@code UsernamePasswordAuthenticationToken} from the given {@code principal} and {@code credentials}
* and passes to Spring Security {@code AuthenticationManager}.
*/
@Override
protected Authentication getAuthentication(final Principal principal, final Credentials credentials) throws RepositoryException {
// only handles SimpleCredential instances; DefaultLoginModule behaves the same way (albeit indirectly)
if (!(credentials instanceof SimpleCredentials)) {
// $NON-NLS-1$
logger.debug("credentials not instance of SimpleCredentials; returning null");
return null;
}
SimpleCredentials simpleCredentials = (SimpleCredentials) credentials;
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(simpleCredentials.getUserID(), String.valueOf(simpleCredentials.getPassword()));
boolean authenticated = false;
try {
org.springframework.security.core.Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getName().equals(simpleCredentials.getUserID())) {
// see if there's already an active Authentication for this user.
authenticated = true;
} else {
// delegate to Spring Security
getAuthenticationManager().authenticate(token);
authenticated = true;
}
} catch (AuthenticationException e) {
// $NON-NLS-1$
logger.debug("authentication exception", e);
}
final boolean authenticateResult = authenticated;
return new Authentication() {
public boolean canHandle(Credentials credentials) {
// this is decided earlier in getAuthentication
return true;
}
public boolean authenticate(Credentials credentials) throws RepositoryException {
return authenticateResult;
}
};
}
use of org.springframework.security.core.AuthenticationException in project thingsboard by thingsboard.
the class RestPublicLoginProcessingFilter method attemptAuthentication.
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
if (!HttpMethod.POST.name().equals(request.getMethod())) {
if (log.isDebugEnabled()) {
log.debug("Authentication method not supported. Request method: " + request.getMethod());
}
throw new AuthMethodNotSupportedException("Authentication method not supported");
}
PublicLoginRequest loginRequest;
try {
loginRequest = objectMapper.readValue(request.getReader(), PublicLoginRequest.class);
} catch (Exception e) {
throw new AuthenticationServiceException("Invalid public login request payload");
}
if (StringUtils.isBlank(loginRequest.getPublicId())) {
throw new AuthenticationServiceException("Public Id is not provided");
}
UserPrincipal principal = new UserPrincipal(UserPrincipal.Type.PUBLIC_ID, loginRequest.getPublicId());
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(principal, "");
return this.getAuthenticationManager().authenticate(token);
}
Aggregations