public AccessToken(Map<String, String> credentials) { if (credentials == null || credentials.isEmpty()) { throw new BadCredentialsException("Credentials cannot be empty!"); } this.credentials = credentials; }
public UsernamePassword(String username, String password) throws BadCredentialsException { this.username = username; this.password = password; if (!StringUtils.hasLength(username)) { throw new BadCredentialsException("Username cannot be blank!"); } if (!StringUtils.hasLength(password)) { throw new BadCredentialsException("Password cannot be blank!"); } }
public Authentication authenticate(Authentication a) { throw new BadCredentialsException("Rejected"); } });
public Authentication authenticate(Authentication authentication) throws AuthenticationException { RunAsUserToken token = (RunAsUserToken) authentication; if (token.getKeyHash() == key.hashCode()) { return authentication; } else { throw new BadCredentialsException(messages.getMessage( "RunAsImplAuthenticationProvider.incorrectKey", "The presented RunAsUserToken does not contain the expected key")); } }
public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } if (this.key.hashCode() != ((AnonymousAuthenticationToken) authentication) .getKeyHash()) { throw new BadCredentialsException( messages.getMessage("AnonymousAuthenticationProvider.incorrectKey", "The presented AnonymousAuthenticationToken does not contain the expected key")); } return authentication; }
public Assertion validate(final String ticket, final String service) throws TicketValidationException { if (returnTicket) { return new AssertionImpl("rod"); } throw new BadCredentialsException("As requested from mock"); } }
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { if (exception instanceof BadCredentialsException) { exception = new BadCredentialsException(exception.getMessage(), new BadClientCredentialsException()); } authenticationEntryPoint.commence(request, response, exception); } });
@Test public void serializeBadCredentialsExceptionMixinTest() throws JsonProcessingException, JSONException { BadCredentialsException exception = new BadCredentialsException("message"); String serializedJson = mapper.writeValueAsString(exception); JSONAssert.assertEquals(EXCEPTION_JSON, serializedJson, true); }
@Test public void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates() throws Exception { final Authentication authReq = mock(Authentication.class); ProviderManager mgr = new ProviderManager( Arrays.asList(createProviderWhichThrows(new BadCredentialsException("", new Throwable())), createProviderWhichReturns(authReq))); assertThat(mgr.authenticate(mock(Authentication.class))).isSameAs(authReq); }
@Test(expected = AuthenticationException.class) public void callIsntMadeWhenAuthenticationManagerRejectsAuthentication() throws Exception { final TestingAuthenticationToken token = new TestingAuthenticationToken("Test", "Password"); SecurityContextHolder.getContext().setAuthentication(token); mdsReturnsUserRole(); when(authman.authenticate(token)).thenThrow( new BadCredentialsException("rejected")); advisedTarget.makeLowerCase("HELLO"); }
@Test(expected = RemoteAuthenticationException.class) public void testFailedAuthenticationReturnsRemoteAuthenticationException() { RemoteAuthenticationManagerImpl manager = new RemoteAuthenticationManagerImpl(); AuthenticationManager am = mock(AuthenticationManager.class); when(am.authenticate(any(Authentication.class))).thenThrow( new BadCredentialsException("")); manager.setAuthenticationManager(am); manager.attemptAuthentication("rod", "password"); }
@Test public void authenticateWhenBadCredentialsThenError() { when(delegate.authenticate(any())).thenThrow(new BadCredentialsException("Failed")); Mono<Authentication> result = manager.authenticate(authentication); StepVerifier.create(result) .expectError(BadCredentialsException.class) .verify(); } }
@Test public void authenticationExceptionIsRethrownIfNoLaterProviderAuthenticates() throws Exception { ProviderManager mgr = new ProviderManager(Arrays.asList( createProviderWhichThrows(new BadCredentialsException("")), createProviderWhichReturns(null))); try { mgr.authenticate(mock(Authentication.class)); fail("Expected BadCredentialsException"); } catch (BadCredentialsException expected) { } }
@Test public void commenceWhenNoBearerTokenErrorThenStatus401AndAuthHeader() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); this.authenticationEntryPoint.commence(request, response, new BadCredentialsException("test")); assertThat(response.getStatus()).isEqualTo(401); assertThat(response.getHeader("WWW-Authenticate")).isEqualTo("Bearer"); }
@Test public void commenceWhenNotOAuth2AuthenticationExceptionThenBearer() { this.entryPoint.commence(this.exchange, new BadCredentialsException("")).block(); assertThat(getResponse().getHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE)).isEqualTo("Bearer"); assertThat(getResponse().getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); }
@Test public void commenceWhenNoBearerTokenErrorAndRealmSetThenStatus401AndAuthHeaderWithRealm() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); this.authenticationEntryPoint.setRealmName("test"); this.authenticationEntryPoint.commence(request, response, new BadCredentialsException("test")); assertThat(response.getStatus()).isEqualTo(401); assertThat(response.getHeader("WWW-Authenticate")).isEqualTo("Bearer realm=\"test\""); }
@Test public void commenceWhenRealmNameThenHasRealmName() { this.entryPoint.setRealmName("Realm"); this.entryPoint.commence(this.exchange, new BadCredentialsException("")).block(); assertThat(getResponse().getHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE)).isEqualTo("Bearer realm=\"Realm\""); assertThat(getResponse().getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); }
@Test public void authenticateWhenBadCredentialsThenDelegate2NotInvokedAndError() { when(this.delegate1.authenticate(any())).thenReturn(Mono.error(new BadCredentialsException("Test"))); DelegatingReactiveAuthenticationManager manager = new DelegatingReactiveAuthenticationManager(this.delegate1, this.delegate2); StepVerifier.create(manager.authenticate(this.authentication)) .expectError(BadCredentialsException.class) .verify(); } }
@Test public void publishNullPublisher() { provider.setApplicationEventPublisher(null); AuthenticationException ae = new BadCredentialsException("Failed to login"); provider.publishFailureEvent(token, ae); provider.publishSuccessEvent(token); }
@Test public void commenceWhenNoSubscriberThenNothingHappens() { this.entryPoint.commence(this.exchange, new BadCredentialsException("")); assertThat(getResponse().getHeaders()).isEmpty(); assertThat(getResponse().getStatusCode()).isNull(); }