@Test public void loginTestForOauthClient() throws MalformedURLException { String token = "12345678"; RestTemplate restTemplate = mock(RestTemplate.class); ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class); OAuth2AccessToken oauthToken = mock(OAuth2AccessToken.class); when(restTemplate.getRequestFactory()).thenReturn(requestFactory); when(restTemplate.execute( anyString(), any(HttpMethod.class), any(RequestCallback.class), any(ResponseExtractor.class), any(Map.class))) .thenReturn(oauthToken); when(oauthToken.getValue()).thenReturn(token); when(oauthToken.getTokenType()).thenReturn("bearer"); // Run Test OauthClient oauthClient = new OauthClient(new URL("http://uaa.cloud.me"), restTemplate); OAuth2AccessToken ouathToken = oauthClient.getToken("*****@*****.**", "passwd"); String loginToken = oauthToken.getValue(); assertThat(loginToken, is(token)); }
public void updatePassword(CloudCredentials credentials, String newPassword) { if (oauthClient != null) { oauthClient.changePassword(token, credentials.getPassword(), newPassword); } else { Map<String, String> userInfo = getRestTemplate().getForObject(getUrl("users/{id}"), Map.class, credentials.getEmail()); userInfo.put("password", newPassword); getRestTemplate().put(getUrl("users/{id}"), userInfo, credentials.getEmail()); } CloudCredentials newCloudCredentials = new CloudCredentials(credentials.getEmail(), newPassword); if (cloudCredentials.getProxyUser() != null) { cloudCredentials = newCloudCredentials.proxyForUser(cloudCredentials.getProxyUser()); } else { cloudCredentials = newCloudCredentials; } }
public String login() { if (cloudCredentials.getEmail() == null) { Assert.hasLength(cloudCredentials.getToken(), "No authentication details provided"); token = cloudCredentials.getToken(); return token; } Assert.hasLength(cloudCredentials.getEmail(), "Email cannot be null or empty"); Assert.hasLength(cloudCredentials.getPassword(), "Password cannot be null or empty"); if (oauthClient != null) { OAuth2AccessToken token = oauthClient.getToken(cloudCredentials.getEmail(), cloudCredentials.getPassword()); this.token = token.getTokenType() + " " + token.getValue(); return this.token; } else { Map<String, String> payload = new HashMap<String, String>(); payload.put("password", cloudCredentials.getPassword()); Map<String, String> response = getRestTemplate() .postForObject( getUrl("users/{id}/tokens"), payload, Map.class, cloudCredentials.getEmail()); token = response.get("token"); return token; } }