@Test public void shouldEraseCredentials() { //given Contact contact = mock(Contact.class); //when UserProfile userProfile = new UserProfile(contact, null, null, "pKey", null); userProfile.eraseCredentials(); //then verify(contact).setExtendedDetail(eq("pKey"), isNull(String.class)); //noinspection ConstantConditions assertTrue(userProfile instanceof CredentialsContainer); }
@Override public void onApplicationEvent(AbstractAuthenticationEvent event) { Authentication authentication = event.getAuthentication(); if (event instanceof AuthenticationSuccessEvent) { ResourceOwnerPasswordResourceDetails resource = getResourceOwnerPasswordResourceDetails(); resource.setScope(Arrays.asList("words")); resource.setUsername(authentication.getName()); resource.setPassword(authentication.getCredentials().toString()); try { OAuth2AccessToken accessToken = accessTokenProvider.obtainAccessToken(resource, new DefaultAccessTokenRequest()); log.debug("Access token request succeeded for user: '{}', new token is '{}'" , resource.getUsername() , accessToken.getValue()); if (authentication instanceof AbstractAuthenticationToken && authentication.getDetails() instanceof CustomAuthenticationDetails) { ((CustomAuthenticationDetails) ((AbstractAuthenticationToken) authentication).getDetails()) .setBearer(accessToken.getValue()); log.debug("Access token was added to authentication as details"); } else if (log.isDebugEnabled()) { log.debug("Access token could not be added to authentication as details"); } } catch (Exception e) { log.error("Access token request failed for user: '" + resource.getUsername() + "'", e); } } if (authentication instanceof CredentialsContainer) { // Authentication is complete. Remove credentials and other secret data from authentication ((CredentialsContainer)authentication).eraseCredentials(); } }
@Override public void eraseCredentials() { if (originalAuthentication instanceof CredentialsContainer) { ((CredentialsContainer) originalAuthentication).eraseCredentials(); } }