@Override public AuthenticationInfo authenticate(AuthenticationToken authenticationToken) { return delegate.authenticate(authenticationToken); }
/** * Verifies passed in principal/credentials combo, and creates (if not already exists) a npm token mapped to given * principal and returns the newly created token. */ public String login(final String username, final String password) { checkNotNull(username); checkNotNull(password); try { AuthenticationInfo authenticationInfo = securityHelper.getSecurityManager().authenticate( new UsernamePasswordToken(username, password)); return super.createToken(authenticationInfo.getPrincipals()); } catch (AuthenticationException e) { log.debug("Bad credentials provided for npm token creation", e); return null; } }
public boolean reauthenticate(String password) { UserPrincipal principal = (UserPrincipal) SecurityUtils.getSubject().getPrincipal(); AuthenticationToken token = new UsernamePasswordToken(principal.getUserName(), password); boolean result = true; try { SecurityUtils.getSecurityManager().authenticate(token); } catch (AuthenticationException e) { result = false; } return result; } }
@DirectMethod @Validate public String authenticationToken(@NotEmpty final String base64Username, @NotEmpty final String base64Password) throws Exception { Subject subject = securitySystem.getSubject(); if (subject == null || !subject.isAuthenticated()) { authenticate(base64Username, base64Password); } String username = Strings2.decodeBase64(base64Username); String password = Strings2.decodeBase64(base64Password); log.debug("Authenticate w/username: {}, password: {}", username, Strings2.mask(password)); // Require current user to be the requested user to authenticate subject = securitySystem.getSubject(); if (!subject.getPrincipal().toString().equals(username)) { throw new Exception("Username mismatch"); } // Ask the sec-manager to authenticate, this won't alter the current subject try { SecurityUtils.getSecurityManager().authenticate(new UsernamePasswordToken(username, password)); } catch (AuthenticationException e) { throw new Exception("Authentication failed", e); } // At this point we should be authenticated, return a new ticket return authTickets.createTicket(); }
@Override public boolean isValid(String value, ConstraintValidatorContext constraintContext) { if (value != null) { AuthenticationToken token = new UsernamePasswordToken(SecurityUtils.getUser().getName(), value); try { if (SecurityUtils.getSecurityManager().authenticate(token) != null) return true; } catch (Exception e) { } constraintContext.disableDefaultConstraintViolation(); constraintContext.buildConstraintViolationWithTemplate("Current password does not match").addConstraintViolation(); return false; } else { return true; } } }